-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnet_io.h
More file actions
293 lines (234 loc) · 7.27 KB
/
Copy pathnet_io.h
File metadata and controls
293 lines (234 loc) · 7.27 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Cisco router simulation platform.
* Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr)
*
* Network I/O Layer.
*/
#ifndef __NET_IO_H__
#define __NET_IO_H__
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
#include "utils.h"
/* Maximum packet size */
#define NETIO_MAX_PKT_SIZE 32768
/* Maximum device length */
#define NETIO_DEV_MAXLEN 64
enum {
NETIO_TYPE_UNIX = 0,
NETIO_TYPE_VDE,
NETIO_TYPE_TAP,
NETIO_TYPE_UDP,
NETIO_TYPE_TCP_CLI,
NETIO_TYPE_TCP_SER,
#ifdef LINUX_ETH
NETIO_TYPE_LINUX_ETH,
#endif
#ifdef GEN_ETH
NETIO_TYPE_GEN_ETH,
#endif
NETIO_TYPE_FIFO,
NETIO_TYPE_NULL,
NETIO_TYPE_MAX,
};
enum {
NETIO_FILTER_ACTION_DROP = 0,
NETIO_FILTER_ACTION_PASS,
NETIO_FILTER_ACTION_ALTER,
NETIO_FILTER_ACTION_DUPLICATE,
};
typedef struct netio_desc netio_desc_t;
/* VDE switch definitions */
enum vde_request_type { VDE_REQ_NEW_CONTROL };
#define VDE_SWITCH_MAGIC 0xfeedface
#define VDE_SWITCH_VERSION 3
struct vde_request_v3 {
m_uint32_t magic;
m_uint32_t version;
enum vde_request_type type;
struct sockaddr_un sock;
};
/* netio unix descriptor */
typedef struct netio_unix_desc netio_unix_desc_t;
struct netio_unix_desc {
char *local_filename;
struct sockaddr_un remote_sock;
int fd;
};
/* netio vde descriptor */
typedef struct netio_vde_desc netio_vde_desc_t;
struct netio_vde_desc {
char *local_filename;
struct sockaddr_un remote_sock;
int ctrl_fd, data_fd;
};
/* netio tap descriptor */
typedef struct netio_tap_desc netio_tap_desc_t;
struct netio_tap_desc {
char filename[NETIO_DEV_MAXLEN];
int fd;
};
/* netio udp/tcp descriptor */
typedef struct netio_inet_desc netio_inet_desc_t;
struct netio_inet_desc {
int local_port, remote_port;
char *remote_host;
int fd;
};
#ifdef LINUX_ETH
/* netio linux raw ethernet descriptor */
typedef struct netio_lnxeth_desc netio_lnxeth_desc_t;
struct netio_lnxeth_desc {
char dev_name[NETIO_DEV_MAXLEN];
int dev_id, fd;
};
#endif
#ifdef GEN_ETH
/* netio generic raw ethernet descriptor */
typedef struct netio_geneth_desc netio_geneth_desc_t;
struct netio_geneth_desc {
char dev_name[NETIO_DEV_MAXLEN];
pcap_t *pcap_dev;
};
#endif
/* FIFO packet */
typedef struct netio_fifo_pkt netio_fifo_pkt_t;
struct netio_fifo_pkt {
netio_fifo_pkt_t *next;
size_t pkt_len;
char pkt[0];
};
/* Netio FIFO */
typedef struct netio_fifo_desc netio_fifo_desc_t;
struct netio_fifo_desc {
pthread_cond_t cond;
pthread_mutex_t lock, endpoint_lock;
netio_fifo_desc_t *endpoint;
netio_fifo_pkt_t *head, *last;
u_int pkt_count;
};
/* Packet filter */
typedef struct netio_pktfilter netio_pktfilter_t;
struct netio_pktfilter {
char *name;
int (*setup) (netio_desc_t * nio, void **opt, int argc, char *argv[]);
void (*free) (netio_desc_t * nio, void **opt);
int (*pkt_handler) (netio_desc_t * nio, void *pkt, size_t len, void *opt);
netio_pktfilter_t *next;
};
/* Generic netio descriptor */
struct netio_desc {
u_int type;
void *dptr;
char *name;
int debug;
/*can nio recv packet now? */
//u_int can_recv;
/* Frame Relay specific information */
//m_uint8_t fr_lmi_seq;
//void *fr_conn_list;
/* Ethernet specific information */
//u_int vlan_port_type;
//m_uint16_t vlan_id;
//void *vlan_input_vector;
union {
//netio_unix_desc_t nud;
//netio_vde_desc_t nvd;
netio_tap_desc_t ntd;
//netio_inet_desc_t nid;
#ifdef LINUX_ETH
//netio_lnxeth_desc_t nled;
#endif
#ifdef GEN_ETH
//netio_geneth_desc_t nged;
#endif
//netio_fifo_desc_t nfd;
} u;
/* Send and receive prototypes */
ssize_t (*send) (void *desc, void *pkt, size_t len);
ssize_t (*recv) (void *desc, void *pkt, size_t len);
/* Configuration saving */
//void (*save_cfg)(netio_desc_t *nio,FILE *fd);
/* Packet filters */
//netio_pktfilter_t *rx_filter,*tx_filter,*both_filter;
//void *rx_filter_data,*tx_filter_data,*both_filter_data;
/* Next pointer (for RX listener) */
netio_desc_t *rxl_next;
/* Packet data */
u_char rx_pkt[NETIO_MAX_PKT_SIZE];
};
/* RX listener */
typedef int (*netio_rx_handler_t) (netio_desc_t * nio, u_char * pkt,
ssize_t pkt_len, void *arg1, void *arg2);
struct netio_rx_listener {
netio_desc_t *nio;
u_int ref_count;
volatile int running;
netio_rx_handler_t rx_handler;
void *arg1, *arg2;
pthread_t spec_thread;
struct netio_rx_listener *prev, *next;
};
/* Get NETIO type given a description */
int netio_get_type (char *type);
/* Show the NETIO types */
void netio_show_types (void);
/* Create a new NetIO descriptor */
netio_desc_t *netio_desc_create_unix (char *nio_name, char *local,
char *remote);
/* Create a new NetIO descriptor with VDE method */
netio_desc_t *netio_desc_create_vde (char *nio_name, char *control,
char *local);
/* Create a new NetIO descriptor with TAP method */
netio_desc_t *netio_desc_create_tap (char *nio_name, char *tap_name);
/* Create a new NetIO descriptor with TCP_CLI method */
netio_desc_t *netio_desc_create_tcp_cli (char *nio_name, char *addr,
char *port);
/* Create a new NetIO descriptor with TCP_SER method */
netio_desc_t *netio_desc_create_tcp_ser (char *nio_name, char *port);
/* Create a new NetIO descriptor with UDP method */
netio_desc_t *netio_desc_create_udp (char *nio_name, int local_port,
char *remote_host, int remote_port);
#ifdef LINUX_ETH
/* Create a new NetIO descriptor with raw Ethernet method */
netio_desc_t *netio_desc_create_lnxeth (char *nio_name, char *dev_name);
#endif
#ifdef GEN_ETH
/* Create a new NetIO descriptor with generic raw Ethernet method */
netio_desc_t *netio_desc_create_geneth (char *nio_name, char *dev_name);
#endif
/* Establish a cross-connect between two FIFO NetIO */
int netio_fifo_crossconnect (netio_desc_t * a, netio_desc_t * b);
/* Create a new NetIO descriptor with FIFO method */
netio_desc_t *netio_desc_create_fifo (char *nio_name);
/* Create a new NetIO descriptor with NULL method */
netio_desc_t *netio_desc_create_null (char *nio_name);
/* Acquire a reference to NIO from registry (increment reference count) */
netio_desc_t *netio_acquire (char *name);
/* Release an NIO (decrement reference count) */
int netio_release (char *name);
/* Delete a NetIO descriptor */
int netio_delete (char *name);
/* Delete all NetIO descriptors */
int netio_delete_all (void);
/* Save the configuration of a NetIO descriptor */
void netio_save_config (netio_desc_t * nio, FILE * fd);
/* Save configurations of all NetIO descriptors */
void netio_save_config_all (FILE * fd);
/* Send a packet through a NetIO descriptor */
ssize_t netio_send (netio_desc_t * nio, void *pkt, size_t len);
/* Receive a packet through a NetIO descriptor */
ssize_t netio_recv (netio_desc_t * nio, void *pkt, size_t max_len);
/* Get a NetIO FD */
int netio_get_fd (netio_desc_t * nio);
/* Enable a RX listener */
int netio_rxl_enable (netio_desc_t * nio);
/* Add an RX listener in the listener list */
int netio_rxl_add (netio_desc_t * nio, netio_rx_handler_t rx_handler,
void *arg1, void *arg2);
/* Remove a NIO from the listener list */
int netio_rxl_remove (netio_desc_t * nio);
/* Initialize the RXL thread */
int netio_rxl_init (void);
#endif