-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsys.h
365 lines (293 loc) · 7.92 KB
/
sys.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* sys.h
*
* 2006 Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __SYS_H
#define __SYS_H
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <linux/types.h>
#include <linux/unistd.h>
#ifdef KERNEL_NETCHANNEL
#include <linux/netchannel.h>
#else
#include "netchannel.h"
#endif
#define PACKET_NAME "packet"
#ifdef UDEBUG
#define uloga(f, a...) fprintf(stderr, f, ##a)
#else
#define uloga(f, a...)
#endif
#define ulog(f, a...) uloga(f, ##a)
#define ulog_err(f, a...) ulog(f ": %s [%d].\n", ##a, strerror(errno), errno)
#define ulog_info(f, a...) fprintf(stderr, f, ##a)
#define MAX_HEADER_SIZE 100
enum atcp_init_state {
NETCHANNEL_ATCP_CONNECT = 0,
NETCHANNEL_ATCP_LISTEN,
};
struct nc_buff_head {
/* These two members must be first. */
struct nc_buff *next;
struct nc_buff *prev;
__u32 qlen;
};
struct netchannel;
struct ncb_timeval
{
__u32 off_sec;
__u32 off_usec;
};
/* ncb pointers:
* |------|------------------------|-----|
* data head tail end
*
* [data, head] - headers
* [head, tail] - data
*/
struct nc_buff
{
struct nc_buff *next;
struct nc_buff *prev;
void *data, *head, *tail, *end;
unsigned int len, total_size;
int refcnt;
struct netchannel *nc;
union {
struct tcphdr *th;
struct udphdr *uh;
void *raw;
} h;
union {
struct iphdr *iph;
void *raw;
} nh;
struct ncb_timeval tstamp;
__u8 cb[32];
};
extern struct nc_buff *ncb_alloc(unsigned int size);
extern void ncb_free(struct nc_buff *);
extern int transmit_data(struct nc_buff *ncb);
int ip_build_header(struct nc_buff *ncb);
int ip_send_data(struct nc_buff *ncb);
void packet_dump(__u8 *data, unsigned int size);
int packet_ip_process(struct nc_buff *ncb);
static inline void *ncb_push(struct nc_buff *ncb, unsigned int size)
{
if (ncb->head < ncb->data + size) {
ulog("%s: head: %p, data: %p, size: %u [%u], req_size: %u.\n",
__func__, ncb->head, ncb->data, ncb->len, ncb->total_size, size);
return NULL;
}
ncb->head -= size;
ncb->len += size;
return ncb->head;
}
static inline void *ncb_pull(struct nc_buff *ncb, unsigned int size)
{
void *head = ncb->head;
if (ncb->tail < ncb->head + size) {
ulog("%s: head: %p, data: %p, size: %u [%u], req_size: %u.\n",
__func__, ncb->head, ncb->data, ncb->len, ncb->total_size, size);
return NULL;
}
ncb->head += size;
ncb->len -= size;
return head;
}
static inline void *ncb_trim(struct nc_buff *ncb, unsigned int size)
{
if (size > ncb->len) {
ulog("%s: head: %p, data: %p, size: %u, req_size: %u.\n",
__func__, ncb->head, ncb->data, ncb->len, size);
return NULL;
}
ncb->tail = ncb->head + size;
ncb->len = size;
return ncb->head;
}
static inline struct nc_buff *ncb_get(struct nc_buff *ncb)
{
ncb->refcnt++;
return ncb;
}
static inline struct nc_buff *ncb_clone(struct nc_buff *ncb)
{
return ncb_get(ncb);
}
static inline void ncb_put(struct nc_buff *ncb)
{
if (ncb->refcnt <= 0)
ulog("%s: BUG: refcnt: %d.\n", __func__, ncb->refcnt);
else if (--ncb->refcnt == 0)
ncb_free(ncb);
}
static inline void ncb_queue_init(struct nc_buff_head *list)
{
list->prev = list->next = (struct nc_buff *)list;
list->qlen = 0;
}
static inline int ncb_queue_empty(const struct nc_buff_head *list)
{
return list->next == (struct nc_buff *)list;
}
static inline void ncb_queue_tail(struct nc_buff_head *list, struct nc_buff *ncb)
{
struct nc_buff *prev, *next;
list->qlen++;
next = (struct nc_buff *)list;
prev = next->prev;
ncb->next = next;
ncb->prev = prev;
next->prev = prev->next = ncb;
}
static inline struct nc_buff *ncb_dequeue(struct nc_buff_head *list)
{
struct nc_buff *next, *prev, *result;
prev = (struct nc_buff *) list;
next = prev->next;
result = NULL;
if (next != prev) {
result = next;
next = next->next;
list->qlen--;
next->prev = prev;
prev->next = next;
result->next = result->prev = NULL;
}
return result;
}
static inline void ncb_insert(struct nc_buff *newsk,
struct nc_buff *prev, struct nc_buff *next,
struct nc_buff_head *list)
{
newsk->next = next;
newsk->prev = prev;
next->prev = prev->next = newsk;
list->qlen++;
}
static inline struct nc_buff *ncb_peek(struct nc_buff_head *list_)
{
struct nc_buff *list = ((struct nc_buff *)list_)->next;
if (list == (struct nc_buff *)list_)
list = NULL;
return list;
}
static inline struct nc_buff *ncb_peek_tail(struct nc_buff_head *list_)
{
struct nc_buff *list = ((struct nc_buff *)list_)->prev;
if (list == (struct nc_buff *)list_)
list = NULL;
return list;
}
static inline unsigned int ncb_tailroom(struct nc_buff *ncb)
{
return ncb->end - ncb->tail;
}
static inline void ncb_unlink(struct nc_buff *ncb, struct nc_buff_head *head)
{
struct nc_buff *prev = ncb->prev;
struct nc_buff *next = ncb->next;
prev->next = next;
next->prev = prev;
head->qlen--;
}
static inline void ncb_timestamp(struct ncb_timeval *tv)
{
struct timeval tm;
gettimeofday(&tm, NULL);
tv->off_sec = tm.tv_sec;
tv->off_usec = tm.tv_usec;
}
static inline void netchannel_flush_list_head(struct nc_buff_head *list)
{
struct nc_buff *ncb;
while ((ncb = ncb_dequeue(list)))
ncb_put(ncb);
}
#define NIPQUAD(addr) \
((unsigned char *)&addr)[0], \
((unsigned char *)&addr)[1], \
((unsigned char *)&addr)[2], \
((unsigned char *)&addr)[3]
#define min_t(type,x,y) \
({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
#define max_t(type,x,y) \
({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
extern struct common_protocol atcp_common_protocol;
extern struct common_protocol udp_common_protocol;
struct netchannel;
struct common_protocol
{
unsigned int size;
int (*create)(struct netchannel *);
int (*destroy)(struct netchannel *);
int (*process_in)(struct netchannel *, void *, unsigned int);
int (*process_out)(struct netchannel *, void *, unsigned int);
};
struct netchannel
{
struct nc_buff_head recv_queue;
struct netchannel_control ctl;
unsigned long long hit;
int fd;
unsigned int state, header_size;
struct common_protocol *proto; /* Must be the last member in the structure */
};
struct netchannel *netchannel_create(struct netchannel_control *ctl, unsigned int state);
void netchannel_remove(struct netchannel *nc);
int netchannel_bind(struct netchannel *nc);
int netchannel_recv(struct netchannel *nc, void *buf, unsigned int size);
int netchannel_send(struct netchannel *nc, void *buf, unsigned int size);
int netchannel_send_raw(struct nc_buff *ncb);
int netchannel_recv_raw(struct netchannel *nc, unsigned int tm);
static inline __u16 in_csum(__u16 *addr, unsigned int len)
{
unsigned int nleft = len;
__u16 *w = addr;
unsigned int sum = 0;
__u16 answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(__u8 *)(&answer) = *(__u8 *)w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return answer;
}
static inline __u32 num2ip(const __u8 a1, const __u8 a2, const __u8 a3, const __u8 a4)
{
__u32 r = a1;
r <<= 8;
r |= a2;
r <<= 8;
r |= a3;
r <<= 8;
r |= a4;
return r;
}
extern unsigned long syscall_recv, syscall_send;
#endif /* __SYS_H */