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