1 /* udp_i.c -- inject packets into the network for test */
4 #include <sys/socket.h>
5 #include <netinet/in.h>
12 #define UDP_PORT 10094
22 char buf[BUFSIZE], *buftext;
23 struct sockaddr_in to;
24 struct hostent *hp, *gethostbyname();
26 char myhost[65], *the_host;
29 gethostname(myhost,65);
35 fprintf(stderr,"Usage: %s [<hostname> [<portnumber>]]\n",argv[0]);
38 if(argc>1)the_host = argv[1];
39 if(argc>2)portno = atoi(argv[2]);
41 /* Find the host number */
42 hp = gethostbyname(the_host);
44 fprintf(stderr,"%s: unknown host\n",argv[1]);
49 sock = socket(AF_INET, SOCK_DGRAM, 0);
51 perror("opening raw socket");
55 /* if(setsockopt(sock, SOL_SOCKET, SO_DONTROUTE, &on, sizeof on)<0){
56 perror("setting socket options");
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);
65 /* give the user a prompt */
69 /* fill in as much of the header as we care about for now */
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");
92 /* all done, close the socket and exit */