1/*
2 * Copyright (c) 2009-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * nbo.h
26 * - network byte order
27 * - inlines to set/get values to/from network byte order
28 */
29
30#ifndef _S_NBO_H
31#define _S_NBO_H
32
33#include "symbol_scope.h"
34#include <stdint.h>
35#include <strings.h>
36#include <sys/_endian.h>
37
38/*
39 * Function: net_uint16_set
40 * Purpose:
41 *   Set a field in a structure that's at least 16 bits to the given
42 *   value, putting it into network byte order
43 */
44INLINE void
45net_uint16_set(uint8_t * field, uint16_t value)
46{
47    uint16_t tmp_value = htons(value);
48    bcopy((void *)&tmp_value, (void *)field,
49	  sizeof(uint16_t));
50    return;
51}
52
53/*
54 * Function: net_uint16_get
55 * Purpose:
56 *   Get a field in a structure that's at least 16 bits, converting
57 *   to host byte order.
58 */
59INLINE uint16_t
60net_uint16_get(const uint8_t * field)
61{
62    uint16_t tmp_field;
63
64    bcopy((void *)field, (void *)&tmp_field,
65	  sizeof(uint16_t));
66    return (ntohs(tmp_field));
67}
68
69/*
70 * Function: net_uint32_set
71 * Purpose:
72 *   Set a field in a structure that's at least 32 bits to the given
73 *   value, putting it into network byte order
74 */
75INLINE void
76net_uint32_set(uint8_t * field, uint32_t value)
77{
78    uint32_t tmp_value = htonl(value);
79
80    bcopy((void *)&tmp_value, (void *)field,
81	  sizeof(uint32_t));
82    return;
83}
84
85/*
86 * Function: net_uint32_get
87 * Purpose:
88 *   Get a field in a structure that's at least 32 bits, converting
89 *   to host byte order.
90 */
91INLINE uint32_t
92net_uint32_get(const uint8_t * field)
93{
94    uint32_t tmp_field;
95
96    bcopy((void *)field, &tmp_field,
97	  sizeof(uint32_t));
98    return (ntohl(tmp_field));
99}
100
101#endif /* _S_NBO_H */
102