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