-
Notifications
You must be signed in to change notification settings - Fork 4
/
raw.c
63 lines (54 loc) · 1.18 KB
/
raw.c
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
#include "raw.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "raw.h"
#ifdef HAVE_TAP
#include "raw/tap.h"
extern struct rawdev_ops tap_dev_ops;
#endif
#ifdef HAVE_PF_PACKET
#include "raw/soc.h"
#define RAWDEV_TYPE_DEFAULT RAWDEV_TYPE_SOCKET
extern struct rawdev_ops soc_dev_ops;
#endif
static uint8_t rawdev_detect_type(char *name) {
if (strncmp(name, "tap", 3) == 0) {
return RAWDEV_TYPE_TAP;
}
return RAWDEV_TYPE_DEFAULT;
}
struct rawdev *rawdev_alloc(uint8_t type, char *name) {
struct rawdev *dev;
struct rawdev_ops *ops;
if (type == RAWDEV_TYPE_AUTO) {
type = rawdev_detect_type(name);
}
// set ops
switch (type) {
#ifdef HAVE_TAP
case RAWDEV_TYPE_TAP:
ops = &tap_dev_ops;
break;
#endif
#ifdef HAVE_PF_PACKET
case RAWDEV_TYPE_SOCKET:
ops = &soc_dev_ops;
break;
#endif
default:
fprintf(stderr, "unsupported raw device type (%u)\n", type);
return NULL;
}
dev = malloc(sizeof(struct rawdev));
if (!dev) {
fprintf(stderr, "malloc rawdev: failure\n");
return NULL;
}
dev->type = type;
dev->name = name;
dev->ops = ops;
dev->priv = NULL;
return dev;
}