liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <unistd.h> |
| 3 | #include <signal.h> |
| 4 | #include <errno.h> |
| 5 | #include <sys/epoll.h> |
| 6 | #include <sys/socket.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <arpa/inet.h> |
| 9 | #include <sys/un.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <netdb.h> |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 13 | #include <pthread.h> |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 14 | |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 15 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 16 | #ifdef MBTK_POLARSSL_SUPPORT |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 17 | #include <polarssl/net.h> |
| 18 | #include <polarssl/ssl.h> |
| 19 | #include <polarssl/entropy.h> |
| 20 | #include <polarssl/ctr_drbg.h> |
| 21 | #include <polarssl/certs.h> |
| 22 | #include <polarssl/x509.h> |
| 23 | #include <polarssl/error.h> |
| 24 | #include <polarssl/debug.h> |
| 25 | #include <polarssl/config.h> |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 26 | #else |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 27 | #include <resolv.h> |
| 28 | #include <openssl/ssl.h> |
| 29 | #include <openssl/err.h> |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 30 | |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 31 | //#define SSL_VERIFY_PEER 0x01 |
| 32 | //#define SSL_FILETYPE_PEM 0x01 |
| 33 | //#define SSL_VERIFY_FAIL_IF_NO_PEER_CERT 0x02 |
| 34 | |
| 35 | #define DFL_CA_FILE "/ca.crt" |
| 36 | #define DFL_CRT_FILE "/client.crt" |
| 37 | #define DFL_KEY_FILE "/client.key" |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 38 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 39 | #endif |
| 40 | |
b.liu | 94baa7c | 2023-09-26 16:26:10 +0800 | [diff] [blame] | 41 | #include <sys/ioctl.h> |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 42 | |
| 43 | #ifdef LOG_TAG |
| 44 | #undef LOG_TAG |
| 45 | #endif |
| 46 | #define LOG_TAG "mbtk_sock" |
| 47 | #include <mbtk_log.h> |
| 48 | |
| 49 | #include "mbtk_sock2.h" |
| 50 | #include "mbtk_sock_internal.h" |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 51 | #include "mbtk_str.h" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 52 | //#include "mbtk_openssl.h" |
| 53 | |
| 54 | #define SA struct sockaddr |
| 55 | |
| 56 | // Must define LOG_TAG in the first. |
| 57 | //#include "mbtk_log.h" |
| 58 | static int epoll_fd = -1; |
| 59 | static int pipe_fds[2]; |
| 60 | static struct epoll_event epoll_events[20]; |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 61 | static pthread_t sock_thread_id; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 62 | static bool sock_thread_running = FALSE; |
| 63 | static mbtk_sock_s *mbtk_sock[MBTK_HANDLE_MAX_NUM] = {NULL}; |
| 64 | |
| 65 | static int sock_find_first_free(const mbtk_sock_inter_info_s *inter_info) |
| 66 | { |
| 67 | if(inter_info == NULL) { |
| 68 | LOGE("inter_info is NULL."); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | int index = 0; |
| 73 | //while((inter_info + index)->fd > 0) { |
| 74 | while(inter_info[index].fd > 0) { |
| 75 | index++; |
| 76 | } |
| 77 | |
| 78 | if(index == MBTK_SOCK_MAX_NUM) { |
| 79 | LOGE("sock_infos too more."); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | return index; |
| 84 | } |
| 85 | |
| 86 | static bool sock_info_check(int handle,mbtk_sock_inter_info_s *inter_info) |
| 87 | { |
| 88 | if(inter_info == NULL || mbtk_sock[handle] == NULL) { |
| 89 | LOGE("internal_info is NULL."); |
| 90 | return FALSE; |
| 91 | } |
| 92 | |
| 93 | int index = 0; |
| 94 | while(index < MBTK_SOCK_MAX_NUM) { |
| 95 | if(inter_info->fd == |
| 96 | mbtk_sock[handle]->inter_infos[index].fd) { |
| 97 | return TRUE; |
| 98 | } |
| 99 | index++; |
| 100 | } |
| 101 | |
| 102 | return FALSE; |
| 103 | } |
| 104 | |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 105 | #if 0 |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 106 | static bool sock_is_close(int sockfd) |
| 107 | { |
| 108 | char buff[32]; |
| 109 | int recvBytes = recv(sockfd, buff, sizeof(buff), MSG_PEEK); |
| 110 | |
| 111 | int err = errno; |
| 112 | //cout << "In close function, recv " << recvBytes << " bytes, err " << sockErr << endl; |
| 113 | |
| 114 | if(recvBytes > 0) //Get data |
| 115 | return FALSE; |
| 116 | |
| 117 | if((recvBytes == -1) && (err == EWOULDBLOCK)) //No receive data |
| 118 | return FALSE; |
| 119 | |
| 120 | return TRUE; |
| 121 | } |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 122 | #endif |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 123 | |
| 124 | static int sock_info_find_by_fd(int handle,int fd) |
| 125 | { |
| 126 | int index = 0; |
| 127 | while(index < MBTK_SOCK_MAX_NUM) { |
| 128 | if(fd == mbtk_sock[handle]->inter_infos[index].fd) { |
| 129 | return index; |
| 130 | } |
| 131 | index++; |
| 132 | } |
| 133 | |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | static void* sock_thread_run(void *data) |
| 138 | { |
| 139 | LOGD("socket thread is running..."); |
| 140 | if(data != NULL) |
| 141 | LOGD("sock_thread_run has arg."); |
| 142 | |
| 143 | int nready; |
| 144 | if (socketpair( AF_UNIX, SOCK_STREAM, 0, pipe_fds) < 0) { |
| 145 | LOGE("socketpair() error."); |
| 146 | return NULL; |
| 147 | } else { |
| 148 | struct epoll_event ev; |
| 149 | ev.data.fd = pipe_fds[1]; |
| 150 | ev.events = EPOLLIN | EPOLLET; |
| 151 | epoll_ctl(epoll_fd,EPOLL_CTL_ADD,pipe_fds[1],&ev); |
| 152 | } |
| 153 | |
| 154 | while(sock_thread_running) { |
| 155 | nready = epoll_wait(epoll_fd,epoll_events,20,-1); |
| 156 | int i; |
| 157 | for(i=0;i<nready;++i) { |
b.liu | 94baa7c | 2023-09-26 16:26:10 +0800 | [diff] [blame] | 158 | LOGV("fd[%d] event = %x",epoll_events[i].data.fd,epoll_events[i].events); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 159 | if(pipe_fds[1] == epoll_events[i].data.fd) { |
| 160 | LOGD("Get exist sig."); |
| 161 | sock_thread_running = FALSE; |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | int handle = 0; |
| 166 | while(handle < MBTK_HANDLE_MAX_NUM) { |
| 167 | if(mbtk_sock[handle] != NULL) { |
| 168 | int index = sock_info_find_by_fd(handle,epoll_events[i].data.fd); |
| 169 | if(index >= 0 && mbtk_sock[handle]->init_info.sock_cb != NULL) { |
| 170 | mbtk_sock_inter_info_s *inter_info = &(mbtk_sock[handle]->inter_infos[index]); |
b.liu | 8181e14 | 2023-09-26 10:31:10 +0800 | [diff] [blame] | 171 | mbtk_sock_info *info = &(mbtk_sock[handle]->infos[index]); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 172 | |
| 173 | //if(sock_is_close(epoll_events[i].data.fd)) { |
| 174 | // LOGE("Socket %d is closed.",epoll_events[i].data.fd); |
| 175 | // break; |
| 176 | //} |
b.liu | 8181e14 | 2023-09-26 10:31:10 +0800 | [diff] [blame] | 177 | mbtk_sock_cb_info_s sock_info; |
| 178 | sock_info.sock_fd = inter_info->fd; |
| 179 | sock_info.event = epoll_events[i].events; |
| 180 | sock_info.sock_type = info->type; |
| 181 | mbtk_sock[handle]->init_info.sock_cb(handle, &sock_info); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 182 | |
| 183 | if(epoll_events[i].events & EPOLLERR){ // error[ UDP server can't use.] |
| 184 | LOGD("%d EPOLLERR.",epoll_events[i].data.fd); |
| 185 | } |
| 186 | |
| 187 | if ((epoll_events[i].events & EPOLLIN) |
| 188 | && (epoll_events[i].events & EPOLLOUT)) { |
| 189 | LOGD("%d can read and write.",epoll_events[i].data.fd); |
| 190 | int error = -1; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 191 | socklen_t len = sizeof(int); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 192 | if(getsockopt(epoll_events[i].data.fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0){ |
| 193 | LOGE("getsockopt fail.[%d]",errno); |
| 194 | }else{ |
| 195 | LOGD("error = %d",error); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (epoll_events[i].events & EPOLLOUT) { // Can write. |
| 200 | LOGD("%d can write.",epoll_events[i].data.fd); |
| 201 | } |
| 202 | |
| 203 | if (epoll_events[i].events & EPOLLIN) { // Can read. |
| 204 | LOGD("%d can read.",epoll_events[i].data.fd); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | handle++; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | LOGD("socket thread exit."); |
| 215 | return ((void*)0); |
| 216 | } |
| 217 | |
| 218 | static int sock_thread_start() |
| 219 | { |
| 220 | sock_thread_running = TRUE; |
| 221 | if (0 != pthread_create(&sock_thread_id, NULL, sock_thread_run, NULL)) |
| 222 | { |
| 223 | LOGE("error when create pthread,%d\n", errno); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 230 | void net_state_callback_func(mbtk_net_change_type_t type, const void *data) |
| 231 | { |
| 232 | if(type == MBTK_NET_CHANGE_ADDR && data != NULL) { |
| 233 | int handle = 0; |
| 234 | const mbtk_net_addr_change_info_t *addr_info = (const mbtk_net_addr_change_info_t *)data; |
| 235 | while(handle < MBTK_HANDLE_MAX_NUM) { |
| 236 | if(mbtk_sock[handle] != NULL) { |
| 237 | if(mbtk_sock[handle]->init_info.net_cb != NULL) { |
b.liu | 8181e14 | 2023-09-26 10:31:10 +0800 | [diff] [blame] | 238 | mbtk_net_cb_info_s net_info; |
| 239 | net_info.state = (addr_info->type == MBTK_NET_ADDR_CHANGE_TYPE_ADD) ? 1 : 0; |
| 240 | net_info.addr = addr_info->addr; |
| 241 | net_info.if_name = addr_info->if_name; |
| 242 | mbtk_sock[handle]->init_info.net_cb(handle, &net_info); |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | handle++; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 251 | extern mbtk_sock_handle mbtk_sock_init(mbtk_init_info *info) |
| 252 | { |
| 253 | mbtk_sock_handle handle = 0; |
| 254 | while(handle < MBTK_HANDLE_MAX_NUM) { |
| 255 | if(mbtk_sock[handle] == NULL) |
| 256 | break; |
| 257 | |
| 258 | handle++; |
| 259 | } |
| 260 | |
| 261 | if(handle == MBTK_HANDLE_MAX_NUM) { |
| 262 | LOGE("Socket handle is full."); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | mbtk_sock[handle] = (mbtk_sock_s*)malloc(sizeof(mbtk_sock_s)); |
| 267 | memset(mbtk_sock[handle],0x0,sizeof(mbtk_sock_s)); |
| 268 | if(info != NULL) { |
| 269 | mbtk_sock[handle]->init_info.net_type = info->net_type; |
| 270 | mbtk_sock[handle]->init_info.net_cb = info->net_cb; |
| 271 | mbtk_sock[handle]->init_info.sock_cb = info->sock_cb; |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 272 | if(!str_empty(info->if_name)) { |
| 273 | memcpy(mbtk_sock[handle]->init_info.if_name, info->if_name, strlen(info->if_name)); |
| 274 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 275 | } else { |
| 276 | mbtk_sock[handle]->init_info.net_type = MBTK_NET_LINUX; |
| 277 | mbtk_sock[handle]->init_info.net_cb = NULL; |
| 278 | mbtk_sock[handle]->init_info.sock_cb = NULL; |
| 279 | } |
| 280 | |
| 281 | if(!sock_thread_running) { |
| 282 | epoll_fd = epoll_create(256); |
| 283 | if(sock_thread_start()) { |
| 284 | LOGE("Start thread fail."); |
| 285 | return -1; |
| 286 | } |
| 287 | } |
| 288 | |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 289 | if(mbtk_net_monitor_reg(str_empty(info->if_name) ? NULL : info->if_name, net_state_callback_func)) { |
| 290 | LOGE("mbtk_net_monitor_reg() fail."); |
| 291 | return -1; |
| 292 | } |
| 293 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 294 | return handle; |
| 295 | } |
| 296 | |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 297 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 298 | #ifdef MBTK_POLARSSL_SUPPORT |
| 299 | static int mbtk_polarssl_open(int fd ,bool ingnore_cert,mbtk_sock_inter_info_s* inter_info) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 300 | { |
| 301 | LOGE("8\n"); |
| 302 | int ret = 0, len, tail_len, i, written, frags; |
| 303 | unsigned char buf[SSL_MAX_CONTENT_LEN + 1]; |
| 304 | const char *pers = "ssl_client"; |
| 305 | opt.server_name = DFL_SERVER_NAME; |
| 306 | opt.server_addr = DFL_SERVER_ADDR; |
| 307 | opt.server_port = DFL_SERVER_PORT; |
| 308 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 309 | opt.nbio = DFL_NBIO; |
| 310 | opt.request_page = DFL_REQUEST_PAGE; |
| 311 | opt.request_size = DFL_REQUEST_SIZE; |
| 312 | opt.ca_file = DFL_CA_FILE; |
| 313 | opt.ca_path = DFL_CA_PATH; |
| 314 | opt.crt_file = DFL_CRT_FILE; |
| 315 | opt.key_file = DFL_KEY_FILE; |
| 316 | opt.psk = DFL_PSK; |
| 317 | opt.psk_identity = DFL_PSK_IDENTITY; |
| 318 | opt.force_ciphersuite[0]= DFL_FORCE_CIPHER; |
| 319 | opt.renegotiation = DFL_RENEGOTIATION; |
| 320 | opt.allow_legacy = DFL_ALLOW_LEGACY; |
| 321 | opt.renegotiate = DFL_RENEGOTIATE; |
| 322 | opt.exchanges = DFL_EXCHANGES; |
| 323 | opt.min_version = DFL_MIN_VERSION; |
| 324 | opt.max_version = DFL_MAX_VERSION; |
| 325 | opt.auth_mode = DFL_AUTH_MODE; |
| 326 | opt.mfl_code = DFL_MFL_CODE; |
| 327 | opt.trunc_hmac = DFL_TRUNC_HMAC; |
| 328 | opt.reconnect = DFL_RECONNECT; |
| 329 | opt.reco_delay = DFL_RECO_DELAY; |
| 330 | opt.tickets = DFL_TICKETS; |
| 331 | opt.alpn_string = DFL_ALPN_STRING; |
| 332 | |
| 333 | entropy_context entropy; |
| 334 | ctr_drbg_context ctr_drbg; |
| 335 | ssl_context ssl; |
| 336 | ssl_session saved_session; |
| 337 | x509_crt cacert; |
| 338 | x509_crt clicert; |
| 339 | pk_context pkey; |
| 340 | |
| 341 | memset( &ssl, 0, sizeof( ssl_context ) ); |
| 342 | memset( &saved_session, 0, sizeof( ssl_session ) ); |
| 343 | x509_crt_init( &cacert ); |
| 344 | x509_crt_init( &clicert ); |
| 345 | pk_init( &pkey ); |
| 346 | LOGE("9\n"); |
| 347 | /* |
| 348 | * 0. Initialize the RNG and the session data |
| 349 | */ |
| 350 | |
| 351 | entropy_init( &entropy ); |
| 352 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 353 | (const unsigned char *) pers, |
| 354 | strlen( pers ) ) ) != 0 ) |
| 355 | { |
| 356 | LOGE( " failed\n ! ctr_drbg_init returned -0x%x\n", -ret ); |
| 357 | return -1; |
| 358 | } |
| 359 | if(!ingnore_cert) |
| 360 | { |
| 361 | LOGE("10\n"); |
| 362 | /* |
| 363 | * 1.1. Load the trusted CA |
| 364 | */ |
| 365 | //ret = x509_crt_parse(&cacert,ca1_cert,strlen(ca1_cert)); |
| 366 | ret = x509_crt_parse_file( &cacert, opt.ca_path ); |
| 367 | if( ret < 0 ) |
| 368 | { |
| 369 | LOGE( " failed\n ! ca x509_crt_parse returned -0x%x\n\n", -ret ); |
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * 1.2. Load own certificate and private key |
| 375 | * |
| 376 | * (can be skipped if client authentication is not required) |
| 377 | */ |
| 378 | |
| 379 | ret = x509_crt_parse_file( &clicert, opt.crt_file ); |
| 380 | if( ret != 0 ) |
| 381 | { |
| 382 | LOGE( " failed\n ! crt x509_crt_parse returned -0x%x\n\n", -ret ); |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | ret = pk_parse_keyfile( &pkey, opt.key_file, NULL); |
| 387 | if( ret != 0 ) |
| 388 | { |
| 389 | LOGE( " failed\n ! key x509_crt_parse returned -0x%x\n\n", -ret ); |
| 390 | return -1; |
| 391 | } |
| 392 | } |
| 393 | /* |
| 394 | * 2. Setup stuff |
| 395 | */ |
| 396 | LOGE( " . Setting up the SSL/TLS structure..." ); |
| 397 | |
| 398 | if( ( ret = ssl_init( &ssl ) ) != 0 ) |
| 399 | { |
| 400 | LOGE( " failed\n ! ssl_init returned -0x%x\n\n", -ret ); |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); |
| 405 | if(ingnore_cert) |
| 406 | { |
| 407 | opt.auth_mode = SSL_VERIFY_OPTIONAL; |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | opt.auth_mode = SSL_VERIFY_REQUIRED; |
| 412 | } |
| 413 | |
| 414 | ssl_set_authmode( &ssl, opt.auth_mode ); |
| 415 | |
| 416 | ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg ); |
| 417 | |
| 418 | ssl_set_bio( &ssl, net_recv, &fd, net_send, &fd ); |
| 419 | |
| 420 | ssl_set_renegotiation( &ssl, opt.renegotiation ); |
| 421 | ssl_legacy_renegotiation( &ssl, opt.allow_legacy ); |
| 422 | |
| 423 | ssl_set_ca_chain( &ssl, &cacert, NULL, NULL ); |
| 424 | if(!ingnore_cert) |
| 425 | { |
| 426 | if( ( ret = ssl_set_own_cert( &ssl, &clicert, &pkey ) ) != 0 ) |
| 427 | { |
| 428 | LOGE( " failed\n ! ssl_set_own_cert returned %d\n\n", ret ); |
| 429 | return -1; |
| 430 | } |
| 431 | } |
| 432 | if( opt.min_version != -1 ) |
| 433 | ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version ); |
| 434 | if( opt.max_version != -1 ) |
| 435 | ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version ); |
| 436 | /* |
| 437 | * 3. Handshake |
| 438 | */ |
| 439 | LOGE( " . Performing the SSL/TLS handshake..." ); |
| 440 | |
| 441 | while( ( ret = ssl_handshake( &ssl ) ) != 0 ) |
| 442 | { |
| 443 | if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 444 | { |
| 445 | LOGE( " failed\n ! ssl_handshake returned -0x%x\n", -ret ); |
| 446 | if( ret == POLARSSL_ERR_X509_CERT_VERIFY_FAILED ) |
| 447 | LOGE( |
| 448 | " Unable to verify the server's certificate. " |
| 449 | "Either it is invalid,\n" |
| 450 | " or you didn't set ca_file or ca_path " |
| 451 | "to an appropriate value.\n" |
| 452 | " Alternatively, you may want to use " |
| 453 | "auth_mode=optional for testing purposes.\n" ); |
| 454 | LOGE( "\n" ); |
| 455 | return -1;; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | LOGE( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) ); |
| 460 | printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) ); |
| 461 | |
| 462 | /* |
| 463 | * 4. Verify the server certificate |
| 464 | */ |
| 465 | LOGE( " . Verifying peer X.509 certificate..." ); |
| 466 | |
| 467 | if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 ) |
| 468 | { |
| 469 | LOGE( " failed\n" ); |
| 470 | |
| 471 | if( ( ret & BADCERT_EXPIRED ) != 0 ) |
| 472 | LOGE( " ! server certificate has expired\n" ); |
| 473 | |
| 474 | if( ( ret & BADCERT_REVOKED ) != 0 ) |
| 475 | LOGE( " ! server certificate has been revoked\n" ); |
| 476 | |
| 477 | if( ( ret & BADCERT_CN_MISMATCH ) != 0 ) |
| 478 | LOGE( " ! CN mismatch (expected CN=%s)\n", opt.server_name ); |
| 479 | |
| 480 | if( ( ret & BADCERT_NOT_TRUSTED ) != 0 ) |
| 481 | LOGE( " ! self-signed or not signed by a trusted CA\n" ); |
| 482 | |
| 483 | } |
| 484 | |
| 485 | if( ssl_get_peer_cert( &ssl ) != NULL ) |
| 486 | { |
| 487 | LOGE( " . Peer certificate information ...\n" ); |
| 488 | x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ", |
| 489 | ssl_get_peer_cert( &ssl ) ); |
| 490 | LOGE( "%s\n", buf ); |
| 491 | } |
| 492 | |
| 493 | inter_info->cacert = &cacert; |
| 494 | inter_info->clicert = &clicert; |
| 495 | inter_info->ctr_drbg = &ctr_drbg; |
| 496 | inter_info->entropy = &entropy; |
| 497 | inter_info->pkey = &pkey; |
| 498 | inter_info->saved_session = &saved_session; |
| 499 | inter_info->ssl = &ssl; |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 504 | static int mbtk_polarssl_close(mbtk_sock_inter_info_s *inter_info) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 505 | { |
| 506 | if (inter_info == NULL) |
| 507 | { |
| 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | int ret = -1; |
| 512 | while( ( ret = ssl_close_notify( inter_info->ssl ) ) < 0 ) |
| 513 | { |
| 514 | if( ret == POLARSSL_ERR_NET_CONN_RESET ) |
| 515 | { |
| 516 | LOGE( " ok (already closed by peer)\n" ); |
| 517 | ret = 0; |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | if( ret != POLARSSL_ERR_NET_WANT_READ && |
| 522 | ret != POLARSSL_ERR_NET_WANT_WRITE ) |
| 523 | { |
| 524 | LOGE( " failed\n ! ssl_close_notify returned %d\n\n", ret ); |
| 525 | return -1; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | x509_crt_free( inter_info->clicert ); |
| 530 | x509_crt_free( inter_info->cacert ); |
| 531 | pk_free( inter_info->pkey ); |
| 532 | ssl_session_free( inter_info->saved_session ); |
| 533 | ssl_free( inter_info->ssl ); |
| 534 | ctr_drbg_free( inter_info->ctr_drbg ); |
| 535 | entropy_free( inter_info->entropy ); |
| 536 | return 0; |
| 537 | } |
| 538 | |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 539 | static int mbtk_polarssl_write( ssl_context *ssl, const unsigned char *buf, size_t len ) |
| 540 | { |
| 541 | return ssl_write(ssl, buf, len); |
| 542 | } |
| 543 | |
| 544 | static int mbtk_polarssl_read( ssl_context *ssl, unsigned char *buf, size_t len ) |
| 545 | { |
| 546 | return ssl_read(ssl, buf, len); |
| 547 | } |
| 548 | |
| 549 | #else |
| 550 | |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 551 | void ShowCerts(SSL * ssl) |
| 552 | { |
| 553 | X509 *cert; |
| 554 | char *line; |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 555 | |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 556 | cert = SSL_get_peer_certificate(ssl); |
| 557 | // SSL_get_verify_result()是重点,SSL_CTX_set_verify()只是配置启不启用并没有执行认证,调用该函数才会真证进行证书认证 |
| 558 | // 如果验证不通过,那么程序抛出异常中止连接 |
| 559 | if(SSL_get_verify_result(ssl) == X509_V_OK){ |
| 560 | printf("证书验证通过\n"); |
| 561 | } |
| 562 | if (cert != NULL) { |
| 563 | printf("数字证书信息:\n"); |
| 564 | line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); |
| 565 | printf("证书: %s\n", line); |
| 566 | free(line); |
| 567 | line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); |
| 568 | printf("颁发者: %s\n", line); |
| 569 | free(line); |
| 570 | X509_free(cert); |
| 571 | } else |
| 572 | printf("无证书信息!\n"); |
| 573 | } |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 574 | |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 575 | static int mbtk_openssl_open(int fd ,bool ingnore_cert,mbtk_sock_inter_info_s* inter_info) |
| 576 | { |
| 577 | SSL_CTX *ctx; |
| 578 | SSL *ssl; |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 579 | |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 580 | /* SSL 库初始化,参看 ssl-server.c 代码 */ |
| 581 | SSL_library_init(); |
| 582 | OpenSSL_add_all_algorithms(); |
| 583 | SSL_load_error_strings(); |
| 584 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 585 | if (ctx == NULL) { |
| 586 | ERR_print_errors_fp(stdout); |
| 587 | return -1; |
| 588 | } |
| 589 | |
| 590 | if(!ingnore_cert) |
| 591 | { |
| 592 | // 双向验证 |
| 593 | // SSL_VERIFY_PEER---要求对证书进行认证,没有证书也会放行 |
| 594 | // SSL_VERIFY_FAIL_IF_NO_PEER_CERT---要求客户端需要提供证书,但验证发现单独使用没有证书也会放行 |
| 595 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL); |
| 596 | // 设置信任根证书 |
| 597 | if (SSL_CTX_load_verify_locations(ctx, "/ca.crt",NULL)<=0){ |
| 598 | ERR_print_errors_fp(stdout); |
| 599 | printf("fail SSL_CTX_load_verify_locations()\n"); |
| 600 | return -1; |
| 601 | } |
| 602 | |
| 603 | /* 载入用户的数字证书, 此证书用来发送给客户端。 证书里包含有公钥 */ |
| 604 | if (SSL_CTX_use_certificate_file(ctx, DFL_CRT_FILE, SSL_FILETYPE_PEM) <= 0) { |
| 605 | ERR_print_errors_fp(stdout); |
| 606 | printf("fail SSL_CTX_use_certificate_file()\n"); |
| 607 | return -1; |
| 608 | } |
| 609 | /* 载入用户私钥 */ |
| 610 | if (SSL_CTX_use_PrivateKey_file(ctx, DFL_KEY_FILE, SSL_FILETYPE_PEM) <= 0) { |
| 611 | ERR_print_errors_fp(stdout); |
| 612 | printf("fail SSL_CTX_use_PrivateKey_file()\n"); |
| 613 | return -1; |
| 614 | } |
| 615 | /* 检查用户私钥是否正确 */ |
| 616 | if (!SSL_CTX_check_private_key(ctx)) { |
| 617 | ERR_print_errors_fp(stdout); |
| 618 | printf("fail SSL_CTX_check_private_key()\n"); |
| 619 | return -1; |
| 620 | } |
| 621 | |
| 622 | } |
| 623 | |
| 624 | /* 基于 ctx 产生一个新的 SSL */ |
| 625 | ssl = SSL_new(ctx); |
| 626 | SSL_set_fd(ssl, fd); |
| 627 | /* 建立 SSL 连接 */ |
| 628 | if (SSL_connect(ssl) == -1) |
| 629 | ERR_print_errors_fp(stderr); |
| 630 | else { |
| 631 | printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); |
| 632 | if(!ingnore_cert) |
| 633 | { |
| 634 | ShowCerts(ssl); |
| 635 | } |
| 636 | } |
| 637 | |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 638 | inter_info->ctx = ctx; |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 639 | |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 640 | inter_info->ssl = ssl; |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | static int mbtk_openssl_close(mbtk_sock_inter_info_s *inter_info) |
| 646 | { |
| 647 | SSL_shutdown(inter_info->ssl); |
| 648 | SSL_free(inter_info->ssl); |
| 649 | // close(sockfd); |
| 650 | SSL_CTX_free(inter_info->ctx); |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static int mbtk_openssl_write( SSL *ssl, const unsigned char *buf, size_t len ) |
| 655 | { |
| 656 | return SSL_write(ssl, buf, len); |
| 657 | } |
| 658 | |
| 659 | static int mbtk_openssl_read( SSL *ssl, unsigned char *buf, size_t len ) |
| 660 | { |
| 661 | return SSL_read(ssl, buf, len); |
| 662 | } |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 663 | |
| 664 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 665 | #endif |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 666 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 667 | extern mbtk_sock_session mbtk_sock_open(mbtk_sock_handle handle,mbtk_sock_info *info, |
| 668 | unsigned int timeout, |
| 669 | int *mbtk_errno) |
| 670 | { |
| 671 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 672 | || mbtk_sock[handle] == NULL) { |
| 673 | LOGE("Socket not inited."); |
| 674 | return -1; |
| 675 | } |
| 676 | |
| 677 | *mbtk_errno = MBTK_SOCK_ERROR; |
| 678 | if(info == NULL) { |
| 679 | LOGE("mbtk_sock_info not be NULL."); |
| 680 | return -1; |
| 681 | } |
| 682 | |
| 683 | int index_free = sock_find_first_free(mbtk_sock[handle]->inter_infos); |
| 684 | if(index_free < 0) { |
| 685 | LOGE("sock_find_first_free() fail."); |
| 686 | return -1; |
| 687 | } |
| 688 | |
| 689 | memcpy(&(mbtk_sock[handle]->infos[index_free]),info,sizeof(mbtk_sock_info)); |
| 690 | if(info->type == MBTK_SOCK_UDP) { // UDP |
| 691 | if((mbtk_sock[handle]->inter_infos[index_free].fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
| 692 | LOGE("socket() fail.[%d]",errno); |
| 693 | goto result_fail; |
| 694 | } |
| 695 | } else { // TCP |
| 696 | if((mbtk_sock[handle]->inter_infos[index_free].fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ |
| 697 | LOGE("socket() fail.[%d]",errno); |
| 698 | goto result_fail; |
| 699 | } |
| 700 | } |
| 701 | // Set O_NONBLOCK |
| 702 | int flags = fcntl(mbtk_sock[handle]->inter_infos[index_free].fd, F_GETFL, 0); |
| 703 | if (flags < 0) { |
| 704 | LOGE("Get flags error:%s\n", strerror(errno)); |
| 705 | goto result_fail_with_close; |
| 706 | } |
| 707 | flags |= O_NONBLOCK; |
| 708 | if (fcntl(mbtk_sock[handle]->inter_infos[index_free].fd, F_SETFL, flags) < 0) { |
| 709 | LOGE("Set flags error:%s\n", strerror(errno)); |
| 710 | goto result_fail_with_close; |
| 711 | } |
| 712 | |
| 713 | // Connect |
| 714 | LOGD("Start conn:%s:%d",info->address,info->port); |
| 715 | if(strlen(info->address) > 0 && info->port > 0) { |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 716 | if(strlen(info->local_address) > 0 || info->local_port > 0) { |
| 717 | // 指定本地IP和端口,不指定内核会自动指定(一般不指定) |
| 718 | struct sockaddr_in loc_addr; |
| 719 | memset(&loc_addr, 0, sizeof(struct sockaddr_in)); |
| 720 | loc_addr.sin_family = AF_INET; |
| 721 | |
| 722 | // 指定IP |
| 723 | if(strlen(info->local_address) > 0) { |
| 724 | if(inet_pton(AF_INET, info->local_address, &loc_addr.sin_addr) < 0) { |
| 725 | LOGE("inet_pton() error:%d", errno); |
| 726 | goto result_fail_with_close; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | if(info->local_port > 0) { |
| 731 | loc_addr.sin_port = htons(info->local_port); |
| 732 | } |
| 733 | if(bind(mbtk_sock[handle]->inter_infos[index_free].fd, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) { |
| 734 | LOGE("bind() error:%d", errno); |
| 735 | if(errno == EADDRINUSE) { // 地址已在使用 |
| 736 | LOGE("EADDRINUSE : Local port already occupied."); |
| 737 | } |
| 738 | goto result_fail_with_close; |
| 739 | } else { |
| 740 | LOGD("Bind ip/port success."); |
| 741 | } |
| 742 | } |
| 743 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 744 | struct sockaddr_in servaddr; |
| 745 | bzero(&servaddr, sizeof(servaddr)); |
| 746 | servaddr.sin_family = AF_INET; |
| 747 | servaddr.sin_port = htons(info->port); |
| 748 | |
| 749 | struct hostent *he = gethostbyname(info->address); |
| 750 | if (he == NULL){ |
| 751 | LOGE("gethostbyname() fail.[%d]",errno); |
| 752 | goto result_fail_with_close; |
| 753 | } else { |
| 754 | LOGD("Ip : %s",he->h_addr_list[0]); |
| 755 | } |
| 756 | memcpy(&servaddr.sin_addr, he->h_addr_list[0], sizeof(struct in_addr)); |
| 757 | |
| 758 | if(connect(mbtk_sock[handle]->inter_infos[index_free].fd, (SA *) &servaddr, sizeof(servaddr)) < 0){ |
| 759 | if(EINPROGRESS != errno){ |
| 760 | LOGE("connect() fail.[%d]",errno); |
| 761 | goto result_fail_with_close; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | fd_set rset, wset; |
| 766 | FD_ZERO(&rset); |
| 767 | FD_ZERO(&wset); |
| 768 | FD_SET(mbtk_sock[handle]->inter_infos[index_free].fd, &rset); |
| 769 | FD_SET(mbtk_sock[handle]->inter_infos[index_free].fd, &wset); |
| 770 | struct timeval time_out; |
| 771 | time_out.tv_sec = timeout/1000; |
| 772 | time_out.tv_usec = timeout%1000*1000; |
| 773 | int nready = select(mbtk_sock[handle]->inter_infos[index_free].fd + 1, |
| 774 | &rset, &wset, NULL, &time_out); |
| 775 | LOGD("nready = %d",nready); |
| 776 | if(nready == 0){// Timeout |
| 777 | LOGE("Timeout."); |
| 778 | printf("Timeout.\n"); |
| 779 | goto result_fail_with_close; |
| 780 | }else{ |
| 781 | if (FD_ISSET(mbtk_sock[handle]->inter_infos[index_free].fd, &rset) |
| 782 | && FD_ISSET(mbtk_sock[handle]->inter_infos[index_free].fd, &wset)) { |
| 783 | int error = -1; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 784 | socklen_t len = sizeof(int); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 785 | LOGE("Can read and write."); |
| 786 | if(getsockopt(mbtk_sock[handle]->inter_infos[index_free].fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0){ |
| 787 | LOGE("getsockopt fail.[%d]",errno); |
| 788 | goto result_fail_with_close; |
| 789 | }else{ |
| 790 | LOGE("error = %d",error); |
| 791 | if(error != 0){ // Fail |
| 792 | goto result_fail_with_close; |
| 793 | } |
| 794 | } |
| 795 | }else if(FD_ISSET(mbtk_sock[handle]->inter_infos[index_free].fd, &wset)){ |
| 796 | LOGI("Can write."); |
| 797 | printf("Can write.\n"); |
| 798 | }else{ |
| 799 | LOGE("Can read(Impossible)."); |
| 800 | goto result_fail_with_close; |
| 801 | } |
| 802 | } |
| 803 | } else { |
| 804 | LOGE("Can not conn."); |
| 805 | goto result_fail_with_close; |
| 806 | } |
| 807 | |
| 808 | if(mbtk_sock[handle]->init_info.sock_cb) { |
| 809 | struct epoll_event ev; |
| 810 | ev.data.fd = mbtk_sock[handle]->inter_infos[index_free].fd; |
| 811 | ev.events = EPOLLIN | EPOLLET; |
| 812 | epoll_ctl(epoll_fd,EPOLL_CTL_ADD,mbtk_sock[handle]->inter_infos[index_free].fd,&ev); |
| 813 | } |
| 814 | #if 1 |
| 815 | if(info->ftp_ssl_support) |
| 816 | { |
| 817 | if(info->is_support_ssl){ |
| 818 | mbtk_sock[handle]->infos[index_free].is_support_ssl = 0; |
| 819 | unsigned char mbtk_ftp_ssl_read_buf_s[256]; |
| 820 | int err_rw; |
| 821 | memset(mbtk_ftp_ssl_read_buf_s,0,sizeof(mbtk_ftp_ssl_read_buf_s)); |
| 822 | mbtk_sock_read(handle,mbtk_sock[handle]->inter_infos[index_free].fd, |
| 823 | mbtk_ftp_ssl_read_buf_s, |
| 824 | sizeof(mbtk_ftp_ssl_read_buf_s), |
| 825 | 60000, |
| 826 | &err_rw); |
| 827 | printf("\nmbtk_sock_read:\n%s\n",mbtk_ftp_ssl_read_buf_s); |
| 828 | |
| 829 | char cmd_buff[50]; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 830 | int len=0; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 831 | memset(cmd_buff,0,sizeof(cmd_buff)); |
| 832 | |
| 833 | len = snprintf(cmd_buff, 50, "AUTH TLS\r\n"); |
| 834 | cmd_buff[len] = '\0'; |
| 835 | //printf("\n cmd_buff = %s\n", cmd_buff); |
| 836 | |
| 837 | mbtk_sock_write(handle,mbtk_sock[handle]->inter_infos[index_free].fd, |
| 838 | cmd_buff, |
| 839 | strlen(cmd_buff), |
| 840 | 60000, |
| 841 | &err_rw); |
| 842 | |
| 843 | memset(mbtk_ftp_ssl_read_buf_s,0,sizeof(mbtk_ftp_ssl_read_buf_s)); |
| 844 | mbtk_sock_read(handle,mbtk_sock[handle]->inter_infos[index_free].fd, |
| 845 | mbtk_ftp_ssl_read_buf_s, |
| 846 | sizeof(mbtk_ftp_ssl_read_buf_s), |
| 847 | 60000, |
| 848 | &err_rw); |
| 849 | printf("\nmbtk_sock_read:\n%s\n",mbtk_ftp_ssl_read_buf_s); |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 850 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 851 | mbtk_sock[handle]->infos[index_free].is_support_ssl=1; |
| 852 | }else{ |
| 853 | mbtk_sock[handle]->infos[index_free].is_support_ssl=1; |
| 854 | } |
| 855 | } |
| 856 | #endif |
| 857 | if(info->is_support_ssl){ |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 858 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 859 | #ifdef MBTK_POLARSSL_SUPPORT |
| 860 | if(mbtk_polarssl_open(mbtk_sock[handle]->inter_infos[index_free].fd,info->ingnore_cert,&mbtk_sock[handle]->inter_infos[index_free]) == -1){ |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 861 | LOGE("mbtk_openssl_init fail"); |
| 862 | goto result_fail_with_close; |
| 863 | } |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 864 | #else |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 865 | if(mbtk_openssl_open(mbtk_sock[handle]->inter_infos[index_free].fd,info->ingnore_cert,&mbtk_sock[handle]->inter_infos[index_free]) == -1){ |
| 866 | LOGE("mbtk_openssl_init fail"); |
| 867 | goto result_fail_with_close; |
| 868 | } |
| 869 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 870 | |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 871 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 872 | |
| 873 | #endif |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | *mbtk_errno = MBTK_SOCK_SUCCESS; |
| 877 | |
| 878 | mbtk_sock[handle]->sock_num++; |
| 879 | return mbtk_sock[handle]->inter_infos[index_free].fd; |
| 880 | result_fail_with_close: |
| 881 | close(mbtk_sock[handle]->inter_infos[index_free].fd); |
| 882 | mbtk_sock[handle]->inter_infos[index_free].fd = -1; |
| 883 | result_fail: |
| 884 | memset(&(mbtk_sock[handle]->inter_infos[index_free]),0x0,sizeof(mbtk_sock_inter_info_s)); |
| 885 | memset(&(mbtk_sock[handle]->infos[index_free]),0x0,sizeof(mbtk_sock_info)); |
| 886 | LOGE("mbtk_sock_open() end:fail"); |
| 887 | return -1; |
| 888 | } |
| 889 | extern int mbtk_ssl_init_func(mbtk_sock_handle handle ,bool ingnore_cert,mbtk_sock_session fd) |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 890 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 891 | int i=0; |
| 892 | int index_free=0; |
| 893 | |
| 894 | for (i=0;i<10;i++) |
| 895 | { |
| 896 | if(mbtk_sock[handle]->inter_infos[i].fd == fd) |
| 897 | { |
| 898 | index_free = i; |
| 899 | break; |
| 900 | } |
| 901 | } |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 902 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 903 | #ifdef MBTK_POLARSSL_SUPPORT |
| 904 | return mbtk_polarssl_open(mbtk_sock[handle]->inter_infos[index_free].fd,ingnore_cert,&mbtk_sock[handle]->inter_infos[index_free]); |
| 905 | #else |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 906 | return mbtk_openssl_open(mbtk_sock[handle]->inter_infos[index_free].fd,ingnore_cert,&mbtk_sock[handle]->inter_infos[index_free]); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 907 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 908 | #else |
| 909 | return -1; |
| 910 | #endif |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 911 | } |
| 912 | extern int mbtk_ssl_close_func(mbtk_sock_handle handle ,bool ingnore_cert,mbtk_sock_session fd) |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 913 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 914 | int i=0; |
| 915 | int index_free=0; |
| 916 | |
| 917 | for (i=0;i<10;i++) |
| 918 | { |
| 919 | if(mbtk_sock[handle]->inter_infos[i].fd == fd) |
| 920 | { |
| 921 | index_free = i; |
| 922 | break; |
| 923 | } |
| 924 | } |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 925 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 926 | #ifdef MBTK_POLARSSL_SUPPORT |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 927 | if(mbtk_sock[handle]->inter_infos[index_free].ssl!=NULL) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 928 | printf("\nmbtk_sock[handle]->inter_infos[index_free].ssl not empty\n"); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 929 | return mbtk_polarssl_close(&mbtk_sock[handle]->inter_infos[index_free]); |
| 930 | #else |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 931 | if(mbtk_sock[handle]->inter_infos[index_free].ssl!=NULL) |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 932 | printf("\nmbtk_sock[handle]->inter_infos[index_free].ssl not empty\n"); |
| 933 | return mbtk_openssl_close(&mbtk_sock[handle]->inter_infos[index_free]); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 934 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 935 | |
| 936 | #else |
| 937 | return -1; |
| 938 | #endif |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | extern int mbtk_sock_write(mbtk_sock_handle handle,mbtk_sock_session session, |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 942 | const void *buffer, |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 943 | unsigned int buf_len, |
| 944 | unsigned int timeout, |
| 945 | int *mbtk_errno) |
| 946 | { |
| 947 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 948 | || session < 0 || mbtk_sock[handle] == NULL) { |
| 949 | LOGE("Socket not inited."); |
| 950 | return -1; |
| 951 | } |
| 952 | |
| 953 | *mbtk_errno = MBTK_SOCK_ERROR; |
| 954 | if(buffer == NULL) { |
| 955 | LOGE("mbtk_sock_write() args error."); |
| 956 | return -1; |
| 957 | } |
| 958 | |
| 959 | mbtk_sock_inter_info_s *inter_info = NULL; |
| 960 | int index = 0; |
| 961 | while(index < MBTK_SOCK_MAX_NUM) { |
| 962 | if(session == |
| 963 | mbtk_sock[handle]->inter_infos[index].fd) { |
| 964 | inter_info = &(mbtk_sock[handle]->inter_infos[index]); |
| 965 | break; |
| 966 | } |
| 967 | index++; |
| 968 | } |
| 969 | |
| 970 | if(!sock_info_check(handle,inter_info)) { |
| 971 | LOGE("sock_info_check() fail."); |
| 972 | return -1; |
| 973 | } |
| 974 | |
| 975 | index = sock_info_find_by_fd(handle,inter_info->fd); |
| 976 | if(index < 0) { |
| 977 | LOGE("No such socket in session list."); |
| 978 | return -1; |
| 979 | } |
| 980 | |
| 981 | int len = 0; |
| 982 | unsigned int count = 0; |
| 983 | if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) { |
| 984 | while(count < buf_len){ |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 985 | if(mbtk_sock[handle]->infos[index].is_support_ssl) { |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 986 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 987 | #ifdef MBTK_POLARSSL_SUPPORT |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 988 | len = mbtk_polarssl_write(inter_info->ssl,(const unsigned char*)buffer + count,buf_len - count); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 989 | #else |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 990 | len = mbtk_openssl_write(inter_info->ssl,(const unsigned char *)buffer + count,buf_len - count); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 991 | |
| 992 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 993 | |
| 994 | #else |
| 995 | return -1; |
| 996 | #endif |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 997 | } else |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 998 | len = write(inter_info->fd,(char*)buffer + count,buf_len - count); |
| 999 | if(len < 0){ |
| 1000 | if(errno == EWOULDBLOCK){ |
| 1001 | usleep(50000); |
| 1002 | continue; |
| 1003 | } else { |
| 1004 | LOGE("write error.[%d]",errno); |
| 1005 | if(count <= 0) |
| 1006 | count = -1; |
| 1007 | break; |
| 1008 | } |
| 1009 | } else if(len == 0) { |
| 1010 | LOGE("write error(len == 0).[%d]",errno); |
| 1011 | } else { |
| 1012 | count += len; |
| 1013 | } |
| 1014 | } |
| 1015 | } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP){ |
| 1016 | // Start send data |
| 1017 | while(count < buf_len){ |
| 1018 | len = sendto(inter_info->fd,(char*)buffer + count,buf_len - count,0,NULL,0); |
| 1019 | if(len < 0){ |
| 1020 | if(errno == EWOULDBLOCK){ |
| 1021 | usleep(50000); |
| 1022 | continue; |
| 1023 | } else { |
| 1024 | LOGE("sendto error.[%d]",errno); |
| 1025 | if(ECONNREFUSED == errno) { // Disconnected. |
| 1026 | LOGD("Socket Disconnected."); |
| 1027 | } |
| 1028 | break; |
| 1029 | } |
| 1030 | } else if(len == 0) { |
| 1031 | LOGD("write error(len == 0).[%d]",errno); |
| 1032 | } else { |
| 1033 | count += len; |
| 1034 | } |
| 1035 | } |
| 1036 | } else { |
| 1037 | LOGE("Socket type error."); |
| 1038 | return -1; |
| 1039 | } |
| 1040 | |
| 1041 | if(count == buf_len){ |
| 1042 | LOGD("Write data[%d/%d] success.",count,buf_len); |
| 1043 | } else { // Open session fail |
| 1044 | LOGD("Write data[%d/%d] fail.",count,buf_len); |
| 1045 | } |
| 1046 | |
| 1047 | *mbtk_errno = MBTK_SOCK_SUCCESS; |
| 1048 | return count; |
| 1049 | } |
| 1050 | |
| 1051 | extern int mbtk_sock_read(mbtk_sock_handle handle,mbtk_sock_session session, |
| 1052 | void *buffer, |
| 1053 | unsigned int buf_len, |
| 1054 | unsigned int timeout, |
| 1055 | int *mbtk_errno) |
| 1056 | { |
| 1057 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 1058 | || session < 0 || mbtk_sock[handle] == NULL) { |
| 1059 | LOGE("Socket not inited."); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
| 1063 | *mbtk_errno = MBTK_SOCK_ERROR; |
| 1064 | if(buffer == NULL) { |
| 1065 | LOGE("mbtk_sock_write() args error."); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
| 1069 | mbtk_sock_inter_info_s *inter_info = NULL; |
| 1070 | int index = 0; |
| 1071 | while(index < MBTK_SOCK_MAX_NUM) { |
| 1072 | if(session == |
| 1073 | mbtk_sock[handle]->inter_infos[index].fd) { |
| 1074 | inter_info = &(mbtk_sock[handle]->inter_infos[index]); |
| 1075 | break; |
| 1076 | } |
| 1077 | index++; |
| 1078 | } |
| 1079 | |
| 1080 | if(!sock_info_check(handle,inter_info)) { |
| 1081 | LOGE("sock_info_check() fail."); |
| 1082 | return -1; |
| 1083 | } |
| 1084 | |
| 1085 | index = sock_info_find_by_fd(handle,inter_info->fd); |
| 1086 | if(index < 0) { |
| 1087 | LOGE("No such socket in session list."); |
| 1088 | return -1; |
| 1089 | } |
| 1090 | |
| 1091 | unsigned int count = 0; |
| 1092 | if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) { |
| 1093 | int len = 0; |
| 1094 | int try_count = 0; |
| 1095 | int times = timeout / 50; |
| 1096 | memset(buffer,0x0,buf_len); |
| 1097 | while(count < buf_len){ |
| 1098 | try_count++; |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1099 | if(mbtk_sock[handle]->infos[index].is_support_ssl) { |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1100 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1101 | #ifdef MBTK_POLARSSL_SUPPORT |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1102 | len = mbtk_polarssl_read(inter_info->ssl,(unsigned char*)buffer + count,buf_len - count); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1103 | #else |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1104 | len = mbtk_openssl_read(inter_info->ssl,(unsigned char *)buffer + count,buf_len - count); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1105 | |
| 1106 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1107 | #else |
| 1108 | return -1; |
| 1109 | #endif |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1110 | } else |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1111 | len = read(inter_info->fd,(char*)buffer + count,buf_len - count); |
| 1112 | if(len < 0){ |
| 1113 | if(errno == EWOULDBLOCK){ |
| 1114 | if(count > 0) // Read data |
| 1115 | break; // Read data end. |
| 1116 | |
| 1117 | if(try_count >= times){ // Timeout |
| 1118 | count = -1; |
| 1119 | if(times != 0) { |
| 1120 | *mbtk_errno = MBTK_SOCK_ETIMEOUT; |
| 1121 | } |
| 1122 | LOGE("Not read enough data,return.[%d/%d]",count,buf_len); |
| 1123 | break; |
| 1124 | } else { |
| 1125 | usleep(50000); |
| 1126 | continue; |
| 1127 | } |
| 1128 | } else { |
| 1129 | LOGE("read error.[%d]",errno); |
| 1130 | if(count <= 0) |
| 1131 | count = -1; |
| 1132 | break; |
| 1133 | } |
| 1134 | } else if(len == 0) { |
| 1135 | LOGE("read error(len == 0).[%d]",errno); |
| 1136 | if(errno == EINPROGRESS) { |
| 1137 | if(close(inter_info->fd) == 0) {// Success |
| 1138 | LOGD("Socket disconnected.Close it."); |
| 1139 | } |
| 1140 | if(count <= 0) |
| 1141 | count = -1; |
| 1142 | } else { |
| 1143 | if(count <= 0) |
| 1144 | count = 0; |
| 1145 | } |
| 1146 | break; |
| 1147 | } else { |
| 1148 | count += len; |
| 1149 | } |
| 1150 | } |
| 1151 | } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) { |
| 1152 | // Start recv data |
| 1153 | struct sockaddr_in seraddr; |
| 1154 | socklen_t seraddr_len; |
| 1155 | int try_count = 0; |
| 1156 | int times = timeout / 50; |
| 1157 | int len = 0; |
| 1158 | memset(buffer,0x0,buf_len); |
| 1159 | while(TRUE){ |
| 1160 | try_count++; |
| 1161 | seraddr_len = sizeof(struct sockaddr_in); |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1162 | len = recvfrom(inter_info->fd,buffer,buf_len,0,(struct sockaddr*)&seraddr,&seraddr_len); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1163 | if(len < 0){ |
| 1164 | if(errno == EWOULDBLOCK){// No data can read. |
| 1165 | if(count > 0) // Read data |
| 1166 | break; // Read data end. |
| 1167 | |
| 1168 | if(try_count >= times){ // Timeout |
| 1169 | if(times == 0) { |
| 1170 | LOGE("Can not read."); |
| 1171 | } else { |
| 1172 | LOGE("Timeout"); |
| 1173 | *mbtk_errno = MBTK_SOCK_ETIMEOUT; |
| 1174 | } |
| 1175 | count = -1; |
| 1176 | LOGE("Not read enough data,return.[%d/%d]",count,buf_len); |
| 1177 | break; |
| 1178 | } else { |
| 1179 | usleep(50000); |
| 1180 | continue; |
| 1181 | } |
| 1182 | } else { |
| 1183 | LOGE("recvfrom error.[%d]",errno); |
| 1184 | if(count <= 0) |
| 1185 | count = -1; |
| 1186 | break; |
| 1187 | } |
| 1188 | } else if(len == 0) { |
| 1189 | LOGE("write error(len == 0).[%d]",errno); |
| 1190 | if(count <= 0) |
| 1191 | count = 0; |
| 1192 | break; |
| 1193 | } else { |
| 1194 | count += len; |
| 1195 | } |
| 1196 | } |
| 1197 | } else { |
| 1198 | LOGE("Socket type error."); |
| 1199 | return -1; |
| 1200 | } |
| 1201 | |
| 1202 | // if(count == buf_len){ |
| 1203 | // LOGD("Read data[%d/%d] success.",count,buf_len); |
| 1204 | // } else { // Open session fail |
| 1205 | // LOGD("Read data[%d/%d] fail.",count,buf_len); |
| 1206 | // } |
| 1207 | |
| 1208 | LOGV("Read data[%d/%d].",count,buf_len); |
| 1209 | |
| 1210 | *mbtk_errno = MBTK_SOCK_SUCCESS; |
| 1211 | return count; |
| 1212 | } |
| 1213 | extern int mbtk_sock_readline(mbtk_sock_handle handle,mbtk_sock_session session, |
| 1214 | void *buffer, |
| 1215 | unsigned int buf_len, |
| 1216 | unsigned int timeout, |
| 1217 | int *mbtk_errno, |
| 1218 | int *read_line_count, |
| 1219 | char *buf_ptr) |
| 1220 | { |
| 1221 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 1222 | || session < 0 || mbtk_sock[handle] == NULL) { |
| 1223 | LOGE("Socket not inited."); |
| 1224 | return -1; |
| 1225 | } |
| 1226 | |
| 1227 | *mbtk_errno = MBTK_SOCK_ERROR; |
| 1228 | if(buffer == NULL) { |
| 1229 | LOGE("mbtk_sock_write() args error."); |
| 1230 | return -1; |
| 1231 | } |
| 1232 | |
| 1233 | mbtk_sock_inter_info_s *inter_info = NULL; |
| 1234 | int index = 0; |
| 1235 | while(index < MBTK_SOCK_MAX_NUM) { |
| 1236 | if(session == |
| 1237 | mbtk_sock[handle]->inter_infos[index].fd) { |
| 1238 | inter_info = &(mbtk_sock[handle]->inter_infos[index]); |
| 1239 | break; |
| 1240 | } |
| 1241 | index++; |
| 1242 | } |
| 1243 | |
| 1244 | if(!sock_info_check(handle,inter_info)) { |
| 1245 | LOGE("sock_info_check() fail."); |
| 1246 | return -1; |
| 1247 | } |
| 1248 | |
| 1249 | index = sock_info_find_by_fd(handle,inter_info->fd); |
| 1250 | if(index < 0) { |
| 1251 | LOGE("No such socket in session list."); |
| 1252 | return -1; |
| 1253 | } |
| 1254 | |
| 1255 | unsigned int count = 0; |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1256 | // unsigned int read_count = 0; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1257 | memset(buf_ptr, 0, buf_len); |
| 1258 | char *temp_ptr = (char *)buffer; |
| 1259 | copy_angin_ssl: |
| 1260 | while(*read_line_count > 0 && *temp_ptr != '\n') { |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1261 | if(temp_ptr == NULL) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1262 | { |
| 1263 | printf("\n*temp_ptr is null\n"); |
| 1264 | goto read_end; |
| 1265 | } |
| 1266 | *buf_ptr++ = *temp_ptr++; |
| 1267 | (*read_line_count)--; |
| 1268 | count++; |
| 1269 | } |
| 1270 | if(*read_line_count == 0) |
| 1271 | { |
| 1272 | if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) { |
| 1273 | int len = 0; |
| 1274 | int try_count = 0; |
| 1275 | int times = timeout / 50; |
| 1276 | memset(buffer,0x0,buf_len); |
| 1277 | while(count < buf_len){ |
| 1278 | try_count++; |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1279 | if(mbtk_sock[handle]->infos[index].is_support_ssl) { |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1280 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1281 | #ifdef MBTK_POLARSSL_SUPPORT |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1282 | len = mbtk_polarssl_read(inter_info->ssl,(unsigned char*)buffer + count,buf_len - count); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1283 | #else |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1284 | len = mbtk_openssl_read(inter_info->ssl,(unsigned char*)buffer + count,buf_len - count); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1285 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1286 | #else |
| 1287 | return -1; |
| 1288 | #endif |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1289 | } else |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1290 | len = read(inter_info->fd,(char*)buffer + count,buf_len - count); |
| 1291 | *read_line_count = len; |
| 1292 | if(len < 0){ |
| 1293 | if(errno == EWOULDBLOCK){ |
| 1294 | if(count > 0) // Read data |
| 1295 | { |
| 1296 | *read_line_count = count; |
| 1297 | count = 0; |
| 1298 | goto copy_angin_ssl; |
| 1299 | break; // Read data end. |
| 1300 | } |
| 1301 | else |
| 1302 | { |
| 1303 | //printf("\nread_end\n"); |
| 1304 | goto read_end; |
| 1305 | } |
| 1306 | if(try_count >= times){ // Timeout |
| 1307 | count = -1; |
| 1308 | if(times != 0) { |
| 1309 | *mbtk_errno = MBTK_SOCK_ETIMEOUT; |
| 1310 | } |
| 1311 | LOGE("Not read enough data,return.[%d/%d]",count,buf_len); |
| 1312 | goto read_end; |
| 1313 | break; |
| 1314 | } else { |
| 1315 | usleep(50000); |
| 1316 | continue; |
| 1317 | } |
| 1318 | } else { |
| 1319 | LOGE("read error.[%d]",errno); |
| 1320 | if(count <= 0) |
| 1321 | count = -1; |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 1322 | else { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1323 | *read_line_count = count; |
| 1324 | } |
| 1325 | break; |
| 1326 | } |
| 1327 | } else if(len == 0) { |
| 1328 | LOGE("read error(len == 0).[%d]",errno); |
| 1329 | if(errno == EINPROGRESS) { |
| 1330 | if(close(inter_info->fd) == 0) {// Success |
| 1331 | LOGD("Socket disconnected.Close it."); |
| 1332 | } |
| 1333 | if(count <= 0) |
| 1334 | count = -1; |
| 1335 | } else { |
| 1336 | if(count <= 0) |
| 1337 | count = 0; |
| 1338 | else |
| 1339 | count = -1; |
| 1340 | } |
| 1341 | goto read_end; |
| 1342 | break; |
| 1343 | } else { |
| 1344 | count += len; |
| 1345 | } |
| 1346 | } |
| 1347 | } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) { |
| 1348 | // Start recv data |
| 1349 | struct sockaddr_in seraddr; |
| 1350 | socklen_t seraddr_len; |
| 1351 | int try_count = 0; |
| 1352 | int times = timeout / 50; |
| 1353 | int len = 0; |
| 1354 | memset(buffer,0x0,buf_len); |
| 1355 | while(TRUE){ |
| 1356 | try_count++; |
| 1357 | seraddr_len = sizeof(struct sockaddr_in); |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1358 | len = recvfrom(inter_info->fd,buffer,buf_len,0,(struct sockaddr*)&seraddr,&seraddr_len); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1359 | if(len < 0){ |
| 1360 | if(errno == EWOULDBLOCK){// No data can read. |
| 1361 | if(count > 0) // Read data |
| 1362 | break; // Read data end. |
| 1363 | |
| 1364 | if(try_count >= times){ // Timeout |
| 1365 | if(times == 0) { |
| 1366 | LOGE("Can not read."); |
| 1367 | //printf("Can not read.\n"); |
| 1368 | } else { |
| 1369 | LOGE("Timeout"); |
| 1370 | //printf("Timeout\n"); |
| 1371 | *mbtk_errno = MBTK_SOCK_ETIMEOUT; |
| 1372 | } |
| 1373 | count = -1; |
| 1374 | LOGE("Not read enough data,return.[%d/%d]",count,buf_len); |
| 1375 | //printf("Not read enough data,return.[%d/%d]\n",count,buf_len); |
| 1376 | break; |
| 1377 | } else { |
| 1378 | usleep(50000); |
| 1379 | continue; |
| 1380 | } |
| 1381 | } else { |
| 1382 | LOGE("recvfrom error.[%d]",errno); |
| 1383 | if(count <= 0) |
| 1384 | count = -1; |
| 1385 | break; |
| 1386 | } |
| 1387 | } else if(len == 0) { |
| 1388 | LOGE("write error(len == 0).[%d]",errno); |
| 1389 | if(count <= 0) |
| 1390 | count = 0; |
| 1391 | break; |
| 1392 | } else { |
| 1393 | count += len; |
| 1394 | } |
| 1395 | } |
| 1396 | } else { |
| 1397 | LOGE("Socket type error."); |
| 1398 | //printf("Socket type error.\n"); |
| 1399 | return -1; |
| 1400 | } |
| 1401 | count = 0; |
| 1402 | goto copy_angin_ssl; |
| 1403 | } else if(*temp_ptr == '\n') { // Read line. |
| 1404 | *buf_ptr++ = '\n'; |
| 1405 | (*read_line_count)--; |
| 1406 | count++; |
| 1407 | |
| 1408 | if(*read_line_count > 0) |
| 1409 | memcpy(buffer, temp_ptr + 1, *read_line_count); |
| 1410 | return count; |
| 1411 | } |
| 1412 | LOGV("Read data[%d/%d].",count,buf_len); |
| 1413 | read_end: |
| 1414 | *mbtk_errno = MBTK_SOCK_SUCCESS; |
| 1415 | return count; |
| 1416 | } |
| 1417 | |
| 1418 | extern int mbtk_sock_read_async(mbtk_sock_handle handle,mbtk_sock_session session, |
| 1419 | void *buffer, |
| 1420 | unsigned int buf_len) |
| 1421 | { |
| 1422 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 1423 | || session < 0 || mbtk_sock[handle] == NULL) { |
| 1424 | LOGE("Socket not inited."); |
| 1425 | return -1; |
| 1426 | } |
| 1427 | |
| 1428 | if(buffer == NULL) { |
| 1429 | LOGE("mbtk_sock_write() args error."); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | |
| 1433 | mbtk_sock_inter_info_s *inter_info = NULL; |
| 1434 | int index = 0; |
| 1435 | while(index < MBTK_SOCK_MAX_NUM) { |
| 1436 | if(session == |
| 1437 | mbtk_sock[handle]->inter_infos[index].fd) { |
| 1438 | inter_info = &(mbtk_sock[handle]->inter_infos[index]); |
| 1439 | break; |
| 1440 | } |
| 1441 | index++; |
| 1442 | } |
| 1443 | if(!sock_info_check(handle,inter_info)) { |
| 1444 | LOGE("sock_info_check() fail."); |
| 1445 | return -1; |
| 1446 | } |
| 1447 | |
| 1448 | index = sock_info_find_by_fd(handle,inter_info->fd); |
| 1449 | if(index < 0) { |
| 1450 | LOGE("No such socket in session list."); |
| 1451 | return -1; |
| 1452 | } |
| 1453 | |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 1454 | int len = 0; |
| 1455 | int read_count = 0; |
| 1456 | if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) { |
| 1457 | memset(buffer,0x0,buf_len); |
| 1458 | while(read_count < buf_len) { |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1459 | if(mbtk_sock[handle]->infos[index].is_support_ssl) { |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1460 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1461 | #ifdef MBTK_POLARSSL_SUPPORT |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1462 | len = ssl_read(inter_info->ssl,(unsigned char*)buffer + read_count,buf_len - read_count); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1463 | #else |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1464 | len = mbtk_openssl_read(inter_info->ssl,(unsigned char*)buffer + read_count,buf_len - read_count); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1465 | |
| 1466 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1467 | #else |
| 1468 | return -1; |
| 1469 | #endif |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1470 | } else |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 1471 | len = read(inter_info->fd,(char*)buffer + read_count,buf_len - read_count); |
| 1472 | |
| 1473 | if(len > 0) { |
| 1474 | read_count += len; |
| 1475 | } else { |
b.liu | 94baa7c | 2023-09-26 16:26:10 +0800 | [diff] [blame] | 1476 | if(errno == EWOULDBLOCK) { // No data |
| 1477 | break; |
| 1478 | } else { |
| 1479 | LOGE("Will retry : len = %d, errno = %d", len, errno); |
| 1480 | } |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) { |
| 1484 | // Start recv data |
| 1485 | struct sockaddr_in seraddr; |
| 1486 | socklen_t seraddr_len; |
| 1487 | memset(buffer,0x0,buf_len); |
| 1488 | seraddr_len = sizeof(struct sockaddr_in); |
| 1489 | memset(buffer,0x0,buf_len); |
| 1490 | |
| 1491 | while(read_count < buf_len) { |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1492 | len = recvfrom(inter_info->fd,buffer + read_count,buf_len - read_count,0,(struct sockaddr*)&seraddr,&seraddr_len); |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 1493 | |
| 1494 | if(len > 0) { |
| 1495 | read_count += len; |
| 1496 | } else { |
b.liu | 94baa7c | 2023-09-26 16:26:10 +0800 | [diff] [blame] | 1497 | if(errno == EWOULDBLOCK) { // No data |
| 1498 | break; |
| 1499 | } else { |
| 1500 | LOGE("Will retry : len = %d, errno = %d", len, errno); |
| 1501 | } |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | } else { |
| 1505 | LOGE("Socket type error."); |
| 1506 | return -1; |
| 1507 | } |
| 1508 | |
b.liu | 94baa7c | 2023-09-26 16:26:10 +0800 | [diff] [blame] | 1509 | LOGV("Read data[%d/%d].",read_count,buf_len); |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 1510 | |
| 1511 | return read_count; |
| 1512 | } |
| 1513 | |
| 1514 | extern int mbtk_sock_read_sync(mbtk_sock_handle handle,mbtk_sock_session session, |
| 1515 | void *buffer, |
| 1516 | unsigned int buf_len) |
| 1517 | { |
| 1518 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 1519 | || session < 0 || mbtk_sock[handle] == NULL) { |
| 1520 | LOGE("Socket not inited."); |
| 1521 | return -1; |
| 1522 | } |
| 1523 | |
| 1524 | if(buffer == NULL) { |
| 1525 | LOGE("mbtk_sock_write() args error."); |
| 1526 | return -1; |
| 1527 | } |
| 1528 | |
| 1529 | mbtk_sock_inter_info_s *inter_info = NULL; |
| 1530 | int index = 0; |
| 1531 | while(index < MBTK_SOCK_MAX_NUM) { |
| 1532 | if(session == |
| 1533 | mbtk_sock[handle]->inter_infos[index].fd) { |
| 1534 | inter_info = &(mbtk_sock[handle]->inter_infos[index]); |
| 1535 | break; |
| 1536 | } |
| 1537 | index++; |
| 1538 | } |
| 1539 | if(!sock_info_check(handle,inter_info)) { |
| 1540 | LOGE("sock_info_check() fail."); |
| 1541 | return -1; |
| 1542 | } |
| 1543 | |
| 1544 | index = sock_info_find_by_fd(handle,inter_info->fd); |
| 1545 | if(index < 0) { |
| 1546 | LOGE("No such socket in session list."); |
| 1547 | return -1; |
| 1548 | } |
| 1549 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1550 | int len; |
| 1551 | if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) { |
| 1552 | TCP_READ_AGAIN: |
| 1553 | memset(buffer,0x0,buf_len); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1554 | if(mbtk_sock[handle]->infos[index].is_support_ssl) { |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1555 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1556 | #ifdef MBTK_POLARSSL_SUPPORT |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1557 | len = ssl_read(inter_info->ssl,(unsigned char*)buffer,buf_len); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1558 | #else |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1559 | len = mbtk_openssl_read(inter_info->ssl,(unsigned char*)buffer,buf_len); |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1560 | |
| 1561 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1562 | #else |
| 1563 | return -1; |
| 1564 | #endif |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1565 | } else |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1566 | len = read(inter_info->fd,(char*)buffer,buf_len); |
| 1567 | if(len < 0){ |
| 1568 | if(errno == EWOULDBLOCK){ |
| 1569 | usleep(100000); |
b.liu | eb04065 | 2023-09-25 18:50:56 +0800 | [diff] [blame] | 1570 | LOGW("Read retry..."); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1571 | goto TCP_READ_AGAIN; |
| 1572 | } else { |
| 1573 | LOGE("read error.[%d]",errno); |
| 1574 | return -1; |
| 1575 | } |
| 1576 | } |
| 1577 | } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) { |
| 1578 | // Start recv data |
| 1579 | struct sockaddr_in seraddr; |
| 1580 | socklen_t seraddr_len; |
| 1581 | UDP_READ_AGAIN: |
| 1582 | memset(buffer,0x0,buf_len); |
| 1583 | seraddr_len = sizeof(struct sockaddr_in); |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1584 | len = recvfrom(inter_info->fd,buffer,buf_len,0,(struct sockaddr*)&seraddr,&seraddr_len); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1585 | if(len < 0){ |
| 1586 | if(errno == EWOULDBLOCK){ |
| 1587 | usleep(100000); |
| 1588 | goto UDP_READ_AGAIN; |
| 1589 | } else { |
| 1590 | LOGE("read error.[%d]",errno); |
| 1591 | return -1; |
| 1592 | } |
| 1593 | } |
| 1594 | } else { |
| 1595 | LOGE("Socket type error."); |
| 1596 | return -1; |
| 1597 | } |
| 1598 | |
| 1599 | LOGV("Read data[%d/%d].",len,buf_len); |
| 1600 | |
| 1601 | return len; |
| 1602 | } |
| 1603 | |
b.liu | 94baa7c | 2023-09-26 16:26:10 +0800 | [diff] [blame] | 1604 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1605 | extern int mbtk_sock_close(mbtk_sock_handle handle,mbtk_sock_session session, |
| 1606 | unsigned int timeout, |
| 1607 | int *mbtk_errno) |
| 1608 | { |
| 1609 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 1610 | || session < 0 || mbtk_sock[handle] == NULL) { |
| 1611 | LOGE("Socket not inited."); |
| 1612 | return -1; |
| 1613 | } |
| 1614 | |
| 1615 | *mbtk_errno = MBTK_SOCK_ERROR; |
| 1616 | mbtk_sock_inter_info_s *inter_info = NULL; |
| 1617 | int index = 0; |
| 1618 | while(index < MBTK_SOCK_MAX_NUM) { |
| 1619 | if(session == mbtk_sock[handle]->inter_infos[index].fd) { |
| 1620 | inter_info = &(mbtk_sock[handle]->inter_infos[index]); |
| 1621 | break; |
| 1622 | } |
| 1623 | index++; |
| 1624 | } |
| 1625 | if(!sock_info_check(handle,inter_info)) { |
| 1626 | LOGE("sock_info_check() fail."); |
| 1627 | return -1; |
| 1628 | } |
| 1629 | |
| 1630 | index = sock_info_find_by_fd(handle,inter_info->fd); |
| 1631 | if(index < 0) { |
| 1632 | LOGE("No such socket in session list."); |
| 1633 | return -1; |
| 1634 | } |
| 1635 | |
| 1636 | int i; |
| 1637 | for(i = 0;i < MBTK_SOCK_MAX_NUM;i++) { |
| 1638 | if(mbtk_sock[handle]->inter_infos[i].fd == inter_info->fd){ |
| 1639 | if(mbtk_sock[handle]->init_info.sock_cb) { |
| 1640 | struct epoll_event ev; |
| 1641 | ev.data.fd = inter_info->fd; |
| 1642 | ev.events = EPOLLIN | EPOLLET; |
| 1643 | epoll_ctl(epoll_fd,EPOLL_CTL_DEL,inter_info->fd,&ev); |
| 1644 | } |
| 1645 | |
| 1646 | if(close(inter_info->fd) < 0) {// Success |
| 1647 | LOGE("Close socket fail[%d].",errno); |
| 1648 | //break; |
| 1649 | } |
| 1650 | mbtk_sock[handle]->inter_infos[i].fd = -1; |
| 1651 | memset(&(mbtk_sock[handle]->infos[i]),0x0,sizeof(mbtk_sock_info)); |
| 1652 | mbtk_sock[handle]->sock_num--; |
| 1653 | break; |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | if(mbtk_sock[handle]->infos[index].is_support_ssl){ |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1658 | #ifdef MBTK_SSL_SUPPORT |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1659 | #ifdef MBTK_POLARSSL_SUPPORT |
| 1660 | if(mbtk_polarssl_close(inter_info)== -1) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1661 | { |
| 1662 | LOGE("close ssl fail"); |
| 1663 | return -1; |
| 1664 | } |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1665 | #else |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 1666 | if(mbtk_openssl_close(inter_info)== -1) |
| 1667 | { |
| 1668 | LOGE("close ssl fail"); |
| 1669 | return -1; |
| 1670 | } |
b.liu | 9a8e82b | 2023-10-10 16:09:50 +0800 | [diff] [blame] | 1671 | |
| 1672 | #endif |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame^] | 1673 | #endif |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | *mbtk_errno = MBTK_SOCK_SUCCESS; |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | extern int mbtk_sock_deinit(mbtk_sock_handle handle) |
| 1681 | { |
| 1682 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 1683 | || mbtk_sock[handle] == NULL) { |
| 1684 | LOGE("Socket not inited."); |
| 1685 | return -1; |
| 1686 | } |
| 1687 | |
| 1688 | if(mbtk_sock[handle]->sock_num > 0) { |
| 1689 | LOGE("There are socket not close."); |
| 1690 | return MBTK_SOCK_ERROR; |
| 1691 | } |
| 1692 | |
| 1693 | LOGD("mbtk_sock_deinit() start."); |
| 1694 | #if 0 |
| 1695 | sock_thread_running = FALSE; |
| 1696 | write(pipe_fds[0],"0",1); |
| 1697 | |
| 1698 | // Wait for thread exist. |
| 1699 | while(sock_inited) { |
| 1700 | usleep(100); |
| 1701 | } |
| 1702 | #endif |
| 1703 | |
| 1704 | int i; |
| 1705 | for(i = 0;i < MBTK_SOCK_MAX_NUM;i++) { |
| 1706 | if(mbtk_sock[handle]->inter_infos[i].fd > 0){ |
| 1707 | if(mbtk_sock[handle]->init_info.sock_cb) { |
| 1708 | struct epoll_event ev; |
| 1709 | ev.data.fd = mbtk_sock[handle]->inter_infos[i].fd; |
| 1710 | ev.events = EPOLLIN | EPOLLET; |
| 1711 | epoll_ctl(epoll_fd,EPOLL_CTL_DEL,mbtk_sock[handle]->inter_infos[i].fd,&ev); |
| 1712 | } |
| 1713 | |
| 1714 | if(close(mbtk_sock[handle]->inter_infos[i].fd) < 0) {// Success |
| 1715 | LOGE("Close socket fail[%d].",errno); |
| 1716 | //break; |
| 1717 | } |
| 1718 | mbtk_sock[handle]->inter_infos[i].fd = -1; |
| 1719 | memset(&(mbtk_sock[handle]->infos[i]),0x0,sizeof(mbtk_sock_info)); |
| 1720 | break; |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | //memset(&mbtk_sock,0x0,sizeof(mbtk_sock_s)); |
| 1725 | free(mbtk_sock[handle]); |
| 1726 | mbtk_sock[handle] = NULL; |
| 1727 | LOGD("mbtk_sock_deinit() end."); |
| 1728 | return MBTK_SOCK_SUCCESS; |
| 1729 | } |
| 1730 | |
b.liu | 94baa7c | 2023-09-26 16:26:10 +0800 | [diff] [blame] | 1731 | /* |
| 1732 | * Get TCP RECV buffer data length. |
| 1733 | */ |
| 1734 | int mbtk_sock_tcp_recv_len_get(mbtk_sock_handle handle,mbtk_sock_session session) |
| 1735 | { |
| 1736 | if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM |
| 1737 | || session < 0 || mbtk_sock[handle] == NULL) { |
| 1738 | LOGE("Socket not inited."); |
| 1739 | return -1; |
| 1740 | } |
| 1741 | |
| 1742 | mbtk_sock_inter_info_s *inter_info = NULL; |
| 1743 | int index = 0; |
| 1744 | while(index < MBTK_SOCK_MAX_NUM) { |
| 1745 | if(session == |
| 1746 | mbtk_sock[handle]->inter_infos[index].fd) { |
| 1747 | inter_info = &(mbtk_sock[handle]->inter_infos[index]); |
| 1748 | break; |
| 1749 | } |
| 1750 | index++; |
| 1751 | } |
| 1752 | if(!sock_info_check(handle,inter_info)) { |
| 1753 | LOGE("sock_info_check() fail."); |
| 1754 | return -1; |
| 1755 | } |
| 1756 | |
| 1757 | index = sock_info_find_by_fd(handle,inter_info->fd); |
| 1758 | if(index < 0) { |
| 1759 | LOGE("No such socket in session list."); |
| 1760 | return -1; |
| 1761 | } |
| 1762 | |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1763 | // unsigned int count = 0; |
b.liu | 94baa7c | 2023-09-26 16:26:10 +0800 | [diff] [blame] | 1764 | int len = 0; |
| 1765 | if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) { |
| 1766 | if(ioctl(inter_info->fd, FIONREAD, &len)) |
| 1767 | { |
| 1768 | LOGE("Get ioctl FIONREAD fail:%d", errno); |
| 1769 | return -1; |
| 1770 | } |
| 1771 | } else { |
| 1772 | LOGE("Only surrport for TCP."); |
| 1773 | return -1; |
| 1774 | } |
| 1775 | |
| 1776 | return len; |
| 1777 | } |
| 1778 | |
b.liu | bcf86c9 | 2024-08-19 19:48:28 +0800 | [diff] [blame] | 1779 | void mbtk_net_lib_info_print() |
| 1780 | { |
| 1781 | MBTK_SOURCE_INFO_PRINT("mbtk_net_lib"); |
| 1782 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1783 | |
luojin | 8fbb343 | 2023-10-18 09:47:46 +0800 | [diff] [blame] | 1784 | |