1
1
/*
2
2
* PostgreSQL type definitions for MAC addresses.
3
3
*
4
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.17 2000/08/03 23:07:46 tgl Exp $
4
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.18 2000/08/23 06:04:33 thomas Exp $
5
5
*/
6
6
7
7
#include "postgres.h"
8
8
9
9
#include "utils/builtins.h"
10
10
#include "utils/inet.h"
11
11
12
- /*
13
- * XXX this table of manufacturers is long out of date, and should never
14
- * have been wired into the code in the first place.
15
- */
16
-
17
- typedef struct manufacturer
18
- {
19
- unsigned char a ;
20
- unsigned char b ;
21
- unsigned char c ;
22
- char * name ;
23
- } manufacturer ;
24
-
25
- static manufacturer manufacturers [] = {
26
- {0x00 , 0x00 , 0x0C , "Cisco" },
27
- {0x00 , 0x00 , 0x0E , "Fujitsu" },
28
- {0x00 , 0x00 , 0x0F , "NeXT" },
29
- {0x00 , 0x00 , 0x10 , "Sytek" },
30
- {0x00 , 0x00 , 0x1D , "Cabletron" },
31
- {0x00 , 0x00 , 0x20 , "DIAB" },
32
- {0x00 , 0x00 , 0x22 , "Visual Technology" },
33
- {0x00 , 0x00 , 0x2A , "TRW" },
34
- {0x00 , 0x00 , 0x32 , "GPT Limited" },
35
- {0x00 , 0x00 , 0x5A , "S & Koch" },
36
- {0x00 , 0x00 , 0x5E , "IANA" },
37
- {0x00 , 0x00 , 0x65 , "Network General" },
38
- {0x00 , 0x00 , 0x6B , "MIPS" },
39
- {0x00 , 0x00 , 0x77 , "MIPS" },
40
- {0x00 , 0x00 , 0x7A , "Ardent" },
41
- {0x00 , 0x00 , 0x89 , "Cayman Systems" },
42
- {0x00 , 0x00 , 0x93 , "Proteon" },
43
- {0x00 , 0x00 , 0x9F , "Ameristar Technology" },
44
- {0x00 , 0x00 , 0xA2 , "Wellfleet" },
45
- {0x00 , 0x00 , 0xA3 , "Network Application Technology" },
46
- {0x00 , 0x00 , 0xA6 , "Network General" },
47
- {0x00 , 0x00 , 0xA7 , "NCD" },
48
- {0x00 , 0x00 , 0xA9 , "Network Systems" },
49
- {0x00 , 0x00 , 0xAA , "Xerox" },
50
- {0x00 , 0x00 , 0xB3 , "CIMLinc" },
51
- {0x00 , 0x00 , 0xB7 , "Dove Fastnet" },
52
- {0x00 , 0x00 , 0xBC , "Allen-Bradley" },
53
- {0x00 , 0x00 , 0xC0 , "Western Digital" },
54
- {0x00 , 0x00 , 0xC5 , "Farallon" },
55
- {0x00 , 0x00 , 0xC6 , "Hewlett-Packard" },
56
- {0x00 , 0x00 , 0xC8 , "Altos" },
57
- {0x00 , 0x00 , 0xC9 , "Emulex" },
58
- {0x00 , 0x00 , 0xD7 , "Dartmouth College" },
59
- {0x00 , 0x00 , 0xD8 , "3Com (?)" },
60
- {0x00 , 0x00 , 0xDD , "Gould" },
61
- {0x00 , 0x00 , 0xDE , "Unigraph" },
62
- {0x00 , 0x00 , 0xE2 , "Acer Counterpoint" },
63
- {0x00 , 0x00 , 0xEF , "Alantec" },
64
- {0x00 , 0x00 , 0xFD , "High Level Hardware" },
65
- {0x00 , 0x01 , 0x02 , "BBN internal usage" },
66
- {0x00 , 0x20 , 0xAF , "3Com" },
67
- {0x00 , 0x17 , 0x00 , "Kabel" },
68
- {0x00 , 0x80 , 0x64 , "Wyse Technology" },
69
- {0x00 , 0x80 , 0x2B , "IMAC (?)" },
70
- {0x00 , 0x80 , 0x2D , "Xylogics, Inc." },
71
- {0x00 , 0x80 , 0x8C , "Frontier Software Development" },
72
- {0x00 , 0x80 , 0xC2 , "IEEE 802.1 Committee" },
73
- {0x00 , 0x80 , 0xD3 , "Shiva" },
74
- {0x00 , 0xAA , 0x00 , "Intel" },
75
- {0x00 , 0xDD , 0x00 , "Ungermann-Bass" },
76
- {0x00 , 0xDD , 0x01 , "Ungermann-Bass" },
77
- {0x02 , 0x07 , 0x01 , "Racal InterLan" },
78
- {0x02 , 0x04 , 0x06 , "BBN internal usage" },
79
- {0x02 , 0x60 , 0x86 , "Satelcom MegaPac" },
80
- {0x02 , 0x60 , 0x8C , "3Com" },
81
- {0x02 , 0xCF , 0x1F , "CMC" },
82
- {0x08 , 0x00 , 0x02 , "3Com" },
83
- {0x08 , 0x00 , 0x03 , "ACC" },
84
- {0x08 , 0x00 , 0x05 , "Symbolics" },
85
- {0x08 , 0x00 , 0x08 , "BBN" },
86
- {0x08 , 0x00 , 0x09 , "Hewlett-Packard" },
87
- {0x08 , 0x00 , 0x0A , "Nestar Systems" },
88
- {0x08 , 0x00 , 0x0B , "Unisys" },
89
- {0x08 , 0x00 , 0x11 , "Tektronix" },
90
- {0x08 , 0x00 , 0x14 , "Excelan" },
91
- {0x08 , 0x00 , 0x17 , "NSC" },
92
- {0x08 , 0x00 , 0x1A , "Data General" },
93
- {0x08 , 0x00 , 0x1B , "Data General" },
94
- {0x08 , 0x00 , 0x1E , "Apollo" },
95
- {0x08 , 0x00 , 0x20 , "Sun" },
96
- {0x08 , 0x00 , 0x22 , "NBI" },
97
- {0x08 , 0x00 , 0x25 , "CDC" },
98
- {0x08 , 0x00 , 0x26 , "Norsk Data" },
99
- {0x08 , 0x00 , 0x27 , "PCS Computer Systems GmbH" },
100
- {0x08 , 0x00 , 0x28 , "Texas Instruments" },
101
- {0x08 , 0x00 , 0x2B , "DEC" },
102
- {0x08 , 0x00 , 0x2E , "Metaphor" },
103
- {0x08 , 0x00 , 0x2F , "Prime Computer" },
104
- {0x08 , 0x00 , 0x36 , "Intergraph" },
105
- {0x08 , 0x00 , 0x37 , "Fujitsu-Xerox" },
106
- {0x08 , 0x00 , 0x38 , "Bull" },
107
- {0x08 , 0x00 , 0x39 , "Spider Systems" },
108
- {0x08 , 0x00 , 0x41 , "DCA Digital Comm. Assoc." },
109
- {0x08 , 0x00 , 0x45 , "Xylogics (?)" },
110
- {0x08 , 0x00 , 0x46 , "Sony" },
111
- {0x08 , 0x00 , 0x47 , "Sequent" },
112
- {0x08 , 0x00 , 0x49 , "Univation" },
113
- {0x08 , 0x00 , 0x4C , "Encore" },
114
- {0x08 , 0x00 , 0x4E , "BICC" },
115
- {0x08 , 0x00 , 0x56 , "Stanford University" },
116
- {0x08 , 0x00 , 0x58 , "DECsystem 20 (?)" },
117
- {0x08 , 0x00 , 0x5A , "IBM" },
118
- {0x08 , 0x00 , 0x67 , "Comdesign" },
119
- {0x08 , 0x00 , 0x68 , "Ridge" },
120
- {0x08 , 0x00 , 0x69 , "Silicon Graphics" },
121
- {0x08 , 0x00 , 0x6E , "Concurrent" },
122
- {0x08 , 0x00 , 0x75 , "DDE" },
123
- {0x08 , 0x00 , 0x7C , "Vitalink" },
124
- {0x08 , 0x00 , 0x80 , "XIOS" },
125
- {0x08 , 0x00 , 0x86 , "Imagen/QMS" },
126
- {0x08 , 0x00 , 0x87 , "Xyplex" },
127
- {0x08 , 0x00 , 0x89 , "Kinetics" },
128
- {0x08 , 0x00 , 0x8B , "Pyramid" },
129
- {0x08 , 0x00 , 0x8D , "XyVision" },
130
- {0x08 , 0x00 , 0x90 , "Retix Inc" },
131
- {0x48 , 0x44 , 0x53 , "HDS (?)" },
132
- {0x80 , 0x00 , 0x10 , "AT&T" },
133
- {0xAA , 0x00 , 0x00 , "DEC" },
134
- {0xAA , 0x00 , 0x01 , "DEC" },
135
- {0xAA , 0x00 , 0x02 , "DEC" },
136
- {0xAA , 0x00 , 0x03 , "DEC" },
137
- {0xAA , 0x00 , 0x04 , "DEC" },
138
- {0x00 , 0x00 , 0x00 , NULL }
139
- };
140
-
141
12
/*
142
13
* Utility macros used for sorting and comparing:
143
14
*/
@@ -151,6 +22,7 @@ static manufacturer manufacturers[] = {
151
22
/*
152
23
* MAC address reader. Accepts several common notations.
153
24
*/
25
+
154
26
Datum
155
27
macaddr_in (PG_FUNCTION_ARGS )
156
28
{
@@ -205,6 +77,7 @@ macaddr_in(PG_FUNCTION_ARGS)
205
77
/*
206
78
* MAC address output function. Fixed format.
207
79
*/
80
+
208
81
Datum
209
82
macaddr_out (PG_FUNCTION_ARGS )
210
83
{
@@ -226,6 +99,57 @@ macaddr_out(PG_FUNCTION_ARGS)
226
99
PG_RETURN_CSTRING (result );
227
100
}
228
101
102
+ /* macaddr_text()
103
+ * Convert macaddr to text data type.
104
+ */
105
+
106
+ Datum
107
+ macaddr_text (PG_FUNCTION_ARGS )
108
+ {
109
+ /* Input is a macaddr, but may as well leave it in Datum form */
110
+ Datum addr = PG_GETARG_DATUM (0 );
111
+ text * result ;
112
+ char * str ;
113
+ int len ;
114
+
115
+ str = DatumGetCString (DirectFunctionCall1 (macaddr_out , addr ));
116
+
117
+ len = (strlen (str ) + VARHDRSZ );
118
+
119
+ result = palloc (len );
120
+
121
+ VARATT_SIZEP (result ) = len ;
122
+ memmove (VARDATA (result ), str , (len - VARHDRSZ ));
123
+
124
+ pfree (str );
125
+
126
+ PG_RETURN_TEXT_P (result );
127
+ }
128
+
129
+ /* text_macaddr()
130
+ * Convert text to macaddr data type.
131
+ */
132
+
133
+ Datum
134
+ text_macaddr (PG_FUNCTION_ARGS )
135
+ {
136
+ Datum result ;
137
+ text * addr = PG_GETARG_TEXT_P (0 );
138
+ char str [18 ];
139
+ int len ;
140
+
141
+ len = (VARSIZE (addr )- VARHDRSZ );
142
+ if (len >= 18 )
143
+ elog (ERROR , "Text is too long to convert to MAC address" );
144
+
145
+ memmove (str , VARDATA (addr ), len );
146
+ * (str + len ) = '\0' ;
147
+
148
+ result = DirectFunctionCall1 (macaddr_in , CStringGetDatum (str ));
149
+
150
+ return (result );
151
+ }
152
+
229
153
/*
230
154
* Comparison function for sorting:
231
155
*/
@@ -257,6 +181,7 @@ macaddr_cmp(PG_FUNCTION_ARGS)
257
181
/*
258
182
* Boolean comparisons.
259
183
*/
184
+
260
185
Datum
261
186
macaddr_lt (PG_FUNCTION_ARGS )
262
187
{
@@ -312,36 +237,24 @@ macaddr_ne(PG_FUNCTION_ARGS)
312
237
}
313
238
314
239
/*
315
- * The special manufacturer fetching function.
240
+ * Truncation function to allow comparing mac manufacturers.
241
+ * From suggestion by Alex Pilosov <alex@pilosoft.com>
316
242
*/
317
243
318
244
Datum
319
- macaddr_manuf (PG_FUNCTION_ARGS )
245
+ macaddr_trunc (PG_FUNCTION_ARGS )
320
246
{
247
+ macaddr * result ;
321
248
macaddr * addr = PG_GETARG_MACADDR_P (0 );
322
- manufacturer * manuf ;
323
- int length ;
324
- text * result ;
325
249
326
- for (manuf = manufacturers ; manuf -> name != NULL ; manuf ++ )
327
- {
328
- if ((manuf -> a == addr -> a ) &&
329
- (manuf -> b == addr -> b ) &&
330
- (manuf -> c == addr -> c ))
331
- break ;
332
- }
333
- if (manuf -> name == NULL )
334
- {
335
- /* Not known, so return empty string */
336
- result = palloc (VARHDRSZ );
337
- VARATT_SIZEP (result ) = VARHDRSZ ;
338
- }
339
- else
340
- {
341
- length = strlen (manuf -> name );
342
- result = palloc (length + VARHDRSZ );
343
- VARATT_SIZEP (result ) = length + VARHDRSZ ;
344
- memcpy (VARDATA (result ), manuf -> name , length );
345
- }
346
- PG_RETURN_TEXT_P (result );
250
+ result = (macaddr * ) palloc (sizeof (macaddr ));
251
+
252
+ result -> a = addr -> a ;
253
+ result -> b = addr -> b ;
254
+ result -> c = addr -> c ;
255
+ result -> d = 0 ;
256
+ result -> e = 0 ;
257
+ result -> f = 0 ;
258
+
259
+ PG_RETURN_MACADDR_P (result );
347
260
}
0 commit comments