forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw.c
70 lines (64 loc) · 1.42 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
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.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
#ifdef HAVE_BPF
#include "raw/bpf.h"
#define RAWDEV_TYPE_DEFAULT RAWDEV_TYPE_BPF
extern struct rawdev_ops bpf_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 *raw;
struct rawdev_ops *ops;
if (type == RAWDEV_TYPE_AUTO) {
type = rawdev_detect_type(name);
}
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
#ifdef HAVE_BPF
case RAWDEV_TYPE_BPF:
ops = &bpf_dev_ops;
break;
#endif
default:
fprintf(stderr, "unsupported raw device type (%u)\n", type);
return NULL;
}
raw = malloc(sizeof(struct rawdev));
if (!raw) {
fprintf(stderr, "malloc: failure\n");
return NULL;
}
raw->type = type;
raw->name = name;
raw->ops = ops;
raw->priv = NULL;
return raw;
}