1 
2 /* ********************************************************************	*
3  * ni_fixups.h	version 0.02 2-25-09					*
4  *									*
5  *     COPYRIGHT 2008-2009 Michael Robinton <michael@bizsystems.com>	*
6  *									*
7  * This program is free software; you can redistribute it and/or modify	*
8  * it under the terms of either:					*
9  *									*
10  *  a) the GNU General Public License as published by the Free		*
11  *  Software Foundation; either version 2, or (at your option) any	*
12  *  later version, or							*
13  *									*
14  *  b) the "Artistic License" which comes with this distribution.	*
15  *									*
16  * This program is distributed in the hope that it will be useful,	*
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of	*
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either	*
19  * the GNU General Public License or the Artistic License for more 	*
20  * details.								*
21  *									*
22  * You should have received a copy of the Artistic License with this	*
23  * distribution, in the file named "Artistic".  If not, I'll be glad 	*
24  * to provide one.							*
25  *									*
26  * You should also have received a copy of the GNU General Public 	*
27  * License along with this program in the file named "Copying". If not, *
28  * write to the 							*
29  *									*
30  *	Free Software Foundation, Inc.					*
31  *	59 Temple Place, Suite 330					*
32  *	Boston, MA  02111-1307, USA					*
33  *									*
34  * or visit their web page on the internet at:				*
35  *									*
36  *	http://www.gnu.org/copyleft/gpl.html.				*
37  * ********************************************************************	*/
38 
39 #ifndef _NI_FIXUPS_H
40 #define _NI_FIXUPS_H 1
41 
42 #ifndef HAVE_CADDR_T
43 #error 'caddr_t' undefined on this platform
44 #endif
45 
46 /* **************************************************** *
47  *	if this OS has buggy memory allocation, fix it	*
48  * **************************************************** */
49 
50 #if HAVE_MALLOC == 0 || HAVE_REALLOC == 0
51 
52 #undef malloc
53 #undef calloc
54 #undef realloc
55 #undef free
56 
57 void * ni_rpl_malloc();
58 void * ni_rpl_calloc();
59 void * ni_rpl_realloc();
60 void ni_rpl_free();
61 
62 #define malloc ni_rpl_malloc
63 #define calloc ni_rpl_calloc
64 #define realloc ni_rpl_realloc
65 #define free ni_rpl_free
66 
67 #warning FUNCTIONS calloc, malloc, realloc, free re-defined because of buggy C lib
68 
69 #endif	/* HAVE_MALLOC == 0 || HAVE_REALLOC == 0	*/
70 
71 /* **************************************************** *
72  *	If field sa_len is missing and there is		*
73  *	no OS supplied work-around, do it here		*
74  * **************************************************** */
75 
76 #ifndef SA_LEN
77 # ifndef HAVE_SA_LEN
78 static int
__libc_sa_len(const sa_family_t af)79 __libc_sa_len (const sa_family_t af)
80 {
81    switch(af)
82    {
83 #   ifdef LOCAL_SIZEOF_SOCKADDR_IN
84     case AF_INET:
85 	return LOCAL_SIZEOF_SOCKADDR_IN;
86 #   endif
87 #   ifdef LOCAL_SIZEOF_SOCKADDR_AT
88     case AF_APPLETALK:
89 	return LOCAL_SIZEOF_SOCKADDR_AT;
90 #   endif
91 #   ifdef LOCAL_SIZEOF_SOCKADDR_ASH
92     case AF_ASH:
93 	return LOCAL_SIZEOF_SOCKADDR_ASH;
94 #   endif
95 #   ifdef LOCAL_SIZEOF_SOCKADDR_X25
96     case AF_X25:
97 	return LOCAL_SIZEOF_SOCKADDR_X25;
98 #   endif
99 #   ifdef LOCAL_SIZEOF_SOCKADDR_EC
100     case AF_ECONET:
101 	return LOCAL_SIZEOF_SOCKADDR_EC;
102 #   endif
103 #   ifdef LOCAL_SIZEOF_SOCKADDR_IN6
104     case AF_INET6:
105 	return LOCAL_SIZEOF_SOCKADDR_IN6;
106 #   endif
107 #   ifdef LOCAL_SIZEOF_SOCKADDR_IPX
108     case AF_IPX:
109 	return LOCAL_SIZEOF_SOCKADDR_IPX;
110 #   endif
111 #   ifdef LOCAL_SIZEOF_SOCKADDR_UN
112     case AF_LOCAL:		/* also AF_UNIX, AF_FILE */
113 	return LOCAL_SIZEOF_SOCKADDR_UN;
114 #   endif
115 #   ifdef LOCAL_SIZEOF_SOCKADDR_LL
116     case AF_PACKET:
117 	return LOCAL_SIZEOF_SOCKADDR_LL;
118 #   endif
119 #   ifdef LOCAL_SIZEOF_SOCKADDR_ROSE
120     case AF_ROSE:
121 	return LOCAL_SIZEOF_SOCKADDR_ROSE;
122 #   endif
123 #   ifdef LOCAL_SIZEOF_SOCKADDR_DL
124     case AF_LINK:
125 	return LOCAL_SIZEOF_SOCKADDR_DL;
126 #   endif
127 /*	This one is the same as AF_INET
128  * #ifdef LOCAL_SIZEOF_SOCKADDR_INARP
129  *   case AF_
130  *	return LOCAL_SIZEOF_SOCKADDR_INARP;
131  * #endif
132  */
133 /*	Multiple socket families use ISO, some conflict	*/
134 #   ifdef LOCAL_SIZEOF_SOCKADDR_ISO
135     case AF_ISO:
136 	return LOCAL_SIZEOF_SOCKADDR_ISO;
137 #   endif
138 #   ifdef LOCAL_SIZEOF_SOCKADDR_TP
139     case AF_ISO:
140 	return LOCAL_SIZEOF_SOCKADDR_TP;
141 #   endif
142 #   ifdef LOCAL_SIZEOF_SOCKADDR_EON
143     case AF_ISO:
144 	return LOCAL_SIZEOF_SOCKADDR_EON;
145 #   endif
146 #   ifdef LOCAL_SIZEOF_SOCKADDR_OSITP
147     case AF_ISO:
148 	return LOCAL_SIZEOF_SOCKADDR_OSITP;
149 #   endif
150 #   ifdef LOCAL_SIZEOF_SOCKADDR_NS
151     case AF_NUTSS:
152 	return LOCAL_SIZEOF_SOCKADDR_NS;
153 #   endif
154 #   ifdef LOCAL_SIZEOF_SOCKADDR_AX25
155     case AF_AX25:
156 	return LOCAL_SIZEOF_SOCKADDR_AX25;
157 #   endif
158 #   ifdef LOCAL_SIZEOF_SOCKADDR_DECnet
159     case AF_DECnet:
160 	return LOCAL_SIZEOF_SOCKADDR_DECnet;
161 #   endif
162    }
163    return 0;
164 }
165 # define SA_LEN(sa) __libc_sa_len((sa)->sa_family)
166 # else
167 # define SA_LEN(sa) ((sa)->sa_len)
168 # endif
169 #endif
170 
171 /* **************************************************** *
172  * If the OS does not supply _SIZE_OF_ADDR_IFREQ use	*
173  * this universal model for ifreq, in6_ifreq, lifreq	*
174  * **************************************************** */
175 
176 #ifdef _SIZEOF_ADDR_IFREQ
177 
178 int
179 ni_SIZEOF_ADDR_IFREQ(struct ifreq * ifrp,struct sockaddr * sa,int size);
180 
181 #else
182 #define ni_SIZEOF_ADDR_IFREQ(ifr,sa,size) \
183 	(SA_LEN(sa) > sizeof(struct sockaddr) ? \
184 	size - sizeof(struct sockaddr) + SA_LEN(sa) : size)
185 #endif
186 
187 /* **************************************************** *
188  *    If local libc does not have 'strlcpy', 'memcmp'	*
189  *    some openbsd / sparc systems are missing memcmp,	*
190  *    strlcpy is missing in a lot of places		*
191  * **************************************************** */
192 
193 #ifndef HAVE_STRLCPY
194 #include "ni_strlcpy.h"
195 #endif
196 
197 #ifndef HAVE_MEMCMP
198 #include "ni_memcmp.h"
199 #endif
200 
201 /* **************************************************** *
202  *	define if_data if it is missing			*
203  * **************************************************** */
204 
205 #ifndef HAVE_STRUCT_IF_DATA
206 
207 #define   LINK_STATE_UNKNOWN      0       /* link invalid/unknown */
208 #define   LINK_STATE_DOWN         1       /* link is down */
209 #define   LINK_STATE_UP           2       /* link is up */
210 
211 /* ************************************************************	*
212  * Structure describing information about an interface		*
213  * which may be of interest to management entities.		*
214  *								*
215  *	THIS STRUCT IS INCOMPLETE, INSTATNIATE WITH CARE	*
216  * ************************************************************	*/
217 
218 struct if_data {
219         /* generic interface information */
220         u_char  ifi_type;               /* ethernet, tokenring, etc */
221         u_char  ifi_physical;           /* e.g., AUI, Thinnet, 10base-T, etc */
222         u_char  ifi_addrlen;            /* media address length */
223         u_char  ifi_hdrlen;             /* media header length */
224         u_char  ifi_link_state;         /* current link state */
225         u_char  ifi_spare_char1;        /* spare byte */
226         u_char  ifi_spare_char2;        /* spare byte */
227         u_char  ifi_datalen;            /* length of this data struct */
228         u_long  ifi_mtu;                /* maximum transmission unit */
229         u_long  ifi_metric;             /* routing metric (external only) */
230         u_long  ifi_baudrate;           /* linespeed */
231 /* ************************************************************	*
232  * 	incomplete -- this struct is longer but the remaining	*
233  *	data is volitile and we will never use it, thus 	*
234  *	we don't need to know for this application.		*
235  * ************************************************************	*/
236 };
237 
238 #endif	/* HAVE_STRUCT_IF_DATA	*/
239 
240 /* **************************************************** *
241  *	define getifaddrs if the OS does not have one	*
242  * **************************************************** */
243 
244 #ifndef HAVE_IFADDRS_H
245 
246 /*	some of the structure members have names
247  *	that conflict with definitions in <net/if.h>
248  *	for "struct ifaddr", don't need those here...
249  */
250 
251 #   ifdef ifa_next
252 #   undef ifa_next
253 #   endif
254 #   ifdef ifa_name
255 #   undef ifa_name
256 #   endif
257 #   ifdef ifa_flags
258 #   undef ifa_flags
259 #   endif
260 #   ifdef ifa_addr
261 #   undef ifa_addr
262 #   endif
263 #   ifdef ifa_netmask
264 #   undef ifa_netmask
265 #   endif
266 #   ifdef ifa_dstaddr
267 #   undef ifa_dstaddr
268 #   endif
269 #   ifdef ifa_data
270 #   undef ifa_data
271 #   endif
272 
273 struct ifaddrs {
274 	struct ifaddrs  *ifa_next;
275 	char		*ifa_name;
276 	u_int		 ifa_flags;
277 	struct sockaddr	*ifa_addr;
278 	struct sockaddr	*ifa_netmask;
279 	struct sockaddr	*ifa_dstaddr;
280 	void		*ifa_data;
281 };
282 
283 #endif 	/* not defined HAVE_IFADDRS_H	*/
284 
285 void
286 ni_freeifaddrs(struct ifaddrs *ifp);
287 
288 #ifndef HAVE_IFADDRS_H
289 #define getifaddrs(__ifap) ni_getifaddrs(__ifap,0)
290 #define freeifaddrs ni_freeifaddrs
291 #endif
292 
293 #ifdef LOCAL_SIZEOF_SOCKADDR_IN6
294 
295 /* **************************************************** *
296  *	define missing IPV6 filter macros if needed	*
297  * **************************************************** */
298 
299 /*
300  * Unspecified
301  */
302 #ifndef IN6_IS_ADDR_UNSPECIFIED
303 #define IN6_IS_ADDR_UNSPECIFIED(a) (	\
304 	(*(const u_int32_t *)(const void *)(&(a)->s6_addr[0]) |	\
305 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[4]) |	\
306 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[8]) |	\
307 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[12])) == 0)
308 #endif
309 /*
310  * Loopback
311  */
312 #ifndef IN6_IS_ADDR_LOOPBACK
313 #define IN6_IS_ADDR_LOOPBACK(a) (	\
314 	(*(const u_int32_t *)(const void *)(&(a)->s6_addr[0]) |	\
315 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[4]) |	\
316 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[8])) == 0 && \
317 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[12]) == ntohl(1))
318 #endif
319 /*
320  * IPv4 compatible
321  */
322 #ifndef IN6_IS_ADDR_V4COMPAT
323 #define IN6_IS_ADDR_V4COMPAT(a) (	\
324 	(*(const u_int32_t *)(const void *)(&(a)->s6_addr[0]) |	\
325 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[4]) |	\
326 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[8])) == 0 && \
327 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[12]) != 0 && \
328 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[12]) != ntohl(1))
329 #endif
330 /*
331  * Mapped
332  */
333 #ifndef IN6_IS_ADDR_V4MAPPED
334 #define IN6_IS_ADDR_V4MAPPED(a) (	\
335 	(*(const u_int32_t *)(const void *)(&(a)->s6_addr[0]) |	\
336 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[4])) == 0 && \
337 	 *(const u_int32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000FFFF))
338 #endif
339 #endif 	/* LOCAL_SIZEOF_SOCKADDR_IN6 */
340 
341 #endif	/* _NI_FIXUPS_H */
342