blob: fadb1a690ee448d1b750ee02673c910898138845 [file] [log] [blame]
b.liu9fc00602023-09-24 13:16:50 +08001#include <stdio.h>
2#include <stdlib.h>
b.liu9fc00602023-09-24 13:16:50 +08003#include <unistd.h>
b.liueb040652023-09-25 18:50:56 +08004#include <errno.h>
5#include <sys/socket.h>
6#include <sys/un.h>
7#include <netinet/in.h>
8#include <pthread.h>
9#include <sys/epoll.h>
10#include <string.h>
11#include <fcntl.h>
12#include <signal.h>
13#include <netinet/tcp.h>
b.liu9fc00602023-09-24 13:16:50 +080014
15#include "mbtk_log.h"
16#include "mbtk_tcpip.h"
b.liueb040652023-09-25 18:50:56 +080017#include "mbtk_net_control.h"
18
19#define MBTK_TCPIP_CID_DEFAULT 0
20#define MBTK_TCPIP_READ_BUFF_SIZE 2048
21#define TCPIP_DEBUG 0
b.liu9fc00602023-09-24 13:16:50 +080022
23typedef struct {
b.liueb040652023-09-25 18:50:56 +080024 mbtk_sock_session sock_fd;
b.liu9fc00602023-09-24 13:16:50 +080025 char ser_addr[256];
26 int ser_port;
27 char local_addr[256];
28 int local_port;
29 bool ack_support;
30 bool ssl_support;
31 bool ignore_cert;
32 uint32 heartbeat_time;
33 uint32 delay_time;
34
35 mbtk_tcpip_read_callback_func read_cb;
36
37 uint32 data_traffic_send;
38 uint32 data_traffic_recv;
39
40} mbtk_tcpip_cli_info_t;
41
42typedef struct {
b.liueb040652023-09-25 18:50:56 +080043 mbtk_sock_session sock_fd;
b.liu9fc00602023-09-24 13:16:50 +080044 char ser_addr[256];
45 int ser_port;
46
47 int cli_num;
48 mbtk_tcpip_cli_info_t *cli_list;
49} mbtk_tcpip_ser_info_t;
50
51typedef struct {
52 int link_id;
53 int link_cid;
b.liueb040652023-09-25 18:50:56 +080054 bool link_connected;
55 mbtk_sock_type prot_type; // TCP/UDP
b.liu9fc00602023-09-24 13:16:50 +080056
57 mbtk_tcpip_type_enum type;
58 union
59 {
60 mbtk_tcpip_cli_info_t cli_info;
61 mbtk_tcpip_ser_info_t ser_info;
62 } tcpip_info;
63
64} mbtk_tcpip_link_t;
65
66static mbtk_tcpip_link_t tcpip_link[MBTK_TCPIP_LINK_MAX];
b.liueb040652023-09-25 18:50:56 +080067static bool tcpip_inited = FALSE;
68static mbtk_sock_handle tcpip_handle;
69static mbtk_tcpip_net_callback_func tcpip_net_cb = NULL;
70static mbtk_tcpip_sock_callback_func tcpip_sock_cb = NULL;
b.liu9fc00602023-09-24 13:16:50 +080071
b.liueb040652023-09-25 18:50:56 +080072/*
73struct tcp_info
b.liu9fc00602023-09-24 13:16:50 +080074{
b.liueb040652023-09-25 18:50:56 +080075 u_int8_t tcpi_state;
76 u_int8_t tcpi_ca_state;
77 u_int8_t tcpi_retransmits;
78 u_int8_t tcpi_probes;
79 u_int8_t tcpi_backoff;
80 u_int8_t tcpi_options;
81 u_int8_t tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;
82
83 u_int32_t tcpi_rto;
84 u_int32_t tcpi_ato;
85 u_int32_t tcpi_snd_mss;
86 u_int32_t tcpi_rcv_mss;
87
88 u_int32_t tcpi_unacked;
89 u_int32_t tcpi_sacked;
90 u_int32_t tcpi_lost;
91 u_int32_t tcpi_retrans;
92 u_int32_t tcpi_fackets;
93
94 u_int32_t tcpi_last_data_sent;
95 u_int32_t tcpi_last_ack_sent;
96 u_int32_t tcpi_last_data_recv;
97 u_int32_t tcpi_last_ack_recv;
98
99 u_int32_t tcpi_pmtu;
100 u_int32_t tcpi_rcv_ssthresh;
101 u_int32_t tcpi_rtt;
102 u_int32_t tcpi_rttvar;
103 u_int32_t tcpi_snd_ssthresh;
104 u_int32_t tcpi_snd_cwnd;
105 u_int32_t tcpi_advmss;
106 u_int32_t tcpi_reordering;
107
108 u_int32_t tcpi_rcv_rtt;
109 u_int32_t tcpi_rcv_space;
110
111 u_int32_t tcpi_total_retrans;
112};
113
114*/
115void tcp_info_print(struct tcp_info *tcp)
116{
117 if(tcp) {
118 LOGD("tcpi_state = %d", tcp->tcpi_state);
119 LOGD("tcpi_ca_state = %d", tcp->tcpi_ca_state);
120 LOGD("tcpi_retransmits = %d", tcp->tcpi_retransmits);
121 LOGD("tcpi_probes = %d", tcp->tcpi_probes);
122 LOGD("tcpi_backoff = %d", tcp->tcpi_backoff);
123 LOGD("tcpi_options = %d", tcp->tcpi_options);
124 LOGD("tcpi_snd_wscale = %d", 0x0F & tcp->tcpi_snd_wscale);
125 LOGD("tcpi_rcv_wscale = %d", 0xF0 & tcp->tcpi_rcv_wscale);
126 LOGD("tcpi_rto = %d", tcp->tcpi_rto);
127 LOGD("tcpi_ato = %d", tcp->tcpi_ato);
128 LOGD("tcpi_snd_mss = %d", tcp->tcpi_snd_mss);
129 LOGD("tcpi_rcv_mss = %d", tcp->tcpi_rcv_mss);
130 LOGD("tcpi_unacked = %d", tcp->tcpi_unacked);
131 LOGD("tcpi_sacked = %d", tcp->tcpi_sacked);
132 LOGD("tcpi_lost = %d", tcp->tcpi_lost);
133 LOGD("tcpi_retrans = %d", tcp->tcpi_retrans);
134 LOGD("tcpi_fackets = %d", tcp->tcpi_fackets);
135 LOGD("tcpi_last_data_sent = %d", tcp->tcpi_last_data_sent);
136 LOGD("tcpi_last_ack_sent = %d", tcp->tcpi_last_ack_sent);
137 LOGD("tcpi_last_data_recv = %d", tcp->tcpi_last_data_recv);
138 LOGD("tcpi_last_ack_recv = %d", tcp->tcpi_last_ack_recv);
139 LOGD("tcpi_pmtu = %d", tcp->tcpi_pmtu);
140 LOGD("tcpi_rcv_ssthresh = %d", tcp->tcpi_rcv_ssthresh);
141 LOGD("tcpi_rtt = %d", tcp->tcpi_rtt);
142 LOGD("tcpi_rttvar = %d", tcp->tcpi_rttvar);
143 LOGD("tcpi_snd_ssthresh = %d", tcp->tcpi_snd_ssthresh);
144 LOGD("tcpi_snd_cwnd = %d", tcp->tcpi_snd_cwnd);
145 LOGD("tcpi_advmss = %d", tcp->tcpi_advmss);
146 LOGD("tcpi_reordering = %d", tcp->tcpi_reordering);
147 LOGD("tcpi_rcv_rtt = %d", tcp->tcpi_rcv_rtt);
148 LOGD("tcpi_rcv_space = %d", tcp->tcpi_rcv_space);
149 LOGD("tcpi_total_retrans = %d", tcp->tcpi_total_retrans);
150 }
151}
152
153static int tcpip_fd_2_link(int fd)
154{
155 int link_id = 0;
156 mbtk_tcpip_link_t *link = NULL;
157 for(; link_id < MBTK_TCPIP_LINK_MAX; link_id++) {
158 link = tcpip_link + link_id;
159 if(link->link_connected) {
160 if(link->type == MBTK_TCPIP_TYPE_CLIENT) {
161 if(link->tcpip_info.cli_info.sock_fd > 0 && link->tcpip_info.cli_info.sock_fd == fd) {
162 break;
163 }
164 } else {
165 // Not support.
166
167 }
168 }
169 }
170
171 if(link_id == MBTK_TCPIP_LINK_MAX) {
172 return -1;
173 } else {
174 return link_id;
175 }
176}
177
b.liu8181e142023-09-26 10:31:10 +0800178static void tcpip_sock_cb_func(int handle, mbtk_sock_cb_info_s *sock_info)
b.liueb040652023-09-25 18:50:56 +0800179{
180 if(tcpip_inited && tcpip_handle == handle/* && http_fd == fd*/) {
b.liu8181e142023-09-26 10:31:10 +0800181 if(sock_info->event & (EPOLLIN | EPOLLOUT)) { // Cand read or write.
b.liueb040652023-09-25 18:50:56 +0800182 int sock_error = 0;
183 socklen_t socklen = sizeof(sock_error);
b.liu8181e142023-09-26 10:31:10 +0800184 if(getsockopt(sock_info->sock_fd, SOL_SOCKET, SO_ERROR, &sock_error, (socklen_t*)&socklen) == 0) {
b.liu94baa7c2023-09-26 16:26:10 +0800185 LOGV("Socket error:%d", sock_error);
b.liueb040652023-09-25 18:50:56 +0800186 }
187
188 if(sock_error) {
189 LOGW("errno = %d", errno);
wangyouqiang6ade7fb2023-10-11 14:00:07 +0800190 if(sock_error == ECONNRESET|| errno == EAGAIN)
191 {
192 //
193 }
194 else
195 {
196 return;
197 }
b.liueb040652023-09-25 18:50:56 +0800198 }
199
b.liu8181e142023-09-26 10:31:10 +0800200 if(sock_info->sock_type == MBTK_SOCK_TCP) {
201 struct tcp_info info;
202 int len = sizeof(info);
203 if(getsockopt(sock_info->sock_fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&len) == 0) {
b.liu94baa7c2023-09-26 16:26:10 +0800204 LOGV("State : %d", info.tcpi_state);
b.liueb040652023-09-25 18:50:56 +0800205#if TCPIP_DEBUG
b.liu94baa7c2023-09-26 16:26:10 +0800206 //tcp_info_print(&info);
b.liueb040652023-09-25 18:50:56 +0800207#endif
b.liueb040652023-09-25 18:50:56 +0800208 }
b.liu8181e142023-09-26 10:31:10 +0800209
210 if(TCP_ESTABLISHED != info.tcpi_state) {
211 LOGW("errno = %d", errno);
212 int link_id = tcpip_fd_2_link(sock_info->sock_fd);
213 if(link_id >= 0) {
214 // Socket disconnected?
215 mbtk_tcpip_sock_close(link_id);
216
217 if(tcpip_sock_cb) {
218 tcpip_sock_cb(link_id, 0);
219 }
220 }
221 return;
222 }
b.liueb040652023-09-25 18:50:56 +0800223 }
224 }
225
b.liu8181e142023-09-26 10:31:10 +0800226 if(sock_info->event & EPOLLIN) { // READ
b.liu94baa7c2023-09-26 16:26:10 +0800227 LOGV("fd[%d] can read.", sock_info->sock_fd);
b.liu8181e142023-09-26 10:31:10 +0800228 int link_id = tcpip_fd_2_link(sock_info->sock_fd);
b.liueb040652023-09-25 18:50:56 +0800229 if(link_id >= 0) {
230 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
231 if(tcpip_link[link_id].tcpip_info.cli_info.read_cb) {
232 char buff[MBTK_TCPIP_READ_BUFF_SIZE];
233 memset(buff, 0x0, MBTK_TCPIP_READ_BUFF_SIZE);
b.liu8181e142023-09-26 10:31:10 +0800234 int read_len = mbtk_sock_read_async(tcpip_handle, sock_info->sock_fd, buff, MBTK_TCPIP_READ_BUFF_SIZE);
b.liueb040652023-09-25 18:50:56 +0800235 if(read_len > 0) {
236 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += read_len;
237 tcpip_link[link_id].tcpip_info.cli_info.read_cb(link_id, (const char*)buff, read_len);
238 }
239#if 0
240 else { // Socket error(Such as server disconnected.).
241 LOGW("errno = %d", errno);
242 // Socket disconnected?
243 mbtk_tcpip_sock_close(link_id);
244
245 if(tcpip_sock_cb) {
246 tcpip_sock_cb(link_id, 0);
247 }
248 }
249#endif
250 while(read_len > 0) {
251 memset(buff, 0x0, MBTK_TCPIP_READ_BUFF_SIZE);
b.liu8181e142023-09-26 10:31:10 +0800252 read_len = mbtk_sock_read_async(tcpip_handle, sock_info->sock_fd, buff, MBTK_TCPIP_READ_BUFF_SIZE);
b.liueb040652023-09-25 18:50:56 +0800253 // LOGD("read_len = %d", read_len);
254 if(read_len > 0) {
255 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += read_len;
256 tcpip_link[link_id].tcpip_info.cli_info.read_cb(link_id, (const char*)buff, read_len);
257 }
258 }
259 }
260 } else {
261 // Not support.
262
263 }
264 }
b.liu8181e142023-09-26 10:31:10 +0800265 } else if(sock_info->event & EPOLLRDHUP) { // Close
266 LOGD("fd[%d] Closed?", sock_info->sock_fd);
b.liueb040652023-09-25 18:50:56 +0800267 } else {
b.liu8181e142023-09-26 10:31:10 +0800268 LOGW("Unknown event:%x",sock_info->event);
b.liueb040652023-09-25 18:50:56 +0800269 }
270 }
271}
272
b.liu8181e142023-09-26 10:31:10 +0800273static void tcpip_net_cb_func(mbtk_sock_handle handle, mbtk_net_cb_info_s *info)
b.liueb040652023-09-25 18:50:56 +0800274{
275 if(tcpip_inited && tcpip_handle == handle) {
b.liu8181e142023-09-26 10:31:10 +0800276 LOGD("Net state : %d, %s, %s", info->state, info->if_name, info->addr);
277 if(info->state == 0) {
b.liueb040652023-09-25 18:50:56 +0800278 mbtk_tcpip_net_close();
279 }
280
281 if(tcpip_net_cb) {
b.liu8181e142023-09-26 10:31:10 +0800282 tcpip_net_cb(info->state, info->addr);
b.liueb040652023-09-25 18:50:56 +0800283 }
284 }
285}
286
287static bool tcpip_link_check(int link_id)
288{
289 if(tcpip_inited && link_id >= 0 && link_id < MBTK_TCPIP_LINK_MAX) {
290 return TRUE;
291 }
292
293 return FALSE;
294}
295
296static bool tcpip_link_connected(int link_id)
297{
298 if(!tcpip_link_check(link_id)) {
299 return FALSE;
300 }
301
302 if(!tcpip_link[link_id].link_connected) {
303 return FALSE;
304 }
305
306 return TRUE;
307}
308
309static int tcpip_link_reset(mbtk_tcpip_link_t *link, int link_id)
310{
311 if(link) {
312 // Close socket if necessary.
313 if(link->link_connected) {
314 if(link->type == MBTK_TCPIP_TYPE_CLIENT) {
315 if(link->tcpip_info.cli_info.sock_fd > 0) {
316 int err;
317 if(mbtk_sock_close(tcpip_handle, link->tcpip_info.cli_info.sock_fd, 3000, &err) == 0
318 && err == MBTK_SOCK_SUCCESS) {
319 LOGD("Close socket[%d] success.", link->tcpip_info.cli_info.sock_fd);
320 } else {
321 LOGE("Close socket[%d] fail.", link->tcpip_info.cli_info.sock_fd);
322 return -1;
323 }
324 }
325 } else {
326 // Not support.
327
328 return -1;
329 }
330 }
331
332 memset(link, 0x0, sizeof(mbtk_tcpip_link_t));
333 link->link_id = link_id;
334 link->link_cid = MBTK_TCPIP_CID_DEFAULT;
335 return 0;
336 }
337
338 return -1;
339}
340
341mbtk_tcpip_err_enum mbtk_tcpip_net_open(mbtk_tcpip_net_callback_func net_cb, mbtk_tcpip_sock_callback_func sock_cb)
342{
343 if(tcpip_inited) {
344 LOGW("TCP/IP has inited.");
345 return MBTK_TCPIP_ERR_SUCCESS;
346 } else {
347 //mbtk_log_init("radio", "MBTK_TCPIP");
348 mbtk_net_state_t net_state = mbtk_net_state_get();
349 if(net_state == MBTK_NET_STATE_OFF) {
350 LOGE("Network unavailable.");
351 return MBTK_TCPIP_ERR_NET_UNAVAILABLE;
352 }
353
354 memset(&tcpip_link, 0x0, sizeof(mbtk_tcpip_link_t) * MBTK_TCPIP_LINK_MAX);
355 int i = 0;
356 for(; i < MBTK_TCPIP_LINK_MAX; i++) {
357 tcpip_link_reset(tcpip_link + i, i);
358 }
359
360 mbtk_init_info init_info;
361 init_info.net_type = MBTK_NET_LINUX;
362 init_info.net_cb = tcpip_net_cb_func;
363 init_info.sock_cb = tcpip_sock_cb_func;
364 sprintf(init_info.if_name, "ccinet%d", MBTK_TCPIP_CID_DEFAULT);
365 tcpip_handle = mbtk_sock_init(&init_info);
366 if(tcpip_handle < 0) {
367 LOGE("mbtk_sock_init() fail.");
368 return MBTK_TCPIP_ERR_NET_HANDLE;
369 }
370
371 tcpip_net_cb = net_cb;
372 tcpip_sock_cb = sock_cb;
373 tcpip_inited = TRUE;
374 return MBTK_TCPIP_ERR_SUCCESS;
375 }
b.liu9fc00602023-09-24 13:16:50 +0800376}
377
378mbtk_tcpip_err_enum mbtk_tcpip_net_close()
379{
b.liueb040652023-09-25 18:50:56 +0800380 if(tcpip_inited) {
381 int i = 0;
382 mbtk_tcpip_err_enum tcpip_err = MBTK_TCPIP_ERR_SUCCESS;
383
384 // Close all socket.
385 for(; i < MBTK_TCPIP_LINK_MAX; i++) {
386 if(tcpip_link_reset(tcpip_link + i, i)) {
387 tcpip_err = MBTK_TCPIP_ERR_UNKNOWN;
388 }
389 }
390
391 if(tcpip_err == MBTK_TCPIP_ERR_SUCCESS) {
392 tcpip_inited = FALSE;
393 }
394 return tcpip_err;
395 } else {
396 LOGW("TCP/IP not inited.");
397 return MBTK_TCPIP_ERR_SUCCESS;
398 }
b.liu9fc00602023-09-24 13:16:50 +0800399}
400
401mbtk_tcpip_err_enum mbtk_tcpip_sock_open(const mbtk_tcpip_info_t *tcpip_info)
402{
b.liueb040652023-09-25 18:50:56 +0800403 if(tcpip_info == NULL || strlen(tcpip_info->ser_addr) == 0 || tcpip_info->ser_port <= 0) {
404 LOGE("ARG error.");
405 return MBTK_TCPIP_ERR_ARG;
406 }
407
408 if(!tcpip_link_check(tcpip_info->link_id)) {
409 LOGE("Link[%d] error.", tcpip_info->link_id);
410 return MBTK_TCPIP_ERR_LINK_UNAVAILABLE;
411 }
412
413 if(tcpip_link_connected(tcpip_info->link_id)) {
414 LOGE("Link[%d] has connected.", tcpip_info->link_id);
415 return MBTK_TCPIP_ERR_LINK_UNAVAILABLE;
416 }
417
418 if(tcpip_info->tcpip_type == MBTK_TCPIP_TYPE_CLIENT) {
419 mbtk_tcpip_link_t *link = tcpip_link + tcpip_info->link_id;
420
421 int err;
422 mbtk_sock_info sock_info;
423 memset(&sock_info, 0x0, sizeof(mbtk_sock_info));
424 sock_info.type = tcpip_info->prot_type;
425 sock_info.is_support_ssl = tcpip_info->ssl_support;
426 sock_info.ingnore_cert = tcpip_info->ignore_cert;
427 memcpy(sock_info.address, tcpip_info->ser_addr, strlen(tcpip_info->ser_addr));
428 sock_info.port = tcpip_info->ser_port;
429 sock_info.local_port = tcpip_info->local_port;
430
431 link->tcpip_info.cli_info.sock_fd = mbtk_sock_open(tcpip_handle, &sock_info, 3000, &err);
432 if(link->tcpip_info.cli_info.sock_fd > 0) {
433 link->prot_type = tcpip_info->prot_type;
434 link->type = MBTK_TCPIP_TYPE_CLIENT;
435 memcpy(link->tcpip_info.cli_info.ser_addr, tcpip_info->ser_addr, strlen(tcpip_info->ser_addr));
436 link->tcpip_info.cli_info.ser_port = tcpip_info->ser_port;
437 link->tcpip_info.cli_info.local_port = tcpip_info->local_port;
438 link->tcpip_info.cli_info.ack_support = tcpip_info->ack_support;
439 link->tcpip_info.cli_info.ssl_support = tcpip_info->ssl_support;
440 link->tcpip_info.cli_info.ignore_cert = tcpip_info->ignore_cert;
441 link->tcpip_info.cli_info.heartbeat_time = tcpip_info->heartbeat_time;
442 link->tcpip_info.cli_info.delay_time = tcpip_info->delay_time;
443 link->tcpip_info.cli_info.read_cb = tcpip_info->read_cb;
444 link->link_connected = TRUE;
445 LOGD("Open socket[%d] success.");
446 return MBTK_TCPIP_ERR_SUCCESS;
447 } else {
448 LOGE("Open socket[%d] fail.");
449 return MBTK_TCPIP_ERR_UNKNOWN;
450 }
451 } else {
452 LOGE("Only support CLIENT now!");
453 return MBTK_TCPIP_ERR_UNKNOWN;
454 }
b.liu9fc00602023-09-24 13:16:50 +0800455}
456
457mbtk_tcpip_err_enum mbtk_tcpip_sock_close(int link_id)
458{
b.liueb040652023-09-25 18:50:56 +0800459 if(!tcpip_link_connected(link_id)) {
460 LOGE("Link[%d] not connected.", link_id);
461 return MBTK_TCPIP_ERR_LINK_NOT_CONNECT;
462 }
463
464 if(tcpip_link_reset(tcpip_link + link_id, link_id)) {
465 LOGE("Close link[%d] fail.", link_id);
466 return MBTK_TCPIP_ERR_UNKNOWN;
467 }
468
b.liu9fc00602023-09-24 13:16:50 +0800469 return MBTK_TCPIP_ERR_SUCCESS;
470}
471
472int mbtk_tcpip_send(int link_id, const char* data, int data_len, const char* ser_addr, int ser_port)
473{
b.liueb040652023-09-25 18:50:56 +0800474 if(!tcpip_link_connected(link_id)) {
475 LOGE("Link[%d] not connected.", link_id);
476 return -1;
477 }
478
479 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
480 int err;
481 int len = mbtk_sock_write(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.sock_fd,
482 data, data_len, 3000, &err);
483 if(len > 0) {
484 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_send += len;
485 }
486 return len;
487 } else {
488 // Not support.
489 return -1;
490 }
b.liu9fc00602023-09-24 13:16:50 +0800491
492 return 0;
493}
494
495int mbtk_tcpip_read(int link_id, char* buff, int buff_size)
496{
b.liueb040652023-09-25 18:50:56 +0800497 if(!tcpip_link_connected(link_id)) {
498 LOGE("Link[%d] not connected.", link_id);
499 return -1;
500 }
501
502 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
503 if(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.read_cb) {
504 LOGE("Set read_cb function,can not manual read.");
505 return -1;
506 }
b.liu94baa7c2023-09-26 16:26:10 +0800507 int len = mbtk_sock_read_async(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.sock_fd,
508 buff, buff_size);
b.liueb040652023-09-25 18:50:56 +0800509 if(len > 0) {
510 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += len;
511 }
512 return len;
513 } else {
514 // Not support.
515 return -1;
516 }
b.liu9fc00602023-09-24 13:16:50 +0800517}
518
519/*
b.liueb040652023-09-25 18:50:56 +0800520* Get the data traffic of the specified link. Return -1 if fail.
b.liu9fc00602023-09-24 13:16:50 +0800521*/
522int mbtk_tcpip_data_traffic_get(int link_id)
523{
b.liueb040652023-09-25 18:50:56 +0800524 if(!tcpip_link_connected(link_id)) {
525 LOGE("Link[%d] not connected.", link_id);
526 return -1;
527 }
528
529 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
530 return tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv
531 + tcpip_link[link_id].tcpip_info.cli_info.data_traffic_send;
532 } else {
533 // Not support.
534 return -1;
535 }
b.liu9fc00602023-09-24 13:16:50 +0800536}
537
538/*
539* Reset the data traffic of the specified link.
540*/
541mbtk_tcpip_err_enum mbtk_tcpip_data_traffic_reset(int link_id)
542{
b.liueb040652023-09-25 18:50:56 +0800543 if(!tcpip_link_connected(link_id)) {
544 LOGE("Link[%d] not connected.", link_id);
545 return MBTK_TCPIP_ERR_LINK_NOT_CONNECT;
546 }
547
548 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
549 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv = 0;
550 tcpip_link[link_id].tcpip_info.cli_info.data_traffic_send = 0;
551 return MBTK_TCPIP_ERR_SUCCESS;
552 } else {
553 // Not support.
554
555 }
556
557 return MBTK_TCPIP_ERR_UNKNOWN;
b.liu9fc00602023-09-24 13:16:50 +0800558}
559
560/*
b.liueb040652023-09-25 18:50:56 +0800561* Return 0 if disconnected, 1 for connected, other for fail.
b.liu9fc00602023-09-24 13:16:50 +0800562*/
563int mbtk_tcpip_link_state_get(int link_id)
564{
b.liueb040652023-09-25 18:50:56 +0800565 if(!tcpip_link_check(link_id)) {
566 LOGE("Link error.");
567 return -1;
568 }
569
570 return tcpip_link[link_id].link_connected ? 1 : 0;
b.liu9fc00602023-09-24 13:16:50 +0800571}
572
b.liu94baa7c2023-09-26 16:26:10 +0800573/*
574* Get TCP state informations.
575*/
576int mbtk_tcpip_info_get(int link_id, mbtk_tcpip_tcp_state_info_s *state_info)
577{
578 if(!tcpip_link_check(link_id)) {
579 LOGE("Link error.");
580 return -1;
581 }
582
583 if(state_info == NULL) {
584 LOGE("ARG error.");
585 return -1;
586 }
587
588 memset(state_info, 0x0, sizeof(mbtk_tcpip_tcp_state_info_s));
589
590 struct tcp_info info;
591 memset(&info, 0x0, sizeof(struct tcp_info));
592 int len = sizeof(info);
593 if(tcpip_link[link_id].prot_type == MBTK_SOCK_TCP) {
594 if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&len)) {
595 LOGE("Get TCP_INFO fail:%d", errno);
596 return -1;
597 }
598
599#if TCPIP_DEBUG
600 tcp_info_print(&info);
601#endif
602 state_info->state = info.tcpi_state;
603 } else {
604 state_info->state = 0;
605 }
606
607#if TCPIP_DEBUG
608 int rcvbuf_size;
609 len = sizeof(rcvbuf_size);
610 if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, (socklen_t*)&len)) {
611 LOGE("Get SO_RCVBUF fail:%d", errno);
612 return -1;
613 }
614 LOGD("SO_RCVBUF = %d", rcvbuf_size);
615
616 int sndbuf_size;
617 len = sizeof(sndbuf_size);
618 if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, (socklen_t*)&len)) {
619 LOGE("Get SO_SNDBUF fail:%d", errno);
620 return -1;
621 }
622 LOGD("SO_SNDBUF = %d", sndbuf_size);
623
624 int rcvlowat_size;
625 len = sizeof(rcvlowat_size);
626 if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, SOL_SOCKET, SO_RCVLOWAT, &rcvlowat_size, (socklen_t*)&len)) {
627 LOGE("Get SO_RCVLOWAT fail:%d", errno);
628 return -1;
629 }
630 LOGD("SO_RCVLOWAT = %d", rcvlowat_size);
631
632 int sndlowat_size;
633 len = sizeof(sndlowat_size);
634 if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, SOL_SOCKET, SO_SNDLOWAT, &sndlowat_size, (socklen_t*)&len)) {
635 LOGE("Get SO_SNDLOWAT fail:%d", errno);
636 return -1;
637 }
638 LOGD("SO_SNDLOWAT = %d", sndlowat_size);
639#endif
640
641 state_info->link_id = link_id;
642 state_info->sock_fd = tcpip_link[link_id].tcpip_info.cli_info.sock_fd;
643 state_info->recv_data_len = mbtk_sock_tcp_recv_len_get(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.sock_fd);
644
645 return 0;
646}
647
b.liua0c9df42023-09-27 21:22:01 +0800648/*
649* Set socket auto read callback function,NULL for close auto read.
650*/
651void mbtk_tcpip_set_read_cb(int link_id, mbtk_tcpip_read_callback_func read_cb)
652{
653 if(!tcpip_link_check(link_id)) {
654 LOGE("Link error.");
655 return;
656 }
657
658 tcpip_link[link_id].tcpip_info.cli_info.read_cb = read_cb;
659}
660
b.liubcf86c92024-08-19 19:48:28 +0800661void mbtk_tcpip_lib_info_print()
662{
663 MBTK_SOURCE_INFO_PRINT("mbtk_tcpip_lib");
664}
665