Annotation of embedaddon/libnet/sample/bgp4_hdr.c, revision 1.1
1.1 ! misho 1: /*
! 2: *
! 3: * libnet 1.1
! 4: * Build a BGP4 header 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: *
! 10: * Example:
! 11: *
! 12: * Sending of a BGP KEEPALIVE
! 13: *
! 14: * # ./bgp4_hdr -s 1.1.1.1 -d 2.2.2.2 -t 4
! 15: * libnet 1.1 packet shaping: BGP4 hdr + payload[raw]
! 16: * Wrote 59 byte TCP packet; check the wire.
! 17: *
! 18: * 13:55:53.811579 1.1.1.1.26214 > 2.2.2.2.179: S [tcp sum ok]
! 19: * 16843009:16843028(19) win 32767: BGP (ttl 64, id 242, len 59)
! 20: * 0x0000 4500 003b 00f2 0000 4006 73c6 0101 0101 E..;....@.s.....
! 21: * 0x0010 0202 0202 6666 00b3 0101 0101 0202 0202 ....ff..........
! 22: * 0x0020 5002 7fff b090 0000 0101 0101 0101 0101 P...............
! 23: * 0x0030 0101 0101 0101 0101 0013 04 ...........
! 24: *
! 25: *
! 26: * Redistribution and use in source and binary forms, with or without
! 27: * modification, are permitted provided that the following conditions
! 28: * are met:
! 29: * 1. Redistributions of source code must retain the above copyright
! 30: * notice, this list of conditions and the following disclaimer.
! 31: * 2. Redistributions in binary form must reproduce the above copyright
! 32: * notice, this list of conditions and the following disclaimer in the
! 33: * documentation and/or other materials provided with the distribution.
! 34: *
! 35: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 36: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 37: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 38: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
! 39: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 40: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 41: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 42: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 43: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 44: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 45: * SUCH DAMAGE.
! 46: *
! 47: */
! 48: #if (HAVE_CONFIG_H)
! 49: #include "../include/config.h"
! 50: #endif
! 51: #include "./libnet_test.h"
! 52:
! 53: int
! 54: main(int argc, char *argv[])
! 55: {
! 56: int c;
! 57: libnet_t *l;
! 58: u_long src_ip, dst_ip, length;
! 59: libnet_ptag_t t = 0;
! 60: char errbuf[LIBNET_ERRBUF_SIZE];
! 61: u_char *payload = NULL;
! 62: u_long payload_s = 0;
! 63: u_char marker[LIBNET_BGP4_MARKER_SIZE];
! 64: u_char type;
! 65:
! 66: printf("libnet 1.1 packet shaping: BGP4 hdr + payload[raw]\n");
! 67:
! 68: /*
! 69: * Initialize the library. Root priviledges are required.
! 70: */
! 71: l = libnet_init(
! 72: LIBNET_RAW4, /* injection type */
! 73: NULL, /* network interface */
! 74: errbuf); /* error buffer */
! 75:
! 76: if (l == NULL)
! 77: {
! 78: fprintf(stderr, "libnet_init() failed: %s", errbuf);
! 79: exit(EXIT_FAILURE);
! 80: }
! 81:
! 82: src_ip = 0;
! 83: dst_ip = 0;
! 84: memset(marker, 0x1, LIBNET_BGP4_MARKER_SIZE);
! 85: type = 0;
! 86:
! 87: while ((c = getopt(argc, argv, "d:s:t:m:p:")) != EOF)
! 88: {
! 89: switch (c)
! 90: {
! 91: /*
! 92: * We expect the input to be of the form `ip.ip.ip.ip.port`. We
! 93: * point cp to the last dot of the IP address/port string and
! 94: * then seperate them with a NULL byte. The optarg now points to
! 95: * just the IP address, and cp points to the port.
! 96: */
! 97: case 'd':
! 98: if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
! 99: {
! 100: fprintf(stderr, "Bad destination IP address: %s\n", optarg);
! 101: exit(EXIT_FAILURE);
! 102: }
! 103: break;
! 104:
! 105: case 's':
! 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:
! 113: case 'm':
! 114: memcpy(marker, optarg, LIBNET_BGP4_MARKER_SIZE);
! 115: break;
! 116:
! 117: case 't':
! 118: type = atoi(optarg);
! 119: break;
! 120:
! 121: case 'p':
! 122: payload = optarg;
! 123: payload_s = strlen(optarg);
! 124: break;
! 125:
! 126: default:
! 127: exit(EXIT_FAILURE);
! 128: }
! 129: }
! 130:
! 131: if (!src_ip || !dst_ip)
! 132: {
! 133: usage(argv[0]);
! 134: exit(EXIT_FAILURE);
! 135: }
! 136:
! 137:
! 138: length = LIBNET_BGP4_HEADER_H + payload_s;
! 139: t = libnet_build_bgp4_header(
! 140: marker, /* marker */
! 141: length, /* length */
! 142: type, /* message type */
! 143: payload, /* payload */
! 144: payload_s, /* payload size */
! 145: l, /* libnet handle */
! 146: 0); /* libnet id */
! 147: if (t == -1)
! 148: {
! 149: fprintf(stderr, "Can't build BGP4 header: %s\n", libnet_geterror(l));
! 150: goto bad;
! 151: }
! 152:
! 153: length+=LIBNET_TCP_H;
! 154: t = libnet_build_tcp(
! 155: 0x6666, /* source port */
! 156: 179, /* destination port */
! 157: 0x01010101, /* sequence number */
! 158: 0x02020202, /* acknowledgement num */
! 159: TH_SYN, /* control flags */
! 160: 32767, /* window size */
! 161: 0, /* checksum */
! 162: 0, /* urgent pointer */
! 163: length, /* TCP packet size */
! 164: NULL, /* payload */
! 165: 0, /* payload size */
! 166: l, /* libnet handle */
! 167: 0); /* libnet id */
! 168: if (t == -1)
! 169: {
! 170: fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
! 171: goto bad;
! 172: }
! 173:
! 174: length+=LIBNET_IPV4_H;
! 175: t = libnet_build_ipv4(
! 176: length, /* length */
! 177: 0, /* TOS */
! 178: 242, /* IP ID */
! 179: 0, /* IP Frag */
! 180: 64, /* TTL */
! 181: IPPROTO_TCP, /* protocol */
! 182: 0, /* checksum */
! 183: src_ip, /* source IP */
! 184: dst_ip, /* destination IP */
! 185: NULL, /* payload */
! 186: 0, /* payload size */
! 187: l, /* libnet handle */
! 188: 0); /* libnet id */
! 189: if (t == -1)
! 190: {
! 191: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
! 192: goto bad;
! 193: }
! 194:
! 195: /*
! 196: * Write it to the wire.
! 197: */
! 198: c = libnet_write(l);
! 199: if (c == -1)
! 200: {
! 201: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
! 202: goto bad;
! 203: }
! 204: else
! 205: {
! 206: fprintf(stderr, "Wrote %d byte TCP packet; check the wire.\n", c);
! 207: }
! 208:
! 209: libnet_destroy(l);
! 210: return (EXIT_SUCCESS);
! 211: bad:
! 212: libnet_destroy(l);
! 213: return (EXIT_FAILURE);
! 214: }
! 215:
! 216: void
! 217: usage(char *name)
! 218: {
! 219: fprintf(stderr,
! 220: "usage: %s -s source_ip -d destination_ip"
! 221: " [-m marker] [-p payload] [-t BGP type]\n",
! 222: name);
! 223: }
! 224:
! 225: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>