ARP.xs

filter: Statementstmt 21.4% 15 / 70 Branchbran 3.1% 1 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 15.6% 16 / 102 SCAR 0
CC
0
Cov
0%
CRAP 0
« send_packet_linux.c ↑ Summary arp_lookup_linux.c »
1 /*
2 Perl ARP Extension
3 Create and send an arp packets, lookup mac addresses
4  
5 Programmed by Bastian Ballmann
6 Last update: 22.06.2013
7  
8 This program is free software; you can redistribute
9 it and/or modify it under the terms of the
10 GNU General Public License version 2 as published
11 by the Free Software Foundation.
12  
13 This program is distributed in the hope that it will
14 be useful, but WITHOUT ANY WARRANTY; without even
15 the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE.
17 See the GNU General Public License for more details.
18 */
19  
20 #include "EXTERN.h"
21 #include "perl.h"
22 #include "XSUB.h"
23  
24 #include "ppport.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <net/ethernet.h>
30 #include <netinet/ether.h>
31 #include <net/if.h>
32 #include <arpa/inet.h>
33 #include "arp.h"
34  
35 MODULE = Net::ARP PACKAGE = Net::ARP
36  
37 int
38 send_packet(dev, sip, dip, smac, dmac, type)
39 const char *dev;
40 const char *sip;
41 const char *dip;
42 const char *smac;
43 const char *dmac;
44 const char *type;
45  
46 CODE:
47 int uid;
48 2 unsigned int packetsize = sizeof(struct my_arphdr) + sizeof(struct ether_header);
49 2 unsigned char packet[packetsize];
50 2 struct ether_header *ethhdr = (struct ether_header *)packet;
51 2 struct my_arphdr *arp = (struct my_arphdr *)(packet + sizeof(struct ether_header));
52 u_short op;
53 in_addr_t ipaddr;
54  
55 2 RETVAL = 1;
56  
57 // Are you root?
58 2 uid = getuid();
59 2 ▶ if(uid != 0)
Branch: 1 branch
BranchTrueFalseTotal
if(uid != 0) 2 0 2
60 {
61 2 printf("You must have UID 0 instead of %d.\n",uid);
62 2 exit(0);
63 }
64  
65 // Initialize packet buffer
66 0 memset(packet,0,packetsize);
67  
68 // What's the ARP operation type?
69 0 ▶ if(!strcmp(type,"request"))
Branch: 1 branch
BranchTrueFalseTotal
if(!strcmp(type,"request")) 0 0 0
70 {
71 0 op = ARPOP_REQUEST;
72 }
73 0 ▶ else if(!strcmp(type,"reply"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"reply")) 0 0 0
74 {
75 0 op = ARPOP_REPLY;
76 }
77 0 ▶ else if(!strcmp(type,"revrequest"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"revrequest")) 0 0 0
78 {
79 0 op = ARPOP_REVREQUEST;
80 }
81 0 ▶ else if(!strcmp(type,"revreply"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"revreply")) 0 0 0
82 {
83 0 op = ARPOP_REVREPLY;
84 }
85 0 ▶ else if(!strcmp(type,"invrequest"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"invrequest")) 0 0 0
86 {
87 0 op = ARPOP_INVREQUEST;
88 }
89 0 ▶ else if(!strcmp(type,"invreply"))
Branch: 1 branch
BranchTrueFalseTotal
else if(!strcmp(type,"invreply")) 0 0 0
90 {
91 0 op = ARPOP_INVREPLY;
92 }
93 else
94 {
95 0 printf("Unknown ARP operation\n");
96 0 RETVAL = 0;
97 }
98  
99 0 ▶ if(smac == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(smac == NULL) 0 0 0
100 {
101 0 printf("Parameter smac is NULL! Terminating.\n");
102 0 RETVAL = 0;
103 }
104  
105 0 ▶ if(dmac == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(dmac == NULL) 0 0 0
106 {
107 0 printf("Parameter dmac is NULL! Terminating.\n");
108 0 RETVAL = 0;
109 }
110  
111 // Found a dollar sign?
112 0 ▶ if(strchr(smac,36))
Branch: 1 branch
BranchTrueFalseTotal
if(strchr(smac,36)) 0 0 0
113 {
114 0 printf("Found a $ char in smac! Terminating.\n");
115 0 RETVAL = 0;
116 }
117  
118 0 ▶ if(strchr(dmac,36))
Branch: 1 branch
BranchTrueFalseTotal
if(strchr(dmac,36)) 0 0 0
119 {
120 0 printf("Found a $ char in dmac! Terminating.\n");
121 0 RETVAL = 0;
122 }
123  
124 0 ▶ if(ether_aton(smac) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(ether_aton(smac) == NULL) 0 0 0
125 {
126 0 printf("Invalid source mac address! Terminating.\n");
127 0 RETVAL = 0;
128 }
129  
130 0 ▶ if(ether_aton(dmac) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(ether_aton(dmac) == NULL) 0 0 0
131 {
132 0 printf("Invalid destination mac address! Terminating.\n");
133 0 RETVAL = 0;
134 }
135  
136 // Check ips
137 0 ▶ if(inet_addr(sip) == INADDR_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if(inet_addr(sip) == INADDR_NONE) 0 0 0
138 {
139 0 printf("Invalid source ip address! Terminating.\n");
140 0 RETVAL = 0;
141 }
142  
143 0 ▶ if(inet_addr(dip) == INADDR_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if(inet_addr(dip) == INADDR_NONE) 0 0 0
144 {
145 0 printf("Invalid destination ip address! Terminating.\n");
146 0 RETVAL = 0;
147 }
148  
149 // Construct and send packet
150 0 ▶ if(RETVAL != 0)
Branch: 1 branch
BranchTrueFalseTotal
if(RETVAL != 0) 0 0 0
151 {
152 // Ethernet header
153 0 memcpy(ethhdr->ether_dhost,(u_char *)ether_aton(dmac),ETHER_ADDR_LEN); // Destination MAC
154 0 memcpy(ethhdr->ether_shost,(u_char *)ether_aton(smac),ETHER_ADDR_LEN); // Source MAC
155 0 ethhdr->ether_type = htons(ETHERTYPE_ARP); // ARP protocol
156  
157 // ARP header
158 0 arp->hw_type = htons(ARPHDR_ETHER); // Hardware address type
159 0 arp->proto_type = htons(ETH_P_IP); // Protocol address type
160 0 arp->ha_len = ETH_ALEN; // Hardware address length
161 0 arp->pa_len = IP_ALEN; // Protocol address length
162 0 arp->opcode = htons(op); // ARP operation
163 0 memcpy(arp->source_add,(u_char *)ether_aton(smac),ETH_ALEN); // Source MAC
164 0 memcpy(arp->dest_add,(u_char *)ether_aton(dmac),ETH_ALEN); // Destination MAC
165 0 ipaddr = inet_addr(sip);
166 0 memcpy(arp->source_ip, (u_char *)&ipaddr, IP_ALEN); // Source IP
167  
168 0 ipaddr = inet_addr(dip);
169 0 memcpy(arp->dest_ip, (u_char *)&ipaddr, IP_ALEN); // Destination IP
170  
171  
172  
173 // Run packet!! Run!
174 // FreeBSD code
175 if(SOCK_TYPE == SOCK_RAW)
176 {
177 RETVAL = send_packet_bsd(dev,packet,packetsize);
178 }
179  
180 // Linux code
181 else
182 {
183 0 RETVAL = send_packet_linux(dev,packet,packetsize);
184 }
185 }
186  
187 OUTPUT:
188 RETVAL
189  
190 char *
191 get_mac(dev)
192 const char *dev;
193 CODE:
194 1 char tmp[HEX_HW_ADDR_LEN] = "unknown";
195  
196 if(SOCK_TYPE == SOCK_RAW)
197 {
198 get_mac_bsd(dev,tmp);
199 }
200 else
201 {
202 1 get_mac_linux(dev,tmp);
203 }
204  
205 1 RETVAL = tmp;
206  
207 OUTPUT:
208 RETVAL
209  
210  
211 char *
212 arp_lookup(dev, ip)
213 const char *dev;
214 const char *ip;
215  
216 CODE:
217 3 char tmp[HEX_HW_ADDR_LEN] = "unknown";
218  
219 if(SOCK_TYPE == SOCK_RAW)
220 {
221 arp_lookup_bsd(dev,ip,tmp);
222 }
223 else
224 {
225 3 arp_lookup_linux(dev,ip,tmp);
226 }
227  
228 3 RETVAL = tmp;
229  
230 OUTPUT:
231 RETVAL
232  
« send_packet_linux.c ↑ Summary arp_lookup_linux.c »