source: trunk/minix/commands/simple/ping.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.6 KB
Line 
1/*
2ping.c
3*/
4
5#define DEBUG 1
6
7#include <sys/types.h>
8#include <errno.h>
9#include <signal.h>
10#include <net/gen/netdb.h>
11#include <sys/ioctl.h>
12#include <stdlib.h>
13#include <stdio.h>
14#include <unistd.h>
15#include <net/gen/oneCsum.h>
16#include <fcntl.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>
22
23#define WRITE_SIZE 30
24char buffer[16*1024];
25
26int main(int argc, char *argv[]);
27
28#if DEBUG
29#define where() fprintf(stderr, "%s %d:", __FILE__, __LINE__);
30#endif
31
32#if __STDC__
33#define PROTO(x,y) x y
34#else
35#define PROTO(x,y) X ()
36#endif
37
38PROTO (int main, (int argc, char *argv[]) );
39static PROTO (void sig_hand, (int signal) );
40
41main(argc, argv)
42int argc;
43char *argv[];
44{
45 int fd, i;
46 int result, result1;
47 nwio_ipopt_t ipopt;
48 ip_hdr_t *ip_hdr;
49 int ihl;
50 icmp_hdr_t *icmp_hdr;
51 ipaddr_t dst_addr;
52 struct hostent *hostent;
53 int length;
54
55 if (argc<2 || argc>3)
56 {
57 fprintf(stderr, "Usage: %s hostname [-l length] [-t timeout]\n",
58 argv[0]);
59 exit(1);
60 }
61 hostent= gethostbyname(argv[1]);
62 if (!hostent)
63 {
64 dst_addr= inet_addr(argv[1]);
65 if (dst_addr == -1)
66 {
67 fprintf(stderr, "%s: unknown host (%s)\n",
68 argv[0], argv[1]);
69 exit(1);
70 }
71 }
72 else
73 dst_addr= *(ipaddr_t *)(hostent->h_addr);
74 if (argc == 3)
75 {
76 length= strtol (argv[2], (char **)0, 0);
77 if (length< sizeof(icmp_hdr_t) + IP_MIN_HDR_SIZE)
78 {
79 fprintf(stderr, "%s: length too small (%s)\n",
80 argv[0], argv[2]);
81 exit(1);
82 }
83 }
84 else
85 length= WRITE_SIZE;
86
87 fd= open ("/dev/ip", O_RDWR);
88 if (fd<0)
89 perror("open"), exit(1);
90
91 ipopt.nwio_flags= NWIO_COPY | NWIO_PROTOSPEC;
92 ipopt.nwio_proto= 1;
93
94 result= ioctl (fd, NWIOSIPOPT, &ipopt);
95 if (result<0)
96 perror("ioctl (NWIOSIPOPT)"), exit(1);
97
98 result= ioctl (fd, NWIOGIPOPT, &ipopt);
99 if (result<0)
100 perror("ioctl (NWIOGIPOPT)"), exit(1);
101
102 for (i= 0; i< 20; i++)
103 {
104 ip_hdr= (ip_hdr_t *)buffer;
105 ip_hdr->ih_dst= dst_addr;
106
107 icmp_hdr= (icmp_hdr_t *)(buffer+20);
108 icmp_hdr->ih_type= 8;
109 icmp_hdr->ih_code= 0;
110 icmp_hdr->ih_chksum= 0;
111 icmp_hdr->ih_chksum= ~oneC_sum(0, (u16_t *)icmp_hdr,
112 WRITE_SIZE-20);
113 result= write(fd, buffer, length);
114 if (result<0)
115 {
116 perror("write");
117 exit(1);
118 }
119 if (result != length)
120 {
121 where();
122 fprintf(stderr, "result= %d\n", result);
123 exit(1);
124 }
125
126 alarm(0);
127 signal (SIGALRM, sig_hand);
128 alarm(1);
129
130 result= read(fd, buffer, sizeof(buffer));
131 if (result>= 0 || errno != EINTR)
132 break;
133 }
134 if (i >= 20)
135 {
136 printf("no answer from %s\n", argv[1]);
137 exit(1);
138 }
139 if (result<0)
140 {
141 perror ("read");
142 exit(1);
143 }
144 printf("%s is alive\n", argv[1]);
145 exit(0);
146}
147
148static void sig_hand(signal)
149int signal;
150{
151}
Note: See TracBrowser for help on using the repository browser.