-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintf.h
122 lines (97 loc) · 2.87 KB
/
intf.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
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
#ifndef NETSTACK_INTERFACE_H
#define NETSTACK_INTERFACE_H
#include <stdbool.h>
#include <semaphore.h>
#include <net/if.h>
#include <sys/types.h>
#include <netstack.h>
#include <netstack/addr.h>
#include <netstack/frame.h>
#include <netstack/col/llist.h>
// Fix circular include issue
struct frame;
// Interface types
#define INTF_RAWSOCK 0x01
#define INTF_TPACKET 0x01
#define INTF_TAP 0x03
#define INTF_TUN 0x04
// Interface thread ids
#define INTF_THR_RECV 0x00
#define INTF_THR_SEND 0x01
#define INTF_THR_MAX 0x02
// TODO: Implement 'virtual' network interfaces
// `man netdevice` gives a good overview
struct intf {
uint8_t type;
proto_t proto; /* Defines the protocol running in the interface */
// Link layer information
char name[IFNAMSIZ];
void *ll;
uint8_t *ll_addr;
size_t mtu;
// Internet Addresses (IPv4/6)
llist_t inet;
// TODO: Move arptbl into an 'ethernet' hardware struct into `void *ll`
llist_t arptbl;
// Outbound queue for packets to neighbouring hosts (see neigh.c)
llist_t neigh_outqueue;
// Interface send/recv thread ids
pthread_t threads[INTF_THR_MAX];
// Blocking function call that reads a frame from the interface.
/* Implementing method is responsible for populating frame->buffer using
* frame_init_buf() and providing a suitable frame buffer (can be using
* malloc) */
long (*recv_frame)(struct frame *);
long (*send_frame)(struct frame *);
void *(*new_buffer)(struct intf *intf, size_t size);
void (*free_buffer)(struct intf *intf, void *buffer);
// Cleans up an allocated interface data, excluding the interface struct
// itself (may not have been dynamically allocated)
void (*free)(struct intf *);
};
/*!
* Pushes a frame into the interface dispatch queue
* Increases the frame reference count to prevent it being deallocated
* Note: Frames must be locked for reading/writing by the calling thread
* @return 0 on success, otherwise on error
*/
int intf_dispatch(struct frame *frame);
/*!
*
* @param intf
* @return
*/
int intf_init(struct intf *intf);
/*!
*
* @param frame
* @param buf_size
* @return
*/
struct frame *intf_frame_new(struct intf *frame, size_t buf_size);
/*!
*
* @param intf
* @param size
* @return
*/
void *intf_malloc_buffer(struct intf *intf, size_t size);
/*!
*
* @param intf
* @param buffer
*/
void intf_free_buffer(struct intf *intf, void *buffer);
/*!
* Calculates the maximum frame size for an interface
*/
size_t intf_max_frame_size(struct intf *intf);
/*!
* Checks whether an interface has the specified address
* @param intf interface to check
* @param addr address & protocol to check
* @return true if requested protocol and address match
*/
bool intf_has_addr(struct intf *intf, addr_t *addr);
bool intf_get_addr(struct intf *intf, addr_t *addr);
#endif //NETSTACK_INTERFACE_H