Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

udp.c

Go to the documentation of this file.
00001 /*
00002  * udp.c
00003  *
00004  * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
00005  *
00006  * $Id: udp.c,v 1.8 2002/02/08 07:43:53 dugsong Exp $
00007  */
00008 
00009 #include "config.h"
00010 
00011 #include <sys/types.h>
00012 
00013 #include <err.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <time.h>
00018 #include <unistd.h>
00019 
00020 #include "dnet.h"
00021 #include "aton.h"
00022 #include "mod.h"
00023 
00024 void
00025 udp_usage(void)
00026 {
00027         fprintf(stderr, "Usage: dnet udp [sport|dport <value>] ...\n");
00028         exit(1);
00029 }
00030 
00031 int
00032 udp_main(int argc, char *argv[])
00033 {
00034         struct udp_hdr *udp;
00035         u_char *p, buf[IP_LEN_MAX];     /* XXX */
00036         char *name, *value;
00037         int c, len;
00038 
00039         srand(time(NULL));
00040         
00041         udp = (struct udp_hdr *)buf;
00042         
00043         memset(udp, 0, sizeof(*udp));
00044         udp->uh_sport = rand() & 0xffff;
00045         udp->uh_dport = rand() & 0xffff;
00046         
00047         for (c = 1; c + 1 < argc; c += 2) {
00048                 name = argv[c];
00049                 value = argv[c + 1];
00050 
00051                 if (strcmp(name, "sport") == 0) {
00052                         if (port_aton(value, &udp->uh_sport) < 0)
00053                                 udp_usage();
00054                 } else if (strcmp(name, "dport") == 0) {
00055                         if (port_aton(value, &udp->uh_dport) < 0)
00056                                 udp_usage();
00057                 } else
00058                         udp_usage();
00059         }
00060         argc -= c;
00061         argv += c;
00062 
00063         if (argc != 0)
00064                 udp_usage();
00065 
00066         p = buf + UDP_HDR_LEN;
00067         
00068         if (!isatty(STDIN_FILENO)) {
00069                 len = sizeof(buf) - (p - buf);
00070                 while ((c = read(STDIN_FILENO, p, len)) > 0) {
00071                         p += c;
00072                         len -= c;
00073                 }
00074         }
00075         len = p - buf;
00076         udp->uh_ulen = htons(len);
00077         
00078         if (write(STDOUT_FILENO, buf, len) != len)
00079                 err(1, "write");
00080 
00081         return (0);
00082 }
00083 
00084 struct mod mod_udp = {
00085         "udp",
00086         MOD_TYPE_ENCAP,
00087         udp_main
00088 };

Generated on Sun May 14 14:51:12 2006 by  doxygen 1.4.2