-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathWuNetwork.cpp
98 lines (75 loc) · 1.85 KB
/
WuNetwork.cpp
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
#include "WuNetwork.h"
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
void HexDump(const uint8_t* src, size_t len) {
for (size_t i = 0; i < len; i++) {
if (i % 8 == 0) printf("%04x ", uint32_t(i));
printf("%02x ", src[i]);
if ((i + 1) % 8 == 0) printf("\n");
}
printf("\n");
}
ssize_t SocketWrite(int fd, const uint8_t* buf, size_t len) {
const ssize_t towrite = (ssize_t)len;
ssize_t written = 0;
while (written != towrite) {
ssize_t r = write(fd, buf + written, towrite - written);
if (r == 0) return written;
if (r == -1) {
return -1;
}
written += r;
}
return written;
}
ssize_t SocketWrite(int fd, const char* buf, size_t len) {
return SocketWrite(fd, (const uint8_t*)buf, len);
}
int MakeNonBlocking(int sfd) {
int flags = fcntl(sfd, F_GETFL, 0);
if (flags == -1) {
return -1;
}
flags |= O_NONBLOCK;
int s = fcntl(sfd, F_SETFL, flags);
if (s == -1) {
return -1;
}
return 0;
}
int CreateSocket(const char* port, SocketType type) {
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = type == ST_TCP ? SOCK_STREAM : SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
struct addrinfo* result = NULL;
int s = getaddrinfo(NULL, port, &hints, &result);
if (s != 0) {
return -1;
}
int sfd = -1;
struct addrinfo* rp = result;
for (; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1) {
continue;
}
int enable = 1;
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
s = bind(sfd, rp->ai_addr, rp->ai_addrlen);
if (s == 0) {
break;
}
close(sfd);
}
freeaddrinfo(result);
if (rp == NULL) {
return -1;
}
return sfd;
}