commit | author | age
|
29b331
|
1 |
/******************************************************************************** |
GW |
2 |
* Copyright: (C) 2022 LingYun IoT System Studio |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: socket.c |
|
6 |
* Description: This file is for socket API functions |
|
7 |
* |
|
8 |
* Version: 1.0.0(18/04/22) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "18/04/22 17:09:59" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
#include <stdio.h> |
|
14 |
#include <unistd.h> |
|
15 |
#include <string.h> |
|
16 |
#include <netdb.h> |
|
17 |
#include <fcntl.h> |
|
18 |
#include <unistd.h> |
|
19 |
#include <sys/un.h> |
|
20 |
#include <poll.h> |
|
21 |
#include <errno.h> |
|
22 |
#include <sys/types.h> |
|
23 |
#include <sys/socket.h> |
|
24 |
#include <linux/sockios.h> |
|
25 |
#include <sys/ioctl.h> |
|
26 |
#include <sys/socket.h> |
|
27 |
#include <netinet/in.h> |
|
28 |
#include <netinet/tcp.h> |
|
29 |
#include <arpa/inet.h> |
|
30 |
#include <sys/resource.h> |
|
31 |
|
|
32 |
#include "socket.h" |
|
33 |
#include "logger.h" |
|
34 |
|
|
35 |
/* description: initial socket context |
|
36 |
* input args: |
|
37 |
* $sock: socket context pointer |
|
38 |
* $host: connect server hostname for client mode, unused for server mode |
|
39 |
* $port: connect server port for client mode or listen port for server mode |
|
40 |
* return value: <0: failure 0:ok |
|
41 |
*/ |
|
42 |
int socket_init(socket_ctx_t *sock, char *host, int port) |
|
43 |
{ |
|
44 |
if( !sock || port<=0 ) |
|
45 |
return -1; |
|
46 |
|
|
47 |
memset( sock, 0, sizeof(*sock) ); |
|
48 |
sock->fd = -1; |
|
49 |
sock->port = port; |
|
50 |
if( host ) /* server no need it */ |
|
51 |
{ |
|
52 |
strncpy(sock->host, host, HOSTNAME_LEN); |
|
53 |
} |
|
54 |
|
|
55 |
return 0; |
|
56 |
} |
|
57 |
|
|
58 |
/* description: close socket |
|
59 |
* input args: |
|
60 |
* $sock: socket context pointer |
|
61 |
* return value: <0: failure 0:ok |
|
62 |
*/ |
|
63 |
int socket_term(socket_ctx_t *sock) |
|
64 |
{ |
|
65 |
if( !sock ) |
|
66 |
return -1; |
|
67 |
|
|
68 |
if( sock->fd > 0) |
|
69 |
{ |
|
70 |
close(sock->fd); |
|
71 |
sock->fd = -1; |
|
72 |
} |
|
73 |
|
|
74 |
return 0; |
|
75 |
} |
|
76 |
|
|
77 |
/* description: socket server start listen |
|
78 |
* input args: |
|
79 |
* $sock: socket context pointer |
|
80 |
* return value: <0: failure 0:ok |
|
81 |
*/ |
|
82 |
#if 0 /* --TBD-- */ |
|
83 |
int socket_listen(socket_ctx_t *sock) |
|
84 |
{ |
|
85 |
int rv = 0; |
|
86 |
struct sockaddr_in addr; |
|
87 |
int backlog = 13; |
|
88 |
|
|
89 |
if( !sock ) |
|
90 |
return -1; |
|
91 |
|
|
92 |
set_socket_rlimit(); /* set max open socket count */ |
|
93 |
} |
|
94 |
#endif |
|
95 |
|
|
96 |
/* description: socket connect to server in block mode |
|
97 |
* input args: |
|
98 |
* $sock: socket context pointer |
|
99 |
* return value: <0: failure 0:ok |
|
100 |
*/ |
|
101 |
int socket_connect(socket_ctx_t *sock) |
|
102 |
{ |
|
103 |
int rv = 0; |
|
104 |
int sockfd = 0; |
|
105 |
char service[20]; |
|
106 |
struct addrinfo hints, *rp; |
|
107 |
struct addrinfo *res = NULL; |
|
108 |
struct in_addr inaddr; |
|
109 |
struct sockaddr_in addr; |
|
110 |
int len = sizeof(addr); |
|
111 |
|
|
112 |
if( !sock ) |
|
113 |
return -1; |
|
114 |
|
|
115 |
socket_term(sock); |
|
116 |
|
|
117 |
/*+--------------------------------------------------+ |
|
118 |
*| use getaddrinfo() to do domain name translation | |
|
119 |
*+--------------------------------------------------+*/ |
|
120 |
|
|
121 |
memset(&hints, 0, sizeof(struct addrinfo)); |
|
122 |
hints.ai_family = AF_INET; /* Only support IPv4 */ |
|
123 |
hints.ai_socktype = SOCK_STREAM; |
|
124 |
hints.ai_protocol = IPPROTO_TCP; /* TCP protocol */ |
|
125 |
|
|
126 |
/* If $host is a valid IP address, then don't use name resolution */ |
|
127 |
if( inet_aton(sock->host, &inaddr) ) |
|
128 |
{ |
|
129 |
//log_info("%s is a valid IP address, don't use domain name resolution.\n", sock->host); |
|
130 |
hints.ai_flags |= AI_NUMERICHOST; |
|
131 |
} |
|
132 |
|
|
133 |
/* Obtain address(es) matching host/port */ |
|
134 |
snprintf(service, sizeof(service), "%d", sock->port); |
|
135 |
if( (rv=getaddrinfo(sock->host, service, &hints, &res)) ) |
|
136 |
{ |
|
137 |
log_error("getaddrinfo() parser [%s:%s] failed: %s\n", sock->host, service, gai_strerror(rv)); |
|
138 |
return -3; |
|
139 |
} |
|
140 |
|
|
141 |
/* getaddrinfo() returns a list of address structures. Try each |
|
142 |
address until we successfully connect or bind */ |
|
143 |
for (rp=res; rp!=NULL; rp=rp->ai_next) |
|
144 |
{ |
|
145 |
#if 0 |
|
146 |
char ipaddr[INET_ADDRSTRLEN]; |
|
147 |
struct sockaddr_in *sp = (struct sockaddr_in *) rp->ai_addr; |
|
148 |
|
|
149 |
/* print domain name translation result */ |
|
150 |
memset( ipaddr, 0, sizeof(ipaddr) ); |
|
151 |
if( inet_ntop(AF_INET, &sp->sin_addr, ipaddr, sizeof(ipaddr)) ) |
|
152 |
{ |
|
153 |
log_info("domain name resolution [%s->%s]\n", sock->host, ipaddr); |
|
154 |
} |
|
155 |
#endif |
|
156 |
|
|
157 |
/* Create the socket */ |
|
158 |
sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
|
159 |
if( sockfd < 0) |
|
160 |
{ |
|
161 |
log_error("socket() create failed: %s\n", strerror(errno)); |
|
162 |
rv = -3; |
|
163 |
continue; |
|
164 |
} |
|
165 |
|
|
166 |
/* connect to server */ |
|
167 |
rv = connect(sockfd, rp->ai_addr, len); |
|
168 |
if( 0 == rv ) |
|
169 |
{ |
|
170 |
sock->fd = sockfd; |
|
171 |
log_info("Connect to server[%s:%d] on fd[%d] successfully!\n", sock->host, sock->port, sockfd); |
|
172 |
break; |
|
173 |
} |
|
174 |
else |
|
175 |
{ |
|
176 |
/* socket connect get error, try another IP address */ |
|
177 |
close(sockfd); |
|
178 |
continue; |
|
179 |
} |
|
180 |
} |
|
181 |
|
|
182 |
freeaddrinfo(res); |
|
183 |
return rv; |
|
184 |
} |
|
185 |
|
|
186 |
/* description: send data from the socket |
|
187 |
* input args: |
|
188 |
* $sock : socket context pointer |
|
189 |
* $data : socket send data |
|
190 |
* $bytes: socket send data bytes |
|
191 |
* return value: <0: failure 0:ok |
|
192 |
*/ |
|
193 |
int socket_send(socket_ctx_t *sock, char *data, int bytes) |
|
194 |
{ |
|
195 |
int rv = 0; |
|
196 |
int i = 0; |
|
197 |
int left_bytes = bytes; |
|
198 |
|
|
199 |
if( !sock || !data || bytes<= 0 ) |
|
200 |
return -1; |
|
201 |
|
|
202 |
while( left_bytes > 0 ) |
|
203 |
{ |
|
204 |
rv=write(sock->fd, &data[i], left_bytes); |
|
205 |
if( rv < 0 ) |
|
206 |
{ |
|
207 |
log_info("socket[%d] write() failure: %s, close socket now\n", sock->fd, strerror(errno)); |
|
208 |
socket_term(sock); |
|
209 |
return -2; |
|
210 |
} |
|
211 |
else if( rv == left_bytes ) |
|
212 |
{ |
|
213 |
log_info("socket send %d bytes data over\n", bytes); |
|
214 |
return 0; |
|
215 |
} |
|
216 |
else |
|
217 |
{ |
|
218 |
/* not send over this time, continue to send left data */ |
|
219 |
i += rv; |
|
220 |
left_bytes -= rv; |
|
221 |
continue; |
|
222 |
} |
|
223 |
} |
|
224 |
|
|
225 |
return 0; |
|
226 |
} |
|
227 |
|
|
228 |
/* description: receive data from the socket |
|
229 |
* input args: |
|
230 |
* $sock : socket context pointer |
|
231 |
* $buf : socket receive data buffer |
|
232 |
* $size : socket receive data buffer size |
|
233 |
* $timeout: receive data time, <=0 will don't timeout |
|
234 |
* return value: <0: failure 0:ok |
|
235 |
*/ |
|
236 |
int socket_recv(socket_ctx_t *sock, char *buf, int size, int timeout) |
|
237 |
{ |
|
238 |
int rv = 0; |
|
239 |
fd_set rdset; |
|
240 |
int maxfd; |
|
241 |
|
|
242 |
if( !sock || !buf || size<= 0 ) |
|
243 |
return -1; |
|
244 |
|
|
245 |
memset(buf, 0, size); |
|
246 |
|
|
247 |
maxfd = sock->fd; |
|
248 |
FD_ZERO(&rdset); |
|
249 |
FD_SET(sock->fd, &rdset); |
|
250 |
|
|
251 |
if( timeout <= 0 ) /* no timeout */ |
|
252 |
{ |
|
253 |
rv=select(maxfd+1, &rdset, NULL, NULL, NULL); |
|
254 |
} |
|
255 |
else |
|
256 |
{ |
|
257 |
struct timeval tv; |
|
258 |
tv.tv_sec = timeout; |
|
259 |
tv.tv_usec = 0; |
|
260 |
rv=select(maxfd+1, &rdset, NULL, NULL, &tv); |
|
261 |
} |
|
262 |
|
|
263 |
if( rv < 0 ) |
|
264 |
{ |
|
265 |
log_error("select() on socket[%d] got error: %s\n", sock->fd, strerror(errno)); |
|
266 |
return -2; |
|
267 |
} |
|
268 |
else if( rv == 0 ) |
|
269 |
{ |
|
270 |
log_error("select() on socket[%d] get timeout\n", sock->fd); |
|
271 |
return 0; |
|
272 |
} |
|
273 |
else |
|
274 |
{ |
|
275 |
rv = read(sock->fd, buf, size); |
|
276 |
if( rv <= 0 ) |
|
277 |
{ |
|
278 |
log_error("socket[%d] read() failure or got disconnected: %s, close socket now\n", sock->fd, strerror(errno)); |
|
279 |
socket_term(sock); |
|
280 |
return -2; |
|
281 |
} |
|
282 |
else |
|
283 |
{ |
|
284 |
log_debug("socket[%d] receive %d bytes data\n", sock->fd, rv); |
|
285 |
return rv; |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
|
|
290 |
|
|
291 |
|
|
292 |
/*+-------------------------------------------------------------------+ |
|
293 |
*| socket utils function | |
|
294 |
*+-------------------------------------------------------------------+*/ |
|
295 |
|
|
296 |
|
|
297 |
/* socket connected or not: <0: failure 0:ok */ |
|
298 |
int sock_check_connect(int sockfd) |
|
299 |
{ |
|
300 |
struct tcp_info info; |
|
301 |
int len=sizeof(info); |
|
302 |
|
|
303 |
if( sockfd < 0 ) |
|
304 |
return -1; |
|
305 |
|
|
306 |
getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *)&len); |
|
307 |
|
|
308 |
if( TCP_CLOSE==info.tcpi_state || TCP_CLOSING==info.tcpi_state || TCP_CLOSE_WAIT==info.tcpi_state ) |
|
309 |
{ |
|
310 |
return -3; |
|
311 |
} |
|
312 |
|
|
313 |
return -0; |
|
314 |
} |
|
315 |
|
|
316 |
/* description: set socket listen port as reusable, fix port already used bug */ |
|
317 |
int socket_set_reuseaddr(int sockfd) |
|
318 |
{ |
|
319 |
int opt = 1; |
|
320 |
int len = sizeof (int); |
|
321 |
|
|
322 |
if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, len)) |
|
323 |
{ |
|
324 |
log_error("Set socket[%d] option SO_REUSEADDR failed:%s\n", sockfd, strerror(errno)); |
|
325 |
return -1; |
|
326 |
} |
|
327 |
log_debug("Set socket[%d] option SO_REUSEADDR ok\n", sockfd); |
|
328 |
|
|
329 |
return 0; |
|
330 |
} |
|
331 |
|
|
332 |
/* set socket as non-block mode, common socket default work as block mode */ |
|
333 |
int socket_set_nonblock(int sockfd) |
|
334 |
{ |
|
335 |
int opts; |
|
336 |
/* |
|
337 |
* fcntl may set: |
|
338 |
* |
|
339 |
* EACCES, EAGAIN: Operation is prohibited by locks held by other |
|
340 |
* processes. Or, operation is prohibited because the file has |
|
341 |
* been memory-mapped by another process. |
|
342 |
* EBADF: fd is not an open file descriptor, or the command was F_SETLK |
|
343 |
* or F_SETLKW and the file descriptor open mode doesn't match |
|
344 |
* with the type of lock requested. |
|
345 |
* EDEADLK: It was detected that the specified F_SETLKW command would |
|
346 |
* cause a deadlock. |
|
347 |
* EFAULT: lock is outside your accessible address space. |
|
348 |
* EINTR: For F_SETLKW, the command was interrupted by a signal. For |
|
349 |
* F_GETLK and F_SETLK, the command was interrupted by a signal |
|
350 |
* before the lock was checked or acquired. Most likely when |
|
351 |
* locking a remote file (e.g. locking over NFS), but can |
|
352 |
* sometimes happen locally. |
|
353 |
* EINVAL: For F_DUPFD, arg is negative or is greater than the maximum |
|
354 |
* allowable value. For F_SETSIG, arg is not an allowable signal |
|
355 |
* number. |
|
356 |
* EMFILE: For F_DUPFD, the process already has the maximum number of |
|
357 |
* file descriptors open. |
|
358 |
* ENOLCK: Too many segment locks open, lock table is full, or a remote |
|
359 |
* locking protocol failed (e.g. locking over NFS). |
|
360 |
* EPERM: Attempted to clear the O_APPEND flag on a file that has the |
|
361 |
* append-only attribute set. |
|
362 |
*/ |
|
363 |
opts = fcntl(sockfd, F_GETFL); |
|
364 |
if (opts < 0) |
|
365 |
{ |
|
366 |
log_warn("fcntl() get socket options failure: %s\n", strerror(errno)); |
|
367 |
return -1; |
|
368 |
} |
|
369 |
|
|
370 |
opts |= O_NONBLOCK; |
|
371 |
|
|
372 |
if (fcntl(sockfd, F_SETFL, opts) < 0) |
|
373 |
{ |
|
374 |
log_warn("fcntl() set socket options failure: %s\n", strerror(errno)); |
|
375 |
return -1; |
|
376 |
} |
|
377 |
|
|
378 |
log_debug("Set socket[%d] none blocking\n", sockfd); |
|
379 |
return opts; |
|
380 |
} |
|
381 |
|
|
382 |
|
|
383 |
/* set socket receive and send buffer size in linux kernel space */ |
|
384 |
int socket_set_buffer(int sockfd, int rsize, int ssize) |
|
385 |
{ |
|
386 |
int opt; |
|
387 |
socklen_t optlen = sizeof(opt); |
|
388 |
|
|
389 |
if(sockfd < 0) |
|
390 |
return -1; |
|
391 |
|
|
392 |
/* Get system default receive buffer size, Linux X86: 85K */ |
|
393 |
if (getsockopt (sockfd, SOL_SOCKET, SO_RCVBUF, (char *) &opt, &optlen)) |
|
394 |
{ |
|
395 |
log_warn("getsockopt() get receive buffer failure: %s\n", strerror(errno)); |
|
396 |
return -2; |
|
397 |
} |
|
398 |
|
|
399 |
/* Only when current receive buffer size larger than the default one will change it */ |
|
400 |
if(rsize > opt) |
|
401 |
{ |
|
402 |
opt = (int) rsize; |
|
403 |
if (setsockopt (sockfd, SOL_SOCKET, SO_RCVBUF, (char *) &opt, optlen)) |
|
404 |
{ |
|
405 |
log_warn("setsockopt() set receive buffer to %d failure: %s\n", opt, strerror(errno)); |
|
406 |
return -2; |
|
407 |
} |
|
408 |
} |
|
409 |
|
|
410 |
/* Get system default send buffer size, Linux X86: 16K */ |
|
411 |
if (getsockopt (sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &opt, &optlen)) |
|
412 |
{ |
|
413 |
log_warn("getsockopt() get send buffer failure: %s\n", strerror(errno)); |
|
414 |
return -3; |
|
415 |
} |
|
416 |
|
|
417 |
/* Only when current receive buffer size larger than the default one will change it */ |
|
418 |
if(ssize > opt) |
|
419 |
{ |
|
420 |
opt = (int) ssize; |
|
421 |
if (setsockopt (sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &opt, optlen)) |
|
422 |
{ |
|
423 |
log_warn("setsockopt() set send buffer to %d failure: %s\n", opt, strerror(errno)); |
|
424 |
return -3; |
|
425 |
} |
|
426 |
} |
|
427 |
|
|
428 |
log_info("Set socket[%d] RCVBUF size:%d SNDBUF size:%d\n", sockfd, rsize, ssize); |
|
429 |
return 0; |
|
430 |
} |
|
431 |
|
|
432 |
/* |
|
433 |
* Enable socket SO_KEEPALIVE, if the connection disconnected, any system call on socket |
|
434 |
* will return immediately and errno will be set to "WSAENOTCONN" |
|
435 |
* |
|
436 |
* keepalive is not program related, but socket related, * so if you have multiple sockets, |
|
437 |
* you can handle keepalive for each of them separately. |
|
438 |
* |
|
439 |
* Reference: http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/ |
|
440 |
*/ |
|
441 |
int socket_set_keepalive(int sockfd, int keepintvl, int keepcnt) |
|
442 |
{ |
|
443 |
int opt; |
|
444 |
|
|
445 |
if(sockfd < 0) |
|
446 |
return -1; |
|
447 |
|
|
448 |
/* Enable the KEEPALIVE flag */ |
|
449 |
opt = 1; |
|
450 |
if (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof (opt))) |
|
451 |
{ |
|
452 |
log_warn("setsockopt() enable SO_KEEPALIVE failure: %s\n", strerror(errno)); |
|
453 |
return -2; |
|
454 |
} |
|
455 |
|
|
456 |
if(keepintvl || keepcnt) |
|
457 |
{ |
|
458 |
/* |
|
459 |
* The tcp_keepidle parameter specifies the interval between the last data packet sent |
|
460 |
* (simple ACKs are not considered data) and the first keepalive probe; after the |
|
461 |
* connection is marked to need keepalive, this counter is not used any further. |
|
462 |
* ~ >: cat /proc/sys/net/ipv4/tcp_keepalive_time |
|
463 |
* 7200 |
|
464 |
*/ |
|
465 |
opt = 3; /* 3 seconds */ |
|
466 |
if (setsockopt (sockfd, SOL_TCP, TCP_KEEPIDLE, (char *) &opt, sizeof (opt))) |
|
467 |
{ |
|
468 |
log_error("setsockopt() set TCP_KEEPIDLE to %d seconds failure: %s\n", opt, strerror(errno)); |
|
469 |
return -3; |
|
470 |
} |
|
471 |
|
|
472 |
if((opt=keepintvl) > 0) |
|
473 |
{ |
|
474 |
/* |
|
475 |
* The tcp_keepintvl parameter specifies the interval between subsequential keepalive |
|
476 |
* probes, regardless of what the connection has exchanged in the meantime. |
|
477 |
* ~ >: cat /proc/sys/net/ipv4/tcp_keepalive_intvl |
|
478 |
* 75 |
|
479 |
*/ |
|
480 |
if (setsockopt (sockfd, SOL_TCP, TCP_KEEPINTVL, (char *) &opt, sizeof (opt))) |
|
481 |
{ |
|
482 |
log_error("setsockopt() set TCP_KEEPINTVL to %d failure: %s\n", opt, strerror(errno)); |
|
483 |
return -4; |
|
484 |
} |
|
485 |
} |
|
486 |
|
|
487 |
if((opt=keepcnt) > 0) |
|
488 |
{ |
|
489 |
/* |
|
490 |
* The TCP_KEEPCNT option specifies the maximum number of unacknowledged probes to |
|
491 |
* send before considering the connection dead and notifying the application layer |
|
492 |
* probes to be sent. The value of TCP_KEEPCNT is an integer value between 1 and n, |
|
493 |
* where n is the value of the systemwide tcp_keepcnt parameter. |
|
494 |
* ~ >: cat /proc/sys/net/ipv4/tcp_keepalive_probes |
|
495 |
* 9 |
|
496 |
*/ |
|
497 |
if (setsockopt (sockfd, SOL_TCP, TCP_KEEPCNT, (char *) &opt, sizeof (opt))) |
|
498 |
{ |
|
499 |
log_error("setsockopt() set TCP_KEEPCNT to %d failure: %s\n", opt, strerror(errno)); |
|
500 |
return -5; |
|
501 |
} |
|
502 |
} |
|
503 |
} |
|
504 |
|
|
505 |
log_debug("Set socket[%d] KEEPINTVL:%d KEEPCNT:%d\n", sockfd, keepintvl, keepcnt); |
|
506 |
return 0; |
|
507 |
} |
|
508 |
|
|
509 |
|
|
510 |
/* Set open file description count to max */ |
|
511 |
void set_socket_rlimit(void) |
|
512 |
{ |
|
513 |
struct rlimit limit = {0}; |
|
514 |
|
|
515 |
getrlimit(RLIMIT_NOFILE, &limit ); |
|
516 |
limit.rlim_cur = limit.rlim_max; |
|
517 |
setrlimit(RLIMIT_NOFILE, &limit ); |
|
518 |
|
|
519 |
log_info("set socket open fd max count to %d\n", limit.rlim_max); |
|
520 |
} |
|
521 |
|