-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoticeserver.c
246 lines (221 loc) · 8.77 KB
/
noticeserver.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "common.h"
void usage() {
puts ("Usage: noticeserver [-a <address>] [-p <port number>] [-f <-a address> <-p port>] [-d]\n");
puts ("-a - ip address to bind to (admin client address in -f mode)\n");
puts ("-p - port number (admin client port in -f mode)\n");
puts ("-f - register 'admin' address and port\n");
puts ("-d - debug mode, message output to syslog\n");
exit(0);
}
int main(int argc, char *argv[]) {
extern int optind,opterr,optopt;
extern char* optarg;
int ch;
int rc; /* system calls return value storage */
int sfd; /* socket descriptor */
char buf[BUFLEN+1]; /* buffer for incoming data */
struct sockaddr_in sa; /* Internet address struct */
struct sockaddr_in csa; /* Client's Internet address struct */
unsigned int sa_len;
char ip[50];
char tport[7];
unsigned short port=0;
struct hostent* h;
int option_value; /* needed for setsockopt */
DEPOT* ipDB;
DEPOT* portDB;
int success=1; /* indicates a successful registration - send confirmation */
int debug=0;
int fflag=0, aflag=0, pflag=0;
strcpy(ip,"");
/* parse option parameters */
while ((ch = getopt(argc, argv, "a:p:fd")) != EOF) {
switch ((char)ch) {
case 'a': /* get address */
aflag = 1;
strcpy(ip,optarg);
break;
case 'p': /* get port */
pflag = 1;
port = atoi(optarg);
break;
case 'f': /* admin registration */
fflag = 1;
break;
case 'd': /* turn on debug */
debug = 1;
break;
default:
usage();
break;
}
}
if (fflag) {
if (aflag & pflag) {
if(!(ipDB = dpopen(IP_DBNAME, DP_OWRITER | DP_OCREAT, -1))) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpopen ipDB (admin): %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* store the record */
if(!dpput(ipDB, "admin", -1, ip, -1, DP_DOVER)) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpput ipDB (admin): %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* close the database*/
if(!dpclose(ipDB)){
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpclose ipDB (admin): %s", dperrmsg(dpecode));
closelog();
exit(1);
}
// store client port number
/* open the database */
if(!(portDB = dpopen(PORT_DBNAME, DP_OWRITER | DP_OCREAT, -1))) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpopen portDB (admin): %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* store the record */
sprintf(tport, "%i", port);
if(!dpput(portDB, "admin", -1, tport, -1, DP_DOVER)) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpput portDB (admin): %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* close the database*/
if(!dpclose(portDB)) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpclose portDB (admin): %s", dperrmsg(dpecode));
closelog();
exit(1);
}
exit(0);
} else {
usage();
}
} else {
/* initiate Internet address structure */
sa_len = sizeof(sa);
if (port == 0) port=S_PORT;
memset(&sa, 0, sa_len);
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
if (!strcmp(ip,"")) {
/* Local machine on IP. */
sa.sin_addr.s_addr = htonl(INADDR_ANY);
} else {
/* Get IP address (no check if input is IP address or DNS name). */
h = gethostbyname(ip);
if (h == NULL) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "Unknown host '%s'", ip);
closelog();
exit(1);
}
memcpy(&sa.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
}
/* allocate a socket */
if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "socket: failed!");
closelog();
exit(1);
}
/* make listening socket's port reusable - must be before bind */
option_value = 1;
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char *)&option_value,
sizeof(option_value)) < 0) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "setsockopt failed");
closelog();
exit(1);
}
/* bind the socket to the address */
if (bind(sfd, (struct sockaddr *)&sa, sa_len)) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "bind: failed!");
closelog();
exit(1);
}
while(1) {
rc = recvfrom(sfd, buf, BUFLEN, 0, (struct sockaddr *)&csa, &sa_len);
if (rc < 0) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "recvfrom error");
closelog();
} else {
buf[rc] = '\0';
if (debug) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_INFO, "Registration: %s IP: %s Port: %u", buf, inet_ntoa(csa.sin_addr), ntohs(csa.sin_port));
closelog();
}
sprintf(tport, "%u", ntohs(csa.sin_port));
// store client IP address
/* open the database */
if(!(ipDB = dpopen(IP_DBNAME, DP_OWRITER | DP_OCREAT, -1))) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpopen ipDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* store the record */
if(!dpput(ipDB, buf, -1, inet_ntoa(csa.sin_addr), -1, DP_DOVER)) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpput ipDB: %s", dperrmsg(dpecode));
closelog();
success = 0;
}
/* close the database*/
if(!dpclose(ipDB)){
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpclose ipDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
// store client port number
/* open the database */
if(!(portDB = dpopen(PORT_DBNAME, DP_OWRITER | DP_OCREAT, -1))) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpopen portDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* store the record */
if(!dpput(portDB, buf, -1, tport, -1, DP_DOVER)) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpput portDB: %s", dperrmsg(dpecode));
closelog();
success = 0;
}
/* close the database*/
if(!dpclose(portDB)) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpclose portDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
if (success) {
if (debug) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_INFO, "Sending confirmation: %s", buf);
closelog();
}
rc = sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&csa, sa_len);
if (rc < 0) {
openlog("noticeserver", 0, LOG_DAEMON);
syslog(LOG_ERR, "sendto error - confirmation %s", buf);
closelog();
}
}
}
}
}
exit(0);
}