10 #include <net/gen/netdb.h>
11 #include <sys/ioctl.h>
15 #include <net/gen/oneCsum.h>
17 #include <net/gen/in.h>
18 #include <net/gen/inet.h>
19 #include <net/gen/ip_hdr.h>
20 #include <net/gen/icmp_hdr.h>
21 #include <net/gen/ip_io.h>
26 int main(int argc
, char *argv
[]);
29 #define where() fprintf(stderr, "%s %d:", __FILE__, __LINE__);
33 #define PROTO(x,y) x y
35 #define PROTO(x,y) X ()
38 PROTO (int main
, (int argc
, char *argv
[]) );
39 static PROTO (void sig_hand
, (int signal
) );
52 struct hostent
*hostent
;
57 fprintf(stderr
, "Usage: %s hostname [length]\n", argv
[0]);
60 hostent
= gethostbyname(argv
[1]);
63 dst_addr
= inet_addr(argv
[1]);
66 fprintf(stderr
, "%s: unknown host (%s)\n",
72 dst_addr
= *(ipaddr_t
*)(hostent
->h_addr
);
75 length
= strtol (argv
[2], (char **)0, 0);
76 if (length
< sizeof(icmp_hdr_t
) + IP_MIN_HDR_SIZE
)
78 fprintf(stderr
, "%s: length too small (%s)\n",
86 fd
= open ("/dev/ip", O_RDWR
);
88 perror("open"), exit(1);
90 ipopt
.nwio_flags
= NWIO_COPY
| NWIO_PROTOSPEC
;
93 result
= ioctl (fd
, NWIOSIPOPT
, &ipopt
);
95 perror("ioctl (NWIOSIPOPT)"), exit(1);
97 result
= ioctl (fd
, NWIOGIPOPT
, &ipopt
);
99 perror("ioctl (NWIOGIPOPT)"), exit(1);
101 for (i
= 0; i
< 20; i
++)
103 ip_hdr
= (ip_hdr_t
*)buffer
;
104 ip_hdr
->ih_dst
= dst_addr
;
106 icmp_hdr
= (icmp_hdr_t
*)(buffer
+20);
107 icmp_hdr
->ih_type
= 8;
108 icmp_hdr
->ih_code
= 0;
109 icmp_hdr
->ih_chksum
= 0;
110 icmp_hdr
->ih_chksum
= ~oneC_sum(0, (u16_t
*)icmp_hdr
,
112 result
= write(fd
, buffer
, length
);
118 if (result
!= length
)
121 fprintf(stderr
, "result= %d\n", result
);
126 signal (SIGALRM
, sig_hand
);
129 result
= read(fd
, buffer
, sizeof(buffer
));
130 if (result
>= 0 || errno
!= EINTR
)
135 printf("no answer from %s\n", argv
[1]);
143 printf("%s is alive\n", argv
[1]);
147 static void sig_hand(signal
)