The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * 3. All advertising materials mentioning features or use of this software |
| 13 | * must display the following acknowledgement: |
| 14 | * This product includes software developed by the University of |
| 15 | * California, Berkeley and its contributors. |
| 16 | * 4. Neither the name of the University nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | */ |
| 32 | #ifndef _SLIRP_HELPER_H |
| 33 | #define _SLIRP_HELPER_H |
| 34 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 35 | #ifdef _WIN32 |
| 36 | # include <winsock2.h> /* for ntohl */ |
| 37 | #endif |
| 38 | |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 39 | typedef union { |
| 40 | u_int32_t addr; |
| 41 | u_int8_t data[4]; |
| 42 | } ipaddr_t; |
| 43 | |
| 44 | /* return ip address in network order */ |
| 45 | static __inline__ uint32_t |
| 46 | ip_getn( ipaddr_t ip ) |
| 47 | { |
| 48 | return ip.addr; |
| 49 | } |
| 50 | |
| 51 | /* return ip address in host order */ |
| 52 | static __inline__ uint32_t |
| 53 | ip_geth( ipaddr_t ip ) |
| 54 | { |
| 55 | return ntohl(ip.addr); |
| 56 | } |
| 57 | |
| 58 | /* set ip address in network order */ |
| 59 | static __inline__ ipaddr_t |
| 60 | ip_setn( uint32_t val ) |
| 61 | { |
| 62 | ipaddr_t ip; |
| 63 | ip.addr = val; |
| 64 | return ip; |
| 65 | } |
| 66 | |
| 67 | /* set ip address in host order */ |
| 68 | static __inline__ ipaddr_t |
| 69 | ip_seth( uint32_t val ) |
| 70 | { |
| 71 | ipaddr_t ip; |
| 72 | ip.addr = htonl(val); |
| 73 | return ip; |
| 74 | } |
| 75 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame] | 76 | static __inline__ ipaddr_t |
| 77 | ip_read( const void* buf ) |
| 78 | { |
| 79 | ipaddr_t ip; |
| 80 | memcpy(ip.data, buf, 4); |
| 81 | return ip; |
| 82 | } |
| 83 | |
| 84 | static __inline__ void |
| 85 | ip_write( ipaddr_t ip, void* buf ) |
| 86 | { |
| 87 | memcpy(buf, ip.data, 4); |
| 88 | } |
| 89 | |
| 90 | static __inline__ uint32_t |
| 91 | ip_read32h( const void* buf ) |
| 92 | { |
| 93 | ipaddr_t addr = ip_read(buf); |
| 94 | return ip_geth(addr); |
| 95 | } |
| 96 | |
| 97 | static __inline__ void |
| 98 | ip_write32h( uint32_t ip, void* buf ) |
| 99 | { |
| 100 | ipaddr_t addr = ip_seth(ip); |
| 101 | ip_write(addr, buf); |
| 102 | } |
| 103 | |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 104 | static __inline__ int |
| 105 | ip_equal( ipaddr_t ip1, ipaddr_t ip2 ) |
| 106 | { |
| 107 | return ip1.addr == ip2.addr; |
| 108 | } |
| 109 | |
| 110 | typedef union { |
| 111 | u_int16_t port; |
| 112 | u_int8_t data[2]; |
| 113 | } port_t; |
| 114 | |
| 115 | static __inline__ uint16_t |
| 116 | port_getn( port_t p ) |
| 117 | { |
| 118 | return p.port; |
| 119 | } |
| 120 | |
| 121 | static __inline__ uint16_t |
| 122 | port_geth( port_t p ) |
| 123 | { |
| 124 | return ntohs(p.port); |
| 125 | } |
| 126 | |
| 127 | static __inline__ port_t |
| 128 | port_setn( uint16_t val ) |
| 129 | { |
| 130 | port_t p; |
| 131 | p.port = val; |
| 132 | return p; |
| 133 | } |
| 134 | |
| 135 | static __inline__ port_t |
| 136 | port_seth( uint16_t val ) |
| 137 | { |
| 138 | port_t p; |
| 139 | p.port = htons(val); |
| 140 | return p; |
| 141 | } |
| 142 | |
| 143 | #endif /* _SLIRP_HELPER_H */ |