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