forked from viabtc/viabtc_exchange_server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nw_sock.c
283 lines (253 loc) · 6.79 KB
/
nw_sock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Description:
* History: [email protected], 2016/03/16, create
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <errno.h>
# include <fcntl.h>
# include <netdb.h>
# include <unistd.h>
# include "nw_sock.h"
char *nw_sock_human_addr(nw_addr_t *addr)
{
static char str[128];
char ip[INET6_ADDRSTRLEN];
switch (addr->family) {
case AF_INET:
inet_ntop(addr->family, &addr->in.sin_addr, ip, sizeof(ip));
snprintf(str, sizeof(str), "%s:%u", ip, ntohs(addr->in.sin_port));
break;
case AF_INET6:
inet_ntop(addr->family, &addr->in6.sin6_addr, ip, sizeof(ip));
snprintf(str, sizeof(str), "%s:%u", ip, ntohs(addr->in6.sin6_port));
break;
case AF_UNIX:
snprintf(str, sizeof(str), "%s:%s", "unix", (addr->un.sun_path));
break;
default:
str[0] = 0;
break;
}
return str;
}
char *nw_sock_human_addr_s(nw_addr_t *addr, char *dest)
{
char ip[INET6_ADDRSTRLEN];
switch (addr->family) {
case AF_INET:
inet_ntop(addr->family, &addr->in.sin_addr, ip, sizeof(ip));
snprintf(dest, NW_HUMAN_ADDR_SIZE, "%s:%u", ip, ntohs(addr->in.sin_port));
break;
case AF_INET6:
inet_ntop(addr->family, &addr->in6.sin6_addr, ip, sizeof(ip));
snprintf(dest, NW_HUMAN_ADDR_SIZE, "%s:%u", ip, ntohs(addr->in6.sin6_port));
break;
case AF_UNIX:
snprintf(dest, NW_HUMAN_ADDR_SIZE, "%s:%s", "unix", (addr->un.sun_path));
break;
default:
dest[0] = 0;
break;
}
return dest;
}
char *nw_sock_ip(nw_addr_t *addr)
{
static char ip[INET6_ADDRSTRLEN];
switch (addr->family) {
case AF_INET:
inet_ntop(addr->family, &addr->in.sin_addr, ip, sizeof(ip));
break;
case AF_INET6:
inet_ntop(addr->family, &addr->in6.sin6_addr, ip, sizeof(ip));
break;
default:
ip[0] = 0;
break;
}
return ip;
}
char *nw_sock_ip_s(nw_addr_t *addr, char *ip)
{
switch (addr->family) {
case AF_INET:
inet_ntop(addr->family, &addr->in.sin_addr, ip, NW_SOCK_IP_SIZE);
break;
case AF_INET6:
inet_ntop(addr->family, &addr->in6.sin6_addr, ip, NW_SOCK_IP_SIZE);
break;
default:
ip[0] = 0;
break;
}
return ip;
}
int nw_sock_set_mode(nw_addr_t *addr, mode_t mode)
{
if (addr->family != AF_UNIX)
return 0;
return chmod(addr->un.sun_path, mode);
}
int nw_sock_peer_addr(int sockfd, nw_addr_t *addr)
{
addr->addrlen = sizeof(addr->un);
if (getpeername(sockfd, NW_SOCKADDR(addr), &addr->addrlen) == 0)
{
addr->family = NW_SOCKADDR(addr)->sa_family;
return 0;
}
return -1;
}
int nw_sock_host_addr(int sockfd, nw_addr_t *addr)
{
addr->addrlen = sizeof(addr->un);
if (getsockname(sockfd, NW_SOCKADDR(addr), &addr->addrlen) == 0)
{
addr->family = NW_SOCKADDR(addr)->sa_family;
return 0;
}
return -1;
}
static int nw_sock_addr_fill_inet(nw_addr_t *addr, const char *host, const char *port)
{
memset(addr, 0, sizeof(nw_addr_t));
if (strchr(host, '.') != NULL) {
addr->in.sin_family = AF_INET;
if (inet_pton(addr->in.sin_family, host, &addr->in.sin_addr) <= 0) {
return -1;
}
addr->in.sin_port = htons(strtoul(port, NULL, 0));
addr->family = addr->in.sin_family;
addr->addrlen = sizeof(addr->in);
} else {
addr->in6.sin6_family = AF_INET6;
if (inet_pton(addr->in6.sin6_family, host, &addr->in6.sin6_addr) <= 0) {
return -1;
}
addr->in6.sin6_port = htons(strtoul(port, NULL, 0));
addr->family = addr->in6.sin6_family;
addr->addrlen = sizeof(addr->in6);
}
return 0;
}
int nw_sock_addr_fill_unix(nw_addr_t *addr, const char* unix_path)
{
size_t pathlen = strlen(unix_path);
if (pathlen >= sizeof(addr->un.sun_path)) {
return -1;
}
addr->un.sun_family = AF_UNIX;
strcpy(addr->un.sun_path, unix_path);
addr->family = addr->un.sun_family;
addr->addrlen = sizeof(addr->un);
return 0;
}
int nw_sock_cfg_parse(const char *cfg, nw_addr_t *addr, int *sock_type)
{
char *s = strdup(cfg);
char *sep = strchr(s, '@');
if (sep == NULL) {
free(s);
return -1;
}
*sep = '\0';
char *type = s;
char *name = sep + 1;
int is_inet = 0;
if (strcasecmp(type, "tcp") == 0) {
*sock_type = SOCK_STREAM;
is_inet = 1;
} else if (strcasecmp(type, "udp") == 0) {
*sock_type = SOCK_DGRAM;
is_inet = 1;
} else if (strcasecmp(type, "stream") == 0) {
*sock_type = SOCK_STREAM;
} else if (strcasecmp(type, "dgram") == 0) {
*sock_type = SOCK_DGRAM;
} else if (strcasecmp(type, "seqpacket") == 0) {
*sock_type = SOCK_SEQPACKET;
} else {
free(s);
return -2;
}
if (is_inet) {
sep = strchr(name, ':');
if (sep == NULL) {
free(s);
return -3;
}
*sep = '\0';
char *host = name;
char *port = sep + 1;
if (nw_sock_addr_fill_inet(addr, host, port) < 0) {
free(s);
return -4;
}
} else {
if (nw_sock_addr_fill_unix(addr, name) < 0) {
free(s);
return -5;
}
}
free(s);
return 0;
}
int nw_sock_errno(int sockfd)
{
int _errno = 0;
socklen_t len = sizeof(int);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &_errno, &len) == 0)
return _errno;
if (errno != 0)
return errno;
return EBADFD;
}
int nw_sock_get_send_buf(int sockfd, int *buf_size)
{
socklen_t len = sizeof(*buf_size);
if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, buf_size, &len) != 0)
return -1;
return 0;
}
int nw_sock_get_recv_buf(int sockfd, int *buf_size)
{
socklen_t len = sizeof(*buf_size);
if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, buf_size, &len) != 0)
return -1;
return 0;
}
int nw_sock_set_send_buf(int sockfd, int buf_size)
{
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &buf_size, sizeof(buf_size)) != 0)
return -1;
return 0;
}
int nw_sock_set_recv_buf(int sockfd, int buf_size)
{
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof(buf_size)) != 0)
return -1;
return 0;
}
int nw_sock_set_nonblock(int sockfd)
{
int flags = fcntl(sockfd, F_GETFL, 0);
if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) != 0)
return -1;
return 0;
}
int nw_sock_set_no_delay(int sockfd)
{
int val = 1;
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) != 0)
return -1;
return 0;
}
int nw_sock_set_reuse_addr(int sockfd)
{
int val = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) != 0)
return -1;
return 0;
}