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

1.1     ! misho       1: /*
        !             2:  *  $Id: icmp_timestamp.c,v 1.2 2004/01/03 20:31:01 mike Exp $
        !             3:  *
        !             4:  *  libnet 1.1
        !             5:  *  Build an ICMP_TSTAMP 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: 
        !            38: int
        !            39: main(int argc, char **argv)
        !            40: {
        !            41:     int c;
        !            42:     libnet_t *l;
        !            43:     libnet_ptag_t t;
        !            44:     u_long src_ip, dst_ip;
        !            45:     char errbuf[LIBNET_ERRBUF_SIZE];
        !            46: 
        !            47:     printf("libnet 1.1 packet shaping: ICMP timestamp[raw]\n");
        !            48: 
        !            49:     /*
        !            50:      *  Initialize the library.  Root priviledges are required.
        !            51:      */
        !            52:     l = libnet_init(
        !            53:             LIBNET_RAW4,                            /* injection type */
        !            54:             NULL,                                   /* network interface */
        !            55:             errbuf);                                /* errbuf */
        !            56:  
        !            57:     if (l == NULL)
        !            58:     {
        !            59:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
        !            60:         exit(EXIT_FAILURE);
        !            61:     }
        !            62: 
        !            63:     src_ip = 0;
        !            64:     dst_ip = 0;
        !            65:     while((c = getopt(argc, argv, "d:s:")) != EOF)
        !            66:     {
        !            67:         switch (c)
        !            68:         {
        !            69:             case 'd':
        !            70:                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
        !            71:                 {
        !            72:                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
        !            73:                     exit(1);
        !            74:                 }
        !            75:                 break;
        !            76:             case 's':
        !            77:                 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
        !            78:                 {
        !            79:                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
        !            80:                     exit(1);
        !            81:                 }
        !            82:                 break;
        !            83:         }
        !            84:     }
        !            85:     if (!src_ip || !dst_ip)
        !            86:     {
        !            87:         usage(argv[0]);
        !            88:         exit(EXIT_FAILURE);
        !            89:     }
        !            90: 
        !            91:     t = libnet_build_icmpv4_timestamp(
        !            92:         ICMP_TSTAMP,                                /* type */
        !            93:         0,                                          /* code */
        !            94:         0,                                          /* checksum */
        !            95:         242,                                        /* id */
        !            96:         424,                                        /* sequence number */
        !            97:         1000,                                       /* otime */
        !            98:         2000,                                       /* rtime */
        !            99:         3000,                                       /* ttime */
        !           100:         NULL,                                       /* payload */
        !           101:         0,                                          /* payload size */
        !           102:         l,                                          /* libnet handle */
        !           103:         0);
        !           104:     if (t == -1)
        !           105:     {
        !           106:         fprintf(stderr, "Can't build ICMP header: %s\n", libnet_geterror(l));
        !           107:         goto bad;
        !           108:     }
        !           109: 
        !           110:     t = libnet_build_ipv4(
        !           111:         LIBNET_IPV4_H + LIBNET_ICMPV4_TS_H,         /* length */
        !           112:         0,                                          /* TOS */
        !           113:         242,                                        /* IP ID */
        !           114:         0,                                          /* IP Frag */
        !           115:         64,                                         /* TTL */
        !           116:         IPPROTO_ICMP,                               /* protocol */
        !           117:         0,                                          /* checksum */
        !           118:         src_ip,                                     /* source IP */
        !           119:         dst_ip,                                     /* destination IP */
        !           120:         NULL,                                       /* payload */
        !           121:         0,                                          /* payload size */
        !           122:         l,                                          /* libnet handle */
        !           123:         0);
        !           124:     if (t == -1)
        !           125:     {
        !           126:         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
        !           127:         goto bad;
        !           128:     }
        !           129: 
        !           130:     /*
        !           131:      *  Write it to the wire.
        !           132:      */
        !           133:     c = libnet_write(l);
        !           134:     if (c == -1)
        !           135:     {
        !           136:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        !           137:         goto bad;
        !           138:     }
        !           139:     else
        !           140:     {
        !           141:         fprintf(stderr, "Wrote %d byte ICMP packet; check the wire.\n", c);
        !           142:     }
        !           143:     libnet_destroy(l);
        !           144:     return (EXIT_SUCCESS);
        !           145: bad:
        !           146:     libnet_destroy(l);
        !           147:     return (EXIT_FAILURE);
        !           148: }
        !           149: 
        !           150: 
        !           151: void
        !           152: usage(char *name)
        !           153: {
        !           154:     fprintf(stderr, "usage: %s -s source_ip -d destination_ip\n ", name);
        !           155: }
        !           156: 
        !           157: /* EOF */

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