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
|
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>
#include "socket.h"
#include "utils.h"
int socket_nonblock(const psocket *psock)
{
return set_fd_nonblock(psock->fd);
}
int socket_init_in(const char *addr,
const char *port, struct addrinfo **results)
{
int s;
struct addrinfo hints;
assert(addr || port); /* getaddrinfo wants either node or service */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* IPV4 && IPV6 */
hints.ai_socktype = SOCK_STREAM; /* TCP */
hints.ai_flags = AI_PASSIVE; /* all interfaces */
s = getaddrinfo(addr, port, &hints, results);
if (s) {
freeaddrinfo(*results);
*results = NULL;
}
return s;
}
int socket_bind_in(psocket *psock, struct addrinfo **results)
{
int s = 1, fd = -1, rv, reuse_enable = 1;
struct addrinfo *rp = NULL;
assert(psock && results && *results);
psock->fd = -1;
for (rp = *results; rp != NULL; rp = rp->ai_next) {
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd < 0)
continue;
rv = bind(fd, rp->ai_addr, rp->ai_addrlen);
if (!rv)
break;
close(fd);
}
if (!rp)
goto finalise;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_enable, sizeof(int));
psock->fd = fd;
psock->addr_len = rp->ai_addrlen;
psock->addr = *rp->ai_addr;
psock->family = rp->ai_family;
psock->socktype = rp->ai_socktype;
psock->protocol = rp->ai_protocol;
s = socket_nonblock(psock);
finalise:
freeaddrinfo(*results);
*results = NULL;
return s;
}
int socket_listen_in(psocket *psock)
{
assert(psock);
return listen(psock->fd, POTD_BACKLOG);
}
int socket_accept_in(const psocket *psock, psocket *client_psock)
{
int fd;
assert(psock && client_psock);
client_psock->fd = -1;
client_psock->addr_len = psock->addr_len;
fd = accept(psock->fd, &client_psock->addr,
&client_psock->addr_len);
if (fd < 0)
return 1;
if (socket_nonblock(psock)) {
close(fd);
return 1;
}
client_psock->fd = fd;
return 0;
}
int socket_connect_in(psocket *psock, struct addrinfo **results)
{
int s = 1, fd = -1, rv, reuse_enable = 1;
struct addrinfo *rp = NULL;
assert(psock && results && *results);
psock->fd = -1;
for (rp = *results; rp != NULL; rp = rp->ai_next) {
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd < 0)
continue;
rv = connect(fd, rp->ai_addr, rp->ai_addrlen);
if (!rv)
break;
close(fd);
}
if (!rp)
goto finalise;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_enable, sizeof(int));
psock->fd = fd;
psock->addr_len = rp->ai_addrlen;
psock->addr = *(rp->ai_addr);
psock->family = rp->ai_family;
psock->socktype = rp->ai_socktype;
psock->protocol = rp->ai_protocol;
s = socket_nonblock(psock);
finalise:
freeaddrinfo(*results);
*results = NULL;
return s;
}
int socket_addrtostr_in(const psocket *psock,
char hbuf[NI_MAXHOST], char sbuf[NI_MAXSERV])
{
int s;
assert(psock);
s = getnameinfo(&psock->addr,
psock->addr_len,
&hbuf[0], NI_MAXHOST,
&sbuf[0], NI_MAXSERV,
NI_NUMERICHOST | NI_NUMERICSERV);
return s;
}
int socket_reconnect_in(psocket *psock)
{
int rv;
int reuse_enable = 1;
assert(psock);
if (psock->fd >= 0)
return -1;
psock->fd = socket(psock->family, psock->socktype, psock->protocol);
if (psock->fd < 0)
return -2;
rv = connect(psock->fd, &psock->addr, psock->addr_len);
if (rv) {
socket_close(psock);
return -3;
}
if (setsockopt(psock->fd, SOL_SOCKET, SO_REUSEADDR, &reuse_enable, sizeof(int)) < 0) {
socket_close(psock);
return -4;
}
return socket_nonblock(psock);
}
int socket_close(psocket *psock)
{
int rv;
assert(psock);
if (psock->fd < 0)
return 0;
rv = close(psock->fd);
psock->fd = -1;
return rv;
}
void socket_clone(const psocket *src, psocket *dst)
{
assert(src && dst);
memcpy(dst, src, sizeof(*dst));
dst->fd = -1;
}
|