cmds.c revision 1.12 1 1.12 mycroft /* $NetBSD: cmds.c,v 1.12 2002/09/19 00:01:33 mycroft Exp $ */
2 1.6 mrg
3 1.3 cgd /*-
4 1.3 cgd * Copyright (c) 1985, 1993 The Regents of the University of California.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.7 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.7 lukem #if 0
39 1.6 mrg static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 3/26/95";
40 1.7 lukem #else
41 1.12 mycroft __RCSID("$NetBSD: cmds.c,v 1.12 2002/09/19 00:01:33 mycroft Exp $");
42 1.7 lukem #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include "timedc.h"
46 1.3 cgd #include <sys/file.h>
47 1.3 cgd
48 1.1 cgd #include <netinet/in_systm.h>
49 1.1 cgd #include <netinet/ip.h>
50 1.1 cgd #include <netinet/ip_icmp.h>
51 1.3 cgd
52 1.3 cgd #include <stdlib.h>
53 1.8 lukem #include <string.h>
54 1.3 cgd #include <unistd.h>
55 1.3 cgd
56 1.1 cgd #define TSPTYPES
57 1.1 cgd #include <protocols/timed.h>
58 1.3 cgd
59 1.3 cgd #define SECHR (60*60)
60 1.3 cgd #define SECDAY (24*SECHR)
61 1.3 cgd
62 1.3 cgd # define DATE_PROTO "udp"
63 1.3 cgd # define DATE_PORT "time"
64 1.3 cgd
65 1.1 cgd
66 1.1 cgd int sock;
67 1.1 cgd int sock_raw;
68 1.9 mrg char myname[MAXHOSTNAMELEN + 1];
69 1.3 cgd struct hostent *hp;
70 1.1 cgd struct sockaddr_in server;
71 1.3 cgd struct sockaddr_in dayaddr;
72 1.1 cgd extern int measure_delta;
73 1.3 cgd
74 1.3 cgd void bytenetorder(struct tsp *);
75 1.3 cgd void bytehostorder(struct tsp *);
76 1.3 cgd
77 1.3 cgd
78 1.4 mycroft #define BU ((unsigned long)2208988800U) /* seconds before UNIX epoch */
79 1.3 cgd
80 1.3 cgd
81 1.3 cgd /* compute the difference between our date and another machine
82 1.3 cgd */
83 1.3 cgd static int /* difference in days from our time */
84 1.3 cgd daydiff(char *hostname)
85 1.3 cgd {
86 1.3 cgd int i;
87 1.3 cgd int trials;
88 1.12 mycroft int tout;
89 1.12 mycroft struct timeval now;
90 1.12 mycroft struct pollfd set[1];
91 1.3 cgd struct sockaddr from;
92 1.3 cgd int fromlen;
93 1.3 cgd unsigned long sec;
94 1.3 cgd
95 1.3 cgd
96 1.3 cgd /* wait 2 seconds between 10 tries */
97 1.12 mycroft tout = 2000;
98 1.12 mycroft set[0].fd = sock;
99 1.12 mycroft set[0].events = POLLIN;
100 1.3 cgd for (trials = 0; trials < 10; trials++) {
101 1.3 cgd /* ask for the time */
102 1.3 cgd sec = 0;
103 1.3 cgd if (sendto(sock, &sec, sizeof(sec), 0,
104 1.3 cgd (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
105 1.3 cgd perror("sendto(sock)");
106 1.3 cgd return 0;
107 1.3 cgd }
108 1.3 cgd
109 1.3 cgd for (;;) {
110 1.12 mycroft i = poll(set, 1, tout);
111 1.3 cgd if (i < 0) {
112 1.3 cgd if (errno == EINTR)
113 1.3 cgd continue;
114 1.12 mycroft perror("poll(date read)");
115 1.3 cgd return 0;
116 1.3 cgd }
117 1.3 cgd if (0 == i)
118 1.3 cgd break;
119 1.3 cgd
120 1.3 cgd fromlen = sizeof(from);
121 1.3 cgd if (recvfrom(sock,&sec,sizeof(sec),0,
122 1.3 cgd &from,&fromlen) < 0) {
123 1.3 cgd perror("recvfrom(date read)");
124 1.3 cgd return 0;
125 1.3 cgd }
126 1.3 cgd
127 1.3 cgd sec = ntohl(sec);
128 1.3 cgd if (sec < BU) {
129 1.3 cgd fprintf(stderr,
130 1.3 cgd "%s says it is before 1970: %lu",
131 1.3 cgd hostname, sec);
132 1.3 cgd return 0;
133 1.3 cgd }
134 1.3 cgd sec -= BU;
135 1.1 cgd
136 1.3 cgd (void)gettimeofday(&now, (struct timezone*)0);
137 1.3 cgd return (sec - now.tv_sec);
138 1.3 cgd }
139 1.3 cgd }
140 1.3 cgd
141 1.3 cgd /* if we get here, we tried too many times */
142 1.3 cgd fprintf(stderr,"%s will not tell us the date\n", hostname);
143 1.3 cgd return 0;
144 1.3 cgd }
145 1.3 cgd
146 1.3 cgd
147 1.1 cgd /*
148 1.3 cgd * Clockdiff computes the difference between the time of the machine on
149 1.1 cgd * which it is called and the time of the machines given as argument.
150 1.1 cgd * The time differences measured by clockdiff are obtained using a sequence
151 1.1 cgd * of ICMP TSTAMP messages which are returned to the sender by the IP module
152 1.1 cgd * in the remote machine.
153 1.3 cgd * In order to compare clocks of machines in different time zones, the time
154 1.3 cgd * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
155 1.1 cgd * If a hosts uses a different time format, it should set the high order
156 1.1 cgd * bit of the 32-bit quantity it transmits.
157 1.1 cgd * However, VMS apparently transmits the time in milliseconds since midnight
158 1.3 cgd * local time (rather than GMT) without setting the high order bit.
159 1.3 cgd * Furthermore, it does not understand daylight-saving time. This makes
160 1.1 cgd * clockdiff behaving inconsistently with hosts running VMS.
161 1.1 cgd *
162 1.3 cgd * In order to reduce the sensitivity to the variance of message transmission
163 1.3 cgd * time, clockdiff sends a sequence of messages. Yet, measures between
164 1.3 cgd * two `distant' hosts can be affected by a small error. The error can,
165 1.3 cgd * however, be reduced by increasing the number of messages sent in each
166 1.3 cgd * measurement.
167 1.1 cgd */
168 1.3 cgd void
169 1.11 wiz clockdiff(int argc, char *argv[])
170 1.1 cgd {
171 1.1 cgd int measure_status;
172 1.3 cgd extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
173 1.3 cgd register int avg_cnt;
174 1.3 cgd register long avg;
175 1.3 cgd struct servent *sp;
176 1.1 cgd
177 1.3 cgd if (argc < 2) {
178 1.1 cgd printf("Usage: clockdiff host ... \n");
179 1.1 cgd return;
180 1.1 cgd }
181 1.1 cgd
182 1.3 cgd (void)gethostname(myname,sizeof(myname));
183 1.9 mrg myname[sizeof(myname) - 1] = '\0';
184 1.3 cgd
185 1.3 cgd /* get the address for the date ready */
186 1.3 cgd sp = getservbyname(DATE_PORT, DATE_PROTO);
187 1.3 cgd if (!sp) {
188 1.3 cgd (void)fprintf(stderr, "%s/%s is an unknown service\n",
189 1.3 cgd DATE_PORT, DATE_PROTO);
190 1.3 cgd dayaddr.sin_port = 0;
191 1.3 cgd } else {
192 1.3 cgd dayaddr.sin_port = sp->s_port;
193 1.3 cgd }
194 1.1 cgd
195 1.1 cgd while (argc > 1) {
196 1.1 cgd argc--; argv++;
197 1.1 cgd hp = gethostbyname(*argv);
198 1.1 cgd if (hp == NULL) {
199 1.3 cgd fprintf(stderr, "timedc: %s: ", *argv);
200 1.3 cgd herror(0);
201 1.1 cgd continue;
202 1.1 cgd }
203 1.3 cgd
204 1.1 cgd server.sin_family = hp->h_addrtype;
205 1.3 cgd bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
206 1.3 cgd for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
207 1.3 cgd measure_status = measure(10000,100, *argv, &server, 1);
208 1.3 cgd if (measure_status != GOOD)
209 1.3 cgd break;
210 1.3 cgd avg += measure_delta;
211 1.1 cgd }
212 1.3 cgd if (measure_status == GOOD)
213 1.3 cgd measure_delta = avg/avg_cnt;
214 1.3 cgd
215 1.1 cgd switch (measure_status) {
216 1.1 cgd case HOSTDOWN:
217 1.1 cgd printf("%s is down\n", hp->h_name);
218 1.1 cgd continue;
219 1.1 cgd case NONSTDTIME:
220 1.3 cgd printf("%s transmitts a non-standard time format\n",
221 1.3 cgd hp->h_name);
222 1.1 cgd continue;
223 1.1 cgd case UNREACHABLE:
224 1.1 cgd printf("%s is unreachable\n", hp->h_name);
225 1.1 cgd continue;
226 1.3 cgd }
227 1.3 cgd
228 1.3 cgd /*
229 1.3 cgd * Try to get the date only after using ICMP timestamps to
230 1.3 cgd * get the time. This is because the date protocol
231 1.3 cgd * is optional.
232 1.3 cgd */
233 1.3 cgd if (dayaddr.sin_port != 0) {
234 1.3 cgd dayaddr.sin_family = hp->h_addrtype;
235 1.3 cgd bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
236 1.3 cgd hp->h_length);
237 1.3 cgd avg = daydiff(*argv);
238 1.3 cgd if (avg > SECDAY) {
239 1.3 cgd printf("time on %s is %ld days ahead %s\n",
240 1.3 cgd hp->h_name, avg/SECDAY, myname);
241 1.3 cgd continue;
242 1.3 cgd } else if (avg < -SECDAY) {
243 1.3 cgd printf("time on %s is %ld days behind %s\n",
244 1.3 cgd hp->h_name, -avg/SECDAY, myname);
245 1.3 cgd continue;
246 1.3 cgd }
247 1.1 cgd }
248 1.1 cgd
249 1.3 cgd if (measure_delta > 0) {
250 1.3 cgd printf("time on %s is %d ms. ahead of time on %s\n",
251 1.3 cgd hp->h_name, measure_delta, myname);
252 1.3 cgd } else if (measure_delta == 0) {
253 1.3 cgd printf("%s and %s have the same time\n",
254 1.3 cgd hp->h_name, myname);
255 1.3 cgd } else {
256 1.3 cgd printf("time on %s is %d ms. behind time on %s\n",
257 1.3 cgd hp->h_name, -measure_delta, myname);
258 1.3 cgd }
259 1.1 cgd }
260 1.1 cgd return;
261 1.1 cgd }
262 1.3 cgd
263 1.3 cgd
264 1.1 cgd /*
265 1.1 cgd * finds location of master timedaemon
266 1.1 cgd */
267 1.3 cgd void
268 1.3 cgd msite(int argc, char *argv[])
269 1.1 cgd {
270 1.1 cgd int cc;
271 1.12 mycroft struct pollfd set[1];
272 1.1 cgd struct sockaddr_in dest;
273 1.3 cgd int i, length;
274 1.3 cgd struct sockaddr from;
275 1.12 mycroft int tout;
276 1.1 cgd struct tsp msg;
277 1.1 cgd struct servent *srvp;
278 1.3 cgd char *tgtname;
279 1.1 cgd
280 1.3 cgd if (argc < 1) {
281 1.3 cgd printf("Usage: msite [hostname]\n");
282 1.1 cgd return;
283 1.1 cgd }
284 1.1 cgd
285 1.1 cgd srvp = getservbyname("timed", "udp");
286 1.1 cgd if (srvp == 0) {
287 1.1 cgd fprintf(stderr, "udp/timed: unknown service\n");
288 1.1 cgd return;
289 1.1 cgd }
290 1.1 cgd dest.sin_port = srvp->s_port;
291 1.1 cgd dest.sin_family = AF_INET;
292 1.1 cgd
293 1.3 cgd (void)gethostname(myname, sizeof(myname));
294 1.3 cgd i = 1;
295 1.12 mycroft tout = 15000;
296 1.12 mycroft set[0].fd = sock;
297 1.12 mycroft set[0].events = POLLIN;
298 1.3 cgd do {
299 1.3 cgd tgtname = (i >= argc) ? myname : argv[i];
300 1.3 cgd hp = gethostbyname(tgtname);
301 1.3 cgd if (hp == 0) {
302 1.3 cgd fprintf(stderr, "timedc: %s: ", tgtname);
303 1.3 cgd herror(0);
304 1.3 cgd continue;
305 1.3 cgd }
306 1.3 cgd bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
307 1.1 cgd
308 1.5 mrg (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name) - 1);
309 1.3 cgd msg.tsp_type = TSP_MSITE;
310 1.3 cgd msg.tsp_vers = TSPVERSION;
311 1.3 cgd bytenetorder(&msg);
312 1.3 cgd if (sendto(sock, &msg, sizeof(struct tsp), 0,
313 1.3 cgd (struct sockaddr*)&dest,
314 1.3 cgd sizeof(struct sockaddr)) < 0) {
315 1.3 cgd perror("sendto");
316 1.3 cgd continue;
317 1.3 cgd }
318 1.1 cgd
319 1.12 mycroft if (poll(set, 1, tout)) {
320 1.3 cgd length = sizeof(struct sockaddr);
321 1.3 cgd cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
322 1.3 cgd &from, &length);
323 1.3 cgd if (cc < 0) {
324 1.3 cgd perror("recvfrom");
325 1.3 cgd continue;
326 1.3 cgd }
327 1.3 cgd bytehostorder(&msg);
328 1.3 cgd if (msg.tsp_type == TSP_ACK) {
329 1.3 cgd printf("master timedaemon at %s is %s\n",
330 1.3 cgd tgtname, msg.tsp_name);
331 1.3 cgd } else {
332 1.3 cgd printf("received wrong ack: %s\n",
333 1.3 cgd tsptype[msg.tsp_type]);
334 1.3 cgd }
335 1.3 cgd } else {
336 1.3 cgd printf("communication error with %s\n", tgtname);
337 1.1 cgd }
338 1.3 cgd } while (++i < argc);
339 1.1 cgd }
340 1.1 cgd
341 1.1 cgd /*
342 1.1 cgd * quits timedc
343 1.1 cgd */
344 1.3 cgd void
345 1.7 lukem quit(int argc, char *argv[])
346 1.1 cgd {
347 1.1 cgd exit(0);
348 1.1 cgd }
349 1.1 cgd
350 1.1 cgd
351 1.1 cgd /*
352 1.1 cgd * Causes the election timer to expire on the selected hosts
353 1.1 cgd * It sends just one udp message per machine, relying on
354 1.1 cgd * reliability of communication channel.
355 1.1 cgd */
356 1.3 cgd void
357 1.3 cgd testing(int argc, char *argv[])
358 1.1 cgd {
359 1.1 cgd struct servent *srvp;
360 1.3 cgd struct sockaddr_in sin;
361 1.1 cgd struct tsp msg;
362 1.1 cgd
363 1.3 cgd if (argc < 2) {
364 1.3 cgd printf("Usage: election host1 [host2 ...]\n");
365 1.1 cgd return;
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd srvp = getservbyname("timed", "udp");
369 1.1 cgd if (srvp == 0) {
370 1.1 cgd fprintf(stderr, "udp/timed: unknown service\n");
371 1.1 cgd return;
372 1.3 cgd }
373 1.1 cgd
374 1.1 cgd while (argc > 1) {
375 1.1 cgd argc--; argv++;
376 1.1 cgd hp = gethostbyname(*argv);
377 1.1 cgd if (hp == NULL) {
378 1.3 cgd fprintf(stderr, "timedc: %s: ", *argv);
379 1.3 cgd herror(0);
380 1.1 cgd argc--; argv++;
381 1.1 cgd continue;
382 1.1 cgd }
383 1.3 cgd sin.sin_port = srvp->s_port;
384 1.3 cgd sin.sin_family = hp->h_addrtype;
385 1.3 cgd bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
386 1.3 cgd
387 1.3 cgd msg.tsp_type = TSP_TEST;
388 1.3 cgd msg.tsp_vers = TSPVERSION;
389 1.3 cgd (void)gethostname(myname, sizeof(myname));
390 1.3 cgd (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
391 1.3 cgd bytenetorder(&msg);
392 1.3 cgd if (sendto(sock, &msg, sizeof(struct tsp), 0,
393 1.3 cgd (struct sockaddr*)&sin,
394 1.3 cgd sizeof(struct sockaddr)) < 0) {
395 1.1 cgd perror("sendto");
396 1.1 cgd }
397 1.1 cgd }
398 1.1 cgd }
399 1.1 cgd
400 1.3 cgd
401 1.1 cgd /*
402 1.1 cgd * Enables or disables tracing on local timedaemon
403 1.1 cgd */
404 1.3 cgd void
405 1.3 cgd tracing(int argc, char *argv[])
406 1.1 cgd {
407 1.1 cgd int onflag;
408 1.1 cgd int length;
409 1.1 cgd int cc;
410 1.12 mycroft struct pollfd set[1];
411 1.1 cgd struct sockaddr_in dest;
412 1.3 cgd struct sockaddr from;
413 1.12 mycroft int tout;
414 1.1 cgd struct tsp msg;
415 1.1 cgd struct servent *srvp;
416 1.1 cgd
417 1.1 cgd if (argc != 2) {
418 1.1 cgd printf("Usage: tracing { on | off }\n");
419 1.1 cgd return;
420 1.1 cgd }
421 1.1 cgd
422 1.1 cgd srvp = getservbyname("timed", "udp");
423 1.1 cgd if (srvp == 0) {
424 1.1 cgd fprintf(stderr, "udp/timed: unknown service\n");
425 1.1 cgd return;
426 1.3 cgd }
427 1.1 cgd dest.sin_port = srvp->s_port;
428 1.1 cgd dest.sin_family = AF_INET;
429 1.1 cgd
430 1.3 cgd (void)gethostname(myname,sizeof(myname));
431 1.3 cgd hp = gethostbyname(myname);
432 1.1 cgd bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
433 1.1 cgd
434 1.1 cgd if (strcmp(argv[1], "on") == 0) {
435 1.1 cgd msg.tsp_type = TSP_TRACEON;
436 1.1 cgd onflag = ON;
437 1.1 cgd } else {
438 1.1 cgd msg.tsp_type = TSP_TRACEOFF;
439 1.1 cgd onflag = OFF;
440 1.1 cgd }
441 1.1 cgd
442 1.5 mrg (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name) - 1);
443 1.1 cgd msg.tsp_vers = TSPVERSION;
444 1.1 cgd bytenetorder(&msg);
445 1.3 cgd if (sendto(sock, &msg, sizeof(struct tsp), 0,
446 1.3 cgd (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
447 1.1 cgd perror("sendto");
448 1.1 cgd return;
449 1.1 cgd }
450 1.1 cgd
451 1.12 mycroft tout = 5000;
452 1.12 mycroft set[0].fd = sock;
453 1.12 mycroft set[0].events = POLLIN;
454 1.12 mycroft if (poll(set, 1, tout)) {
455 1.3 cgd length = sizeof(struct sockaddr);
456 1.3 cgd cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
457 1.3 cgd &from, &length);
458 1.1 cgd if (cc < 0) {
459 1.1 cgd perror("recvfrom");
460 1.1 cgd return;
461 1.1 cgd }
462 1.1 cgd bytehostorder(&msg);
463 1.1 cgd if (msg.tsp_type == TSP_ACK)
464 1.1 cgd if (onflag)
465 1.1 cgd printf("timed tracing enabled\n");
466 1.1 cgd else
467 1.1 cgd printf("timed tracing disabled\n");
468 1.1 cgd else
469 1.3 cgd printf("wrong ack received: %s\n",
470 1.1 cgd tsptype[msg.tsp_type]);
471 1.1 cgd } else
472 1.1 cgd printf("communication error\n");
473 1.1 cgd }
474 1.1 cgd
475 1.3 cgd int
476 1.11 wiz priv_resources(void)
477 1.1 cgd {
478 1.1 cgd int port;
479 1.1 cgd struct sockaddr_in sin;
480 1.1 cgd
481 1.1 cgd sock = socket(AF_INET, SOCK_DGRAM, 0);
482 1.1 cgd if (sock < 0) {
483 1.1 cgd perror("opening socket");
484 1.1 cgd return(-1);
485 1.1 cgd }
486 1.1 cgd
487 1.1 cgd sin.sin_family = AF_INET;
488 1.1 cgd sin.sin_addr.s_addr = 0;
489 1.1 cgd for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
490 1.1 cgd sin.sin_port = htons((u_short)port);
491 1.3 cgd if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
492 1.1 cgd break;
493 1.1 cgd if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
494 1.1 cgd perror("bind");
495 1.1 cgd (void) close(sock);
496 1.1 cgd return(-1);
497 1.1 cgd }
498 1.1 cgd }
499 1.1 cgd if (port == IPPORT_RESERVED / 2) {
500 1.1 cgd fprintf(stderr, "all reserved ports in use\n");
501 1.1 cgd (void) close(sock);
502 1.1 cgd return(-1);
503 1.1 cgd }
504 1.1 cgd
505 1.3 cgd sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
506 1.1 cgd if (sock_raw < 0) {
507 1.1 cgd perror("opening raw socket");
508 1.3 cgd (void) close(sock);
509 1.1 cgd return(-1);
510 1.1 cgd }
511 1.1 cgd return(1);
512 1.1 cgd }
513