blob: a5eb174ec2adaef8fc369ff7ac026084a0b9550a [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>
b.liudeb8e422024-12-14 17:36:56 +080013#include <pthread.h>
b.liu94baa7c2023-09-26 16:26:10 +080014#include <sys/ioctl.h>
liubin281ac462023-07-19 14:22:54 +080015
16#ifdef LOG_TAG
17#undef LOG_TAG
18#endif
19#define LOG_TAG "mbtk_sock"
20#include <mbtk_log.h>
21
22#include "mbtk_sock2.h"
23#include "mbtk_sock_internal.h"
b.liu9e8584b2024-11-06 19:21:28 +080024#include "mbtk_str.h"
liubin281ac462023-07-19 14:22:54 +080025//#include "mbtk_openssl.h"
26
yq.wang39b54a22025-04-11 15:25:50 +080027#define DFL_CA_FILE "/ca.crt"
28#define DFL_CRT_FILE "/client.crt"
29#define DFL_KEY_FILE "/client.key"
30
liubin281ac462023-07-19 14:22:54 +080031#define SA struct sockaddr
32
33// Must define LOG_TAG in the first.
34//#include "mbtk_log.h"
35static int epoll_fd = -1;
36static int pipe_fds[2];
37static struct epoll_event epoll_events[20];
b.liudeb8e422024-12-14 17:36:56 +080038static pthread_t sock_thread_id;
liubin281ac462023-07-19 14:22:54 +080039static bool sock_thread_running = FALSE;
40static mbtk_sock_s *mbtk_sock[MBTK_HANDLE_MAX_NUM] = {NULL};
41
42static int sock_find_first_free(const mbtk_sock_inter_info_s *inter_info)
43{
44 if(inter_info == NULL) {
45 LOGE("inter_info is NULL.");
46 return -1;
47 }
48
49 int index = 0;
50 //while((inter_info + index)->fd > 0) {
51 while(inter_info[index].fd > 0) {
52 index++;
53 }
54
55 if(index == MBTK_SOCK_MAX_NUM) {
56 LOGE("sock_infos too more.");
57 return -1;
58 }
59
60 return index;
61}
62
63static bool sock_info_check(int handle,mbtk_sock_inter_info_s *inter_info)
64{
65 if(inter_info == NULL || mbtk_sock[handle] == NULL) {
66 LOGE("internal_info is NULL.");
67 return FALSE;
68 }
69
70 int index = 0;
71 while(index < MBTK_SOCK_MAX_NUM) {
72 if(inter_info->fd ==
73 mbtk_sock[handle]->inter_infos[index].fd) {
74 return TRUE;
75 }
76 index++;
77 }
78
79 return FALSE;
80}
81
b.liu9e8584b2024-11-06 19:21:28 +080082#if 0
liubin281ac462023-07-19 14:22:54 +080083static bool sock_is_close(int sockfd)
84{
85 char buff[32];
86 int recvBytes = recv(sockfd, buff, sizeof(buff), MSG_PEEK);
87
88 int err = errno;
89 //cout << "In close function, recv " << recvBytes << " bytes, err " << sockErr << endl;
90
91 if(recvBytes > 0) //Get data
92 return FALSE;
93
94 if((recvBytes == -1) && (err == EWOULDBLOCK)) //No receive data
95 return FALSE;
96
97 return TRUE;
98}
b.liu9e8584b2024-11-06 19:21:28 +080099#endif
liubin281ac462023-07-19 14:22:54 +0800100
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) {
b.liu94baa7c2023-09-26 16:26:10 +0800135 LOGV("fd[%d] event = %x",epoll_events[i].data.fd,epoll_events[i].events);
liubin281ac462023-07-19 14:22:54 +0800136 if(pipe_fds[1] == epoll_events[i].data.fd) {
137 LOGD("Get exist sig.");
138 sock_thread_running = FALSE;
139 break;
140 }
141
142 int handle = 0;
143 while(handle < MBTK_HANDLE_MAX_NUM) {
144 if(mbtk_sock[handle] != NULL) {
145 int index = sock_info_find_by_fd(handle,epoll_events[i].data.fd);
146 if(index >= 0 && mbtk_sock[handle]->init_info.sock_cb != NULL) {
147 mbtk_sock_inter_info_s *inter_info = &(mbtk_sock[handle]->inter_infos[index]);
b.liu8181e142023-09-26 10:31:10 +0800148 mbtk_sock_info *info = &(mbtk_sock[handle]->infos[index]);
liubin281ac462023-07-19 14:22:54 +0800149
150 //if(sock_is_close(epoll_events[i].data.fd)) {
151 // LOGE("Socket %d is closed.",epoll_events[i].data.fd);
152 // break;
153 //}
b.liu8181e142023-09-26 10:31:10 +0800154 mbtk_sock_cb_info_s sock_info;
155 sock_info.sock_fd = inter_info->fd;
156 sock_info.event = epoll_events[i].events;
157 sock_info.sock_type = info->type;
158 mbtk_sock[handle]->init_info.sock_cb(handle, &sock_info);
liubin281ac462023-07-19 14:22:54 +0800159
160 if(epoll_events[i].events & EPOLLERR){ // error[ UDP server can't use.]
161 LOGD("%d EPOLLERR.",epoll_events[i].data.fd);
162 }
163
164 if ((epoll_events[i].events & EPOLLIN)
165 && (epoll_events[i].events & EPOLLOUT)) {
166 LOGD("%d can read and write.",epoll_events[i].data.fd);
167 int error = -1;
b.liu9e8584b2024-11-06 19:21:28 +0800168 socklen_t len = sizeof(int);
liubin281ac462023-07-19 14:22:54 +0800169 if(getsockopt(epoll_events[i].data.fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0){
170 LOGE("getsockopt fail.[%d]",errno);
171 }else{
172 LOGD("error = %d",error);
173 }
174 }
175
176 if (epoll_events[i].events & EPOLLOUT) { // Can write.
177 LOGD("%d can write.",epoll_events[i].data.fd);
178 }
179
180 if (epoll_events[i].events & EPOLLIN) { // Can read.
181 LOGD("%d can read.",epoll_events[i].data.fd);
182 }
183 }
184 }
185
186 handle++;
187 }
188 }
189 }
190
191 LOGD("socket thread exit.");
192 return ((void*)0);
193}
194
195static int sock_thread_start()
196{
197 sock_thread_running = TRUE;
198 if (0 != pthread_create(&sock_thread_id, NULL, sock_thread_run, NULL))
199 {
200 LOGE("error when create pthread,%d\n", errno);
201 return -1;
202 }
203
204 return 0;
205}
206
b.liueb040652023-09-25 18:50:56 +0800207void net_state_callback_func(mbtk_net_change_type_t type, const void *data)
208{
209 if(type == MBTK_NET_CHANGE_ADDR && data != NULL) {
210 int handle = 0;
211 const mbtk_net_addr_change_info_t *addr_info = (const mbtk_net_addr_change_info_t *)data;
212 while(handle < MBTK_HANDLE_MAX_NUM) {
213 if(mbtk_sock[handle] != NULL) {
214 if(mbtk_sock[handle]->init_info.net_cb != NULL) {
b.liu8181e142023-09-26 10:31:10 +0800215 mbtk_net_cb_info_s net_info;
216 net_info.state = (addr_info->type == MBTK_NET_ADDR_CHANGE_TYPE_ADD) ? 1 : 0;
217 net_info.addr = addr_info->addr;
218 net_info.if_name = addr_info->if_name;
219 mbtk_sock[handle]->init_info.net_cb(handle, &net_info);
b.liueb040652023-09-25 18:50:56 +0800220 }
221 }
222
223 handle++;
224 }
225 }
226}
227
yq.wang39b54a22025-04-11 15:25:50 +0800228#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
229static int mbtk_sock_openssl_init(int fd, bool ingnore_cert, mbtk_openssl_info_s *inter_info)
230{
231 if(NULL == inter_info)
232 {
233 LOGE("[%s] inter_info [NULL]", __func__);
234 return -1;
235 }
236
237 mbtk_openssl_result_e mbtk_ssl_ret = MBTK_OPENSSL_RESULT_SUCCESS;
238 mbtk_openssl_options_s opt = {0};
239
240 mbtk_openssl_options_default(&opt);
241 if(!ingnore_cert)
242 {
243 opt.load_cert = true;
244 opt.ca_file = DFL_CA_FILE;
245 opt.crt_file = DFL_CRT_FILE;
246 opt.key_file = DFL_KEY_FILE;
247 opt.safety_level = MBTK_OPENSSL_SAFETY_LEVEL_0;
248 }
249
250 mbtk_ssl_ret = mbtk_openssl_init(fd, &opt, inter_info);
251 if(MBTK_OPENSSL_RESULT_SUCCESS != mbtk_ssl_ret)
252 {
253 LOGE("[%s] mbtk_openssl_init() fail", __func__);
254 return -1;
255 }
256
257 return 0;
258}
259#endif
260
liubin281ac462023-07-19 14:22:54 +0800261extern mbtk_sock_handle mbtk_sock_init(mbtk_init_info *info)
262{
263 mbtk_sock_handle handle = 0;
264 while(handle < MBTK_HANDLE_MAX_NUM) {
265 if(mbtk_sock[handle] == NULL)
266 break;
267
268 handle++;
269 }
270
271 if(handle == MBTK_HANDLE_MAX_NUM) {
272 LOGE("Socket handle is full.");
273 return -1;
274 }
275
276 mbtk_sock[handle] = (mbtk_sock_s*)malloc(sizeof(mbtk_sock_s));
277 memset(mbtk_sock[handle],0x0,sizeof(mbtk_sock_s));
278 if(info != NULL) {
279 mbtk_sock[handle]->init_info.net_type = info->net_type;
280 mbtk_sock[handle]->init_info.net_cb = info->net_cb;
281 mbtk_sock[handle]->init_info.sock_cb = info->sock_cb;
b.liueb040652023-09-25 18:50:56 +0800282 if(!str_empty(info->if_name)) {
283 memcpy(mbtk_sock[handle]->init_info.if_name, info->if_name, strlen(info->if_name));
284 }
liubin281ac462023-07-19 14:22:54 +0800285 } else {
286 mbtk_sock[handle]->init_info.net_type = MBTK_NET_LINUX;
287 mbtk_sock[handle]->init_info.net_cb = NULL;
288 mbtk_sock[handle]->init_info.sock_cb = NULL;
289 }
290
291 if(!sock_thread_running) {
292 epoll_fd = epoll_create(256);
293 if(sock_thread_start()) {
294 LOGE("Start thread fail.");
295 return -1;
296 }
297 }
298
b.liueb040652023-09-25 18:50:56 +0800299 if(mbtk_net_monitor_reg(str_empty(info->if_name) ? NULL : info->if_name, net_state_callback_func)) {
300 LOGE("mbtk_net_monitor_reg() fail.");
301 return -1;
302 }
303
liubin281ac462023-07-19 14:22:54 +0800304 return handle;
305}
306
liubin281ac462023-07-19 14:22:54 +0800307extern mbtk_sock_session mbtk_sock_open(mbtk_sock_handle handle,mbtk_sock_info *info,
308 unsigned int timeout,
309 int *mbtk_errno)
310{
311 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
312 || mbtk_sock[handle] == NULL) {
313 LOGE("Socket not inited.");
314 return -1;
315 }
316
317 *mbtk_errno = MBTK_SOCK_ERROR;
318 if(info == NULL) {
319 LOGE("mbtk_sock_info not be NULL.");
320 return -1;
321 }
322
323 int index_free = sock_find_first_free(mbtk_sock[handle]->inter_infos);
324 if(index_free < 0) {
325 LOGE("sock_find_first_free() fail.");
326 return -1;
327 }
328
329 memcpy(&(mbtk_sock[handle]->infos[index_free]),info,sizeof(mbtk_sock_info));
330 if(info->type == MBTK_SOCK_UDP) { // UDP
331 if((mbtk_sock[handle]->inter_infos[index_free].fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
332 LOGE("socket() fail.[%d]",errno);
333 goto result_fail;
334 }
335 } else { // TCP
336 if((mbtk_sock[handle]->inter_infos[index_free].fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
337 LOGE("socket() fail.[%d]",errno);
338 goto result_fail;
339 }
340 }
341 // Set O_NONBLOCK
342 int flags = fcntl(mbtk_sock[handle]->inter_infos[index_free].fd, F_GETFL, 0);
343 if (flags < 0) {
344 LOGE("Get flags error:%s\n", strerror(errno));
345 goto result_fail_with_close;
346 }
347 flags |= O_NONBLOCK;
348 if (fcntl(mbtk_sock[handle]->inter_infos[index_free].fd, F_SETFL, flags) < 0) {
349 LOGE("Set flags error:%s\n", strerror(errno));
350 goto result_fail_with_close;
351 }
352
353 // Connect
354 LOGD("Start conn:%s:%d",info->address,info->port);
355 if(strlen(info->address) > 0 && info->port > 0) {
b.liueb040652023-09-25 18:50:56 +0800356 if(strlen(info->local_address) > 0 || info->local_port > 0) {
357 // 指定本地IP和端口,不指定内核会自动指定(一般不指定)
358 struct sockaddr_in loc_addr;
359 memset(&loc_addr, 0, sizeof(struct sockaddr_in));
360 loc_addr.sin_family = AF_INET;
361
362 // 指定IP
363 if(strlen(info->local_address) > 0) {
364 if(inet_pton(AF_INET, info->local_address, &loc_addr.sin_addr) < 0) {
365 LOGE("inet_pton() error:%d", errno);
366 goto result_fail_with_close;
367 }
368 }
369
370 if(info->local_port > 0) {
371 loc_addr.sin_port = htons(info->local_port);
372 }
373 if(bind(mbtk_sock[handle]->inter_infos[index_free].fd, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
374 LOGE("bind() error:%d", errno);
375 if(errno == EADDRINUSE) { // 地址已在使用
376 LOGE("EADDRINUSE : Local port already occupied.");
377 }
378 goto result_fail_with_close;
379 } else {
380 LOGD("Bind ip/port success.");
381 }
382 }
383
liubin281ac462023-07-19 14:22:54 +0800384 struct sockaddr_in servaddr;
385 bzero(&servaddr, sizeof(servaddr));
386 servaddr.sin_family = AF_INET;
387 servaddr.sin_port = htons(info->port);
388
389 struct hostent *he = gethostbyname(info->address);
390 if (he == NULL){
391 LOGE("gethostbyname() fail.[%d]",errno);
392 goto result_fail_with_close;
393 } else {
394 LOGD("Ip : %s",he->h_addr_list[0]);
395 }
396 memcpy(&servaddr.sin_addr, he->h_addr_list[0], sizeof(struct in_addr));
397
398 if(connect(mbtk_sock[handle]->inter_infos[index_free].fd, (SA *) &servaddr, sizeof(servaddr)) < 0){
399 if(EINPROGRESS != errno){
400 LOGE("connect() fail.[%d]",errno);
401 goto result_fail_with_close;
402 }
403 }
404
405 fd_set rset, wset;
406 FD_ZERO(&rset);
407 FD_ZERO(&wset);
408 FD_SET(mbtk_sock[handle]->inter_infos[index_free].fd, &rset);
409 FD_SET(mbtk_sock[handle]->inter_infos[index_free].fd, &wset);
410 struct timeval time_out;
411 time_out.tv_sec = timeout/1000;
412 time_out.tv_usec = timeout%1000*1000;
413 int nready = select(mbtk_sock[handle]->inter_infos[index_free].fd + 1,
414 &rset, &wset, NULL, &time_out);
415 LOGD("nready = %d",nready);
416 if(nready == 0){// Timeout
417 LOGE("Timeout.");
418 printf("Timeout.\n");
419 goto result_fail_with_close;
420 }else{
421 if (FD_ISSET(mbtk_sock[handle]->inter_infos[index_free].fd, &rset)
422 && FD_ISSET(mbtk_sock[handle]->inter_infos[index_free].fd, &wset)) {
423 int error = -1;
b.liu9e8584b2024-11-06 19:21:28 +0800424 socklen_t len = sizeof(int);
liubin281ac462023-07-19 14:22:54 +0800425 LOGE("Can read and write.");
426 if(getsockopt(mbtk_sock[handle]->inter_infos[index_free].fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0){
427 LOGE("getsockopt fail.[%d]",errno);
428 goto result_fail_with_close;
429 }else{
430 LOGE("error = %d",error);
431 if(error != 0){ // Fail
432 goto result_fail_with_close;
433 }
434 }
435 }else if(FD_ISSET(mbtk_sock[handle]->inter_infos[index_free].fd, &wset)){
436 LOGI("Can write.");
437 printf("Can write.\n");
438 }else{
439 LOGE("Can read(Impossible).");
440 goto result_fail_with_close;
441 }
442 }
443 } else {
444 LOGE("Can not conn.");
445 goto result_fail_with_close;
446 }
447
448 if(mbtk_sock[handle]->init_info.sock_cb) {
449 struct epoll_event ev;
450 ev.data.fd = mbtk_sock[handle]->inter_infos[index_free].fd;
451 ev.events = EPOLLIN | EPOLLET;
452 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,mbtk_sock[handle]->inter_infos[index_free].fd,&ev);
453 }
454#if 1
455 if(info->ftp_ssl_support)
456 {
457 if(info->is_support_ssl){
458 mbtk_sock[handle]->infos[index_free].is_support_ssl = 0;
459 unsigned char mbtk_ftp_ssl_read_buf_s[256];
460 int err_rw;
461 memset(mbtk_ftp_ssl_read_buf_s,0,sizeof(mbtk_ftp_ssl_read_buf_s));
462 mbtk_sock_read(handle,mbtk_sock[handle]->inter_infos[index_free].fd,
463 mbtk_ftp_ssl_read_buf_s,
464 sizeof(mbtk_ftp_ssl_read_buf_s),
465 60000,
466 &err_rw);
467 printf("\nmbtk_sock_read:\n%s\n",mbtk_ftp_ssl_read_buf_s);
468
469 char cmd_buff[50];
b.liu9e8584b2024-11-06 19:21:28 +0800470 int len=0;
liubin281ac462023-07-19 14:22:54 +0800471 memset(cmd_buff,0,sizeof(cmd_buff));
472
473 len = snprintf(cmd_buff, 50, "AUTH TLS\r\n");
474 cmd_buff[len] = '\0';
475 //printf("\n cmd_buff = %s\n", cmd_buff);
476
477 mbtk_sock_write(handle,mbtk_sock[handle]->inter_infos[index_free].fd,
478 cmd_buff,
479 strlen(cmd_buff),
480 60000,
481 &err_rw);
482
483 memset(mbtk_ftp_ssl_read_buf_s,0,sizeof(mbtk_ftp_ssl_read_buf_s));
484 mbtk_sock_read(handle,mbtk_sock[handle]->inter_infos[index_free].fd,
485 mbtk_ftp_ssl_read_buf_s,
486 sizeof(mbtk_ftp_ssl_read_buf_s),
487 60000,
488 &err_rw);
489 printf("\nmbtk_sock_read:\n%s\n",mbtk_ftp_ssl_read_buf_s);
b.liueb040652023-09-25 18:50:56 +0800490
liubin281ac462023-07-19 14:22:54 +0800491 mbtk_sock[handle]->infos[index_free].is_support_ssl=1;
492 }else{
493 mbtk_sock[handle]->infos[index_free].is_support_ssl=1;
494 }
495 }
496#endif
497 if(info->is_support_ssl){
yq.wang39b54a22025-04-11 15:25:50 +0800498#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
499 int ret = mbtk_sock_openssl_init(mbtk_sock[handle]->inter_infos[index_free].fd, info->ingnore_cert, &mbtk_sock[handle]->inter_infos[index_free].ssl_info);
500 if(0 != ret){
501 LOGE("mbtk_sock_openssl_init() fail");
luojin8fbb3432023-10-18 09:47:46 +0800502 goto result_fail_with_close;
503 }
yq.wang39b54a22025-04-11 15:25:50 +0800504 mbtk_sock[handle]->inter_infos[index_free].fd = mbtk_sock[handle]->inter_infos[index_free].ssl_info.fd;
505#else
506 LOGE("openssl nonsupport");
507 goto result_fail_with_close;
b.liudeb8e422024-12-14 17:36:56 +0800508#endif
liubin281ac462023-07-19 14:22:54 +0800509 }
510
511 *mbtk_errno = MBTK_SOCK_SUCCESS;
512
513 mbtk_sock[handle]->sock_num++;
514 return mbtk_sock[handle]->inter_infos[index_free].fd;
515result_fail_with_close:
yq.wang39b54a22025-04-11 15:25:50 +0800516#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
517 mbtk_openssl_deinit(&mbtk_sock[handle]->inter_infos[index_free].ssl_info);
518#endif
519
liubin281ac462023-07-19 14:22:54 +0800520 close(mbtk_sock[handle]->inter_infos[index_free].fd);
521 mbtk_sock[handle]->inter_infos[index_free].fd = -1;
522result_fail:
523 memset(&(mbtk_sock[handle]->inter_infos[index_free]),0x0,sizeof(mbtk_sock_inter_info_s));
524 memset(&(mbtk_sock[handle]->infos[index_free]),0x0,sizeof(mbtk_sock_info));
525 LOGE("mbtk_sock_open() end:fail");
526 return -1;
527}
528extern int mbtk_ssl_init_func(mbtk_sock_handle handle ,bool ingnore_cert,mbtk_sock_session fd)
b.liueb040652023-09-25 18:50:56 +0800529{
liubin281ac462023-07-19 14:22:54 +0800530 int i=0;
531 int index_free=0;
532
533 for (i=0;i<10;i++)
534 {
535 if(mbtk_sock[handle]->inter_infos[i].fd == fd)
536 {
537 index_free = i;
538 break;
539 }
540 }
yq.wang39b54a22025-04-11 15:25:50 +0800541
542#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
543 int ret = mbtk_sock_openssl_init(mbtk_sock[handle]->inter_infos[index_free].fd, ingnore_cert, &mbtk_sock[handle]->inter_infos[index_free].ssl_info);
544 if(0 != ret){
545 LOGE("mbtk_sock_openssl_init() fail");
546 return -1;
547 }
548 mbtk_sock[handle]->inter_infos[index_free].fd = mbtk_sock[handle]->inter_infos[index_free].ssl_info.fd;
549 return 0;
b.liu9a8e82b2023-10-10 16:09:50 +0800550#else
yq.wang39b54a22025-04-11 15:25:50 +0800551 LOGE("openssl support");
b.liudeb8e422024-12-14 17:36:56 +0800552 return -1;
553#endif
liubin281ac462023-07-19 14:22:54 +0800554}
yq.wang39b54a22025-04-11 15:25:50 +0800555
liubin281ac462023-07-19 14:22:54 +0800556extern int mbtk_ssl_close_func(mbtk_sock_handle handle ,bool ingnore_cert,mbtk_sock_session fd)
b.liueb040652023-09-25 18:50:56 +0800557{
liubin281ac462023-07-19 14:22:54 +0800558 int i=0;
559 int index_free=0;
560
561 for (i=0;i<10;i++)
562 {
563 if(mbtk_sock[handle]->inter_infos[i].fd == fd)
564 {
565 index_free = i;
566 break;
567 }
568 }
yq.wang39b54a22025-04-11 15:25:50 +0800569
570#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
571 mbtk_openssl_deinit(&mbtk_sock[handle]->inter_infos[index_free].ssl_info);
572 return 0;
b.liu9a8e82b2023-10-10 16:09:50 +0800573#else
yq.wang39b54a22025-04-11 15:25:50 +0800574 LOGE("openssl nonsupport");
b.liudeb8e422024-12-14 17:36:56 +0800575 return -1;
576#endif
liubin281ac462023-07-19 14:22:54 +0800577}
578
579extern int mbtk_sock_write(mbtk_sock_handle handle,mbtk_sock_session session,
b.liu9e8584b2024-11-06 19:21:28 +0800580 const void *buffer,
liubin281ac462023-07-19 14:22:54 +0800581 unsigned int buf_len,
582 unsigned int timeout,
583 int *mbtk_errno)
584{
585 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
586 || session < 0 || mbtk_sock[handle] == NULL) {
587 LOGE("Socket not inited.");
588 return -1;
589 }
590
591 *mbtk_errno = MBTK_SOCK_ERROR;
592 if(buffer == NULL) {
593 LOGE("mbtk_sock_write() args error.");
594 return -1;
595 }
596
597 mbtk_sock_inter_info_s *inter_info = NULL;
598 int index = 0;
599 while(index < MBTK_SOCK_MAX_NUM) {
600 if(session ==
601 mbtk_sock[handle]->inter_infos[index].fd) {
602 inter_info = &(mbtk_sock[handle]->inter_infos[index]);
603 break;
604 }
605 index++;
606 }
607
608 if(!sock_info_check(handle,inter_info)) {
609 LOGE("sock_info_check() fail.");
610 return -1;
611 }
612
613 index = sock_info_find_by_fd(handle,inter_info->fd);
614 if(index < 0) {
615 LOGE("No such socket in session list.");
616 return -1;
617 }
618
619 int len = 0;
620 unsigned int count = 0;
621 if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) {
622 while(count < buf_len){
b.liu9a8e82b2023-10-10 16:09:50 +0800623 if(mbtk_sock[handle]->infos[index].is_support_ssl) {
yq.wang39b54a22025-04-11 15:25:50 +0800624#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
625 len = mbtk_openssl_write(inter_info->ssl_info.ssl,(const unsigned char *)buffer + count,buf_len - count);
b.liu9a8e82b2023-10-10 16:09:50 +0800626#else
yq.wang39b54a22025-04-11 15:25:50 +0800627 LOGE("openssl nonsupport");
628 return -1;
b.liudeb8e422024-12-14 17:36:56 +0800629#endif
b.liu9a8e82b2023-10-10 16:09:50 +0800630 } else
liubin281ac462023-07-19 14:22:54 +0800631 len = write(inter_info->fd,(char*)buffer + count,buf_len - count);
632 if(len < 0){
633 if(errno == EWOULDBLOCK){
634 usleep(50000);
635 continue;
636 } else {
637 LOGE("write error.[%d]",errno);
638 if(count <= 0)
639 count = -1;
640 break;
641 }
642 } else if(len == 0) {
643 LOGE("write error(len == 0).[%d]",errno);
644 } else {
645 count += len;
646 }
647 }
648 } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP){
649 // Start send data
650 while(count < buf_len){
651 len = sendto(inter_info->fd,(char*)buffer + count,buf_len - count,0,NULL,0);
652 if(len < 0){
653 if(errno == EWOULDBLOCK){
654 usleep(50000);
655 continue;
656 } else {
657 LOGE("sendto error.[%d]",errno);
658 if(ECONNREFUSED == errno) { // Disconnected.
659 LOGD("Socket Disconnected.");
660 }
661 break;
662 }
663 } else if(len == 0) {
664 LOGD("write error(len == 0).[%d]",errno);
665 } else {
666 count += len;
667 }
668 }
669 } else {
670 LOGE("Socket type error.");
671 return -1;
672 }
673
674 if(count == buf_len){
675 LOGD("Write data[%d/%d] success.",count,buf_len);
676 } else { // Open session fail
677 LOGD("Write data[%d/%d] fail.",count,buf_len);
678 }
679
680 *mbtk_errno = MBTK_SOCK_SUCCESS;
681 return count;
682}
683
684extern int mbtk_sock_read(mbtk_sock_handle handle,mbtk_sock_session session,
685 void *buffer,
686 unsigned int buf_len,
687 unsigned int timeout,
688 int *mbtk_errno)
689{
690 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
691 || session < 0 || mbtk_sock[handle] == NULL) {
692 LOGE("Socket not inited.");
693 return -1;
694 }
695
696 *mbtk_errno = MBTK_SOCK_ERROR;
697 if(buffer == NULL) {
698 LOGE("mbtk_sock_write() args error.");
699 return -1;
700 }
701
702 mbtk_sock_inter_info_s *inter_info = NULL;
703 int index = 0;
704 while(index < MBTK_SOCK_MAX_NUM) {
705 if(session ==
706 mbtk_sock[handle]->inter_infos[index].fd) {
707 inter_info = &(mbtk_sock[handle]->inter_infos[index]);
708 break;
709 }
710 index++;
711 }
712
713 if(!sock_info_check(handle,inter_info)) {
714 LOGE("sock_info_check() fail.");
715 return -1;
716 }
717
718 index = sock_info_find_by_fd(handle,inter_info->fd);
719 if(index < 0) {
720 LOGE("No such socket in session list.");
721 return -1;
722 }
723
724 unsigned int count = 0;
725 if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) {
726 int len = 0;
727 int try_count = 0;
728 int times = timeout / 50;
729 memset(buffer,0x0,buf_len);
730 while(count < buf_len){
731 try_count++;
b.liu9a8e82b2023-10-10 16:09:50 +0800732 if(mbtk_sock[handle]->infos[index].is_support_ssl) {
yq.wang39b54a22025-04-11 15:25:50 +0800733#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
734 len = mbtk_openssl_read(inter_info->ssl_info.ssl,(unsigned char *)buffer + count,buf_len - count);
b.liu9a8e82b2023-10-10 16:09:50 +0800735#else
yq.wang39b54a22025-04-11 15:25:50 +0800736 LOGE("openssl nonsupport");
737 return -1;
b.liudeb8e422024-12-14 17:36:56 +0800738#endif
b.liu9a8e82b2023-10-10 16:09:50 +0800739 } else
liubin281ac462023-07-19 14:22:54 +0800740 len = read(inter_info->fd,(char*)buffer + count,buf_len - count);
741 if(len < 0){
742 if(errno == EWOULDBLOCK){
743 if(count > 0) // Read data
744 break; // Read data end.
745
746 if(try_count >= times){ // Timeout
747 count = -1;
748 if(times != 0) {
749 *mbtk_errno = MBTK_SOCK_ETIMEOUT;
750 }
751 LOGE("Not read enough data,return.[%d/%d]",count,buf_len);
752 break;
753 } else {
754 usleep(50000);
755 continue;
756 }
757 } else {
758 LOGE("read error.[%d]",errno);
759 if(count <= 0)
760 count = -1;
761 break;
762 }
763 } else if(len == 0) {
764 LOGE("read error(len == 0).[%d]",errno);
765 if(errno == EINPROGRESS) {
766 if(close(inter_info->fd) == 0) {// Success
767 LOGD("Socket disconnected.Close it.");
768 }
769 if(count <= 0)
770 count = -1;
771 } else {
772 if(count <= 0)
773 count = 0;
774 }
775 break;
776 } else {
777 count += len;
778 }
779 }
780 } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) {
781 // Start recv data
782 struct sockaddr_in seraddr;
783 socklen_t seraddr_len;
784 int try_count = 0;
785 int times = timeout / 50;
786 int len = 0;
787 memset(buffer,0x0,buf_len);
788 while(TRUE){
789 try_count++;
790 seraddr_len = sizeof(struct sockaddr_in);
b.liudeb8e422024-12-14 17:36:56 +0800791 len = recvfrom(inter_info->fd,buffer,buf_len,0,(struct sockaddr*)&seraddr,&seraddr_len);
liubin281ac462023-07-19 14:22:54 +0800792 if(len < 0){
793 if(errno == EWOULDBLOCK){// No data can read.
794 if(count > 0) // Read data
795 break; // Read data end.
796
797 if(try_count >= times){ // Timeout
798 if(times == 0) {
799 LOGE("Can not read.");
800 } else {
801 LOGE("Timeout");
802 *mbtk_errno = MBTK_SOCK_ETIMEOUT;
803 }
804 count = -1;
805 LOGE("Not read enough data,return.[%d/%d]",count,buf_len);
806 break;
807 } else {
808 usleep(50000);
809 continue;
810 }
811 } else {
812 LOGE("recvfrom 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 if(count <= 0)
820 count = 0;
821 break;
822 } else {
823 count += len;
824 }
825 }
826 } else {
827 LOGE("Socket type error.");
828 return -1;
829 }
830
831// if(count == buf_len){
832// LOGD("Read data[%d/%d] success.",count,buf_len);
833// } else { // Open session fail
834// LOGD("Read data[%d/%d] fail.",count,buf_len);
835// }
836
837 LOGV("Read data[%d/%d].",count,buf_len);
838
839 *mbtk_errno = MBTK_SOCK_SUCCESS;
840 return count;
841}
842extern int mbtk_sock_readline(mbtk_sock_handle handle,mbtk_sock_session session,
843 void *buffer,
844 unsigned int buf_len,
845 unsigned int timeout,
846 int *mbtk_errno,
847 int *read_line_count,
848 char *buf_ptr)
849{
850 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
851 || session < 0 || mbtk_sock[handle] == NULL) {
852 LOGE("Socket not inited.");
853 return -1;
854 }
855
856 *mbtk_errno = MBTK_SOCK_ERROR;
857 if(buffer == NULL) {
858 LOGE("mbtk_sock_write() args error.");
859 return -1;
860 }
861
862 mbtk_sock_inter_info_s *inter_info = NULL;
863 int index = 0;
864 while(index < MBTK_SOCK_MAX_NUM) {
865 if(session ==
866 mbtk_sock[handle]->inter_infos[index].fd) {
867 inter_info = &(mbtk_sock[handle]->inter_infos[index]);
868 break;
869 }
870 index++;
871 }
872
873 if(!sock_info_check(handle,inter_info)) {
874 LOGE("sock_info_check() fail.");
875 return -1;
876 }
877
878 index = sock_info_find_by_fd(handle,inter_info->fd);
879 if(index < 0) {
880 LOGE("No such socket in session list.");
881 return -1;
882 }
883
884 unsigned int count = 0;
b.liu9e8584b2024-11-06 19:21:28 +0800885// unsigned int read_count = 0;
liubin281ac462023-07-19 14:22:54 +0800886 memset(buf_ptr, 0, buf_len);
887 char *temp_ptr = (char *)buffer;
888copy_angin_ssl:
889 while(*read_line_count > 0 && *temp_ptr != '\n') {
b.liu9e8584b2024-11-06 19:21:28 +0800890 if(temp_ptr == NULL)
liubin281ac462023-07-19 14:22:54 +0800891 {
892 printf("\n*temp_ptr is null\n");
893 goto read_end;
894 }
895 *buf_ptr++ = *temp_ptr++;
896 (*read_line_count)--;
897 count++;
898 }
899 if(*read_line_count == 0)
900 {
901 if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) {
902 int len = 0;
903 int try_count = 0;
904 int times = timeout / 50;
905 memset(buffer,0x0,buf_len);
906 while(count < buf_len){
907 try_count++;
b.liu9a8e82b2023-10-10 16:09:50 +0800908 if(mbtk_sock[handle]->infos[index].is_support_ssl) {
yq.wang39b54a22025-04-11 15:25:50 +0800909#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
910 len = mbtk_openssl_read(inter_info->ssl_info.ssl,(unsigned char*)buffer + count,buf_len - count);
b.liu9a8e82b2023-10-10 16:09:50 +0800911#else
yq.wang39b54a22025-04-11 15:25:50 +0800912 LOGE("openssl nonsupport");
b.liudeb8e422024-12-14 17:36:56 +0800913 return -1;
914#endif
b.liu9a8e82b2023-10-10 16:09:50 +0800915 } else
liubin281ac462023-07-19 14:22:54 +0800916 len = read(inter_info->fd,(char*)buffer + count,buf_len - count);
917 *read_line_count = len;
918 if(len < 0){
919 if(errno == EWOULDBLOCK){
920 if(count > 0) // Read data
921 {
922 *read_line_count = count;
923 count = 0;
924 goto copy_angin_ssl;
925 break; // Read data end.
926 }
927 else
928 {
929 //printf("\nread_end\n");
930 goto read_end;
931 }
932 if(try_count >= times){ // Timeout
933 count = -1;
934 if(times != 0) {
935 *mbtk_errno = MBTK_SOCK_ETIMEOUT;
936 }
937 LOGE("Not read enough data,return.[%d/%d]",count,buf_len);
938 goto read_end;
939 break;
940 } else {
941 usleep(50000);
942 continue;
943 }
944 } else {
945 LOGE("read error.[%d]",errno);
946 if(count <= 0)
947 count = -1;
b.liueb040652023-09-25 18:50:56 +0800948 else {
liubin281ac462023-07-19 14:22:54 +0800949 *read_line_count = count;
950 }
951 break;
952 }
953 } else if(len == 0) {
954 LOGE("read error(len == 0).[%d]",errno);
955 if(errno == EINPROGRESS) {
956 if(close(inter_info->fd) == 0) {// Success
957 LOGD("Socket disconnected.Close it.");
958 }
959 if(count <= 0)
960 count = -1;
961 } else {
962 if(count <= 0)
963 count = 0;
964 else
965 count = -1;
966 }
967 goto read_end;
968 break;
969 } else {
970 count += len;
971 }
972 }
973 } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) {
974 // Start recv data
975 struct sockaddr_in seraddr;
976 socklen_t seraddr_len;
977 int try_count = 0;
978 int times = timeout / 50;
979 int len = 0;
980 memset(buffer,0x0,buf_len);
981 while(TRUE){
982 try_count++;
983 seraddr_len = sizeof(struct sockaddr_in);
b.liudeb8e422024-12-14 17:36:56 +0800984 len = recvfrom(inter_info->fd,buffer,buf_len,0,(struct sockaddr*)&seraddr,&seraddr_len);
liubin281ac462023-07-19 14:22:54 +0800985 if(len < 0){
986 if(errno == EWOULDBLOCK){// No data can read.
987 if(count > 0) // Read data
988 break; // Read data end.
989
990 if(try_count >= times){ // Timeout
991 if(times == 0) {
992 LOGE("Can not read.");
993 //printf("Can not read.\n");
994 } else {
995 LOGE("Timeout");
996 //printf("Timeout\n");
997 *mbtk_errno = MBTK_SOCK_ETIMEOUT;
998 }
999 count = -1;
1000 LOGE("Not read enough data,return.[%d/%d]",count,buf_len);
1001 //printf("Not read enough data,return.[%d/%d]\n",count,buf_len);
1002 break;
1003 } else {
1004 usleep(50000);
1005 continue;
1006 }
1007 } else {
1008 LOGE("recvfrom error.[%d]",errno);
1009 if(count <= 0)
1010 count = -1;
1011 break;
1012 }
1013 } else if(len == 0) {
1014 LOGE("write error(len == 0).[%d]",errno);
1015 if(count <= 0)
1016 count = 0;
1017 break;
1018 } else {
1019 count += len;
1020 }
1021 }
1022 } else {
1023 LOGE("Socket type error.");
1024 //printf("Socket type error.\n");
1025 return -1;
1026 }
1027 count = 0;
1028 goto copy_angin_ssl;
1029 } else if(*temp_ptr == '\n') { // Read line.
1030 *buf_ptr++ = '\n';
1031 (*read_line_count)--;
1032 count++;
1033
1034 if(*read_line_count > 0)
1035 memcpy(buffer, temp_ptr + 1, *read_line_count);
1036 return count;
1037 }
1038 LOGV("Read data[%d/%d].",count,buf_len);
1039read_end:
1040 *mbtk_errno = MBTK_SOCK_SUCCESS;
1041 return count;
1042}
1043
1044extern int mbtk_sock_read_async(mbtk_sock_handle handle,mbtk_sock_session session,
1045 void *buffer,
1046 unsigned int buf_len)
1047{
1048 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
1049 || session < 0 || mbtk_sock[handle] == NULL) {
1050 LOGE("Socket not inited.");
1051 return -1;
1052 }
1053
1054 if(buffer == NULL) {
1055 LOGE("mbtk_sock_write() args error.");
1056 return -1;
1057 }
1058
1059 mbtk_sock_inter_info_s *inter_info = NULL;
1060 int index = 0;
1061 while(index < MBTK_SOCK_MAX_NUM) {
1062 if(session ==
1063 mbtk_sock[handle]->inter_infos[index].fd) {
1064 inter_info = &(mbtk_sock[handle]->inter_infos[index]);
1065 break;
1066 }
1067 index++;
1068 }
1069 if(!sock_info_check(handle,inter_info)) {
1070 LOGE("sock_info_check() fail.");
1071 return -1;
1072 }
1073
1074 index = sock_info_find_by_fd(handle,inter_info->fd);
1075 if(index < 0) {
1076 LOGE("No such socket in session list.");
1077 return -1;
1078 }
1079
b.liueb040652023-09-25 18:50:56 +08001080 int len = 0;
1081 int read_count = 0;
1082 if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) {
1083 memset(buffer,0x0,buf_len);
1084 while(read_count < buf_len) {
b.liu9a8e82b2023-10-10 16:09:50 +08001085 if(mbtk_sock[handle]->infos[index].is_support_ssl) {
yq.wang39b54a22025-04-11 15:25:50 +08001086#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
1087 len = mbtk_openssl_read(inter_info->ssl_info.ssl,(unsigned char*)buffer + read_count,buf_len - read_count);
b.liu9a8e82b2023-10-10 16:09:50 +08001088#else
yq.wang39b54a22025-04-11 15:25:50 +08001089 LOGE("openssl nonsupport");
b.liudeb8e422024-12-14 17:36:56 +08001090 return -1;
1091#endif
b.liu9a8e82b2023-10-10 16:09:50 +08001092 } else
b.liueb040652023-09-25 18:50:56 +08001093 len = read(inter_info->fd,(char*)buffer + read_count,buf_len - read_count);
1094
1095 if(len > 0) {
1096 read_count += len;
1097 } else {
b.liu94baa7c2023-09-26 16:26:10 +08001098 if(errno == EWOULDBLOCK) { // No data
1099 break;
1100 } else {
1101 LOGE("Will retry : len = %d, errno = %d", len, errno);
1102 }
b.liueb040652023-09-25 18:50:56 +08001103 }
1104 }
1105 } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) {
1106 // Start recv data
1107 struct sockaddr_in seraddr;
1108 socklen_t seraddr_len;
1109 memset(buffer,0x0,buf_len);
1110 seraddr_len = sizeof(struct sockaddr_in);
1111 memset(buffer,0x0,buf_len);
1112
1113 while(read_count < buf_len) {
b.liudeb8e422024-12-14 17:36:56 +08001114 len = recvfrom(inter_info->fd,buffer + read_count,buf_len - read_count,0,(struct sockaddr*)&seraddr,&seraddr_len);
b.liueb040652023-09-25 18:50:56 +08001115
1116 if(len > 0) {
1117 read_count += len;
1118 } else {
b.liu94baa7c2023-09-26 16:26:10 +08001119 if(errno == EWOULDBLOCK) { // No data
1120 break;
1121 } else {
1122 LOGE("Will retry : len = %d, errno = %d", len, errno);
1123 }
b.liueb040652023-09-25 18:50:56 +08001124 }
1125 }
1126 } else {
1127 LOGE("Socket type error.");
1128 return -1;
1129 }
1130
b.liu94baa7c2023-09-26 16:26:10 +08001131 LOGV("Read data[%d/%d].",read_count,buf_len);
b.liueb040652023-09-25 18:50:56 +08001132
1133 return read_count;
1134}
1135
1136extern int mbtk_sock_read_sync(mbtk_sock_handle handle,mbtk_sock_session session,
1137 void *buffer,
1138 unsigned int buf_len)
1139{
1140 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
1141 || session < 0 || mbtk_sock[handle] == NULL) {
1142 LOGE("Socket not inited.");
1143 return -1;
1144 }
1145
1146 if(buffer == NULL) {
1147 LOGE("mbtk_sock_write() args error.");
1148 return -1;
1149 }
1150
1151 mbtk_sock_inter_info_s *inter_info = NULL;
1152 int index = 0;
1153 while(index < MBTK_SOCK_MAX_NUM) {
1154 if(session ==
1155 mbtk_sock[handle]->inter_infos[index].fd) {
1156 inter_info = &(mbtk_sock[handle]->inter_infos[index]);
1157 break;
1158 }
1159 index++;
1160 }
1161 if(!sock_info_check(handle,inter_info)) {
1162 LOGE("sock_info_check() fail.");
1163 return -1;
1164 }
1165
1166 index = sock_info_find_by_fd(handle,inter_info->fd);
1167 if(index < 0) {
1168 LOGE("No such socket in session list.");
1169 return -1;
1170 }
1171
liubin281ac462023-07-19 14:22:54 +08001172 int len;
1173 if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) {
1174TCP_READ_AGAIN:
1175 memset(buffer,0x0,buf_len);
b.liu9a8e82b2023-10-10 16:09:50 +08001176 if(mbtk_sock[handle]->infos[index].is_support_ssl) {
yq.wang39b54a22025-04-11 15:25:50 +08001177#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
1178 len = mbtk_openssl_read(inter_info->ssl_info.ssl,(unsigned char*)buffer,buf_len);
b.liu9a8e82b2023-10-10 16:09:50 +08001179#else
yq.wang39b54a22025-04-11 15:25:50 +08001180 LOGE("openssl nonsupport");
b.liudeb8e422024-12-14 17:36:56 +08001181 return -1;
1182#endif
b.liu9a8e82b2023-10-10 16:09:50 +08001183 } else
liubin281ac462023-07-19 14:22:54 +08001184 len = read(inter_info->fd,(char*)buffer,buf_len);
1185 if(len < 0){
1186 if(errno == EWOULDBLOCK){
1187 usleep(100000);
b.liueb040652023-09-25 18:50:56 +08001188 LOGW("Read retry...");
liubin281ac462023-07-19 14:22:54 +08001189 goto TCP_READ_AGAIN;
1190 } else {
1191 LOGE("read error.[%d]",errno);
1192 return -1;
1193 }
1194 }
1195 } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) {
1196 // Start recv data
1197 struct sockaddr_in seraddr;
1198 socklen_t seraddr_len;
1199UDP_READ_AGAIN:
1200 memset(buffer,0x0,buf_len);
1201 seraddr_len = sizeof(struct sockaddr_in);
b.liudeb8e422024-12-14 17:36:56 +08001202 len = recvfrom(inter_info->fd,buffer,buf_len,0,(struct sockaddr*)&seraddr,&seraddr_len);
liubin281ac462023-07-19 14:22:54 +08001203 if(len < 0){
1204 if(errno == EWOULDBLOCK){
1205 usleep(100000);
1206 goto UDP_READ_AGAIN;
1207 } else {
1208 LOGE("read error.[%d]",errno);
1209 return -1;
1210 }
1211 }
1212 } else {
1213 LOGE("Socket type error.");
1214 return -1;
1215 }
1216
1217 LOGV("Read data[%d/%d].",len,buf_len);
1218
1219 return len;
1220}
1221
b.liu94baa7c2023-09-26 16:26:10 +08001222
liubin281ac462023-07-19 14:22:54 +08001223extern int mbtk_sock_close(mbtk_sock_handle handle,mbtk_sock_session session,
1224 unsigned int timeout,
1225 int *mbtk_errno)
1226{
1227 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
1228 || session < 0 || mbtk_sock[handle] == NULL) {
1229 LOGE("Socket not inited.");
1230 return -1;
1231 }
1232
1233 *mbtk_errno = MBTK_SOCK_ERROR;
1234 mbtk_sock_inter_info_s *inter_info = NULL;
1235 int index = 0;
1236 while(index < MBTK_SOCK_MAX_NUM) {
1237 if(session == mbtk_sock[handle]->inter_infos[index].fd) {
1238 inter_info = &(mbtk_sock[handle]->inter_infos[index]);
1239 break;
1240 }
1241 index++;
1242 }
1243 if(!sock_info_check(handle,inter_info)) {
1244 LOGE("sock_info_check() fail.");
1245 return -1;
1246 }
1247
1248 index = sock_info_find_by_fd(handle,inter_info->fd);
1249 if(index < 0) {
1250 LOGE("No such socket in session list.");
1251 return -1;
1252 }
1253
1254 int i;
1255 for(i = 0;i < MBTK_SOCK_MAX_NUM;i++) {
1256 if(mbtk_sock[handle]->inter_infos[i].fd == inter_info->fd){
1257 if(mbtk_sock[handle]->init_info.sock_cb) {
1258 struct epoll_event ev;
1259 ev.data.fd = inter_info->fd;
1260 ev.events = EPOLLIN | EPOLLET;
1261 epoll_ctl(epoll_fd,EPOLL_CTL_DEL,inter_info->fd,&ev);
1262 }
1263
1264 if(close(inter_info->fd) < 0) {// Success
1265 LOGE("Close socket fail[%d].",errno);
1266 //break;
1267 }
1268 mbtk_sock[handle]->inter_infos[i].fd = -1;
1269 memset(&(mbtk_sock[handle]->infos[i]),0x0,sizeof(mbtk_sock_info));
1270 mbtk_sock[handle]->sock_num--;
1271 break;
1272 }
1273 }
1274
1275 if(mbtk_sock[handle]->infos[index].is_support_ssl){
yq.wang39b54a22025-04-11 15:25:50 +08001276#ifdef MBTK_OPENSSL_V3_0_0_SUPPORT
1277 mbtk_openssl_deinit(&inter_info->ssl_info);
b.liudeb8e422024-12-14 17:36:56 +08001278#endif
liubin281ac462023-07-19 14:22:54 +08001279 }
1280
1281 *mbtk_errno = MBTK_SOCK_SUCCESS;
1282 return 0;
1283}
1284
1285extern int mbtk_sock_deinit(mbtk_sock_handle handle)
1286{
1287 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
1288 || mbtk_sock[handle] == NULL) {
1289 LOGE("Socket not inited.");
1290 return -1;
1291 }
1292
1293 if(mbtk_sock[handle]->sock_num > 0) {
1294 LOGE("There are socket not close.");
1295 return MBTK_SOCK_ERROR;
1296 }
1297
1298 LOGD("mbtk_sock_deinit() start.");
1299#if 0
1300 sock_thread_running = FALSE;
1301 write(pipe_fds[0],"0",1);
1302
1303 // Wait for thread exist.
1304 while(sock_inited) {
1305 usleep(100);
1306 }
1307#endif
1308
1309 int i;
1310 for(i = 0;i < MBTK_SOCK_MAX_NUM;i++) {
1311 if(mbtk_sock[handle]->inter_infos[i].fd > 0){
1312 if(mbtk_sock[handle]->init_info.sock_cb) {
1313 struct epoll_event ev;
1314 ev.data.fd = mbtk_sock[handle]->inter_infos[i].fd;
1315 ev.events = EPOLLIN | EPOLLET;
1316 epoll_ctl(epoll_fd,EPOLL_CTL_DEL,mbtk_sock[handle]->inter_infos[i].fd,&ev);
1317 }
1318
1319 if(close(mbtk_sock[handle]->inter_infos[i].fd) < 0) {// Success
1320 LOGE("Close socket fail[%d].",errno);
1321 //break;
1322 }
1323 mbtk_sock[handle]->inter_infos[i].fd = -1;
1324 memset(&(mbtk_sock[handle]->infos[i]),0x0,sizeof(mbtk_sock_info));
1325 break;
1326 }
1327 }
1328
1329 //memset(&mbtk_sock,0x0,sizeof(mbtk_sock_s));
1330 free(mbtk_sock[handle]);
1331 mbtk_sock[handle] = NULL;
1332 LOGD("mbtk_sock_deinit() end.");
1333 return MBTK_SOCK_SUCCESS;
1334}
1335
b.liu94baa7c2023-09-26 16:26:10 +08001336/*
1337* Get TCP RECV buffer data length.
1338*/
1339int mbtk_sock_tcp_recv_len_get(mbtk_sock_handle handle,mbtk_sock_session session)
1340{
1341 if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
1342 || session < 0 || mbtk_sock[handle] == NULL) {
1343 LOGE("Socket not inited.");
1344 return -1;
1345 }
1346
1347 mbtk_sock_inter_info_s *inter_info = NULL;
1348 int index = 0;
1349 while(index < MBTK_SOCK_MAX_NUM) {
1350 if(session ==
1351 mbtk_sock[handle]->inter_infos[index].fd) {
1352 inter_info = &(mbtk_sock[handle]->inter_infos[index]);
1353 break;
1354 }
1355 index++;
1356 }
1357 if(!sock_info_check(handle,inter_info)) {
1358 LOGE("sock_info_check() fail.");
1359 return -1;
1360 }
1361
1362 index = sock_info_find_by_fd(handle,inter_info->fd);
1363 if(index < 0) {
1364 LOGE("No such socket in session list.");
1365 return -1;
1366 }
1367
b.liu9e8584b2024-11-06 19:21:28 +08001368// unsigned int count = 0;
b.liu94baa7c2023-09-26 16:26:10 +08001369 int len = 0;
1370 if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) {
1371 if(ioctl(inter_info->fd, FIONREAD, &len))
1372 {
1373 LOGE("Get ioctl FIONREAD fail:%d", errno);
1374 return -1;
1375 }
1376 } else {
1377 LOGE("Only surrport for TCP.");
1378 return -1;
1379 }
1380
1381 return len;
1382}
1383
b.liubcf86c92024-08-19 19:48:28 +08001384void mbtk_net_lib_info_print()
1385{
1386 MBTK_SOURCE_INFO_PRINT("mbtk_net_lib");
1387}
liubin281ac462023-07-19 14:22:54 +08001388
luojin8fbb3432023-10-18 09:47:46 +08001389