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