forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadable_conn.c
56 lines (50 loc) · 1.28 KB
/
readable_conn.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
/* include readable_conn1 */
#include "icmpd.h"
int
readable_conn(int i)
{
int unixfd, recvfd;
char c;
ssize_t n;
socklen_t len;
struct sockaddr_storage ss;
unixfd = client[i].connfd;
recvfd = -1;
if ( (n = Read_fd(unixfd, &c, 1, &recvfd)) == 0) {
err_msg("client %d terminated, recvfd = %d", i, recvfd);
goto clientdone; /* client probably terminated */
}
/* 4data from client; should be descriptor */
if (recvfd < 0) {
err_msg("read_fd did not return descriptor");
goto clienterr;
}
/* end readable_conn1 */
/* include readable_conn2 */
len = sizeof(ss);
if (getsockname(recvfd, (SA *) &ss, &len) < 0) {
err_ret("getsockname error");
goto clienterr;
}
client[i].family = ss.ss_family;
if ( (client[i].lport = sock_get_port((SA *)&ss, len)) == 0) {
client[i].lport = sock_bind_wild(recvfd, client[i].family);
if (client[i].lport <= 0) {
err_ret("error binding ephemeral port");
goto clienterr;
}
}
Write(unixfd, "1", 1); /* tell client all OK */
Close(recvfd); /* all done with client's UDP socket */
return(--nready);
clienterr:
Write(unixfd, "0", 1); /* tell client error occurred */
clientdone:
Close(unixfd);
if (recvfd >= 0)
Close(recvfd);
FD_CLR(unixfd, &allset);
client[i].connfd = -1;
return(--nready);
}
/* end readable_conn2 */