-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.c
More file actions
212 lines (188 loc) · 4.59 KB
/
net.c
File metadata and controls
212 lines (188 loc) · 4.59 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
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
#include "taskimpl.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/poll.h>
int
netannounce(int istcp, char *server, int port)
{
int fd, n, proto;
struct sockaddr_in sa;
socklen_t sn;
uint32_t ip;
taskstate("netannounce");
proto = istcp ? SOCK_STREAM : SOCK_DGRAM;
memset(&sa, 0, sizeof sa);
sa.sin_family = AF_INET;
// 如果是指定了主机地址
if(server != nil && strcmp(server, "*") != 0){
// 查找
if(netlookup(server, &ip) < 0){
taskstate("netlookup failed");
return -1;
}
// 使用ip地址
memmove(&sa.sin_addr, &ip, 4);
}
sa.sin_port = htons(port);
if((fd = socket(AF_INET, proto, 0)) < 0){
taskstate("socket failed");
return -1;
}
/* set reuse flag for tcp */
if(istcp && getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0){
n = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n);
}
// 绑定接口
if(bind(fd, (struct sockaddr*)&sa, sizeof sa) < 0){
taskstate("bind failed");
close(fd);
return -1;
}
// listen
if(proto == SOCK_STREAM)
listen(fd, 16);
fdnoblock(fd);
taskstate("netannounce succeeded");
return fd;
}
int
netaccept(int fd, char *server, int *port)
{
int cfd, one;
struct sockaddr_in sa;
uchar *ip;
socklen_t len;
// 等待该连接可读
fdwait(fd, 'r');
taskstate("netaccept");
len = sizeof sa;
if((cfd = accept(fd, (void*)&sa, &len)) < 0){
taskstate("accept failed");
return -1;
}
if(server){
ip = (uchar*)&sa.sin_addr;
snprint(server, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}
if(port)
*port = ntohs(sa.sin_port);
// 设置为non-block
fdnoblock(cfd);
one = 1;
// 设置no-delay算法
setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one);
taskstate("netaccept succeeded");
return cfd;
}
#define CLASS(p) ((*(unsigned char*)(p))>>6)
static int
parseip(char *name, uint32_t *ip)
{
unsigned char addr[4];
char *p;
int i, x;
p = name;
for(i=0; i<4 && *p; i++){
x = strtoul(p, &p, 0);
if(x < 0 || x >= 256)
return -1;
if(*p != '.' && *p != 0)
return -1;
if(*p == '.')
p++;
addr[i] = x;
}
switch(CLASS(addr)){
case 0:
case 1:
if(i == 3){
addr[3] = addr[2];
addr[2] = addr[1];
addr[1] = 0;
}else if(i == 2){
addr[3] = addr[1];
addr[2] = 0;
addr[1] = 0;
}else if(i != 4)
return -1;
break;
case 2:
if(i == 3){
addr[3] = addr[2];
addr[2] = 0;
}else if(i != 4)
return -1;
break;
}
*ip = *(uint32_t*)addr;
return 0;
}
int
netlookup(char *name, uint32_t *ip)
{
struct hostent *he;
// 解析IP地址
if(parseip(name, ip) >= 0)
return 0;
/* BUG - Name resolution blocks. Need a non-blocking DNS. */
taskstate("netlookup");
if((he = gethostbyname(name)) != 0){
*ip = *(uint32_t*)he->h_addr;
taskstate("netlookup succeeded");
return 0;
}
taskstate("netlookup failed");
return -1;
}
int
netdial(int istcp, char *server, int port)
{
int proto, fd, n;
uint32_t ip;
struct sockaddr_in sa;
socklen_t sn;
if(netlookup(server, &ip) < 0)
return -1;
taskstate("netdial");
proto = istcp ? SOCK_STREAM : SOCK_DGRAM;
if((fd = socket(AF_INET, proto, 0)) < 0){
taskstate("socket failed");
return -1;
}
fdnoblock(fd);
/* for udp */
if(!istcp){
n = 1;
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof n);
}
/* start connecting */
memset(&sa, 0, sizeof sa);
memmove(&sa.sin_addr, &ip, 4);
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
if(connect(fd, (struct sockaddr*)&sa, sizeof sa) < 0 && errno != EINPROGRESS){
taskstate("connect failed");
close(fd);
return -1;
}
/* wait for finish */
fdwait(fd, 'w');
sn = sizeof sa;
if(getpeername(fd, (struct sockaddr*)&sa, &sn) >= 0){
taskstate("connect succeeded");
return fd;
}
/* report error */
sn = sizeof n;
getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&n, &sn);
if(n == 0)
n = ECONNREFUSED;
close(fd);
taskstate("connect failed");
errno = n;
return -1;
}