File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / fddi_tcp1.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 11:54:41 2013 UTC (10 years, 11 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_1_6p5, v1_1_6p4, v1_1_6p0, v1_1_6, HEAD
1.1.6

    1: /*
    2:  *  libnet 1.1
    3:  *  Build a TCP packet to ride on FDDI
    4:  *
    5:  *  Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
    6:  *  All rights reserved.
    7:  *
    8:  * Redistribution and use in source and binary forms, with or without
    9:  * modification, are permitted provided that the following conditions
   10:  * are met:
   11:  * 1. Redistributions of source code must retain the above copyright
   12:  *    notice, this list of conditions and the following disclaimer.
   13:  * 2. Redistributions in binary form must reproduce the above copyright
   14:  *    notice, this list of conditions and the following disclaimer in the
   15:  *    documentation and/or other materials provided with the distribution.
   16:  *
   17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27:  * SUCH DAMAGE.
   28:  *
   29:  */
   30: 
   31: #if (HAVE_CONFIG_H)
   32: #include "../include/config.h"
   33: #endif
   34: #include "./libnet_test.h"
   35: 
   36: int
   37: main(int argc, char *argv[])
   38: {
   39:     int c;
   40:     char *cp;
   41:     libnet_t *l;
   42:     libnet_ptag_t t;
   43:     char *payload;
   44:     u_short payload_s;
   45:     u_long src_ip, dst_ip;
   46:     u_short src_prt, dst_prt;
   47:     char errbuf[LIBNET_ERRBUF_SIZE];
   48: 
   49:     printf("libnet 1.1 packet shaping: TCP (over FDDI) [link]\n");
   50: 
   51:     /*
   52:      *  Initialize the library.  Root priviledges are required.
   53:      *
   54:      *  Currently hardcoded for fddi0.
   55:      */
   56:     l = libnet_init(
   57:             LIBNET_LINK,                            /* injection type */
   58:             "fddi0",                                /* network interface */
   59:             errbuf);                                /* error buffer */
   60: 
   61:     if (l == NULL)
   62:     {
   63:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
   64:         exit(EXIT_FAILURE); 
   65:     }
   66: 
   67:     src_ip  = 0;
   68:     dst_ip  = 0;
   69:     src_prt = 0;
   70:     dst_prt = 0;
   71:     payload = NULL;
   72:     payload_s = 0;
   73:     while ((c = getopt(argc, argv, "d:s:p:")) != EOF)
   74:     {
   75:         switch (c)
   76:         {
   77:             /*
   78:              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
   79:              *  point cp to the last dot of the IP address/port string and
   80:              *  then seperate them with a NULL byte.  The optarg now points to
   81:              *  just the IP address, and cp points to the port.
   82:              */
   83:             case 'd':
   84:                 if (!(cp = strrchr(optarg, '.')))
   85:                 {
   86:                     usage(argv[0]);
   87:                 }
   88:                 *cp++ = 0;
   89:                 dst_prt = (u_short)atoi(cp);
   90:                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
   91:                 {
   92:                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
   93:                     exit(EXIT_FAILURE);
   94:                 }
   95:                 break;
   96:             case 's':
   97:                 if (!(cp = strrchr(optarg, '.')))
   98:                 {
   99:                     usage(argv[0]);
  100:                 }
  101:                 *cp++ = 0;
  102:                 src_prt = (u_short)atoi(cp);
  103:                 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
  104:                 {
  105:                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
  106:                     exit(EXIT_FAILURE);
  107:                 }
  108:                 break;
  109:             case 'p':
  110:                 payload = optarg;
  111:                 payload_s = strlen(payload);
  112:                 break;
  113:             default:
  114:                 exit(EXIT_FAILURE);
  115:         }
  116:     }
  117: 
  118:     if (!src_ip || !src_prt || !dst_ip || !dst_prt)
  119:     {
  120:         usage(argv[0]);
  121:         exit(EXIT_FAILURE);
  122:     }
  123: 
  124: 
  125:     t = libnet_build_tcp(
  126:         src_prt,                                    /* source port */
  127:         dst_prt,                                    /* destination port */
  128:         0x01010101,                                 /* sequence number */
  129:         0x02020202,                                 /* acknowledgement num */
  130:         TH_SYN,                                     /* control flags */
  131:         32767,                                      /* window size */
  132:         0,                                          /* checksum */
  133:         0,                                          /* urgent pointer */
  134:         LIBNET_TCP_H + payload_s,                   /* TCP packet size */
  135:         payload,                                    /* payload */
  136:         payload_s,                                  /* payload size */
  137:         l,                                          /* libnet handle */
  138:         0);                                         /* libnet id */
  139:     if (t == -1)
  140:     {
  141:         fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
  142:         goto bad;
  143:     }
  144: 
  145:     t = libnet_build_ipv4(
  146:         LIBNET_IPV4_H + LIBNET_TCP_H + payload_s,   /* length */
  147:         0,                                          /* TOS */
  148:         242,                                        /* IP ID */
  149:         0,                                          /* IP Frag */
  150:         64,                                         /* TTL */
  151:         IPPROTO_TCP,                                /* protocol */
  152:         0,                                          /* checksum */
  153:         src_ip,                                     /* source IP */
  154:         dst_ip,                                     /* destination IP */
  155:         NULL,                                       /* payload */
  156:         0,                                          /* payload size */
  157:         l,                                          /* libnet handle */
  158:         0);                                         /* libnet id */
  159:     if (t == -1)
  160:     {
  161:         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
  162:         goto bad;
  163:     }
  164: 
  165:     t = libnet_build_fddi(
  166:         LIBNET_FDDI_FC_REQD | 0x04,                 /* Asynch LLC - priority 4 */
  167:         fddi_dst,                                   /* fddi destination */
  168:         fddi_src,                                   /* fddi source */
  169:         LIBNET_SAP_SNAP,                            /* DSAP -> SNAP encap */
  170:         LIBNET_SAP_SNAP,                            /* SSAP -> SNAP encap */
  171:         0x03,                                       /* Unnumbered info/frame */
  172:         org_code,                                   /* Organization Code */
  173:         FDDI_TYPE_IP,                                /* protocol type */
  174:         NULL,                                       /* payload */
  175:         0,                                          /* payload size */
  176:         l,                                          /* libnet handle */
  177:         0);                                         /* libnet id */
  178:     if (t == -1)
  179:     {
  180:         fprintf(stderr, "Can't build fddi header: %s\n", libnet_geterror(l));
  181:         goto bad;
  182:     }
  183: 
  184:     /*
  185:      *  Write it to the wire.
  186:      */
  187:     c = libnet_write(l);
  188:     if (c == -1)
  189:     {
  190:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
  191:         goto bad;
  192:     }
  193:     else
  194:     {
  195:         fprintf(stderr, "Wrote %d byte TCP packet; check the wire.\n", c);
  196:     }
  197:     libnet_destroy(l);
  198:     return (EXIT_SUCCESS);
  199: bad:
  200:     libnet_destroy(l);
  201:     return (EXIT_FAILURE);
  202: }
  203: 
  204: void
  205: usage(char *name)
  206: {
  207:     fprintf(stderr,
  208:         "usage: %s -s source_ip.source_port -d destination_ip.destination_port"
  209:         " [-p payload]\n",
  210:         name);
  211: }
  212: 
  213: /* EOF */

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