summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Franke <cfchris6@ts2server.com>2010-06-15 11:05:51 (GMT)
committerChristian Franke <cfchris6@ts2server.com>2010-06-15 11:05:51 (GMT)
commit4acc4620ad3eeeb3312c1b52f23748134894f69e (patch)
treedcfcc5768c911fc52b03836bcf4b964a4fd1e898
parent8ef8e549d724ca53a387dcad0ecf34cb8e4db56c (diff)
some cleanup and type fixing
-rw-r--r--arpd.c176
1 files changed, 94 insertions, 82 deletions
diff --git a/arpd.c b/arpd.c
index 611eca6..b6ad7f6 100644
--- a/arpd.c
+++ b/arpd.c
@@ -21,13 +21,48 @@
#include <net/if.h>
#include <net/if_arp.h>
+#define IP_ALEN 4
+
+union ether_arp_ip {
+ unsigned char raw[32768];
+ struct __attribute__((packed)) {
+ struct ether_header eth;
+ struct __attribute__((packed)) {
+ uint16_t ar_hrd;
+ uint16_t ar_pro;
+ uint8_t ar_hln;
+ uint8_t ar_pln;
+ uint16_t ar_op;
+ uint8_t ar_sha[ETH_ALEN];
+ uint8_t ar_spa[IP_ALEN];
+ uint8_t ar_tha[ETH_ALEN];
+ uint8_t ar_tpa[IP_ALEN];
+ } arp;
+ } hdr;
+};
+
+#define hdr_eth_dhost hdr.eth.ether_dhost
+#define hdr_eth_shost hdr.eth.ether_shost
+#define hdr_eth_type hdr.eth.ether_type
+#define hdr_arp_hrd hdr.arp.ar_hrd
+#define hdr_arp_pro hdr.arp.ar_pro
+#define hdr_arp_hln hdr.arp.ar_hln
+#define hdr_arp_pln hdr.arp.ar_pln
+#define hdr_arp_op hdr.arp.ar_op
+#define hdr_arp_sha hdr.arp.ar_sha
+#define hdr_arp_spa hdr.arp.ar_spa
+#define hdr_arp_tha hdr.arp.ar_tha
+#define hdr_arp_tpa hdr.arp.ar_tpa
+
+
static int arp_socket = 0;
static char* ifname = NULL;
static unsigned ifindex = 0;
-static unsigned char ifaddr[ETH_ALEN];
+static uint8_t ifaddr[ETH_ALEN];
static void open_socket();
static void get_hwaddr();
-static void print_hwaddr(const char*, const unsigned char*, uint8_t);
+static void print_hwaddr(const char*, const uint8_t*, uint8_t);
+static void main_loop();
int main(int,char**);
static void open_socket()
@@ -85,7 +120,7 @@ static void get_hwaddr()
}
static void print_hwaddr(const char*prompt,
- const unsigned char*buffer,
+ const uint8_t*buffer,
uint8_t size)
{
printf("%s: 0x", prompt);
@@ -95,114 +130,91 @@ static void print_hwaddr(const char*prompt,
printf("\n");
}
-int main(int argc, char **argv)
+static void main_loop()
{
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <ifname>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- ifname = argv[1];
-
- fprintf(stderr, "Opening socket... ");
- open_socket();
- fprintf(stderr, "got it.\n");
- fprintf(stderr, "Getting hardware address... ");
- get_hwaddr();
- fprintf(stderr, "got it.\n");
- print_hwaddr("Hardware Address is", ifaddr, ETH_ALEN);
- fprintf(stderr, "\n");
-
- ssize_t red = 1;
- union {
- unsigned char raw[32768];
- struct __attribute__((packed)) {
- struct ether_header eth;
- struct arphdr arp;
- unsigned char rest;
- } hdr;
- } buffer;
-
- unsigned char sendbuffer[32768];
struct sockaddr_ll lla;
socklen_t lla_size;
+ union ether_arp_ip recvbuffer,sendbuffer;
+ ssize_t red;
while (1) {
memset(&lla, 0, sizeof(lla));
lla_size = sizeof(lla);
- red = recvfrom(arp_socket, buffer.raw, sizeof(buffer), 0,
+ red = recvfrom(arp_socket, recvbuffer.raw, sizeof(recvbuffer), 0,
(struct sockaddr*)(&lla), &lla_size);
if (red <= 0) {
if (red < 0)
perror("Error reading from socket");
break;
}
+
+ if (red < sizeof(recvbuffer.hdr)) {
+ fprintf(stderr, "Received a shorted packet.\n");
+ continue;
+ }
+
if (!(lla.sll_pkttype
& (PACKET_HOST | PACKET_BROADCAST | PACKET_MULTICAST)
|| (lla.sll_pkttype & PACKET_OUTGOING)))
continue;
- if ( (ntohs(buffer.hdr.eth.ether_type) != ETHERTYPE_ARP)
- || (ntohs(buffer.hdr.arp.ar_hrd) != ARPHRD_ETHER)
- || (ntohs(buffer.hdr.arp.ar_pro) != ETHERTYPE_IP)
- || (ntohs(buffer.hdr.arp.ar_op) != ARPOP_REQUEST)
- || (buffer.hdr.arp.ar_hln != ETH_ALEN)
- || (buffer.hdr.arp.ar_pln != 4)) {
+ if ( (ntohs(recvbuffer.hdr_eth_type) != ETHERTYPE_ARP)
+ || (ntohs(recvbuffer.hdr_arp_hrd) != ARPHRD_ETHER)
+ || (ntohs(recvbuffer.hdr_arp_pro) != ETHERTYPE_IP)
+ || (ntohs(recvbuffer.hdr_arp_op) != ARPOP_REQUEST)
+ || (recvbuffer.hdr_arp_hln != ETH_ALEN)
+ || (recvbuffer.hdr_arp_pln != IP_ALEN)) {
continue;
}
fprintf(stderr, "Received ARP request.\n");
- unsigned char *dp = &buffer.hdr.rest;
- unsigned char *ar_sha = dp;
- dp += ETH_ALEN;
- unsigned char *ar_spa = dp;
- dp += 4;
- unsigned char *ar_tha = dp;
- dp += ETH_ALEN;
- unsigned char *ar_tpa = dp;
- dp += 4;
-
- print_hwaddr("Src hw-addr is", ar_sha, ETH_ALEN);
- print_hwaddr("Src protocol addr is", ar_spa, 4);
- print_hwaddr("Dest hw-addr is", ar_tha, ETH_ALEN);
- print_hwaddr("Dest protocol addr is", ar_tpa, 4);
-
- dp = sendbuffer;
- memcpy(dp, buffer.hdr.eth.ether_shost, ETH_ALEN);
- dp += ETH_ALEN;
- memcpy(dp, ifaddr, ETH_ALEN);
- dp += ETH_ALEN;
- uint16_t tmp = htons(ETHERTYPE_ARP);
- memcpy(dp, &tmp, 2);
- dp+=2;
- memcpy(dp, &buffer.hdr.arp.ar_hrd, 2);
- dp += 2;
- memcpy(dp, &buffer.hdr.arp.ar_pro, 2);
- dp += 2;
- *dp = ETH_ALEN;
- dp++;
- *dp = 4;
- dp++;
- tmp = htons(ARPOP_REPLY);
- memcpy(dp, &tmp, 2);
- dp += 2;
- memcpy(dp, ifaddr, ETH_ALEN);
- dp += ETH_ALEN;
- memcpy(dp, ar_tpa, 4);
- dp += 4;
- memcpy(dp, ar_sha, ETH_ALEN);
- dp += ETH_ALEN;
- memcpy(dp, ar_spa, 4);
- dp += 4;
-
- if (send(arp_socket, sendbuffer, dp - sendbuffer, 0) == dp -
- sendbuffer) {
+ print_hwaddr("Src hw-addr is", recvbuffer.hdr_arp_sha, ETH_ALEN);
+ print_hwaddr("Src protocol addr is",recvbuffer.hdr_arp_spa, IP_ALEN);
+ print_hwaddr("Dest hw-addr is", recvbuffer.hdr_arp_tha, ETH_ALEN);
+ print_hwaddr("Dest protocol addr is",recvbuffer.hdr_arp_tpa,IP_ALEN);
+
+ memcpy(sendbuffer.hdr_eth_dhost, recvbuffer.hdr_eth_shost, ETH_ALEN);
+ memcpy(sendbuffer.hdr_eth_shost, ifaddr, ETH_ALEN);
+ sendbuffer.hdr_eth_type = htons(ETHERTYPE_ARP);
+ sendbuffer.hdr_arp_hrd = recvbuffer.hdr_arp_hrd;
+ sendbuffer.hdr_arp_pro = recvbuffer.hdr_arp_pro;
+ sendbuffer.hdr_arp_hln = ETH_ALEN;
+ sendbuffer.hdr_arp_pln = IP_ALEN;
+ sendbuffer.hdr_arp_op = htons(ARPOP_REPLY);
+ memcpy(sendbuffer.hdr_arp_sha, ifaddr, ETH_ALEN);
+ memcpy(sendbuffer.hdr_arp_spa, recvbuffer.hdr_arp_tpa, IP_ALEN);
+ memcpy(sendbuffer.hdr_arp_tha, recvbuffer.hdr_arp_sha, ETH_ALEN);
+ memcpy(sendbuffer.hdr_arp_tpa, recvbuffer.hdr_arp_spa, IP_ALEN);
+
+ if (send(arp_socket, sendbuffer.raw, sizeof(sendbuffer.hdr), 0)
+ == sizeof(sendbuffer.hdr)) {
fprintf(stderr, "Answer sent successfully.\n");
} else {
perror("Error sending the answer");
}
}
+}
+
+int main(int argc, char **argv)
+{
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <ifname>\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ ifname = argv[1];
+
+ fprintf(stderr, "Opening socket... ");
+ open_socket();
+ fprintf(stderr, "got it.\n");
+ fprintf(stderr, "Getting hardware address... ");
+ get_hwaddr();
+ fprintf(stderr, "got it.\n");
+ print_hwaddr("Hardware Address is", ifaddr, ETH_ALEN);
+ fprintf(stderr, "\n");
+
+ main_loop();
close(arp_socket);