forked from rsta2/circle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp9ether.cpp
More file actions
134 lines (106 loc) · 1.65 KB
/
p9ether.cpp
File metadata and controls
134 lines (106 loc) · 1.65 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
#include "p9ether.h"
#include <circle/util.h>
#include <assert.h>
Block *allocb (size_t size)
{
static const size_t maxhdrsize = 64;
size += sizeof (Block) + maxhdrsize;
Block *b = (Block *) new uchar[size];
assert (b != 0);
b->buf = b->data;
b->next = 0;
b->lim = b->buf + size;
b->wp = b->buf + maxhdrsize;
b->rp = b->wp;
return b;
}
void freeb (Block *b)
{
uchar *p = (uchar *) b;
delete [] p;
}
Block *padblock (Block *b, int size)
{
assert (size > 0);
assert (b != 0);
assert (b->rp - b->buf >= size);
b->rp -= size;
return b;
}
unsigned qlen (Queue *q)
{
return q->nelem;
}
Block *qget (Queue *q)
{
Block *b = 0;
assert (q != 0);
if (q->first != 0)
{
b = q->first;
q->first = b->next;
if (q->first == 0)
{
assert (q->last == b);
q->last = 0;
}
assert (q->nelem > 0);
q->nelem--;
}
return b;
}
void qpass (Queue *q, Block *b)
{
assert (b != 0);
b->next = 0;
assert (q != 0);
if (q->first == 0)
{
q->first = b;
}
else
{
assert (q->last != 0);
assert (q->last->next == 0);
q->last->next = b;
}
q->last = b;
q->nelem++;
}
ushort nhgets (const void *p)
{
return be2le16 (*(ushort *) p);
}
uint nhgetl (const void *p)
{
return be2le32 (*(uint *) p);
}
int parseether (uchar *addr, const char *str)
{
for (unsigned i = 1; i <= 6; i++)
{
char buf[3], *p = buf;
if (*str == '\0')
{
return -1;
}
*p++ = *str++;
if (*str == '\0')
{
return -1;
}
*p++ = *str++;
if (i < 6 && *str == ':')
{
str++;
}
*p = '\0';
char *end = 0;
*addr++ = (uchar) strtoul (buf, &end, 16);
if (end != 0 && *end != '\0')
{
return -1;
}
}
return *str == '\0' ? 0 : -1;
}