"Fossies" - the Fresh Open Source Software Archive

Member "fake-1.1.11/send_arp/send_arp.c" (8 Jul 2009, 3709 Bytes) of package /linux/misc/old/fake-1.1.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "send_arp.c" see the Fossies "Dox" file reference documentation.

    1 /* send_arp.c
    2 
    3 This program sends out one ARP packet with source/target IP and Ethernet
    4 hardware addresses suuplied by the user.  It compiles and works on Linux
    5 and will probably work on any Unix that has SOCK_PACKET.
    6 
    7 The idea behind this program is a proof of a concept, nothing more.  It
    8 comes as is, no warranty.  However, you're allowed to use it under one
    9 condition: you must use your brain simultaneously.  If this condition is
   10 not met, you shall forget about this program and go RTFM immediately.
   11 
   12 yuri volobuev'97
   13 volobuev@t1.chem.umn.edu
   14 
   15 */
   16 
   17 #include <stdio.h>
   18 #include <ctype.h>
   19 #include <stdlib.h>
   20 #include <string.h>
   21 #include <errno.h>
   22 #include <netdb.h>
   23 #include <sys/socket.h>
   24 #include <linux/in.h>
   25 #include <arpa/inet.h>
   26 #include <linux/if_ether.h>
   27 
   28 #define ETH_HW_ADDR_LEN 6
   29 #define IP_ADDR_LEN 4
   30 #define ARP_FRAME_TYPE 0x0806
   31 #define ETHER_HW_TYPE 1
   32 #define IP_PROTO_TYPE 0x0800
   33 #define OP_ARP_REQUEST 2
   34 
   35 #define DEFAULT_DEVICE "eth0"
   36 
   37 char usage[]={"send_arp: sends out custom ARP packet. yuri volobuev'97\n\
   38 \tusage: send_arp src_ip_addr src_hw_addr targ_ip_addr tar_hw_addr\n\n"};
   39 
   40 struct arp_packet {
   41         u_char targ_hw_addr[ETH_HW_ADDR_LEN];
   42         u_char src_hw_addr[ETH_HW_ADDR_LEN];
   43         u_short frame_type;
   44         u_short hw_type;
   45         u_short prot_type;
   46         u_char hw_addr_size;
   47         u_char prot_addr_size;
   48         u_short op;
   49         u_char sndr_hw_addr[ETH_HW_ADDR_LEN];
   50         u_char sndr_ip_addr[IP_ADDR_LEN];
   51         u_char rcpt_hw_addr[ETH_HW_ADDR_LEN];
   52         u_char rcpt_ip_addr[IP_ADDR_LEN];
   53         u_char padding[18];
   54 };
   55 
   56 void die(char *);
   57 void get_ip_addr(struct in_addr*,char*);
   58 void get_hw_addr(char*,char*);
   59 
   60 int main(int argc,char** argv){
   61 
   62 struct in_addr src_in_addr,targ_in_addr;
   63 struct arp_packet pkt;
   64 struct sockaddr sa;
   65 int sock;
   66 
   67 if(argc != 5)die(usage);
   68 
   69 sock=socket(AF_INET,SOCK_PACKET,htons(ETH_P_RARP));
   70 if(sock<0){
   71         perror("socket");
   72         exit(1);
   73         }
   74 
   75 pkt.frame_type = htons(ARP_FRAME_TYPE);
   76 pkt.hw_type = htons(ETHER_HW_TYPE);
   77 pkt.prot_type = htons(IP_PROTO_TYPE);
   78 pkt.hw_addr_size = ETH_HW_ADDR_LEN;
   79 pkt.prot_addr_size = IP_ADDR_LEN;
   80 pkt.op=htons(OP_ARP_REQUEST);
   81 
   82 get_hw_addr(pkt.targ_hw_addr,argv[4]);
   83 get_hw_addr(pkt.rcpt_hw_addr,argv[4]);
   84 get_hw_addr(pkt.src_hw_addr,argv[2]);
   85 get_hw_addr(pkt.sndr_hw_addr,argv[2]);
   86 
   87 get_ip_addr(&src_in_addr,argv[1]);
   88 get_ip_addr(&targ_in_addr,argv[3]);
   89 
   90 memcpy(pkt.sndr_ip_addr,&src_in_addr,IP_ADDR_LEN);
   91 memcpy(pkt.rcpt_ip_addr,&targ_in_addr,IP_ADDR_LEN);
   92 
   93 bzero(pkt.padding,18);
   94 
   95 strcpy(sa.sa_data,DEFAULT_DEVICE);
   96 if(sendto(sock,&pkt,sizeof(pkt),0,&sa,sizeof(sa)) < 0){
   97         perror("sendto");
   98         exit(1);
   99         }
  100 exit(0);
  101 }
  102 
  103 void die(char* str){
  104 fprintf(stderr,"%s\n",str);
  105 exit(1);
  106 }
  107 
  108 void get_ip_addr(struct in_addr* in_addr,char* str){
  109 
  110 struct hostent *hostp;
  111 
  112 in_addr->s_addr=inet_addr(str);
  113 if(in_addr->s_addr == -1){
  114         if( (hostp = gethostbyname(str)))
  115                 bcopy(hostp->h_addr,in_addr,hostp->h_length);
  116         else {
  117                 fprintf(stderr,"send_arp: unknown host %s\n",str);
  118                 exit(1);
  119                 }
  120         }
  121 }
  122 
  123 void get_hw_addr(char* buf,char* str){
  124 
  125 int i;
  126 char c,val;
  127 
  128 for(i=0;i<ETH_HW_ADDR_LEN;i++){
  129         if( !(c = tolower(*str++))) die("Invalid hardware address");
  130         if(isdigit(c)) val = c-'0';
  131         else if(c >= 'a' && c <= 'f') val = c-'a'+10;
  132         else die("Invalid hardware address");
  133 
  134         *buf = val << 4;
  135         if( !(c = tolower(*str++))) die("Invalid hardware address");
  136         if(isdigit(c)) val = c-'0';
  137         else if(c >= 'a' && c <= 'f') val = c-'a'+10;
  138         else die("Invalid hardware address");
  139 
  140         *buf++ |= val;
  141 
  142         if(*str == ':')str++;
  143         }
  144 }
  145