-
Notifications
You must be signed in to change notification settings - Fork 0
/
arp.h
94 lines (82 loc) · 2.8 KB
/
arp.h
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
#ifndef ARP_H
#define ARP_H
#include <signal.h>
/* System headers */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
/* net headers */
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <netinet/if_ether.h>
/* program headers */
#include "get_hw_addrs.h"
#include "api.h"
#include "common.h"
#include "debug.h"
#include "cache.h"
/* Used as the ethernet frame type */
#define ETH_P_ARP_SH 0xF31F
#define ARP_ID 0xDEAD
/* Max length of and arp packet
* is 10 byte header plus IPv6 mapped to 8 byte hw address
* [2|2|2|1|1|2|8|16|8|16] = 68 bytes
*/
#define ARP_MAXLEN 68
#define ARP_HDRLEN 10
#define ARP_MAXHWLEN 8
#define ARP_MAXPRLEN 16
#define ARP_MAXDATALEN ((2 * (ARP_MAXHWLEN)) + (2 * (ARP_MAXPRLEN)))
#define ARP_SHA(ptr) ((ptr)->data)
#define ARP_SPA(ptr) ((ptr)->data + (ptr)->hard_len)
#define ARP_THA(ptr) ((ptr)->data + (ptr)->hard_len + (ptr)->prot_len)
#define ARP_TPA(ptr) ((ptr)->data + 2 * (ptr)->hard_len + (ptr)->prot_len)
#define ARP_DATALEN(ptr) (2 * (ptr)->hard_len + 2 * (ptr)->prot_len)
struct arp_hdr {
uint16_t id;
uint16_t hard_type;
uint16_t prot_type;
u_char hard_len;
u_char prot_len;
uint16_t op;
u_char data[ARP_MAXDATALEN];
#if 0
/* Example data payload of IPv4 mapped to Ethernet address */
char send_eth[6];
char send_ip[4];
char target_eth[6];
char target_ip[4];
#endif
#if 0
/* Example data payload of IPv6 mapped to Ethernet address */
char send_eth[6];
char send_ip[16];
char target_eth[6];
char target_ip[16];
#endif
};
int create_unix_domain(void);
int create_pf_socket(void);
int run_arp(int unix_domain, int pf_socket);
int handle_areq(int pf_socket, Cache *conn_entry, struct hwa_info *devices);
ssize_t recv_frame(int pf_socket, struct ethhdr *eh, struct arp_hdr *recvmsg,
struct sockaddr_ll *src);
void ntoh_arp(struct arp_hdr *arp);
struct hwa_info* isDestination(struct hwa_info *devices, struct in_addr *ip_addr);
int send_frame(int sock, void *payload, int size, unsigned char *dstmac, unsigned char *srcmac, int ifi_index);
int maxfd(int pf_socket, int unix_domain, Cache *cache);
int build_arp(u_char *start, int size, uint16_t hard_type, uint16_t prot_type,
u_char hard_len, u_char prot_len, uint16_t op, u_char *send_hwa,
u_char *send_pro, u_char *target_hwa, u_char *target_pro);
int valid_arp(struct arp_hdr *arp);
int handle_req(int pack_fd, struct ethhdr *eh, struct arp_hdr *arp, struct
sockaddr_ll *src);
int handle_reply(int pack_fd, struct ethhdr *eh, struct arp_hdr *arp, struct
sockaddr_ll *src);
u_char *mac_by_ifindex(int index);
/* Signal handling for cleanup */
void cleanup(int signum);
void set_sig_cleanup(void);
#endif