File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / 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:  *  $Id: tcp1.c,v 1.1.1.2 2013/07/22 11:54:41 misho Exp $
    3:  *
    4:  *  libnet 1.1
    5:  *  Build a TCP packet
    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: #ifdef __WIN32__
   38: #include "../include/win32/getopt.h"
   39: #endif
   40: 
   41: int
   42: main(int argc, char *argv[])
   43: {
   44:     int c;
   45:     char *cp;
   46:     libnet_t *l;
   47:     libnet_ptag_t t;
   48:     char *payload;
   49:     u_short payload_s;
   50:     u_long src_ip, dst_ip;
   51:     u_short src_prt, dst_prt;
   52:     char errbuf[LIBNET_ERRBUF_SIZE];
   53: 
   54:     printf("libnet 1.1 packet shaping: TCP + options[link]\n");
   55: 
   56:     /*
   57:      *  Initialize the library.  Root priviledges are required.
   58:      */
   59:     l = libnet_init(
   60:             LIBNET_LINK,                            /* injection type */
   61:             NULL,                                   /* network interface */
   62:             errbuf);                                /* error buffer */
   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:     src_prt = 0;
   73:     dst_prt = 0;
   74:     payload = NULL;
   75:     payload_s = 0;
   76:     while ((c = getopt(argc, argv, "d:s:p:")) != EOF)
   77:     {
   78:         switch (c)
   79:         {
   80:             /*
   81:              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
   82:              *  point cp to the last dot of the IP address/port string and
   83:              *  then seperate them with a NULL byte.  The optarg now points to
   84:              *  just the IP address, and cp points to the port.
   85:              */
   86:             case 'd':
   87:                 if (!(cp = strrchr(optarg, '.')))
   88:                 {
   89:                     usage(argv[0]);
   90:                 }
   91:                 *cp++ = 0;
   92:                 dst_prt = (u_short)atoi(cp);
   93:                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
   94:                 {
   95:                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
   96:                     exit(EXIT_FAILURE);
   97:                 }
   98:                 break;
   99:             case 's':
  100:                 if (!(cp = strrchr(optarg, '.')))
  101:                 {
  102:                     usage(argv[0]);
  103:                 }
  104:                 *cp++ = 0;
  105:                 src_prt = (u_short)atoi(cp);
  106:                 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
  107:                 {
  108:                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
  109:                     exit(EXIT_FAILURE);
  110:                 }
  111:                 break;
  112:             case 'p':
  113:                 payload = optarg;
  114:                 payload_s = strlen(payload);
  115:                 break;
  116:             default:
  117:                 exit(EXIT_FAILURE);
  118:         }
  119:     }
  120: 
  121:     if (!src_ip || !src_prt || !dst_ip || !dst_prt)
  122:     {
  123:         usage(argv[0]);
  124:         exit(EXIT_FAILURE);
  125:     }
  126: 
  127:     t = libnet_build_tcp_options(
  128:         (uint8_t*)"\003\003\012\001\002\004\001\011\010\012\077\077\077\077\000\000\000\000\000\000",
  129:         20,
  130:         l,
  131:         0);
  132:     if (t == -1)
  133:     {
  134:         fprintf(stderr, "Can't build TCP options: %s\n", libnet_geterror(l));
  135:         goto bad;
  136:     }
  137: 
  138:     t = libnet_build_tcp(
  139:         src_prt,                                    /* source port */
  140:         dst_prt,                                    /* destination port */
  141:         0x01010101,                                 /* sequence number */
  142:         0x02020202,                                 /* acknowledgement num */
  143:         TH_SYN,                                     /* control flags */
  144:         32767,                                      /* window size */
  145:         0,                                          /* checksum */
  146:         10,                                          /* urgent pointer */
  147:         LIBNET_TCP_H + 20 + payload_s,              /* TCP packet size */
  148: 	(uint8_t*)payload,                         /* payload */
  149:         payload_s,                                  /* payload size */
  150:         l,                                          /* libnet handle */
  151:         0);                                         /* libnet id */
  152:     if (t == -1)
  153:     {
  154:         fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
  155:         goto bad;
  156:     }
  157: 	
  158:     t = libnet_build_ipv4(
  159:         LIBNET_IPV4_H + LIBNET_TCP_H + 20 + payload_s,/* length */
  160:       	0,                                          /* TOS */
  161:         242,                                        /* IP ID */
  162:         0,                                          /* IP Frag */
  163:         64,                                         /* TTL */
  164:         IPPROTO_TCP,                                /* protocol */
  165:         0,                                          /* checksum */
  166:         src_ip,                                     /* source IP */
  167:         dst_ip,                                     /* destination IP */
  168:         NULL,                                       /* payload */
  169:         0,                                          /* payload size */
  170:         l,                                          /* libnet handle */
  171:         0);                                         /* libnet id */
  172:     if (t == -1)
  173:     {
  174:         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
  175:         goto bad;
  176:     }
  177:   
  178:     t = libnet_build_ethernet(
  179:         enet_dst,                                   /* ethernet destination */
  180:         enet_src,                                   /* ethernet source */
  181:         ETHERTYPE_IP,                               /* protocol type */
  182:         NULL,                                       /* payload */
  183:         0,                                          /* payload size */
  184:         l,                                          /* libnet handle */
  185:         0);                                         /* libnet id */
  186:     if (t == -1)
  187:     {
  188:         fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
  189:         goto bad;
  190:     }
  191: 	
  192:     /*
  193:      *  Write it to the wire.
  194:      */
  195:     c = libnet_write(l);
  196:     if (c == -1)
  197:     {
  198:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
  199:         goto bad;
  200:    }
  201:    else
  202:    {
  203:        fprintf(stderr, "Wrote %d byte TCP packet; check the wire.\n", c);
  204:    }
  205: 
  206:     libnet_destroy(l);
  207:     return (EXIT_SUCCESS);
  208: bad:
  209:     libnet_destroy(l);
  210:     return (EXIT_FAILURE);
  211: }
  212: 
  213: void
  214: usage(char *name)
  215: {
  216:     fprintf(stderr,
  217:         "usage: %s -s source_ip.source_port -d destination_ip.destination_port"
  218:         " [-p payload]\n",
  219:         name);
  220: }
  221: 
  222: #if defined(__WIN32__)
  223: #include <../include/win32/getopt.h>
  224: #include <winsock2.h>
  225: #include <ws2tcpip.h>
  226: #endif  /* __WIN32__ */
  227: /* EOF */

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