-
Notifications
You must be signed in to change notification settings - Fork 0
/
arp.h
77 lines (63 loc) · 1.74 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
//
// arp.h
// router
//
// Created by Peera Yoodee on 9/29/15.
//
#ifndef __router__arp__
#define __router__arp__
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <net/ethernet.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <asm/types.h>
#include <netinet/in.h>
#include <signal.h>
#include <errno.h>
#include "utils.h"
#include "interface.h"
#define BUF_SIZE 1500 // Ethernet(14) + ARP(28)
#define SIZE_ETHERNET 14
#define ETHER_TYPE_FOR_ARP 0x0806
#define HW_TYPE_FOR_ETHER 0x0001
#define HW_LEN_FOR_ETHER 0x06
#define HW_LEN_FOR_IP 0x04
#define PROTO_TYPE_FOR_IP 0x0800
#define OP_CODE_FOR_ARP_REQ 0x0001
#define ARP_TIMEOUT 1 // Timeout in second
//#define _VERBOSE
struct ethernet {
u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */
u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */
u_short ether_type; /* IP? ARP? RARP? etc */
};
struct __attribute__((packed)) arp_packet
{
uint16_t arp_hd;
uint16_t arp_pr;
u_char arp_hdl;
u_char arp_prl;
uint16_t arp_op;
u_char arp_sha[6];
uint32_t arp_spa;
u_char arp_dha[6];
uint32_t arp_dpa;
};
struct arp_record {
uint32_t ip_address;
u_char mac_address[ETHER_ADDR_LEN];
};
struct arp_linkedlist {
struct arp_record node;
struct arp_linkedlist *next;
};
void init_arp_table(struct arp_linkedlist *root);
u_char *get_mac_address(struct arp_linkedlist *root, uint32_t ip_address, struct interface *interface);
u_char *get_mac_from_arp(uint32_t ip_address, struct interface *interface);
#endif /* defined(__router__arp__) */