Annotation of embedaddon/libnet/sample/ip_link.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  *
        !             3:  * libnet 1.1
        !             4:  * Build a IPv4 packet with what you want as payload
        !             5:  *
        !             6:  * Copyright (c) 2003 Frédéric Raynal <pappy@security-labs.org>
        !             7:  * All rights reserved.
        !             8:  *
        !             9:  * Ex:
        !            10:  * - send an UDP packet from port 4369 to port 8738
        !            11:  *    ./ip -s 1.1.1.1 -d 2.2.2.2
        !            12:  *
        !            13:  * - send a TCP SYN from port 4369 to port 8738
        !            14:  *   ./ip -s 1.1.1.1 -d 2.2.2.2 -t -p `printf "\x04\x57\x08\xae\x01\x01\x01\x01\x02\x02\x02\x02\x50\x02\x7f\xff\xd5\x91\x41\x41"`
        !            15:  *
        !            16:  *
        !            17:  * Redistribution and use in source and binary forms, with or without
        !            18:  * modification, are permitted provided that the following conditions
        !            19:  * are met:
        !            20:  * 1. Redistributions of source code must retain the above copyright
        !            21:  *    notice, this list of conditions and the following disclaimer.
        !            22:  * 2. Redistributions in binary form must reproduce the above copyright
        !            23:  *    notice, this list of conditions and the following disclaimer in the
        !            24:  *    documentation and/or other materials provided with the distribution.
        !            25:  *
        !            26:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        !            27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        !            30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            36:  * SUCH DAMAGE.
        !            37:  *
        !            38:  */
        !            39: #if (HAVE_CONFIG_H)
        !            40: #include "../include/config.h"
        !            41: #endif
        !            42: #include "./libnet_test.h"
        !            43: 
        !            44: int
        !            45: main(int argc, char *argv[])
        !            46: {
        !            47:     int c;
        !            48:     libnet_t *l;
        !            49:     char *device = NULL;
        !            50:     char *dst = "2.2.2.2", *src = "1.1.1.1";
        !            51:     char *hwdst = "22:22:22:22:22:22", *hwsrc = "11:11:11:11:11:11";
        !            52:     u_long src_ip, dst_ip;
        !            53:     char errbuf[LIBNET_ERRBUF_SIZE];
        !            54:     libnet_ptag_t ip_ptag = 0;
        !            55:     libnet_ptag_t eth_ptag = 0;
        !            56:     u_short proto = IPPROTO_UDP;
        !            57:     u_char payload[255] = {0x11, 0x11, 0x22, 0x22, 0x00, 0x08, 0xc6, 0xa5};
        !            58:     u_long payload_s = 8;
        !            59: 
        !            60:     printf("libnet 1.1 packet shaping: IP + payload[link]\n");
        !            61: 
        !            62:     while ((c = getopt(argc, argv, "d:s:D:S:tp:i:h")) != EOF)
        !            63:     {
        !            64:         switch (c)
        !            65:         {
        !            66:            case 'D':
        !            67:                hwdst = optarg;
        !            68:                break;
        !            69: 
        !            70:            case 'S':
        !            71:                hwsrc = optarg;
        !            72:                break;
        !            73: 
        !            74:             case 'd':
        !            75:                dst = optarg;
        !            76:                 break;
        !            77: 
        !            78:             case 's':
        !            79:                src = optarg;
        !            80:                 break;
        !            81: 
        !            82:            case 'i':
        !            83:                device = optarg;
        !            84:                break;
        !            85: 
        !            86:            case 't':
        !            87:                proto = IPPROTO_TCP;
        !            88:                break;
        !            89: 
        !            90:            case 'p':
        !            91:                strncpy(payload, optarg, sizeof(payload)-1);
        !            92:                payload_s = strlen(payload);
        !            93:                break;
        !            94: 
        !            95:            case 'h':
        !            96:                usage(argv[0]);
        !            97:                exit(EXIT_SUCCESS);
        !            98: 
        !            99:             default:
        !           100:                 exit(EXIT_FAILURE);
        !           101:         }
        !           102:     }
        !           103: 
        !           104: 
        !           105:     /*
        !           106:      *  Initialize the library.  Root priviledges are required.
        !           107:      */
        !           108:     l = libnet_init(
        !           109:            LIBNET_LINK,                            /* injection type */
        !           110:            device,                                 /* network interface */
        !           111:             errbuf);                                /* error buffer */
        !           112: 
        !           113:     printf("Using device %s\n", l->device);
        !           114: 
        !           115:     if (l == NULL)
        !           116:     {
        !           117:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
        !           118:         exit(EXIT_FAILURE); 
        !           119:     }
        !           120: 
        !           121:     if ((dst_ip = libnet_name2addr4(l, dst, LIBNET_RESOLVE)) == -1)
        !           122:     {
        !           123:        fprintf(stderr, "Bad destination IP address: %s\n", dst);
        !           124:        exit(EXIT_FAILURE);
        !           125:     }
        !           126:     
        !           127:     if ((src_ip = libnet_name2addr4(l, src, LIBNET_RESOLVE)) == -1)
        !           128:     {
        !           129:        fprintf(stderr, "Bad source IP address: %s\n", src);
        !           130:        exit(EXIT_FAILURE);
        !           131:     }
        !           132:     
        !           133:     /*
        !           134:      * Build the packet
        !           135:      */ 
        !           136:     ip_ptag = libnet_build_ipv4(
        !           137:         LIBNET_IPV4_H + payload_s,                  /* length */
        !           138:         0,                                          /* TOS */
        !           139:         242,                                        /* IP ID */
        !           140:         0,                                          /* IP Frag */
        !           141:         64,                                         /* TTL */
        !           142:         proto,                                      /* protocol */
        !           143:         0,                                          /* checksum */
        !           144:         src_ip,                                     /* source IP */
        !           145:         dst_ip,                                     /* destination IP */
        !           146:         payload,                                    /* payload */
        !           147:         payload_s,                                  /* payload size */
        !           148:         l,                                          /* libnet handle */
        !           149:         ip_ptag);                                   /* libnet id */
        !           150:     if (ip_ptag == -1)
        !           151:     {
        !           152:         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
        !           153:         goto bad;
        !           154:     }
        !           155: 
        !           156:     eth_ptag = libnet_build_ethernet(
        !           157:         hwdst,                                      /* ethernet destination */
        !           158:         hwsrc,                                      /* ethernet source */
        !           159:         ETHERTYPE_IP,                               /* protocol type */
        !           160:         NULL,                                       /* payload */
        !           161:         0,                                          /* payload size */
        !           162:         l,                                          /* libnet handle */
        !           163:         0);                                         /* libnet id */
        !           164:     if (eth_ptag == -1)
        !           165:     {
        !           166:         fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l));
        !           167:         goto bad;
        !           168:     }
        !           169: 
        !           170:     /*
        !           171:      *  Write it to the wire.
        !           172:      */
        !           173:     c = libnet_write(l);
        !           174:     if (c == -1)
        !           175:     {
        !           176:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        !           177:         goto bad;
        !           178:     }
        !           179:     else
        !           180:     {
        !           181:         fprintf(stderr, "Wrote %d byte IP packet; check the wire.\n", c);
        !           182:     }
        !           183: 
        !           184:     libnet_destroy(l);
        !           185:     return (EXIT_SUCCESS);
        !           186: bad:
        !           187:     libnet_destroy(l);
        !           188:     return (EXIT_FAILURE);
        !           189: }
        !           190: 
        !           191: void
        !           192: usage(char *name)
        !           193: {
        !           194:     fprintf(stderr,
        !           195:         "usage: %s [-s source_ip] [-d destination_ip]"
        !           196:            " [-S HW src] [-D HW dst]"
        !           197:            " [-i iface] [-p payload] [-t]\n",
        !           198:            name);
        !           199: }
        !           200: 
        !           201: /* EOF */

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