-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnet.h
84 lines (70 loc) · 1.98 KB
/
net.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
#ifndef NET_H
#define NET_H
#include <stdint.h>
#include <unistd.h>
#define NETDEV_TYPE_ETHERNET (0x0001)
#define NETDEV_FLAG_BROADCAST (0x0001)
#define NETDEV_FLAG_MULTICAST (0x0002)
#define NETDEV_FLAG_P2P (0x0004)
#define NETDEV_FLAG_LOOPBACK (0x0008)
#define NETDEV_FLAG_NOARP (0x0010)
#define NETDEV_FLAG_PROMISC (0x0020)
#define NETDEV_FLAG_RUNNING (0x0040)
#define NETDEV_FLAG_UP (0x0080)
#include "ethernet.h"
#define NETDEV_PROTO_IP ETHERNET_TYPE_IP
#define NETDEV_PROTO_ARP ETHERNET_TYPE_ARP
#define NETDEV_PROTO_IPV6 ETHERNET_TYPE_IPV6
#define NETIF_FAMILY_IPV4 (0x02)
#ifndef IFNAMSIZ
#define IFNAMSIZ (16)
#endif
struct netdev;
struct netif {
struct netif *next;
uint8_t family;
struct netdev *dev;
// Depends on implementation of protocols.
};
struct netdev_ops {
int (*open)(struct netdev *dev, int opt);
int (*close)(struct netdev *dev);
int (*run)(struct netdev *dev);
int (*stop)(struct netdev *dev);
ssize_t (*tx)(struct netdev *dev, uint16_t type, uint8_t *packet, size_t size,
const void *dst);
};
struct netdev_def {
uint16_t type;
uint16_t mtu;
uint16_t flags;
uint16_t hlen;
uint16_t alen;
struct netdev_ops *ops;
};
struct netdev {
struct netdev *next;
struct netif *ifs;
char name[IFNAMSIZ];
uint16_t type;
uint16_t mtu;
uint16_t flags;
uint16_t hlen;
uint16_t alen;
uint8_t addr[16];
uint8_t peer[16];
uint8_t broadcast[16];
void (*rx_handler)(struct netdev *dev, uint16_t type, uint8_t *packet,
size_t plen);
struct netdev_ops *ops;
void *priv;
};
int netdev_driver_register(struct netdev_def *def);
int netdev_proto_register(unsigned short type,
void (*handler)(uint8_t *packet, size_t plen,
struct netdev *dev));
struct netdev *netdev_root(void);
struct netdev *netdev_alloc(uint16_t type);
int netdev_add_netif(struct netdev *dev, struct netif *netif);
struct netif *netdev_get_netif(struct netdev *dev, int family);
#endif