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
|
/*
* PostgreSQL type definitions for MAC addresses.
*
* $Id: mac.c,v 1.3 1998/02/26 04:27:44 momjian Exp $
*/
#include <stdio.h>
#include <postgres.h>
#include <utils/palloc.h>
#include "mac.h"
/*
* This is the internal storage format for MAC addresses:
*/
typedef struct macaddr
{
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
unsigned char e;
unsigned char f;
} macaddr;
/*
* Various forward declarations:
*/
macaddr *macaddr_in(char *str);
char *macaddr_out(macaddr * addr);
bool macaddr_lt(macaddr * a1, macaddr * a2);
bool macaddr_le(macaddr * a1, macaddr * a2);
bool macaddr_eq(macaddr * a1, macaddr * a2);
bool macaddr_ge(macaddr * a1, macaddr * a2);
bool macaddr_gt(macaddr * a1, macaddr * a2);
bool macaddr_ne(macaddr * a1, macaddr * a2);
int4 macaddr_cmp(macaddr * a1, macaddr * a2);
text *macaddr_manuf(macaddr * addr);
/*
* Utility macros used for sorting and comparing:
*/
#define hibits(addr) \
((unsigned long)((addr->a<<16)|(addr->b<<8)|(addr->c)))
#define lobits(addr) \
((unsigned long)((addr->c<<16)|(addr->e<<8)|(addr->f)))
/*
* MAC address reader. Accepts several common notations.
*/
macaddr *
macaddr_in(char *str)
{
int a,
b,
c,
d,
e,
f;
macaddr *result;
int count;
if (strlen(str) > 0)
{
count = sscanf(str, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
if (count != 6)
count = sscanf(str, "%x-%x-%x-%x-%x-%x", &a, &b, &c, &d, &e, &f);
if (count != 6)
count = sscanf(str, "%2x%2x%2x:%2x%2x%2x", &a, &b, &c, &d, &e, &f);
if (count != 6)
count = sscanf(str, "%2x%2x%2x-%2x%2x%2x", &a, &b, &c, &d, &e, &f);
if (count != 6)
count = sscanf(str, "%2x%2x.%2x%2x.%2x%2x", &a, &b, &c, &d, &e, &f);
if (count != 6)
{
elog(ERROR, "macaddr_in: error in parsing \"%s\"", str);
return (NULL);
}
if ((a < 0) || (a > 255) || (b < 0) || (b > 255) ||
(c < 0) || (c > 255) || (d < 0) || (d > 255) ||
(e < 0) || (e > 255) || (f < 0) || (f > 255))
{
elog(ERROR, "macaddr_in: illegal address \"%s\"", str);
return (NULL);
}
}
else
{
a = b = c = d = e = f = 0; /* special case for missing
* address */
}
result = (macaddr *) palloc(sizeof(macaddr));
result->a = a;
result->b = b;
result->c = c;
result->d = d;
result->e = e;
result->f = f;
return (result);
}
/*
* MAC address output function. Fixed format.
*/
char *
macaddr_out(macaddr * addr)
{
char *result;
if (addr == NULL)
return (NULL);
result = (char *) palloc(32);
if ((hibits(addr) > 0) || (lobits(addr) > 0))
{
sprintf(result, "%02x:%02x:%02x:%02x:%02x:%02x",
addr->a, addr->b, addr->c, addr->d, addr->e, addr->f);
}
else
{
result[0] = 0; /* special case for missing address */
}
return (result);
}
/*
* Boolean tests.
*/
bool
macaddr_lt(macaddr * a1, macaddr * a2)
{
return ((hibits(a1) < hibits(a2)) ||
((hibits(a1) == hibits(a2)) && lobits(a1) < lobits(a2)));
};
bool
macaddr_le(macaddr * a1, macaddr * a2)
{
return ((hibits(a1) < hibits(a2)) ||
((hibits(a1) == hibits(a2)) && lobits(a1) <= lobits(a2)));
};
bool
macaddr_eq(macaddr * a1, macaddr * a2)
{
return ((hibits(a1) == hibits(a2)) && (lobits(a1) == lobits(a2)));
};
bool
macaddr_ge(macaddr * a1, macaddr * a2)
{
return ((hibits(a1) > hibits(a2)) ||
((hibits(a1) == hibits(a2)) && lobits(a1) >= lobits(a2)));
};
bool
macaddr_gt(macaddr * a1, macaddr * a2)
{
return ((hibits(a1) > hibits(a2)) ||
((hibits(a1) == hibits(a2)) && lobits(a1) > lobits(a2)));
};
bool
macaddr_ne(macaddr * a1, macaddr * a2)
{
return ((hibits(a1) != hibits(a2)) || (lobits(a1) != lobits(a2)));
};
/*
* Comparison function for sorting:
*/
int4
macaddr_cmp(macaddr * a1, macaddr * a2)
{
if (hibits(a1) < hibits(a2))
return -1;
else if (hibits(a1) > hibits(a2))
return 1;
else if (lobits(a1) < lobits(a2))
return -1;
else if (lobits(a1) > lobits(a2))
return 1;
else
return 0;
}
/*
* The special manufacturer fetching function. See "mac.h".
*/
text *
macaddr_manuf(macaddr * addr)
{
manufacturer *manuf;
int length;
text *result;
for (manuf = manufacturers; manuf->name != NULL; manuf++)
{
if ((manuf->a == addr->a) &&
(manuf->b == addr->b) &&
(manuf->c == addr->c))
break;
}
if (manuf->name == NULL)
{
result = palloc(VARHDRSZ + 1);
memset(result, 0, VARHDRSZ + 1);
VARSIZE(result) = VARHDRSZ + 1;
}
else
{
length = strlen(manuf->name) + 1;
result = palloc(length + VARHDRSZ);
memset(result, 0, length + VARHDRSZ);
VARSIZE(result) = length + VARHDRSZ;
memcpy(VARDATA(result), manuf->name, length);
}
return result;
}
/*
* eof
*/
|