File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / tcp1.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 (8 months, 4 weeks ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

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

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