|
|
1.1 root 1: #include <sys/param.h>
2: #include <sys/inet/ip.h>
3: #include <ipc.h>
4:
5: /* Echo or Echo Reply Message
6:
7: 0 1 2 3
8: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
9: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
10: | Type | Code | Checksum |
11: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12: | Identifier | Sequence Number |
13: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
14: | Data ...
15: +-+-+-+-+-
16: */
17:
18: typedef struct {
19: struct ip ip;
20: u_char type;
21: u_char code;
22: u_char cksum[2];
23: u_char id[2];
24: u_char seqno[2];
25: u_char data[128];
26: } EchoMsg;
27:
28: main(ac, av)
29: int ac;
30: char *av[];
31: {
32: long addr, myaddr;
33: int i, fd;
34:
35: if(ac!=2){
36: fprint(2, "usage: %s host\n", av[0]);
37: exit(1);
38: }
39: /*
40: * translate his name into an address
41: */
42: addr = in_address(av[1]);
43: if(addr == 0){
44: fprint(2, "internet address for %s unknown\n", av[1]);
45: exit(1);
46: }
47:
48: /*
49: * find out my own name and get an address for it. preferably
50: * use an address for his network (how do we handle subnets?).
51: */
52: myaddr = in_addronnet(me, addr, 0);
53: if(myaddr == 0){
54: addr = in_address(me);
55: if(addr == 0){
56: fprint(2, "internet address for %s unknown\n", me);
57: exit(1);
58: }
59: }
60:
61: /*
62: * get a connection to the icmp manager
63: */
64: fd = ipcopen("/cs/icmp", "");
65:
66: /*
67: * send 15 messages one second apart
68: */
69: for(i=0; i<15; i++){
70: switch(select(NOFILE, &fds, 0, 1000)){
71: }
72: }
73: }
74:
75: /*
76: * send an ip datagram
77: */
78: ipsend(fd, mp, len, src, dst)
79: IcmpMsg *mp;
80: u_long src;
81: u_long dst;
82: {
83: mp->ip.ip_dst = dst;
84: mp->ip.ip_src = src;
85: mp->ip.ip_len = len;
86: mp->ip.ip_hl = sizeof(struct ip)>>2;
87: mp->ip.ip_off = 0;
88: mp->ip.ip_ttl = 255;
89: cksum(mp, len);
90: return write(fd, mp, len)!=len ? -1 : 0;
91: }
92:
93: /*
94: * echo a datagram
95: */
96: echo(fd, mp, len)
97: IcmpMsg *mp;
98: {
99: mp->type = 0;
100: if(ipsend(fd, mp, len, mp->ip.ip_dst, mp->ip.ip_src) < 0)
101: perror("sending echo");
102: }
103:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.