-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnet.c
286 lines (250 loc) · 7.37 KB
/
mnet.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
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/un.h>
#include "mnet.h"
#include "mdebug.h"
#include "mlog.h"
int maddrlookup(const char *host, const char *service, mSockType socktype) {
int rv;
struct addrinfo *infop = NULL;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // AF_UNSPEC for v4 or v6
hints.ai_flags = AI_PASSIVE;
if (socktype == TCP) {
hints.ai_socktype = SOCK_STREAM;
} else if (socktype == UDP) {
hints.ai_socktype = SOCK_DGRAM;
}
mdebugf("maddrlookup: host %s, service %s\n", host, service);
if ((rv = getaddrinfo(host, service, &hints, &infop)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
goto CLEANUP;
}
mdebugf("getaddrinfo rv was %d\n", rv);
CLEANUP:
mdebugf("CLEANUP: calling freeaddrinfo\n");
if (infop != NULL)
freeaddrinfo(infop);
return rv;
}
int
setup_udp_server(char *address, int port) {
struct sockaddr_in address_inet;
int inet_length;
int socketfd;
int rv;
int rcvbuf = 262144; // 2^18
socklen_t option_size;
int reuseaddr = 1;
socketfd = socket(AF_INET, SOCK_DGRAM, 0);
if (socketfd < 0)
return 0;
// Set up some socket options.
setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
if (setsockopt(socketfd,
SOL_SOCKET,
SO_RCVBUF,
&rcvbuf,
sizeof(rcvbuf)) < 0)
{
fprintf(stderr, "Failed to set SO_RCVBUF: %s", strerror(errno));
}
else
{
option_size = sizeof(int);
if (getsockopt(socketfd,
SOL_SOCKET,
SO_RCVBUF,
&rcvbuf,
&option_size) < 0)
{
fprintf(stderr, "getsockopt failed: %s", strerror(errno));
}
}
memset(&address_inet, 0, sizeof(address_inet));
address_inet.sin_family = AF_INET;
address_inet.sin_port = htons(port);
address_inet.sin_addr.s_addr = inet_addr(address);
if (address_inet.sin_addr.s_addr == INADDR_NONE)
return 0;
inet_length = sizeof(address_inet);
rv = bind(socketfd, (struct sockaddr *)&address_inet, inet_length);
if (rv < 0)
return 0;
return socketfd;
}
char *
read_dgram(int socketfd, char *dgram, size_t dgram_size) {
struct sockaddr_in address_client;
socklen_t inet_length;
int rv;
// NULL out the buffer.
memset(dgram, '\0', dgram_size);
// Check socket for data.
inet_length = sizeof(address_client);
rv = recvfrom(socketfd,
dgram,
dgram_size,
0,
(struct sockaddr *)&address_client,
&inet_length);
// FIXME - should we screen the size of the message here?
if (rv == 0) {
return NULL;
} else if (rv < 0) {
fprintf(stderr, "Error on socket: %s", strerror(errno));
return NULL;
}
return dgram;
}
int
setup_tcp_server(char *address, int port, int backlog) {
struct sockaddr_in address_inet;
int inet_length;
int socketfd;
int rv;
int rcvbuf = 262144; // 2^18
socklen_t option_size;
int reuseaddr = 1;
socketfd = socket(AF_INET, SOCK_STREAM, 0);
if (socketfd < 0) {
perror("socket");
return 0;
}
// Set up some socket options.
setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
if (setsockopt(socketfd,
SOL_SOCKET,
SO_RCVBUF,
&rcvbuf,
sizeof(rcvbuf)) < 0)
{
fprintf(stderr, "Failed to set SO_RCVBUF: %s", strerror(errno));
}
else
{
option_size = sizeof(int);
if (getsockopt(socketfd,
SOL_SOCKET,
SO_RCVBUF,
&rcvbuf,
&option_size) < 0)
{
fprintf(stderr, "getsockopt failed: %s", strerror(errno));
}
}
memset(&address_inet, 0, sizeof(address_inet));
address_inet.sin_family = AF_INET;
address_inet.sin_port = htons(port);
address_inet.sin_addr.s_addr = inet_addr(address);
if (address_inet.sin_addr.s_addr == INADDR_NONE)
return 0;
inet_length = sizeof(address_inet);
rv = bind(socketfd, (struct sockaddr *)&address_inet, inet_length);
if (rv < 0) {
perror("bind");
return 0;
}
if (listen(socketfd, backlog) != 0) {
perror("listen");
return 0;
}
return socketfd;
}
int
connect_tcp_client(const char *address, const char *port) {
mdebugf("connect_tcp_client - address %s, port %s", address, port);
struct addrinfo hints;
struct addrinfo *infop = NULL;
struct addrinfo *current = NULL;
int clientfd, rv;
struct sockaddr_in clientsock;
socklen_t clientsock_len = sizeof(struct sockaddr_in);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // AF_UNSPEC for v4 or v6
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = IPPROTO_TCP;
if ((rv = getaddrinfo(address, port, &hints, &infop)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
rv = -1;
goto CLEANUP;
}
for (current = infop; current != NULL; current = current->ai_next) {
struct sockaddr_in *sa = (struct sockaddr_in *)current->ai_addr;
mdebugf("\n%s port: %d protocol: %d\n",
inet_ntoa(sa->sin_addr),
ntohs(sa->sin_port),
current->ai_protocol);
clientfd = socket(current->ai_family, current->ai_socktype, current->ai_protocol);
if (clientfd == -1) {
fprintf(stderr, "socket creation failed: %s", strerror(errno));
continue;
}
if (connect(clientfd, current->ai_addr, current->ai_addrlen) < 0) {
perror("connect");
rv = -1;
goto CLEANUP;
}
if (getsockname(clientfd, (struct sockaddr*)&clientsock, &clientsock_len) < 0) {
}
else {
mdebugf("local port is %d", ntohs(clientsock.sin_port));
}
// If we're here we connected.
rv = clientfd;
break;
}
if (current == NULL) {
fprintf(stderr, "failed to connect to all resolved hosts");
rv = -1;
goto CLEANUP;
}
CLEANUP:
if (infop != NULL)
freeaddrinfo(infop);
return rv;
}
int
connect_nix_streaming_client(const char *sockpath) {
int fd;
struct sockaddr_un addr;
int ret;
char buff[8192];
struct sockaddr_un from;
int len;
int rv;
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
rv = -1;
goto CLEANUP;
}
/*
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, sockpath);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
rv = -1;
goto CLEANUP;
}
*/
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, sockpath);
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("connect");
rv = -1;
goto CLEANUP;
}
rv = fd;
CLEANUP:
return rv;
}