blob: bd12db61b24c217462128fef462701d2b3adb4b1 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <unistd.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <errno.h>
6#include <sys/epoll.h>
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
10#include <sys/un.h>
11#include <netdb.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <sys/select.h>
15#include <signal.h>
16#include <sys/time.h>
17
18#include "mbtk_sock.h"
19#include "mbtk_log.h"
20
21int sock_net_open(mbtk_net_info_s *net_info, mbtk_addr_family_e addr_family)
22{
23 return 0;
24}
25
26int sock_open(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, uint32 timeout, int *err)
27{
28 if(net_info == NULL || sock_info == NULL) {
29 LOGE("ARG error.");
30 return -1;
31 }
32 int family = AF_INET;
33 if(sock_info->addr_family == MBTK_ADDR_IPV6) { // Only IPv6
34 family = AF_INET6;
35 }
b.liu9e8584b2024-11-06 19:21:28 +080036 if(sock_info->sock_type == MBTK_SOCK_UDP_COM) { // UDP
liubin281ac462023-07-19 14:22:54 +080037 // socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
38 if((sock_info->fd = socket(family, SOCK_DGRAM, IPPROTO_UDP)) < 0){
39 LOGE("socket() fail.[%d]",errno);
40 goto result_fail;
41 }
b.liu9e8584b2024-11-06 19:21:28 +080042 } else if(sock_info->sock_type == MBTK_SOCK_TCP_COM){ // TCP
liubin281ac462023-07-19 14:22:54 +080043 if((sock_info->fd = socket(family, SOCK_STREAM, IPPROTO_TCP)) < 0){
44 LOGE("socket() fail.[%d]",errno);
45 goto result_fail;
46 }
47 } else {
48 LOGE("Unknown socket type:%d", sock_info->sock_type);
49 return -1;
50 }
51#if 1
52 // Set O_NONBLOCK
53 int flags = fcntl(sock_info->fd, F_GETFL, 0);
54 if (flags < 0) {
55 LOGE("Get flags error:%d", errno);
56 goto result_fail_with_close;
57 }
58 flags |= O_NONBLOCK;
59 if (fcntl(sock_info->fd, F_SETFL, flags) < 0) {
60 LOGE("Set flags error:%d", errno);
61 goto result_fail_with_close;
62 }
63#endif
64 // Connect
65 LOGD("Start conn:%s:%d",sock_info->host,sock_info->port);
66 if (strlen(sock_info->host) > 0 && sock_info->port > 0)
67 {
68 if(family == AF_INET6)
69 {
70 struct addrinfo hints;
71 struct addrinfo *answer, *curr;
72 memset(&hints, 0, sizeof(struct addrinfo));
73 hints.ai_family = AF_INET6; /* Allow IPv4 or IPv6 */
74 hints.ai_socktype = sock_info->sock_type; /* Datagram socket */
75 hints.ai_flags = 0;
76 hints.ai_protocol = 0; /* Any protocol */
77
78 int ret = getaddrinfo(sock_info->host, NULL, &hints, &answer);
79 if(ret < 0)
80 {
81 printf("\ngetaddrinfo error\n");
82 goto result_fail_with_close;
83 }
84
85 struct sockaddr_in6 *addr_in6=NULL;
86 struct sockaddr_in6 serverSockAddr;
87 bzero(&serverSockAddr, sizeof(struct sockaddr_in6));
88 serverSockAddr.sin6_family = AF_INET6;
89 serverSockAddr.sin6_port = htons(sock_info->port);
90 //memcpy(&serverSockAddr.sin6_addr, he->h_addr_list[0], sizeof(struct in6_addr));
91 //memcpy(&sock_info->addr.addr.dst_sock_addr6.sin6_addr, he->h_addr_list[0], sizeof(struct in_addr));
92 sock_info->addr.addr_len = sizeof(struct sockaddr_in6);
93
94 for (curr = answer; curr != NULL; curr = curr->ai_next)
95 {
96 addr_in6 = (struct sockaddr_in6 *)curr->ai_addr;
97 memcpy(&(serverSockAddr.sin6_addr), &(addr_in6->sin6_addr), sizeof(struct in6_addr));
98 }
b.liudeb8e422024-12-14 17:36:56 +080099 if (connect(sock_info->fd, (struct sockaddr*)&serverSockAddr, sizeof(struct sockaddr_in6)) < 0)
liubin281ac462023-07-19 14:22:54 +0800100 {
101 if (EINPROGRESS != errno)
102 {
103 LOGE("connect() fail.[%d]", errno);
104 goto result_fail_with_close;
105 }
106 }
107 }
108 else
109 {
110 struct hostent *he = gethostbyname(sock_info->host);
111 if (he == NULL)
112 {
113 LOGE("gethostbyname() fail.[%d]", errno);
114 goto result_fail_with_close;
115 }
116 struct sockaddr_in serverSockAddr;
117 bzero(&serverSockAddr, sizeof(struct sockaddr_in));
118 serverSockAddr.sin_family = AF_INET;
119 serverSockAddr.sin_port = htons(sock_info->port);
120 memcpy(&serverSockAddr.sin_addr, he->h_addr_list[0], sizeof(struct in_addr));
121
122 log_hex("IPv4", he->h_addr_list[0], sizeof(struct in_addr));
123
124 memcpy(&sock_info->addr.addr.serverSockAddr.sin_addr, he->h_addr_list[0], sizeof(struct in_addr));
125 sock_info->addr.addr_len = sizeof(struct sockaddr_in);
126 if (connect(sock_info->fd, (struct sockaddr *) &serverSockAddr, sizeof(serverSockAddr)) < 0)
127 {
128 if (EINPROGRESS != errno)
129 {
130 LOGE("connect() fail.[%d]", errno);
131 goto result_fail_with_close;
132 }
133 }
134 }
135
136 fd_set rset, wset;
137 FD_ZERO(&rset);
138 FD_ZERO(&wset);
139 FD_SET(sock_info->fd, &rset);
140 FD_SET(sock_info->fd, &wset);
141 struct timeval time_out;
142 /*
143 time_out.tv_sec = timeout / 1000;
144 time_out.tv_usec = timeout % 1000 * 1000;
145 */
146 time_out.tv_sec = 100;
147 time_out.tv_usec = 0;
148 int nready = select(sock_info->fd + 1, &rset, &wset, NULL, &time_out);
149 LOGD("nready = %d", nready);
150 if (nready == 0) // Timeout
151 {
152 LOGE("Timeout.");
153 goto result_fail_with_close;
154 }
155 else
156 {
157 if (FD_ISSET(sock_info->fd, &rset) && FD_ISSET(sock_info->fd, &wset))
158 {
159 int error = -1;
160 socklen_t len = sizeof(int);
161 LOGE("Can read and write.");
162 if (getsockopt(sock_info->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
163 {
164 LOGE("getsockopt fail.[%d]", errno);
165 goto result_fail_with_close;
166 }
167 else
168 {
169 LOGE("error = %d", error);
170 if (error != 0) // Fail
171 {
172 goto result_fail_with_close;
173 }
174 }
175 }
176 else if (FD_ISSET(sock_info->fd, &wset))
177 {
178 LOGI("Can write.");
179 }
180 else
181 {
182 LOGE("Can read(Impossible).");
183 goto result_fail_with_close;
184 }
185 }
186 }
187 else
188 {
189 LOGE("Can not conn.");
190 goto result_fail_with_close;
191 }
192
193 if (sock_info->is_ssl)
194 {
195
196 }
197
198 sock_info->read_buff.buffer = (char*)malloc(MBTK_BUFF_SIZE);
199 if(sock_info->read_buff.buffer) {
200 memset(sock_info->read_buff.buffer, 0x0, MBTK_BUFF_SIZE);
201 sock_info->read_buff.size = 0;
202 sock_info->read_buff.size_max = MBTK_BUFF_SIZE;
203 } else {
204 LOGE("malloc() fail.");
205 goto result_fail_with_close;
206 }
207
208 return sock_info->fd;
209result_fail_with_close:
210 close(sock_info->fd);
211 sock_info->fd = -1;
212result_fail:
213 LOGE("mbtk_sock_open() end:fail");
214 return -1;
215}
216
217int sock_addr_set(mbtk_net_info_s *net_info,mbtk_sock_info_s *sock_info,void *host, uint32 port)
218{
219 if(sock_info->fd <= 0) {
220 LOGE("Socket not open.");
221 return -1;
222 }
223
224 sock_info->addr.addr.serverSockAddr.sin_family = AF_INET;
225 sock_info->addr.addr.serverSockAddr.sin_port = htons(port);
226 struct hostent *he = gethostbyname(host);
227 if (he == NULL)
228 {
229 LOGE("gethostbyname() fail.[%d]", errno);
230 return -1;
231 }
232 else
233 {
234 LOGD("Ip(len-%d)", he->h_length);
235 int i = 0;
236 for(;i < he->h_length;i++){
237 LOGD("Ip Addr[%d]:%.2x", i, 0x0FF & he->h_addr_list[0][i]);
238 }
239 }
240 memcpy(&sock_info->addr.addr.serverSockAddr.sin_addr, he->h_addr_list[0], sizeof(struct in_addr));
241 sock_info->addr.addr_len = sizeof(struct sockaddr_in);
242
243 return 0;
244}
245
246int sock_bind(int sockfd, bool is_ipv6, uint16 port)
247{
248 int ret_val;
249 struct sockaddr *ds_sockaddr = NULL;
250 struct sockaddr_in localaddr;
251 uint16 addr_legth = 0;
252
253 memset((char *) &localaddr, 0, sizeof(struct sockaddr_in));
254 localaddr.sin_family = AF_INET;
255 localaddr.sin_addr.s_addr = INADDR_ANY;
256 localaddr.sin_port = htons(port);
257 addr_legth = sizeof(struct sockaddr_in);
258 ds_sockaddr = (struct sockaddr *)&localaddr;
259 ret_val = bind(sockfd,ds_sockaddr,addr_legth);
260 if (ret_val < 0){
261 LOGE("bind() sockfd= %d fail", sockfd);
262 return -1;
263 }
264
265 return 0;
266}
267
268int sock_read(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, void *buffer, uint32 buf_len, uint32 timeout, int *err)
269{
270 unsigned int count = 0;
271 int len = 0;
272 int try_count = 0;
273 int times = timeout / 50;
274 memset(buffer, 0x0, buf_len);
275 while (count < buf_len)
276 {
277 try_count++;
278 if (sock_info->is_ssl)
279 {
280
281 }
282 else
283 {
284 len = read(sock_info->fd, (char*) buffer + count, buf_len - count);
285 }
286 if (len <= 0)
287 {
288 if (errno == EWOULDBLOCK || errno == EAGAIN)
289 {
290 if (count > 0) // Read data
291 break; // Read data end.
292
293 if (try_count >= times) // Timeout
294 {
295 count = -1;
296 if (times != 0)
297 {
298 *err = 10; // FTP_ERR_NET_TIMEOUT
299 }
300 LOGE("Not read enough data,return.[%d/%d]", count, buf_len);
301 break;
302 }
303 else
304 {
305 usleep(50000);
306 continue;
307 }
308 }
309 else
310 {
311 LOGD("read error.[%d]", errno);
312 if (errno == EINPROGRESS)
313 {
314 if (close(sock_info->fd) == 0) // Success
315 {
316 LOGD("Socket disconnected.Close it.");
317 sock_info->fd = -1;
318 }
319 if (count <= 0)
320 count = -1;
321 }
322 else
323 {
324 if (count <= 0)
325 count = 0;
326 }
327 break;
328 }
329 }
330 else
331 {
332 count += len;
333 }
334 }
335
336 LOGV("Read data[%d/%d].", count, buf_len);
337
338 return count;
339}
340
341int sock_read_async(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, void *buffer, uint32 buf_len, uint32 timeout, int *err)
342{
343 int len = -1;
344 int try_count = 0;
345 int times = timeout / 50;
346
347TCP_READ_AGAIN:
348 memset(buffer, 0x0, buf_len);
349 try_count++;
350 if (sock_info->is_ssl)
351 {
352
353 }
354 else
355 {
356 len = read(sock_info->fd, (char*) buffer, buf_len);
357 }
358 if (len < 0)
359 {
360 if (errno == EWOULDBLOCK)
361 {
362 if(try_count == times)
363 {
364 LOGD("read timeout");
365 return -1;
366 }
367 usleep(50000);
368 goto TCP_READ_AGAIN;
369 }
370 else
371 {
372 LOGE("read error.[%d]", errno);
373 return -1;
374 }
375 }
376
377 LOGV("Read data[%d/%d].", len, buf_len);
378
379 return len;
380}
381
382#if 0
383int sock_readline(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, void *buffer, uint32 buf_len, uint32 timeout, int *err)
384{
385 if (sock_info->fd > 0)
386 {
387 char *buf_ptr = (char*)buffer;
388 char read_buf[1];
389 int read_len = 0;
390 while (TRUE)
391 {
392 if (sock_read_async(net_info, sock_info, read_buf, 1, timeout, err) == 1)
393 {
394 *buf_ptr++ = read_buf[0];
395 read_len++;
396
397 if (read_buf[0] == '\n' || read_len >= buf_len)
398 {
399 return read_len;
400 }
401 }
402 else
403 {
404 return -1;
405 }
406 }
407 }
408 return -1;
409}
410#else
411int sock_readline(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, void *buffer, uint32 buf_len, uint32 timeout, int *err)
412{
413 if (sock_info->fd > 0)
414 {
415 memset(buffer,0,buf_len);
416 int read_len = 0;
417 char *buf_ptr = (char*)buffer;
418 char *temp_ptr = sock_info->read_buff.buffer;
419
420 LOGV("TEMP buffer[fd - %d, len-%d]:%s", sock_info->fd, sock_info->read_buff.size, sock_info->read_buff.buffer);
421copy_angin:
422 while(sock_info->read_buff.size > 0 && *temp_ptr != '\n') {
423 *buf_ptr++ = *temp_ptr++;
424 sock_info->read_buff.size--;
425 read_len++;
426 }
427
428 LOGV("SIZE : %d,TEMP is \\n : %d", sock_info->read_buff.size, *temp_ptr == '\n');
429
430 if(sock_info->read_buff.size == 0) {
431 sock_info->read_buff.size = sock_read(net_info, sock_info, sock_info->read_buff.buffer,
432 sock_info->read_buff.size_max, timeout, err);
433 if(sock_info->read_buff.size <= 0) {
434 if(read_len == 0) { // No data.
435 LOGE("sock_read() fail.");
436 return -1;
437 } else {
438 return read_len;
439 }
440 }
441
442 temp_ptr = sock_info->read_buff.buffer;
443 goto copy_angin;
444 } else if(*temp_ptr == '\n') { // Read line.
445 *buf_ptr++ = '\n';
446 sock_info->read_buff.size--;
447 read_len++;
448
449 if(sock_info->read_buff.size > 0)
450 memcpy(sock_info->read_buff.buffer, temp_ptr + 1, sock_info->read_buff.size);
451
452 return read_len;
453 }
454
455 }
456 return -1;
457}
458#endif
459
460int sock_write(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, const void *buffer, uint32 buf_len, uint32 timeout, int *err)
461{
462 int len = 0;
463 uint32 try_count = 0;
464 uint32 times = timeout * 100;
465 unsigned int count = 0;
466 while (count < buf_len)
467 {
468 try_count++;
469 if (sock_info->is_ssl)
470 {
471 //printf(" try_count = %d timeout = %d sock_info->is_ssl = 1\n",try_count,timeout);
472 if(try_count >= times)
473 {
474 printf("over time \n");
475 break;
476 }
477 }
478 else
479 {
480 len = write(sock_info->fd, (const char*)buffer + count, buf_len - count);
481 }
482 if (len < 0)
483 {
484 if (errno == EWOULDBLOCK)
485 {
486 usleep(50000);
487 continue;
488 }
489 else
490 {
491 LOGE("write error.[%d]", errno);
492 if (count <= 0)
493 count = -1;
494 break;
495 }
496 }
497 else if (len == 0)
498 {
499 LOGE("write error(len == 0).[%d]", errno);
500 }
501 else
502 {
503 count += len;
504 }
505 }
506
507 if (count > 0)
508 {
509 LOGV("Write data[%d/%d] success.", count, buf_len);
510 }
511 else // Open session fail
512 {
513 LOGV("Write data[%d/%d] fail.", count, buf_len);
514 }
515
516 return count;
517}
518
519int sock_recv(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, void *buffer, uint32 buf_len, uint32 timeout)
520{
521 unsigned int count = 0;
522 int len = 0;
523 int try_count = 0;
524 int times = timeout / 50; // ms
525 memset(buffer, 0x0, buf_len);
526 while (count < buf_len)
527 {
528 try_count++;
529 len = recvfrom(sock_info->fd, (char*) buffer + count, buf_len - count, 0,
530 (struct sockaddr *)&sock_info->addr.addr.serverSockAddr, (socklen_t *)&sock_info->addr.addr_len);
531 if (len <= 0)
532 {
533 if (errno == EWOULDBLOCK || errno == EAGAIN)
534 {
535 if (count > 0) // Read data
536 break; // Read data end.
537
538 if (try_count >= times) // Timeout
539 {
540 count = -1;
541 break;
542 }
543 else
544 {
545 usleep(50000);
546 continue;
547 }
548 }
549 else if (errno == EINPROGRESS)
550 {
551 if (close(sock_info->fd) == 0) // Success
552 {
553 LOGD("Socket disconnected.Close it.");
554 }
555 if (count <= 0)
556 count = -1;
557 break;
558 }
559 else
560 {
561 LOGE("recv error.[%d]", errno);
562 if (count <= 0)
563 count = -1;
564 break;
565 }
566 }
567 else
568 {
569 count += len;
570 }
571 }
572
573 LOGV("Read data[%d/%d].", count, buf_len);
574
575 return count;
576}
577
578int sock_sendto(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, const void *buffer, uint32 buf_len, uint32 timeout)
579{
580 int len = 0;
581 unsigned int count = 0;
582 int try_count = 0;
583 int times = timeout / 50; // ms
584 while (count < buf_len)
585 {
586 try_count++;
587 len = sendto(sock_info->fd,(char*) buffer + count, buf_len - count, 0,
588 (struct sockaddr *)&sock_info->addr.addr.serverSockAddr, sock_info->addr.addr_len);
589 if (len < 0)
590 {
591 if (errno == EWOULDBLOCK)
592 {
593 if (count > 0) // Send part data
594 break;
595
596 if (try_count >= times) // Timeout
597 {
598 count = -1;
599 break;
600 }
601 else
602 {
603 usleep(50000);
604 continue;
605 }
606 }
607 else
608 {
609 LOGE("sendto error.[%d]", errno);
610 if (count <= 0)
611 count = -1;
612 break;
613 }
614 }
615 else if (len == 0)
616 {
617 LOGE("write error(len == 0).[%d]", errno);
618 }
619 else
620 {
621 count += len;
622 }
623 }
624
625 if (count == buf_len)
626 {
627 LOGV("Sendto data[%d/%d] success.", count, buf_len);
628 }
629 else
630 {
631 LOGV("Sendto data[%d/%d] fail.", count, buf_len);
632 }
633
634 return count;
635}
636
637int sock_close(mbtk_net_info_s *net_info, mbtk_sock_info_s *sock_info, uint32 timeout, int *err)
638{
639 if(sock_info->fd > 0) {
640 if(close(sock_info->fd) < 0)
641 {
642 LOGE("Close socket fail[%d].", errno);
643 return -1;
644 }
645 sock_info->fd = -1;
646 }
647
648 if(sock_info->read_buff.buffer) {
649 free(sock_info->read_buff.buffer);
650 sock_info->read_buff.buffer = NULL;
651 }
652 return 0;
653}
654
655int sock_net_close(mbtk_net_info_s *net_info, uint32 timeout, int *err)
656{
657 return 0;
658}
659