File: ARP.xs

package info (click to toggle)
libnet-arp-perl 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 180 kB
  • ctags: 181
  • sloc: ansic: 297; perl: 41; makefile: 26
file content (233 lines) | stat: -rw-r--r-- 5,534 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Perl ARP Extension
Create and send an arp packets, lookup mac addresses

Programmed by Bastian Ballmann
Last update: 31.01.2007

This program is free software; you can redistribute 
it and/or modify it under the terms of the 
GNU General Public License version 2 as published 
by the Free Software Foundation.

This program is distributed in the hope that it will 
be useful, but WITHOUT ANY WARRANTY; without even 
the implied warranty of MERCHANTABILITY or FITNESS 
FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details. 
*/

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"
#include <stdio.h>           
#include <stdlib.h>          
#include <string.h>          
#include <errno.h>           
#include <net/ethernet.h>    
#include <netinet/ether.h>
#include <net/if.h>
#include <arpa/inet.h>       
#include "arp.h"

MODULE = Net::ARP		PACKAGE = Net::ARP		

int
send_packet(dev, sip, dip, smac, dmac, type)
	unsigned char *dev;
	unsigned char *sip;
	unsigned char *dip;
	unsigned char *smac;
	unsigned char *dmac;
	unsigned char *type;

	CODE:
	  int uid;
	  unsigned int packetsize = sizeof(struct my_arphdr) + sizeof(struct ether_header);
	  unsigned char packet[packetsize];
	  struct ether_header *ethhdr = (struct ether_header *)packet;
	  struct my_arphdr *arp = (struct my_arphdr *)(packet + sizeof(struct ether_header));
	  u_short op;	
	  in_addr_t ipaddr;

          RETVAL = 1;

	  // Are you root?
	  uid = getuid();
	  if(uid != 0) 
	  { 
	    printf("You must have UID 0 instead of %d.\n",uid); 
	    exit(0);
	  }

	  // Initialize packet buffer 
	  memset(packet,0,packetsize);

	  // What's the ARP operation type?
	  if(!strcmp(type,"request"))
	  {
	    op = ARPOP_REQUEST;
	  }
	  else if(!strcmp(type,"reply"))
	  {
	    op = ARPOP_REPLY;
	  }
	  else if(!strcmp(type,"revrequest"))
	  {
	    op = ARPOP_REVREQUEST;
	  }
	  else if(!strcmp(type,"revreply"))
	  {
	    op = ARPOP_REVREPLY;
	  }
	  else if(!strcmp(type,"invrequest"))
	  {
	    op = ARPOP_INVREQUEST;
	  }
	  else if(!strcmp(type,"invreply"))
	  {
	    op = ARPOP_INVREPLY;
	  }
	  else
	  {
	    op = ARPOP_REPLY;
	  }

	  if(smac == NULL)
	  {
	    printf("Parameter smac is NULL! Terminating.\n");
	    RETVAL = 0;
	  }

	  if(dmac == NULL)
	  {
	    printf("Parameter dmac is NULL! Terminating.\n");
	    RETVAL = 0;
	  }

          // Found a dollar sign?
	  if(strchr(smac,36))
	  {
	    printf("Found a $ char in smac! Terminating.\n");
	    RETVAL = 0;
	  }

	  if(strchr(dmac,36))
	  {
	    printf("Found a $ char in dmac! Terminating.\n");
	    RETVAL = 0;
	  }

          if(ether_aton(smac) == NULL)
          {
            printf("Invalid source mac address! Terminating.\n");
            RETVAL = 0;
          }

          if(ether_aton(dmac) == NULL)
          {
            printf("Invalid destination mac address! Terminating.\n");
            RETVAL = 0;
          }

          // Check ips
          if(inet_addr(sip) == INADDR_NONE)
          {
            printf("Invalid source ip address! Terminating.\n");
            RETVAL = 0;
          }

          if(inet_addr(dip) == INADDR_NONE)
          {
            printf("Invalid destination ip address! Terminating.\n");
            RETVAL = 0;
          }

          // Construct and send packet
          if(RETVAL != 0)
          {
	  	// Ethernet header
	        memcpy(ethhdr->ether_dhost,(u_char *)ether_aton(dmac),ETHER_ADDR_LEN); // Destination MAC
	        memcpy(ethhdr->ether_shost,(u_char *)ether_aton(smac),ETHER_ADDR_LEN); // Source MAC
	        ethhdr->ether_type = htons(ETHERTYPE_ARP);                             // ARP protocol

	        // ARP header
	        arp->hw_type = htons(ARPHDR_ETHER);                                    // Hardware address type
	        arp->proto_type = htons(ETH_P_IP);                                     // Protocol address type
	        arp->ha_len = ETH_ALEN;                                                // Hardware address length
	        arp->pa_len = IP_ALEN;                                                 // Protocol address length
	        arp->opcode = htons(op);                                               // ARP operation
	        memcpy(arp->source_add,(u_char *)ether_aton(smac),ETH_ALEN);           // Source MAC
		ipaddr = inet_addr(sip);
		memcpy(arp->source_ip, (u_char *)&ipaddr, IP_ALEN);		       // Source IP

	        if(strcmp(dmac,"ff:ff:ff:ff:ff:ff"))
          		memcpy(arp->dest_add,(u_char *)ether_aton(dmac),ETH_ALEN);     // Destination MAC

		ipaddr = inet_addr(dip);
		memcpy(arp->dest_ip, (u_char *)&ipaddr, IP_ALEN);		       // Destination IP



	         // Run packet!! Run!
	         // FreeBSD code
	         if(SOCK_TYPE == SOCK_RAW)
	         {
	           RETVAL = send_packet_bsd(dev,packet,packetsize);	
	         }

	         // Linux code
	         else
	         {
	           RETVAL = send_packet_linux(dev,packet,packetsize);
	         }
          }

          OUTPUT:
          RETVAL

char *
get_mac(dev)
	unsigned char *dev;
	CODE:
          char tmp[20] = "unknown";

	  if(SOCK_TYPE == SOCK_RAW)
	  {
	    get_mac_bsd(dev,tmp);
	  }
	  else
	  {
	    get_mac_linux(dev,tmp);
	  }

	  RETVAL = tmp;

	OUTPUT:
	RETVAL


char *
arp_lookup(dev, ip)
	unsigned char *dev;
	unsigned char *ip;

	CODE:
	  char tmp[20] = "unknown";

	  if(SOCK_TYPE == SOCK_RAW)
	  {
	    arp_lookup_bsd(dev,ip,tmp);
	  }
	  else
	  {
	    arp_lookup_linux(dev,ip,tmp);
	  }

	  RETVAL = tmp;

          OUTPUT:
          RETVAL