The Netsukuku Project  0.0.9
An Alternative routing method
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
endianness.h
Go to the documentation of this file.
1 /* This file is part of Netsukuku
2  * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
3  *
4  * This source code is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This source code is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * Please refer to the GNU Public License for more details.
13  *
14  * You should have received a copy of the GNU Public License along with
15  * this source code; if not, write to:
16  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #ifndef ENDIANNESS_H
20 #define ENDIANNESS_H
21 
22 #define MAX_INTS_PER_STRUCT 8 /* The maximum number of short/int variables
23  present in a struct */
24 
25 #define IINFO_DYNAMIC_VALUE -1 /* This define is used to fill part in a
26  int_info struct that must be set each time.
27  If that part is not set, -1 will remain, and
28  the int_info functions will call fatal().
29  Therefore this is useful to track bugs. */
30 
31 
32 /* flags for int_info.int_type */
33 #define INT_TYPE_VOID 0 /* Emptiness is loneliness, and loneliness is
34  cleanliness */
35 #define INT_TYPE_32BIT 1 /* The int var is of 32 bits */
36 #define INT_TYPE_16BIT (1<<1) /* The int var is of 16 bits */
37 #define INT_TYPE_WORDS (1<<2) /* The int var is composed by an array of ints,
38  like the ipv6 ip (struct in6_addr) */
39 #define INT_TYPE_NETWORK (1<<3) /* The int var is stored in network order */
40 
41 /*
42  * int_info: this struct is used to keep the information about the int/short
43  * variables present in a struct. It is useful to convert all the int/short
44  * vars in another endian format with a simple function.
45  * WARNING: There is a drawback: the struct must have the __packed__
46  * attribute (but since we are using this for packet structs we don't care).
47  *
48  * Here there is an example which show how to use this int_info:
49  *
50  * given the struct s:
51  * struct
52  * {
53  * u_char a;
54  * int b;
55  * short c;
56  * char d[23];
57  * int e[4];
58  * }__attribute__ ((__packed__)) s;
59  *
60  * its int_info struct should be filled in this way:
61  *
62  * int_info s_int_info = { 3,
63  * {INT_TYPE_32BIT, INT_TYPE_16BIT, INT_TYPE_32BIT},
64  * { sizeof(char), sizeof(char)+sizeof(int),
65  * sizeof(char)+sizeof(int)+sizeof(short)+sizeof(char)*23},
66  * { 1, 1, 4 }
67  * };
68  */
69 typedef struct
70 {
71  /* The total int/short vars present in the struct */
72  int total_ints;
73 
74  /* Each member in the int_type array corresponds to a int/short var
75  * and it is set using the above INT_TYPE_ flags */
76  char int_type[MAX_INTS_PER_STRUCT];
77 
78  /* Each member in the int_offset array specifies the amount of bytes
79  * to be added at the end of the struct to get the relative int/short
80  * var. */
81  size_t int_offset[MAX_INTS_PER_STRUCT];
82 
83  /* int_nmemb[x] is equal to the number of consecutive ints/shorts var,
84  * which start at the int_offset[x] offset. */
85  size_t int_nmemb[MAX_INTS_PER_STRUCT];
86 
87 } int_info;
88 
89 /* Useful to declare constant static int_info structs in .h files */
90 #define INT_INFO const static int_info
91 
92 #if BYTE_ORDER == LITTLE_ENDIAN
93 #include <linux/byteorder/little_endian.h>
94 #else
95 #include <linux/byteorder/big_endian.h>
96 #endif
97 
98 
99 /* * * Functions declaration * * */
100 void *int_info_copy(int_info *dst, const int_info *src);
101 void ints_array_htons(short *netshort, int nmemb);
102 void ints_array_ntohs(short *hostshort, int nmemb);
103 void ints_array_htonl(int *netlong, int nmemb);
104 void ints_array_ntohl(int *hostlong, int nmemb);
105 void ints_network_to_host(void *s, int_info iinfo);
106 void ints_host_to_network(void *s, int_info iinfo);
107 void ints_printf(void *s, int_info iinfo, void(*print_func(const char *, ...)));
108 
109 #endif /*ENDIANNESS_H*/
void ints_printf(void *s, int_info iinfo, void(*print_func(const char *,...)))
Definition: endianness.c:186
void ints_array_htons(short *netshort, int nmemb)
Definition: endianness.c:80
void ints_array_ntohs(short *hostshort, int nmemb)
Definition: endianness.c:70
void * int_info_copy(int_info *dst, const int_info *src)
Definition: endianness.c:45
Definition: endianness.h:69
#define MAX_INTS_PER_STRUCT
Definition: endianness.h:22
void ints_array_htonl(int *netlong, int nmemb)
Definition: endianness.c:60
void ints_array_ntohl(int *hostlong, int nmemb)
Definition: endianness.c:50
void ints_host_to_network(void *s, int_info iinfo)
Definition: endianness.c:143
int total_ints
Definition: endianness.h:72
void ints_network_to_host(void *s, int_info iinfo)
Definition: endianness.c:96