File: ni_util.c

package info (click to toggle)
libnet-interface-perl 1.016-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,252 kB
  • ctags: 478
  • sloc: ansic: 3,397; sh: 2,736; perl: 888; makefile: 3
file content (322 lines) | stat: -rw-r--r-- 8,333 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

/* ********************************************************************	*
 * ni_util.c	version 0.01 1-12-09					*
 *									*
 *     COPYRIGHT 2008-2009 Michael Robinton <michael@bizsystems.com>	*
 *									*
 * This program is free software; you can redistribute it and/or modify	*
 * it under the terms of either:					*
 *									*
 *  a) the GNU General Public License as published by the Free		*
 *  Software Foundation; either version 2, or (at your option) any	*
 *  later version, or							*
 *									*
 *  b) the "Artistic License" which comes with this distribution.	*
 *									*
 * This program is distributed in the hope that it will be useful,	*
 * but WITHOUT ANY WARRANTY; without even the implied warranty of	*
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either	*
 * the GNU General Public License or the Artistic License for more 	*
 * details.								*
 *									*
 * You should have received a copy of the Artistic License with this	*
 * distribution, in the file named "Artistic".  If not, I'll be glad 	*
 * to provide one.							*
 *									*
 * You should also have received a copy of the GNU General Public 	*
 * License along with this program in the file named "Copying". If not, *
 * write to the 							*
 *									*
 *	Free Software Foundation, Inc.					*
 *	59 Temple Place, Suite 330					*
 *	Boston, MA  02111-1307, USA					*
 *									*
 * or visit their web page on the internet at:				*
 *									*
 *	http://www.gnu.org/copyleft/gpl.html.				*
 * ********************************************************************	*/

#include "localconf.h"
 
/* duplicate allocate memory and return pointer **** memory must be freed */

void *
ni_memdup(void * memp, int size)
{
    void * newmp;
    if ((newmp = malloc(size)) == NULL) {
        errno = ENOMEM;
	return NULL;
    }
    return memcpy(newmp,memp,size);
}

/* two functions to conditionally close and reopen an ifconf socket
 *
 * if fd is < 0 it is ignored
 * af is modified if required
 */
 
int
ni_clos_reopn_dgrm(int fd, u_int af)
{
    if (fd >= 0)
    	close(fd);
    af == AF_UNSPEC ? AF_INET : af;
    return socket(af,SOCK_DGRAM,0);
}

/* ****************************************************	*
 *	provide _SIZEOF_ADDR_IFREQ(ifr) equivalent	*
 *	that will cover our use of in6_ifreq, lifreq	*
 * ****************************************************	*/

#ifdef _SIZEOF_ADDR_IFREQ
int
ni_SIZEOF_ADDR_IFREQ(struct ifreq * ifrp,struct sockaddr * sa,int size)
{
    struct ifreq ifr;
    
    memcpy(&ifr,ifrp,sizeof(struct ifreq));
    return _SIZEOF_ADDR_IFREQ(ifr);
}
#endif

/* ****************************************************	*
 *	on systems with embedded scope, get it 		*
 * 	and correct the bits in the addrsess		*
 * ****************************************************	*/
 
#ifdef LOCAL_SIZEOF_SOCKADDR_IN6

u_int
ni_get_scopeid(struct sockaddr_in6 * sin6)
{
    unsigned int scopeid = 0;
# ifdef __KAME__
    if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    	scopeid = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
    	sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
    }
# elif defined HAVE_SIN6_SCOPEID
    scopeid =  sin6->sin6_scopeid;
# endif
    return scopeid;
}

#endif

/* ****************************************************	*
 *	generate a netmask from a prefix length		*
 * ****************************************************	*/
 
void
ni_plen2mask(void * in_addr, int plen, int sizeofaddr)
{
    char * mask = in_addr;
    int ffs, rm, i;
    
    ffs	= plen / 8;
    rm	= plen % 8;

    if (rm != 0) {
    	rm = 8 - rm;
    	rm = 0xFFu << rm;
    }
    for (i = 0; i < ffs; i++)
    	mask[i] = 0xFFu;

    if (rm != 0) {
	mask[i] = rm;
	i++;
    }
    for (; i < sizeofaddr; i++)
        mask[i] = 0;
}

/* ****************************************************	*
 *	calculate the length of netmask prefix		*
 * ****************************************************	*/
 
int
ni_prefix(void * ap, int sz)
{
    unsigned char byte, * addr = (unsigned char *)ap;
    int bit, bp, plen = 0;

    for (bp = 0; bp < sz; bp++) {
      if (addr[bp] != 0xFFu)
          break;
      plen += 8;
    }

    if (bp == sz)
	return plen;

    byte = addr[bp];

    for (bit = 0x80u; bit != 0; bit >>= 1) {	/*  for (bit = 7; bit != 0; bit--, plen++)	*/
        if (byte & bit)	{			/*	if (!(name[byte] & (1 << bit)))		*/
            byte ^= bit;			/*	    break;				*/
            plen++;
        }
        else if (byte != 0)			/*  for (; bit != 0; bit--)			*/
	    return 0;				/*	if (name[byte] & (1 << bit))		*/
        else					/*	    return 0;				*/
            break;
    }

    bp++;
    for (; bp < sz; bp++) {
        if (addr[bp] != 0)
	    return 0;
    }
     return plen;
}

/* ****************************************************	*
 *		constructor registration		*
 * ****************************************************	*/

/*	for ifreq or equivalent flavor support		*/

struct ni_ifconf_flavor * nifcf = NULL;

void
ni_ifcf_register(struct ni_ifconf_flavor * nifp)
{
    nifp->ni_ifcf_next = nifcf;
    nifcf = nifp;
}

struct ni_ifconf_flavor *
ni_ifcf_get(enum ni_FLAVOR type)
{
    struct ni_ifconf_flavor * nifp;
    for (nifp = nifcf; nifp != NULL; nifp = nifp->ni_ifcf_next)
        if (nifp->ni_type == type)
            return nifp;
    errno = ENOSYS;
    return NULL;
}

struct ni_ifconf_flavor *
ni_safe_ifcf_get(enum ni_FLAVOR type)
{
    struct ni_ifconf_flavor * nifp;

    if ((nifp = ni_ifcf_get(type)) != NULL)
	return nifp;
    return ni_ifcf_get(NI_IFREQ);
}

/* ****************************************************	*
 *	developer support, not for production		*
 * ****************************************************	*/

int
ni_developer(enum ni_FLAVOR type)
{
    void * notused = NULL;
    struct ni_ifconf_flavor * nifp = ni_ifcf_get(type);
    if (nifp == NULL)
    	return ENOSYS;
    return nifp->developer(notused);
}

/* ****************************************************	*
 *	IPV6 support for linux like type info		*
 * ****************************************************	*/

/* does not have to be ifdef'd #ifdef LOCAL_SIZEOF_SOCKADDR_IN6	*/

/*	scope flags	rfc-2373
 *
	0 	  reserved
	1	node-local
	2	link-local
	3	  unassigned
	4	  unassigned
	5	site-local
	6	  unassigned
	7	  unassigned
	8	organization-local
	9	  unassigned
	A	  unassigned
	B	  unassigned
	C	  unassigned
	D	  unassigned
	E	global scope
	F	  reserved
 */

# ifdef ___NEVER_DEFINED___for_info
/*			      0x0000	  0xe
#define IPV6_ADDR_LOOPBACK      0x0010u /*      0x1     aka NODELOCAL, INTERFACELOCAL */
#define IPV6_ADDR_LINKLOCAL     0x0020u /*      0x2     */
#define IPV6_ADDR_SITELOCAL     0x0040u /*      0x5     */
#define IPV6_ADDR_COMPATv4	0x0080u /*	0x10	mapped back out of range */
# endif

int
ni_lx_type2scope(int lscope)
{
    lscope &= 0xF0u;
    switch (lscope) {
    case 0 :
	return 0xEu;
    case 0x10 :
	return 0x1;
    case 0x20 :
	return 0x2;
    case 0x40 :
	return 0x5;
    case 0x80:
    	return 0x10;	/* linux compat-v4	*/
    }
    return 0;
}

const ni_iff_t ni_lx_type2txt[] = {
	{ IPV6_ADDR_ANY,		"addr-any" },
	{ IPV6_ADDR_UNICAST,		"unicast" },
	{ IPV6_ADDR_MULTICAST,		"multicast" },
	{ IPV6_ADDR_ANYCAST,		"anycast" },
	{ IPV6_ADDR_LOOPBACK,		"loopback" },
	{ IPV6_ADDR_LINKLOCAL,		"link-local" },
	{ IPV6_ADDR_SITELOCAL,		"site-local" },
	{ IPV6_ADDR_COMPATv4,		"compat-v4" },
	{ IPV6_ADDR_SCOPE_MASK,		"scope-mask" },
	{ IPV6_ADDR_MAPPED,		"mapped" },
	{ IPV6_ADDR_RESERVED,		"reserved" },
	{ IPV6_ADDR_ULUA,		"uniq-lcl-unicast" },
	{ IPV6_ADDR_6TO4,		"6to4" },
	{ IPV6_ADDR_6BONE,		"6bone" },
	{ IPV6_ADDR_AGU,		"global-unicast" },
	{ IPV6_ADDR_UNSPECIFIED,	"unspecified" },
	{ IPV6_ADDR_SOLICITED_NODE,	"solicited-node" },
	{ IPV6_ADDR_ISATAP,		"ISATAP" },
	{ IPV6_ADDR_PRODUCTIVE,		"productive" },
	{ IPV6_ADDR_6TO4_MICROSOFT,	"6to4-ms" },
	{ IPV6_ADDR_TEREDO,		"teredo" },
	{ IPV6_ADDR_ORCHID,		"orchid" },
	{ IPV6_ADDR_NON_ROUTE_DOC,	"non-routeable-doc" }
};

int
ni_sizeof_type2txt()
{
    return sizeof(ni_lx_type2txt);
}

void
ni_linux_scope2txt(uint32_t flags)
{
    int i, n = sizeof(ni_lx_type2txt) / sizeof(ni_iff_t);
    
    for (i=0; i < n; i++)
    	if (flags & ni_lx_type2txt[i].iff_val)
    	    printf("%s ",ni_lx_type2txt[i].iff_nam);
}

/* does not have to be ifdef'd #endif	LOCAL_SIZEOF_SOCKADDR_IN6 */