Imported Debian version 1.1.6
[debian/ipip] / test / udp_i.c
1 /* udp_i.c -- inject packets into the network for test */
2
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 #include <netdb.h>
7
8 #include <stdio.h>
9 #include <string.h>
10
11 #define BUFSIZE 1024
12 #define UDP_PORT 10094
13
14 int on = 1;
15 int off = 0;
16
17 main(argc, argv)
18         int argc;
19         char *argv[];
20 {
21         int sock, portno;
22         char buf[BUFSIZE], *buftext;
23         struct sockaddr_in to;
24         struct hostent *hp, *gethostbyname();
25         int gethostname();
26         char myhost[65], *the_host;
27
28         /* Set defaults */
29         gethostname(myhost,65);
30         the_host = myhost;
31         portno = UDP_PORT;
32
33         /* Test arguments */
34         if(argc>2){
35                 fprintf(stderr,"Usage: %s [<hostname> [<portnumber>]]\n",argv[0]);
36                 exit(1);
37         }
38         if(argc>1)the_host = argv[1];
39         if(argc>2)portno = atoi(argv[2]);
40
41         /* Find the host number */
42         hp = gethostbyname(the_host);
43         if (hp == 0){
44                 fprintf(stderr,"%s: unknown host\n",argv[1]);
45                 exit(2);
46         }
47
48         /* Create socket */
49         sock = socket(AF_INET, SOCK_DGRAM, 0);
50         if (sock<0) {
51                 perror("opening raw socket");
52                 exit(1);
53         }
54
55 /*      if(setsockopt(sock, SOL_SOCKET, SO_DONTROUTE, &on, sizeof on)<0){
56                 perror("setting socket options");
57                 exit(1);
58         }
59 */
60         bzero((char *)&to, sizeof to);
61         bcopy(hp->h_addr_list[0], (char *)&to.sin_addr, hp->h_length);
62         to.sin_family = AF_INET;
63         to.sin_port = htons(portno);
64
65         /* give the user a prompt */
66         putchar('>');
67         fflush(stdout);
68
69         /* fill in as much of the header as we care about for now */
70         buf[12] = 44;
71         buf[13] = 72;
72         buf[14] = 6;
73         buf[15] = 134;
74         buf[16] = 44;
75         buf[17] = 72;
76         buf[18] = 6;
77         buf[19] = 131;
78
79         buftext = buf + 40;
80
81         /* Loop until we hit EOF */
82         while(fgets(buftext,BUFSIZE-40,stdin) != NULL){
83                 if (sendto(sock, buf, (strlen(buftext)+40), 0, (struct sockaddr *)&to, sizeof to) < 0) {
84                         perror("writing of udp socket");
85                         close(sock);
86                         exit(4);
87                 }
88                 putchar('>');
89                 fflush(stdout);
90         }
91
92         /* all done, close the socket and exit */
93         close(sock);
94         exit(0);
95 }