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 <strings.h>
35
36/*
37 * Function: net_uint16_set
38 * Purpose:
39 *   Set a field in a structure that's at least 16 bits to the given
40 *   value, putting it into network byte order
41 */
42INLINE void
43net_uint16_set(uint8_t * field, uint16_t value)
44{
45    uint16_t tmp_value = htons(value);
46    bcopy((void *)&tmp_value, (void *)field,
47	  sizeof(uint16_t));
48    return;
49}
50
51/*
52 * Function: net_uint16_get
53 * Purpose:
54 *   Get a field in a structure that's at least 16 bits, converting
55 *   to host byte order.
56 */
57INLINE uint16_t
58net_uint16_get(const uint8_t * field)
59{
60    uint16_t tmp_field;
61
62    bcopy((void *)field, (void *)&tmp_field,
63	  sizeof(uint16_t));
64    return (ntohs(tmp_field));
65}
66
67/*
68 * Function: net_uint32_set
69 * Purpose:
70 *   Set a field in a structure that's at least 32 bits to the given
71 *   value, putting it into network byte order
72 */
73INLINE void
74net_uint32_set(uint8_t * field, uint32_t value)
75{
76    uint32_t tmp_value = htonl(value);
77
78    bcopy((void *)&tmp_value, (void *)field,
79	  sizeof(uint32_t));
80    return;
81}
82
83/*
84 * Function: net_uint32_get
85 * Purpose:
86 *   Get a field in a structure that's at least 32 bits, converting
87 *   to host byte order.
88 */
89INLINE uint32_t
90net_uint32_get(const uint8_t * field)
91{
92    uint32_t tmp_field;
93
94    bcopy((void *)field, &tmp_field,
95	  sizeof(uint32_t));
96    return (ntohl(tmp_field));
97}
98
99#endif /* _S_NBO_H */
100