File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / udp2.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (9 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: udp2.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  libnet 1.1
    5:  *  Build a UDP packet using port list chains
    6:  *
    7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
    8:  *  All rights reserved.
    9:  *
   10:  * Redistribution and use in source and binary forms, with or without
   11:  * modification, are permitted provided that the following conditions
   12:  * are met:
   13:  * 1. Redistributions of source code must retain the above copyright
   14:  *    notice, this list of conditions and the following disclaimer.
   15:  * 2. Redistributions in binary form must reproduce the above copyright
   16:  *    notice, this list of conditions and the following disclaimer in the
   17:  *    documentation and/or other materials provided with the distribution.
   18:  *
   19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29:  * SUCH DAMAGE.
   30:  *
   31:  */
   32: 
   33: #if (HAVE_CONFIG_H)
   34: #include "../include/config.h"
   35: #endif
   36: #include "./libnet_test.h"
   37: 
   38: 
   39: int
   40: main(int argc, char **argv)
   41: {
   42:     int c, build_ip;
   43:     struct timeval r;
   44:     struct timeval s;
   45:     struct timeval e;
   46:     libnet_t *l;
   47:     libnet_ptag_t udp;
   48:     char *payload;
   49:     libnet_ptag_t t;
   50:     struct libnet_stats ls;
   51:     uint16_t payload_s;
   52:     uint32_t src_ip, dst_ip;
   53:     uint16_t bport, eport, cport;
   54:     libnet_plist_t plist, *plist_p;
   55:     char errbuf[LIBNET_ERRBUF_SIZE];
   56: 
   57:     printf("libnet 1.1.2 packet shaping: UDP2[link]\n");
   58: 
   59:     l = libnet_init(
   60:             LIBNET_LINK,                            /* injection type */
   61:             NULL,                                   /* network interface */
   62:             errbuf);                                /* errbuf */
   63:  
   64:     if (l == NULL)
   65:     {
   66:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
   67:         exit(EXIT_FAILURE);
   68:     }
   69: 
   70:     src_ip = 0;
   71:     dst_ip = 0;
   72:     payload = NULL;
   73:     payload_s = 0;
   74:     plist_p = NULL;
   75:     while ((c = getopt(argc, argv, "d:s:p:P:")) != EOF)
   76:     {
   77:         switch (c)
   78:         {
   79:             case 'd':
   80:                 if ((dst_ip = libnet_name2addr4(l, optarg,
   81:                         LIBNET_RESOLVE)) == -1)
   82:                 {
   83:                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);                    
   84:                     exit(1);
   85:                 }
   86:                 break;
   87:             case 's':
   88:                 if ((src_ip = libnet_name2addr4(l, optarg,
   89:                         LIBNET_RESOLVE)) == -1)
   90:                 {
   91:                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
   92:                     exit(1);
   93:                 }
   94:                 break;
   95:             case 'P':
   96:                 plist_p = &plist;
   97:                 if (libnet_plist_chain_new(l, &plist_p, optarg) == -1)
   98:                 {
   99:                     fprintf(stderr, "Bad token in port list: %s\n",
  100:                             libnet_geterror(l));
  101:                     exit(1);
  102:                 }
  103:                 break;
  104:             case 'p':
  105:                 payload = optarg;
  106:                 payload_s = strlen(payload);
  107:                 break;
  108:             default:
  109:                 usage(argv[0]);
  110:                 exit(EXIT_FAILURE);
  111:         }
  112:     }
  113: 
  114:     if (!src_ip || !dst_ip || !plist_p)
  115:     {
  116:         usage(argv[0]);
  117:         exit(EXIT_FAILURE);
  118:     }
  119: 
  120:     udp = 0;
  121: #if !(__WIN32__)
  122:     gettimeofday(&s, NULL);
  123: #else
  124:     /* This obviously is not as good - but it compiles now. */
  125:     s.tv_sec = time(NULL);
  126:     s.tv_usec = 0;
  127: #endif
  128: 
  129:     build_ip = 1;
  130:     while (libnet_plist_chain_next_pair(plist_p, &bport, &eport))
  131:     {
  132:         while (!(bport > eport) && bport != 0)
  133:         {
  134:             cport = bport++;
  135:             udp = libnet_build_udp(
  136:                 1025,                               /* source port */
  137:                 cport,                              /* destination port */
  138:                 LIBNET_UDP_H + payload_s,           /* packet size */
  139:                 0,                                  /* checksum */
  140:                 (uint8_t *)payload,                 /* payload */
  141:                 payload_s,                          /* payload size */
  142:                 l,                                  /* libnet handle */
  143:                 udp);                               /* libnet id */
  144:             if (udp == -1)
  145:             {
  146:                 fprintf(stderr, "Can't build UDP header (at port %d): %s\n", 
  147:                         cport, libnet_geterror(l));
  148:                 goto bad;
  149:             }
  150:     if (build_ip)
  151:     {
  152:         build_ip = 0;
  153:         t = libnet_build_ipv4(
  154:             LIBNET_IPV4_H + LIBNET_UDP_H + payload_s,   /* length */
  155:             0,                                          /* TOS */
  156:             242,                                        /* IP ID */
  157:             0,                                          /* IP Frag */
  158:             64,                                         /* TTL */
  159:             IPPROTO_UDP,                                /* protocol */
  160:             0,                                          /* checksum */
  161:             src_ip,                                     /* source IP */
  162:             dst_ip,                                     /* destination IP */
  163:             NULL,                                       /* payload */
  164:             0,                                          /* payload size */
  165:             l,                                          /* libnet handle */
  166:             0);
  167:         if (t == -1)
  168:         {
  169:             fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
  170:             goto bad;
  171:         }
  172: 
  173:         t = libnet_build_ethernet(
  174:             enet_dst,                                   /* ethernet dest */
  175:             enet_src,                                   /* ethernet source */
  176:             ETHERTYPE_IP,                               /* protocol type */
  177:             NULL,                                       /* payload */
  178:             0,                                          /* payload size */
  179:             l,                                          /* libnet handle */
  180:             0);
  181:         if (t == -1)
  182:         {
  183:             fprintf(stderr, "Can't build ethernet header: %s\n",
  184:                     libnet_geterror(l));
  185:             goto bad;
  186:         }
  187:     }
  188:             c = libnet_write(l); 
  189:             if (c == -1)
  190:             {
  191:                 fprintf(stderr, "write error: %s\n", libnet_geterror(l));
  192:             }
  193:             else
  194:             {
  195:                fprintf(stderr, "wrote %d byte UDP packet to port %d\r", c, 
  196:                        cport);
  197:             }
  198:         }
  199:         fprintf(stderr, "\n");
  200:     }
  201: 
  202: #if !(__WIN32__)
  203:     gettimeofday(&e, NULL);
  204: #else
  205:     /* This obviously is not as good - but it compiles now. */
  206:     s.tv_sec = time(NULL);
  207:     s.tv_usec = 0;
  208: #endif
  209: 
  210:     libnet_timersub(&e, &s, &r);
  211:     fprintf(stderr, "Total time spent in loop: %d.%d\n", (int)r.tv_sec,
  212: 		    (int)r.tv_usec);
  213: 
  214:     libnet_stats(l, &ls);
  215:     fprintf(stderr, "Packets sent:  %lld\n"
  216:                     "Packet errors: %lld\n"
  217:                     "Bytes written: %lld\n",
  218:                     (long long)ls.packets_sent, (long long)ls.packet_errors,
  219: 		    (long long)ls.bytes_written);
  220:     libnet_destroy(l);
  221:     return (EXIT_SUCCESS);
  222: bad:
  223:     libnet_destroy(l);
  224:     return (EXIT_FAILURE);
  225: }
  226: 
  227: 
  228: void
  229: usage(char *name)
  230: {
  231:     fprintf(stderr, "usage: %s -s s_ip -d d_ip -P port list [-p payload]\n", name);
  232: }
  233: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>