-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.c
More file actions
81 lines (62 loc) · 1.34 KB
/
net.c
File metadata and controls
81 lines (62 loc) · 1.34 KB
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
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "debug.h"
#include "net.h"
#define AI_FLAGS_CLIENT AI_NUMERICSERV
int write_all(int fd, const void *buf, size_t count)
{
const char *s;
int n;
s = buf;
while ((n = write(fd, buf, count)) > 0) {
count -= n;
s += n;
}
return s - (const char *) buf;
}
int tcp_client(char *host, int port)
{
struct addrinfo *result, *rp;
struct addrinfo hints;
char service[16];
int retval;
int sock;
int tmp;
snprintf(service, sizeof(service), "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_FLAGS_CLIENT;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
retval = -1;
result = NULL;
tmp = getaddrinfo(host, service, &hints, &result);
if (tmp != 0) {
msg_warn("getaddrinfo: %s", gai_strerror(tmp));
goto out;
}
tmp = 1;
for (rp = result; rp != NULL; rp = rp->ai_next) {
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock < 0) {
msg_err("%s", "socket");
continue;
}
if (connect(sock, rp->ai_addr, rp->ai_addrlen) == 0)
break;
close(sock);
}
if (rp == NULL) {
msg_warn("%s", "unable to connect");
goto out;
}
retval = sock;
out:
if (result != NULL)
freeaddrinfo(result);
return retval;
}