File: net_types.h

package info (click to toggle)
fcoe-utils 1.0.34-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,040 kB
  • sloc: ansic: 12,049; sh: 1,302; makefile: 80
file content (440 lines) | stat: -rw-r--r-- 11,737 bytes parent folder | download | duplicates (8)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * Copyright (c) 2008, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef _LIBSA_NET_TYPES_H_
#define	_LIBSA_NET_TYPES_H_

#if !defined(NTOHL)
#include <netinet/in.h>
#endif /* NTOHL */

/*
 * Type definitions for network order fields in protocol packets.
 * The access functions below do gets and puts on these structures.
 */
typedef unsigned char net8_t;		/* direct use and assignment allowed */

/*
 * Aligned network order types.
 */
typedef struct {
	u_int16_t	net_data;
} net16_t;

typedef struct {
	u_int32_t	net_data;
} net32_t;

/*
 * The 64-bit type only requires 32-bit alignment.
 */
typedef struct {
	u_int32_t	net_data[2];	/* most significant word first */
} net64_t;

/*
 * 24-bit type.  Byte aligned, in spite of the name.
 */
typedef struct {
	unsigned char	net_data[3];
} net24_t;

/*
 * 48-bit type.  Byte aligned.
 */
typedef struct {
	unsigned char	net_data[6];
} net48_t;

/*
 * Unaligned network order types.
 * Any of these structures can be byte aligned.  No padding is implied.
 */
typedef struct {
	unsigned char	net_data[2];
} ua_net16_t;

typedef struct {
	unsigned char	net_data[4];
} ua_net32_t;

typedef struct {
	unsigned char	net_data[8];
} ua_net64_t;

/*
 * Accessor functions.
 */

/**
 * net8_get(net) - fetch from a network-order 8-bit field.
 *
 * @param net pointer to network-order 8-bit data.
 * @return the host-order value.
 */
static inline u_int8_t net8_get(const net8_t * net)
{
	return *net;
}

/**
 * net8_put(net, val) - store to a network-order 8-bit field.
 *
 * @param net pointer to network-order 8-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void net8_put(net8_t * net, u_int8_t val)
{
	*net = val;
}

/**
 * net16_get(net) - fetch from a network-order 16-bit field.
 *
 * @param net pointer to type net16_t, network-order 16-bit data.
 * @return the host-order value.
 */
static inline u_int16_t net16_get(const net16_t * net)
{
	return ntohs(net->net_data);
}

/**
 * net16_put(net, val) - store to a network-order 16-bit field.
 *
 * @param net pointer to a net16_t, network-order 16-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void net16_put(net16_t * net, u_int16_t val)
{
	net->net_data = htons(val);
}

/**
 * ua_net16_get(net) - fetch from an unaligned network-order 16-bit field.
 *
 * @param net pointer to type ua_net16_t, unaligned, network-order 16-bit data.
 * @return the host-order value.
 */
static inline u_int16_t ua_net16_get(const ua_net16_t * net)
{
	return (net->net_data[0] << 8) | net->net_data[1];
}

/**
 * ua_net16_put(net, val) - store to a network-order 16-bit field.
 *
 * @param net pointer to a ua_net16_t, network-order 16-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void ua_net16_put(ua_net16_t * net, u_int16_t val)
{
	net->net_data[0] = (u_int8_t)((val >> 8) & 0xFF);
	net->net_data[1] = (u_int8_t)(val & 0xFF);
}

/**
 * net24_get(net) - fetch from a network-order 24-bit field.
 *
 * @param net pointer to type net24_t, network-order 24-bit data.
 * @return the host-order value.
 */
static inline u_int32_t net24_get(const net24_t * net)
{
	return (net->net_data[0] << 16) |
		(net->net_data[1] << 8) | net->net_data[2];
}

/**
 * net24_put(net, val) - store to a network-order 24-bit field.
 *
 * @param net pointer to a net24_t, network-order 24-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void net24_put(net24_t * net, u_int32_t val)
{
	net->net_data[0] = (u_int8_t)((val >> 16) & 0xFF);
	net->net_data[1] = (u_int8_t)((val >> 8) & 0xFF);
	net->net_data[2] = (u_int8_t)(val & 0xFF);
}

/**
 * net32_get(net) - fetch from a network-order 32-bit field.
 *
 * @param net pointer to type net32_t, network-order 32-bit data.
 * @return the host-order value.
 */
static inline u_int32_t net32_get(const net32_t * net)
{
	return ntohl(net->net_data);
}

/**
 * net32_put(net, val) - store to a network-order 32-bit field.
 *
 * @param net pointer to a net32_t, network-order 32-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void net32_put(net32_t * net, u_int32_t val)
{
	net->net_data = htonl(val);
}

/**
 * ua_net32_get(net) - fetch from an unaligned network-order 32-bit field.
 *
 * @param net pointer to type ua_net32_t, unaligned, network-order 32-bit data.
 * @return the host-order value.
 */
static inline u_int32_t ua_net32_get(const ua_net32_t * net)
{
	return (net->net_data[0] << 24) | (net->net_data[1] << 16) |
		(net->net_data[2] << 8) | net->net_data[3];
}

/**
 * ua_net32_put(net, val) - store to a network-order 32-bit field.
 *
 * @param net pointer to a ua_net32_t, network-order 32-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void ua_net32_put(ua_net32_t * net, u_int32_t val)
{
	net->net_data[0] = (u_int8_t)((val >> 24) & 0xFF);
	net->net_data[1] = (u_int8_t)((val >> 16) & 0xFF);
	net->net_data[2] = (u_int8_t)((val >> 8) & 0xFF);
	net->net_data[3] = (u_int8_t)(val & 0xFF);
}

/**
 * net48_get(net) - fetch from a network-order 48-bit field.
 *
 * @param net pointer to type net48_t, network-order 48-bit data.
 * @return the host-order value.
 */
static inline u_int64_t net48_get(const net48_t * net)
{
	return ((u_int64_t) net->net_data[0] << 40) |
		((u_int64_t) net->net_data[1] << 32) |
		((u_int64_t) net->net_data[2] << 24) |
		((u_int64_t) net->net_data[3] << 16) |
		((u_int64_t) net->net_data[4] << 8) |
		(u_int64_t) net->net_data[5];
}

/**
 * net48_put(net, val) - store to a network-order 48-bit field.
 *
 * @param net pointer to a net48_t, network-order 48-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void net48_put(net48_t * net, u_int64_t val)
{
	net->net_data[0] = (u_int8_t)((val >> 40) & 0xFF);
	net->net_data[1] = (u_int8_t)((val >> 32) & 0xFF);
	net->net_data[2] = (u_int8_t)((val >> 24) & 0xFF);
	net->net_data[3] = (u_int8_t)((val >> 16) & 0xFF);
	net->net_data[4] = (u_int8_t)((val >> 8) & 0xFF);
	net->net_data[5] = (u_int8_t)(val & 0xFF);
}

/**
 * net64_get(net) - fetch from a network-order 64-bit field.
 *
 * @param net pointer to type net64_t, network-order 64-bit data.
 * @return the host-order value.
 */
static inline u_int64_t net64_get(const net64_t * net)
{
	return ((u_int64_t) ntohl(net->net_data[0]) << 32) |
		ntohl(net->net_data[1]);
}

/**
 * net64_put(net, val) - store to a network-order 64-bit field.
 *
 * @param net pointer to a net64_t, network-order 64-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void net64_put(net64_t * net, u_int64_t val)
{
	net->net_data[0] = (u_int32_t)htonl(val >> 32);
	net->net_data[1] = (u_int32_t)htonl((u_int32_t) val);
}

/**
 * ua_net64_get(net) - fetch from an unaligned network-order 64-bit field.
 *
 * @param net pointer to type ua_net64_t, unaligned, network-order 64-bit data.
 * @return the host-order value.
 */
static inline u_int64_t ua_net64_get(const ua_net64_t * net)
{
	return ((u_int64_t) net->net_data[0] << 56) |
		((u_int64_t) net->net_data[1] << 48) |
		((u_int64_t) net->net_data[2] << 40) |
		((u_int64_t) net->net_data[3] << 32) |
		((u_int64_t) net->net_data[4] << 24) |
		((u_int64_t) net->net_data[5] << 16) |
		((u_int64_t) net->net_data[6] << 8) |
		(u_int64_t) net->net_data[7];
}

/**
 * ua_net64_put(net, val) - store to a network-order 64-bit field.
 *
 * @param net pointer to a ua_net64_t, network-order 64-bit data.
 * @param val host-order value to be stored at net.
 */
static inline void ua_net64_put(ua_net64_t * net, u_int64_t val)
{
	net->net_data[0] = (u_int8_t)((val >> 56) & 0xFF);
	net->net_data[1] = (u_int8_t)((val >> 48) & 0xFF);
	net->net_data[2] = (u_int8_t)((val >> 40) & 0xFF);
	net->net_data[3] = (u_int8_t)((val >> 32) & 0xFF);
	net->net_data[4] = (u_int8_t)((val >> 24) & 0xFF);
	net->net_data[5] = (u_int8_t)((val >> 16) & 0xFF);
	net->net_data[6] = (u_int8_t)((val >> 8) & 0xFF);
	net->net_data[7] = (u_int8_t)(val & 0xFF);
}

/*
 * Compile-time initializers for the network-order type structures.
 * Note that the upper byte of these values is not masked so the
 * compiler will catch initializers that don't fit in the field.
 */

/**
 * NET8_INIT(_val) - initialize a net8_t type.
 *
 * @param _val 8-bit value.
 * @return net8_t network-order value.
 */
#define	NET8_INIT(_val)     (_val)

/**
 * NET24_INIT(_val) - initialize a net24_t type.
 *
 * @param _val host-order value.
 * @return net24_t network-order value.
 */
#define	NET24_INIT(_val)    { {				    \
				((_val) >> 16),		    \
				((_val) >> 8) & 0xff,	    \
				((_val) >> 0) & 0xff	    \
			    } }

/**
 * NET48_INIT(_val) - initialize a net48_t type.
 *
 * @param _val host-order value.
 * @return net48_t network-order value.
 */
#define	NET48_INIT(_val)    { {				    \
				((_val) >> 40),		    \
				((_val) >> 32) & 0xff,	    \
				((_val) >> 24) & 0xff,	    \
				((_val) >> 16) & 0xff,	    \
				((_val) >> 8) & 0xff,	    \
				((_val) >> 0) & 0xff	    \
			    } }

/**
 * NET16_INIT(_val) - initialize a net16_t type.
 *
 * @param _val host-order value.
 * @return net16_t network-order value.
 */
#define	NET16_INIT(_val)    {	htons(_val) }

/**
 * UA_NET16_INIT(_val) - initialize an unaligned 16-bit type.
 *
 * @param _val host-order value.
 * @return ua_net24_t network-order value.
 */
#define	UA_NET16_INIT(_val) { {				    \
				((_val) >> 8),		    \
				((_val) >> 0) & 0xff	    \
			    } }

/**
 * NET32_INIT(_val) - initialize a 32-bit type.
 *
 * @param _val host-order value.
 * @return net32_t network-order value.
 */
#define	NET32_INIT(_val)    {	htonl(_val) }

/**
 * UA_NET32_INIT(_val) - initialize an unaligned 32-bit type.
 *
 * @param _val host-order value.
 * @return ua_net32_t network-order value.
 */
#define	UA_NET32_INIT(_val) { {				    \
				((_val) >> 24),		    \
				((_val) >> 16) & 0xff,	    \
				((_val) >> 8) & 0xff,	    \
				((_val) >> 0) & 0xff	    \
			    } }

/**
 * UA_NET48_INIT(_val) - initialize an unaligned 48-bit type.
 *
 * @param _val host-order value.
 * @return ua_net48_t network-order value.
 */
#define	UA_NET48_INIT(_val) { {				    \
				((_val) >> 40),		    \
				((_val) >> 32) & 0xff,	    \
				((_val) >> 24) & 0xff,	    \
				((_val) >> 16) & 0xff,	    \
				((_val) >> 8) & 0xff,	    \
				((_val) >> 0) & 0xff	    \
			    } }

/**
 * NET64_INIT(_val) - initialize an unaligned 64-bit type.
 *
 * @param _val host-order value.
 * @return ua_net64_t network-order value.
 */
#define	NET64_INIT(_val)    { {				    \
				htonl((_val) >> 32),	    \
				htonl((_val) & 0xffffffff)  \
			    } }

/**
 * UA_NET64_INIT(_val) - initialize a 64-bit type.
 *
 * @param _val host-order value.
 * @return net64_t network-order value.
 */
#define	UA_NET64_INIT(_val) { {				     \
				((_val) >> 56),		    \
				((_val) >> 48) & 0xff,	    \
				((_val) >> 40) & 0xff,	    \
				((_val) >> 32) & 0xff,	    \
				((_val) >> 24) & 0xff,	    \
				((_val) >> 16) & 0xff,	    \
				((_val) >> 8) & 0xff,	    \
				((_val) >> 0) & 0xff	    \
			    } }

#endif /* _LIBSA_NET_TYPES_H_ */