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