/* * $Id: arp.c,v 1.5 2004/01/28 19:45:00 mike Exp $ * * libnet 1.1 * Build an ARP packet * * Copyright (c) 1998 - 2004 Mike D. Schiffman * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ // some modified by hoppang #include int main(int argc, char *argv[]) { int c; // 반환값 체크용 변수. u_int32_t src, dst; // 발신자/수신자 IP 주소를 저장. libnet_t *l; // libnet 객체 libnet_ptag_t t; // protocol tag..라는데. char *device = NULL; // 네트워크 카드 이름..을 저장 u_int8_t *packet; // 패킷 데이터 u_int32_t packet_s; // 패킷 길이. char errbuf[LIBNET_ERRBUF_SIZE]; // 에러메시지 저장용 libnet_ether_addr* src_mac_addr; // 내 컴퓨터의 mac 주소를 담을 변수. // 브로드캐스팅은 전부 0xFF다. 숫자가 다 똑같으니 바이트오더 걱정할 필요도 없구나. u_char broadcast_mac[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; printf("libnet 1.1 packet shaping: ARP[link -- autobuilding ethernet]\n"); if (argc > 1) { device = argv[1]; } // libpcap이 그렇듯이, 관리자 권한을 얻지 못하면 실패한다. l = libnet_init( LIBNET_LINK_ADV, /* injection type */ device, /* network interface */ errbuf); /* errbuf */ if (l == NULL) { fprintf(stderr, "%s", errbuf); exit(EXIT_FAILURE); } else /* * Build the packet, remmebering that order IS important. We must * build the packet from lowest protocol type on up as it would * appear on the wire. So for our ARP packet: * * ------------------------------------------- * | Ethernet | ARP | * ------------------------------------------- * ^ ^ * |------------------ | * libnet_build_ethernet()--| | * | * libnet_build_arp()-----------| */ // 내 컴퓨터의 IP주소 구하기. src = libnet_get_ipaddr4(l); // 이 부분은 문제가 좀 있는데.. 인텔 CPU와 같은 바이트오더(LE)에서만 값이 제대로 들어간다. // PPC나 뭐.. 여튼 문제가 가능성이 많지만 귀찮으니까 그냥 써야지. // 192.168.1.101(내 놋북 IP) dst = (192) + (168<<8) + (1<<16) + (101<<24); // 내 컴퓨터의 mac 주소 구하기. src_mac_addr = libnet_get_hwaddr(l); t = libnet_autobuild_arp( ARPOP_REQUEST, /* operation type */ src_mac_addr->ether_addr_octet, /* sender hardware addr */ (u_int8_t *)&src, /* sender protocol addr */ broadcast_mac, /* target hardware addr */ (u_int8_t *)&dst, /* target protocol addr */ l); /* libnet context */ // 이 밑으로는 전부 arp_new.c와 같음. if (t == -1) { fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(l)); goto bad; } t = libnet_autobuild_ethernet( broadcast_mac, /* ethernet destination */ ETHERTYPE_ARP, /* protocol type */ l); /* libnet handle */ if (t == -1) { fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(l)); goto bad; } if (libnet_adv_cull_packet(l, &packet, &packet_s) == -1) { fprintf(stderr, "%s", libnet_geterror(l)); } else { fprintf(stderr, "packet size: %d\n", packet_s); libnet_adv_free_packet(l, packet); } c = libnet_write(l); if (c == -1) { fprintf(stderr, "Write error: %s\n", libnet_geterror(l)); goto bad; } else { fprintf(stderr, "Wrote %d byte ARP packet from context \"%s\"; " "check the wire.\n", c, libnet_cq_getlabel(l)); } libnet_destroy(l); return (EXIT_SUCCESS); bad: libnet_destroy(l); return (EXIT_FAILURE); } /* EOF */