This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconnection.c
287 lines (232 loc) · 6.66 KB
/
connection.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
283
284
285
286
287
/*
* Copyright (c) 2017 Dariusz Stojaczyk. All Rights Reserved.
* The following source code is released under an MIT-style license,
* that can be found in the LICENSE file.
*/
#include <stdlib.h>
#include <stdatomic.h>
#include <stdio.h>
#include <zconf.h>
#include <stdbool.h>
#include <memory.h>
#include <errno.h>
#include <arpa/inet.h>
#include <poll.h>
#include "connection.h"
#include "log.h"
struct brother_conn {
enum brother_connection_type type;
int fd;
bool connected;
bool is_stream;
struct sockaddr_in sin_me;
struct sockaddr_in sin_oth;
struct timeval timeout;
};
static int
create_socket(struct brother_conn *conn, unsigned timeout_sec)
{
int one = 1;
if (conn->type == BROTHER_CONNECTION_TYPE_UDP) {
conn->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
} else {
conn->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
conn->is_stream = true;
}
if (conn->fd == -1) {
perror("socket");
return -1;
}
conn->timeout.tv_sec = timeout_sec;
conn->timeout.tv_usec = 0;
if (setsockopt(conn->fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&conn->timeout,
sizeof(conn->timeout)) != 0) {
perror("setsockopt recv");
}
if (setsockopt(conn->fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&conn->timeout,
sizeof(conn->timeout)) != 0) {
perror("setsockopt send");
}
if (setsockopt(conn->fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int)) != 0) {
perror("setsockopt");
}
return 0;
}
struct brother_conn *
brother_conn_open(enum brother_connection_type type, unsigned timeout_sec)
{
struct brother_conn *conn;
conn = calloc(1, sizeof(*conn));
if (conn == NULL) {
return NULL;
}
conn->type = type;
if (create_socket(conn, timeout_sec) != 0) {
return NULL;
}
return conn;
}
int
brother_conn_bind(struct brother_conn *conn, in_port_t local_port)
{
conn->sin_me.sin_family = AF_INET;
conn->sin_me.sin_addr.s_addr = htonl(INADDR_ANY);
conn->sin_me.sin_port = local_port;
if (bind(conn->fd, (struct sockaddr *)&conn->sin_me,
sizeof(conn->sin_me)) != 0) {
perror("bind");
return -1;
}
return 0;
}
int
brother_conn_reconnect(struct brother_conn *conn, in_addr_t dest_addr,
in_port_t dest_port)
{
int retries;
if (conn->connected) {
close(conn->fd);
conn->connected = false;
if (create_socket(conn, (unsigned)conn->timeout.tv_sec) != 0) {
return -1;
}
if (conn->sin_me.sin_port &&
brother_conn_bind(conn, conn->sin_me.sin_port) != 0) {
return -1;
}
}
conn->is_stream = true;
conn->sin_oth.sin_addr.s_addr = dest_addr;
conn->sin_oth.sin_family = AF_INET;
conn->sin_oth.sin_port = dest_port;
for (retries = 0; retries < 3; ++retries) {
usleep(1000 * 25);
if (connect(conn->fd, (struct sockaddr *)&conn->sin_oth,
sizeof(conn->sin_oth)) == 0) {
break;
}
}
if (retries == 3) {
perror("connect");
return -1;
}
conn->connected = true;
return 0;
}
int
brother_conn_sendto(struct brother_conn *conn, const void *buf, size_t len,
in_addr_t dest_addr, in_port_t dest_port)
{
struct sockaddr_in sin_oth;
ssize_t sent_bytes;
if (conn->type == BROTHER_CONNECTION_TYPE_TCP) {
LOG_ERR("sendto can't be used with TCP sockets\n");
return -1;
}
sin_oth.sin_addr.s_addr = dest_addr;
sin_oth.sin_family = AF_INET;
sin_oth.sin_port = dest_port;
do {
sent_bytes = sendto(conn->fd, buf, len, 0,
(struct sockaddr *) &sin_oth,
sizeof(sin_oth));
} while (errno == EINTR);
if (sent_bytes < 0) {
perror("sendto");
}
LOG_DEBUG("sent %zd/%zu bytes to %d", sent_bytes, len,
ntohs(sin_oth.sin_port));
DUMP_DEBUG(buf, len);
return (int) sent_bytes;
}
int
brother_conn_poll(struct brother_conn *conn, unsigned timeout_sec)
{
struct pollfd pfd = {};
int rc;
pfd.fd = conn->fd;
pfd.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
do {
rc = poll(&pfd, 1, timeout_sec * 1000);
} while (rc == -EINTR);
if (rc < 0) {
return rc;
}
return pfd.revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL);
}
int
brother_conn_send(struct brother_conn *conn, const void *buf, size_t len)
{
ssize_t sent_bytes;
do {
if (conn->type == BROTHER_CONNECTION_TYPE_UDP) {
sent_bytes = sendto(conn->fd, buf, len, 0,
(struct sockaddr *) &conn->sin_oth,
sizeof(conn->sin_oth));
} else {
sent_bytes = send(conn->fd, buf, len, 0);
}
} while (errno == EINTR);
if (sent_bytes < 0) {
perror("sendto");
}
LOG_DEBUG("sent %zd/%zu bytes to %d", sent_bytes, len,
ntohs(conn->sin_oth.sin_port));
DUMP_DEBUG(buf, len);
return (int) sent_bytes;
}
int
brother_conn_receive(struct brother_conn *conn, void *buf, size_t len)
{
ssize_t recv_bytes;
struct sockaddr_in sin_oth_tmp;
socklen_t slen;
slen = sizeof(sin_oth_tmp);
do {
if (conn->type == BROTHER_CONNECTION_TYPE_UDP) {
recv_bytes = recvfrom(conn->fd, buf, len, 0,
(struct sockaddr *) &sin_oth_tmp, &slen);
} else {
recv_bytes = recv(conn->fd, buf, len, 0);
}
} while (errno == EINTR);
if (recv_bytes < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
perror("recvfrom");
}
return -1;
}
if (conn->type == BROTHER_CONNECTION_TYPE_UDP && !conn->is_stream) {
memcpy(&conn->sin_oth, &sin_oth_tmp, sizeof(conn->sin_oth));
}
LOG_DEBUG("received %zd bytes from %d", recv_bytes,
ntohs(conn->sin_oth.sin_port));
DUMP_DEBUG(buf, recv_bytes);
return (int) recv_bytes;
}
int
brother_conn_get_client_ip(struct brother_conn *conn, char ip[16])
{
const char *ret;
ret = inet_ntop(AF_INET, &conn->sin_oth.sin_addr, ip, 16);
return ret != NULL ? 0 : -1;
}
int
brother_conn_get_local_ip(struct brother_conn *conn, char ip[16])
{
const char *ret;
struct sockaddr_in name;
socklen_t namelen = sizeof(name);
if (getsockname(conn->fd, (struct sockaddr *) &name, &namelen) != 0) {
perror("getsockname");
return -1;
}
ret = inet_ntop(AF_INET, &name.sin_addr, ip, 16);
return ret != NULL ? 0 : -1;
}
void
brother_conn_close(struct brother_conn *conn)
{
close(conn->fd);
free(conn);
}