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