[439] | 1 | #ifndef NETORDER_H
|
---|
| 2 | #define NETORDER_H
|
---|
| 3 |
|
---|
| 4 | #include "sysfuncs.h"
|
---|
| 5 |
|
---|
| 6 | /* [RPAP - Feb 97: WIN32 Port] */
|
---|
| 7 | #ifdef __WIN32__
|
---|
| 8 | #include "win32in.h"
|
---|
| 9 | #else
|
---|
| 10 | # include <netinet/in.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
| 13 | #if defined (BIG_ENDIAN) || defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN)
|
---|
| 14 | # ifndef _BIG_ENDIAN
|
---|
| 15 | # define _BIG_ENDIAN
|
---|
| 16 | # endif
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
| 19 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
| 20 | # ifndef _LITTLE_ENDIAN
|
---|
| 21 | # define _LITTLE_ENDIAN
|
---|
| 22 | # endif
|
---|
| 23 | #endif
|
---|
| 24 |
|
---|
| 25 | #ifdef _LITTLE_ENDIAN
|
---|
| 26 |
|
---|
| 27 | /* double */
|
---|
| 28 | #define HTOND(d) \
|
---|
| 29 | do { \
|
---|
| 30 | unsigned long tmph, tmpl; \
|
---|
| 31 | bcopy ((char *) &d, (char *) &tmph, sizeof(double) >> 1); \
|
---|
| 32 | bcopy ((char *) &d + (sizeof(double) >> 1), (char *) &tmpl, sizeof (double) >> 1); \
|
---|
| 33 | tmph = htonl (tmph); \
|
---|
| 34 | tmpl = htonl (tmpl); \
|
---|
| 35 | bcopy ((char *) &tmpl, (char *) &d, sizeof (double) >> 1); \
|
---|
| 36 | bcopy ((char *) &tmph, (char *) &d + (sizeof(double) >> 1), sizeof (double) >> 1); \
|
---|
| 37 | }while(0)
|
---|
| 38 | #define NTOHD(d) \
|
---|
| 39 | do { \
|
---|
| 40 | unsigned long tmph, tmpl; \
|
---|
| 41 | bcopy ((char *) &d, (char *) &tmph, sizeof(double) >> 1); \
|
---|
| 42 | bcopy ((char *) &d + (sizeof(double) >> 1), (char *) &tmpl, sizeof (double) >> 1); \
|
---|
| 43 | tmph = ntohl (tmph); \
|
---|
| 44 | tmpl = ntohl (tmpl); \
|
---|
| 45 | bcopy ((char *) &tmpl, (char *) &d, sizeof (double) >> 1); \
|
---|
| 46 | bcopy ((char *) &tmph, (char *) &d + (sizeof(double) >> 1), sizeof (double) >> 1); \
|
---|
| 47 | }while(0)
|
---|
| 48 | #define HTOND2(hd, nd) \
|
---|
| 49 | do { \
|
---|
| 50 | unsigned long tmph, tmpl; \
|
---|
| 51 | bcopy ((char *) &hd, (char *) &tmph, sizeof(double) >> 1); \
|
---|
| 52 | bcopy ((char *) &hd + (sizeof(double) >> 1), (char *) &tmpl, sizeof (double) >> 1); \
|
---|
| 53 | tmph = htonl (tmph); \
|
---|
| 54 | tmpl = htonl (tmpl); \
|
---|
| 55 | bcopy ((char *) &tmpl, (char *) &nd, sizeof (double) >> 1); \
|
---|
| 56 | bcopy ((char *) &tmph, (char *) &nd + (sizeof(double) >> 1), sizeof (double) >> 1); \
|
---|
| 57 | }while(0)
|
---|
| 58 | #define NTOHD2(nd, hd) \
|
---|
| 59 | do { \
|
---|
| 60 | unsigned long tmph, tmpl; \
|
---|
| 61 | bcopy ((char *) &nd, (char *) &tmph, sizeof(double) >> 1); \
|
---|
| 62 | bcopy ((char *) &nd + (sizeof(double) >> 1), (char *) &tmpl, sizeof (double) >> 1); \
|
---|
| 63 | tmph = ntohl (tmph); \
|
---|
| 64 | tmpl = ntohl (tmpl); \
|
---|
| 65 | bcopy ((char *) &tmpl, (char *) &hd, sizeof (double) >> 1); \
|
---|
| 66 | bcopy ((char *) &tmph, (char *) &hd + (sizeof(double) >> 1), sizeof (double) >> 1); \
|
---|
| 67 | }while(0)
|
---|
| 68 |
|
---|
| 69 | /* float */
|
---|
| 70 | #define HTONF(f) \
|
---|
| 71 | do { \
|
---|
| 72 | unsigned long tmp; \
|
---|
| 73 | bcopy ((char *) &(f), (char *) &tmp, sizeof (float)); \
|
---|
| 74 | HTONUL (tmp); \
|
---|
| 75 | bcopy ((char *) &tmp, (char *) &(f), sizeof (float)); \
|
---|
| 76 | }while(0)
|
---|
| 77 | #define NTOHF(f) \
|
---|
| 78 | do { \
|
---|
| 79 | unsigned long tmp; \
|
---|
| 80 | bcopy ((char *) &(f), (char *) &tmp, sizeof (float)); \
|
---|
| 81 | NTOHUL (tmp); \
|
---|
| 82 | bcopy ((char *) &tmp, (char *) &(f), sizeof (float)); \
|
---|
| 83 | }while(0)
|
---|
| 84 | #define HTONF2(hf, nf) \
|
---|
| 85 | do { \
|
---|
| 86 | unsigned long tmp; \
|
---|
| 87 | bcopy ((char *) &(hf), (char *) &tmp, sizeof (float)); \
|
---|
| 88 | HTONUL (tmp); \
|
---|
| 89 | bcopy ((char *) &tmp, (char *) &(nf), sizeof (float)); \
|
---|
| 90 | }while(0)
|
---|
| 91 | #define NTOHF2(nf, hf) \
|
---|
| 92 | do { \
|
---|
| 93 | unsigned long tmp; \
|
---|
| 94 | bcopy ((char *) &(nf), (char *) &tmp, sizeof (float)); \
|
---|
| 95 | NTOHUL (tmp); \
|
---|
| 96 | bcopy ((char *) &tmp, (char *) &(hf), sizeof (float)); \
|
---|
| 97 | }while(0)
|
---|
| 98 |
|
---|
| 99 | /* pointers */
|
---|
| 100 | #define HTONP(p) ((p) = (void *) htonl ((unsigned long) p))
|
---|
| 101 | #define NTOHP(p) ((p) = (void *) ntohl ((unsigned long) p))
|
---|
| 102 | #define HTONP2(hp, np) ((np) = (void *) htonl ((unsigned long) hp))
|
---|
| 103 | #define NTOHP2(np, hp) ((hp) = (void *) ntohl ((unsigned long) np))
|
---|
| 104 |
|
---|
| 105 | /* unsigned long */
|
---|
| 106 | #define HTONUL(l) ((l) = htonl((l)))
|
---|
| 107 | #define NTOHUL(l) ((l) = ntohl((l)))
|
---|
| 108 | #define HTONUL2(hl, nl) ((nl) = htonl ((hl)))
|
---|
| 109 | #define NTOHUL2(nl, hl) ((hl) = ntohl ((nl)))
|
---|
| 110 |
|
---|
| 111 | /* signed long */
|
---|
| 112 | #define HTONSL(l) ((l) = (long) htonl ((unsigned long) (l)))
|
---|
| 113 | #define NTOHSL(l) ((l) = (long) ntohl ((unsigned long) (l)))
|
---|
| 114 | #define HTONSL2(hl, nl) ((nl) = (long) htonl ((unsigned long) (hl)))
|
---|
| 115 | #define NTOHSL2(nl, hl) ((hl) = (long) ntohl ((unsigned long) (nl)))
|
---|
| 116 |
|
---|
| 117 | /* unsigned int */
|
---|
| 118 | #define HTONUI(i) ((i) = (unsigned int) htonl ((unsigned long) (i)))
|
---|
| 119 | #define NTOHUI(i) ((i) = (unsigned int) ntohl ((unsigned long) (i)))
|
---|
| 120 | #define HTONUI2(hi, ni) ((ni) = (unsigned int) htonl ((unsigned long) (hi)))
|
---|
| 121 | #define NTOHUI2(ni, hi) ((hi) = (unsigned int) ntohl ((unsigned long) (ni)))
|
---|
| 122 |
|
---|
| 123 | /* signed int */
|
---|
| 124 | #define HTONSI(i) ((i) = (int) htonl ((unsigned long) (i)))
|
---|
| 125 | #define NTOHSI(i) ((i) = (int) ntohl ((unsigned long) (i)))
|
---|
| 126 | #define HTONSI2(hi, ni) ((ni) = (int) htonl ((unsigned long) (hi)))
|
---|
| 127 | #define NTOHSI2(ni, hi) ((hi) = (int) ntohl ((unsigned long) (ni)))
|
---|
| 128 |
|
---|
| 129 | /* unsigned short */
|
---|
| 130 | #define HTONUS(s) ((s) = htons((s)))
|
---|
| 131 | #define NTOHUS(s) ((s) = ntohs((s)))
|
---|
| 132 | #define HTONUS2(hs, ns) ((ns) = htons((hs)))
|
---|
| 133 | #define NTOHUS2(ns, hs) ((hs) = ntohs((ns)))
|
---|
| 134 |
|
---|
| 135 | #else /* _BIG_ENDIAN */
|
---|
| 136 |
|
---|
| 137 | /* double */
|
---|
| 138 | #define HTOND(d) (d)
|
---|
| 139 | #define NTOHD(d) (d)
|
---|
| 140 | #define HTOND2(hd, nd) ((hd) = (nd))
|
---|
| 141 | #define NTOHD2(nd, hd) ((nd) = (hd))
|
---|
| 142 |
|
---|
| 143 | /* float */
|
---|
| 144 | #define HTONF(f) (f)
|
---|
| 145 | #define NTOHF(f) (f)
|
---|
| 146 | #define HTONF2(hf, nf) ((nf) = (hf))
|
---|
| 147 | #define NTOHF2(nf, hf) ((hf) = (nf))
|
---|
| 148 |
|
---|
| 149 | /* pointers */
|
---|
| 150 | #define HTONP(p) (p)
|
---|
| 151 | #define NTOHP(p) (p)
|
---|
| 152 | #define HTONP2(hp, np) ((np) = (hp))
|
---|
| 153 | #define NTOHP2(np, hp) ((hp) = (np))
|
---|
| 154 |
|
---|
| 155 | /* unsigned long */
|
---|
| 156 | #define HTONUL(l) (l)
|
---|
| 157 | #define NTOHUL(l) (l)
|
---|
| 158 | #define HTONUL2(hl, nl) ((nl) = (hl))
|
---|
| 159 | #define NTOHUL2(nl, hl) ((hl) = (nl))
|
---|
| 160 |
|
---|
| 161 | /* signed long */
|
---|
| 162 | #define HTONSL(l) (l)
|
---|
| 163 | #define NTOHSL(l) (l)
|
---|
| 164 | #define HTONSL2(hl, nl) ((nl) = (hl))
|
---|
| 165 | #define NTOHSL2(nl, hl) ((hl) = (nl))
|
---|
| 166 |
|
---|
| 167 | /* unsigned int */
|
---|
| 168 | #define HTONUI(i) (i)
|
---|
| 169 | #define NTOHUI(i) (i)
|
---|
| 170 | #define HTONUI2(hi, ni) ((ni) = (hi))
|
---|
| 171 | #define NTOHUI2(ni, hi) ((hi) = (ni))
|
---|
| 172 |
|
---|
| 173 | /* signed int */
|
---|
| 174 | #define HTONSI(i) (i)
|
---|
| 175 | #define NTOHSI(i) (i)
|
---|
| 176 | #define HTONSI2(hi, ni) ((ni) = (hi))
|
---|
| 177 | #define NTOHSI2(ni, hi) ((hi) = (ni))
|
---|
| 178 |
|
---|
| 179 | /* unsigned short */
|
---|
| 180 | #define HTONUS(s) (s)
|
---|
| 181 | #define NTOHUS(s) (s)
|
---|
| 182 | #define HTONUS2(hs, ns) ((ns) = (hs))
|
---|
| 183 | #define NTOHUS2(ns, hs) ((hs) = (ns))
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | #endif
|
---|
| 188 |
|
---|
| 189 | #endif /* netorder.h */
|
---|