forked from sirius226/Routing-Emulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhub.c
More file actions
executable file
·152 lines (127 loc) · 3.51 KB
/
hub.c
File metadata and controls
executable file
·152 lines (127 loc) · 3.51 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
/*--------------------------------------------------------------------*/
/* chat server */
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include "common.h"
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
char *mylan;
/* clean up before exit */
void cleanup()
{
char linkname[MAXSTRING];
/* unlink the link */
sprintf(linkname, ".%s.info", mylan);
unlink(linkname);
exit(0);
}
/* main routine */
main(int argc, char *argv[])
{
int servsock;
fd_set livesdset;
int livesdmax;
/* check usage */
if (argc != 2) {
fprintf(stderr, "usage : %s <mylan>\n", argv[0]);
exit(1);
}
mylan = strdup(argv[1]);
/* setup signal handlers to clean up */
signal(SIGTERM, cleanup);
signal(SIGINT, cleanup);
/* get ready to receive requests */
servsock = initlan(mylan);
if (servsock == -1) {
exit(1);
}
/* form a set of active descriptors */
FD_ZERO(&livesdset);
FD_SET(servsock, &livesdset);
livesdmax = servsock;
/* accept requests and process them */
while (1) {
int frsock, tosock;
fd_set readset;
/* wait for requests */
memcpy(&readset, &livesdset, sizeof(livesdset));
if (select(livesdmax+1, &readset, NULL, NULL, NULL) == -1) {
perror("select");
exit(1);
}
/* figure out the request and serve it */
for (frsock=3; frsock <= livesdmax; frsock++) {
/* skip the server socket */
if (frsock == servsock) continue;
/* poll existing clients */
if (FD_ISSET(frsock, &readset)) {
EthPkt *pkt;
/* read the message */
pkt = recvethpkt(frsock);
if (!pkt) {
struct sockaddr_in caddr;
int caddrlen;
struct hostent * cent;
/* disconnect from client */
caddrlen = sizeof(caddr);
if (getpeername(frsock, (struct sockaddr *) &caddr, &caddrlen) == -1) {
perror("getpeername");
}
cent = gethostbyaddr((char *) &caddr.sin_addr,
sizeof(caddr.sin_addr), AF_INET);
printf("admin: disconnect from '%s' at '%d'\n",
cent->h_name, frsock);
/* no more watching this sock */
close(frsock);
FD_CLR(frsock, &livesdset);
} else {
/* send the pkt to all others */
for (tosock=3; tosock <= livesdmax; tosock++) {
/* skip server socket and sender socket */
if (tosock == servsock || tosock == frsock) continue;
if (FD_ISSET(tosock, &livesdset))
sendethpkt(tosock, pkt);
}
printf("sent --> ");
showethpkt(pkt);
/* free the pkt */
freeethpkt(pkt);
}
}
}
/* look for connects from new clients */
if (FD_ISSET(servsock, &readset)) {
struct sockaddr_in caddr;
int caddrlen;
int csd;
/* accept a connection request */
caddrlen = sizeof(caddr);
csd = accept(servsock, (struct sockaddr *) &caddr, &caddrlen);
if (csd != -1) {
struct hostent *cent;
/* include this in the active sd set */
FD_SET(csd, &livesdset);
if (csd > livesdmax)
livesdmax = csd;
/* figure out the client */
cent = gethostbyaddr((char *) &caddr.sin_addr,
sizeof(caddr.sin_addr), AF_INET);
printf("admin: connect from '%s' at '%d'\n",
cent->h_name, csd);
} else {
perror("accept");
exit(0);
}
}
}
}
/*--------------------------------------------------------------------*/