File: librfc1884.c

package info (click to toggle)
ipv6calc 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,400 kB
  • sloc: ansic: 73,764; sh: 5,750; perl: 4,030; xml: 1,194; makefile: 888
file content (315 lines) | stat: -rw-r--r-- 10,403 bytes parent folder | download | duplicates (2)
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
/*
 * Project    : ipv6calc
 * File       : librfc1884.c
 * Version    : $Id: e6cc6ce21ba466296307efb0363acdbabe070d62 $
 * Copyright  : 2001-2016 by Peter Bieringer <pb (at) bieringer.de>
 *
 * Information:
 *  Function library for conversions defined in RFC 1884
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "libipv6calcdebug.h"
#include "libipv6addr.h"
#include "libipv6calc.h"
#include "librfc1884.h"
#include "ipv6calctypes.h"


/*
 * function decompress a given IPv6 address (reverse RFC 1884)
 *  
 * in : *addrstring = IPv6 address
 * out: *resultstring = result
 * ret: ==0: ok, !=0: error
 *
 * Based on code in from 'ircd'
 */
int compaddr_to_uncompaddr(const char *addrstring, char *resultstring, const size_t resultstring_length) {
	int retval = 1, cnt;
	char *cp, *op, *strp;
	char tempstring[NI_MAXHOST];

	DEBUGPRINT_WA(DEBUG_librfc1884, "got input: %s", addrstring);

	snprintf(tempstring, sizeof(tempstring), "%s", addrstring);

	strp = strstr(tempstring, "::");
	if (strp) {
			
		DEBUGPRINT_NA(DEBUG_librfc1884, "found '::' in IPv6 address");

		/* check for additional "::" occurance - not allowed! */
		if (strstr(strp+1, "::")) {
			snprintf(resultstring, resultstring_length, "%s", "More than 1 block of 2 colons in address is not allowed!");
			retval = 1;
			return (retval);
		};
		
		cnt = 0;
		cp = tempstring;
		op = resultstring;
	   	while (*cp != '\0') {
			if (*cp == ':')	cnt += 1;
			if (*cp++ == '.') {
				cnt += 1;
				break;
			};
		};
		cp = tempstring;
		while (*cp != '\0') {
			*op++ = *cp++;
			if (*(cp-1) == ':' && *cp == ':') {
				DEBUGPRINT_WA(DEBUG_librfc1884, "cnt: %d", cnt);
				if ((cp-1) == tempstring) {
					DEBUGPRINT_WA(DEBUG_librfc1884, "fill one '0:' (%d)", cnt);
					op--;
					*op++ = '0';
					*op++ = ':';
				};
				if (cnt < 8) {
					DEBUGPRINT_WA(DEBUG_librfc1884, "fill one '0:' (%d)", cnt);
			   		*op++ = '0';
				} else if (cnt == 8) {
					DEBUGPRINT_WA(DEBUG_librfc1884, "replace ':' by '0' (%d)", cnt);
					op--;
				};
				while (cnt++ < 7) {
					DEBUGPRINT_WA(DEBUG_librfc1884, "fill one ':0' (%d)", cnt);
					*op++ = ':';
					*op++ = '0';
				};
			};
		};
		if (*(op-1)==':') *op++ = '0';
		*op = '\0';

		DEBUGPRINT_WA(DEBUG_librfc1884, "result: %s", resultstring);
	} else {
		snprintf(resultstring, resultstring_length, "%s", addrstring);
		DEBUGPRINT_NA(DEBUG_librfc1884, "address is not in compressed format");
	};

	retval = 0;
	return (retval);
};


/*
 * function IPv6addrstruct to compressed format (RFC 1884)
 *
 *  compress the biggest '0' block, leading has precedence
 *
 * in : *addrstring = IPv6 address
 * out: *resultstring = result
 * ret: ==0: ok, !=0: error
 */
int ipv6addrstruct_to_compaddr(const ipv6calc_ipv6addr *ipv6addrp, char *resultstring, const size_t resultstring_length) {
	int retval = -1;

	uint32_t formatoptions = FORMATOPTION_printlowercase;

	/* old style compatibility */
	retval = librfc1884_ipv6addrstruct_to_compaddr(ipv6addrp, resultstring, resultstring_length, formatoptions);

	return (retval);
};
	
int librfc1884_ipv6addrstruct_to_compaddr(const ipv6calc_ipv6addr *ipv6addrp, char *resultstring, const size_t resultstring_length, const uint32_t formatoptions) {
	char tempstring[NI_MAXHOST], temp2string[NI_MAXHOST];
	int retval = 1;
	int zstart = -1, zend = -1, tstart = -1, tend = -1, i, w_max = 7;
	unsigned int s;

	DEBUGPRINT_WA(DEBUG_librfc1884, "typeinfo of IPv6 address: %08x", (unsigned int) ipv6addrp->typeinfo);
	
	if ( (ipv6addrp->typeinfo & IPV6_ADDR_COMPATv4) != 0 ) {
		/* compatv4 address */
			
		DEBUGPRINT_NA(DEBUG_librfc1884, "IPV6_ADDR_COMPATv4 type - fast conversion");
	
		snprintf(tempstring, sizeof(tempstring), "::%u.%u.%u.%u", (unsigned int) ipv6addrp->in6_addr.s6_addr[12], (unsigned int) ipv6addrp->in6_addr.s6_addr[13], (unsigned int) ipv6addrp->in6_addr.s6_addr[14], (unsigned int) ipv6addrp->in6_addr.s6_addr[15]);
		retval = 0;
	} else if ( (ipv6addrp->typeinfo & IPV6_ADDR_MAPPED) != 0 ) {
		/* mapped address */
			
		DEBUGPRINT_NA(DEBUG_librfc1884, "IPV6_ADDR_MAPPED type - fast conversion");
		
		snprintf(tempstring, sizeof(tempstring), "::%x:%u.%u.%u.%u", (unsigned int) ipv6addr_getword(ipv6addrp, 5), (unsigned int) ipv6addrp->in6_addr.s6_addr[12], (unsigned int) ipv6addrp->in6_addr.s6_addr[13], (unsigned int) ipv6addrp->in6_addr.s6_addr[14], (unsigned int) ipv6addrp->in6_addr.s6_addr[15]);
		retval = 0;
	} else if ( (ipv6addr_getdword(ipv6addrp, 0) == 0) && (ipv6addr_getdword(ipv6addrp, 1) == 0) && (ipv6addr_getdword(ipv6addrp, 2) == 0) && (ipv6addr_getdword(ipv6addrp, 3) == 0) ) {
		/* unspecified address */
			
		DEBUGPRINT_NA(DEBUG_librfc1884, "unspecified address - fast conversion");
		
		snprintf(tempstring, sizeof(tempstring), "::");
		retval = 0;
	} else if ( (ipv6addr_getdword(ipv6addrp, 0) == 0) && (ipv6addr_getdword(ipv6addrp, 1) == 0) && (ipv6addr_getdword(ipv6addrp, 2) == 0) && (ipv6addr_getdword(ipv6addrp, 3) == 1) ) {
		/* loopback address */
			
		DEBUGPRINT_NA(DEBUG_librfc1884, "loopback - fast conversion");
		
		snprintf(tempstring, sizeof(tempstring), "::1");
		retval = 0;
	} else {
		/* normal address */

		if ( ((ipv6addrp->typeinfo & IPV6_ADDR_IID_32_63_HAS_IPV4) != 0) && ((ipv6addrp->typeinfo & IPV6_ADDR_ANONYMIZED_IID) == 0)) {
			w_max = 5;
		};
			
		DEBUGPRINT_NA(DEBUG_librfc1884, "normal address, detect '0' blocks now");

		for ( i = 0; i <= w_max; i++ ) {
			if ( ipv6addr_getword(ipv6addrp, (unsigned int) i) == 0 ) {
				/* found a '0' */
					
				DEBUGPRINT_WA(DEBUG_librfc1884, "Found '0' in word '%d'", i);

				if ( tstart == -1 ) {
					/* possible starting '0' block */ 
						
					DEBUGPRINT_WA(DEBUG_librfc1884, "Found a possible '0' starting block at '%d'",  i);
					
					tstart = i;				
				};
			} else {
				/* end of a '0' block */
					
				DEBUGPRINT_WA(DEBUG_librfc1884, "Found non '0' in word '%d'", i);

				if ( tstart != -1 ) {
					tend = i - 1;
					if ( ( tend - tstart ) >= 0 ) {
						/* ok, a block with 1 or more '0' */
							
						DEBUGPRINT_WA(DEBUG_librfc1884, "Found a '0' block from '%d' to '%d' with length '%d'", tstart, tend, tend - tstart + 1);
						
						if ( zstart < 0 ) {
							/* no other block before, init */
							zstart = tstart;
							zend = tend;
						
							DEBUGPRINT_WA(DEBUG_librfc1884, "First found '0' block from '%d' to '%d' with length '%d'",  zstart, zend, zend - zstart + 1);
							
						} else if ( ( zend - zstart ) < ( tend - tstart ) ) {
							/* ok, bigger block found */
							zstart = tstart;
							zend = tend;
							
							DEBUGPRINT_WA(DEBUG_librfc1884, "Found bigger '0' block from '%d' to '%d' with length '%d'",  zstart, zend, zend - zstart + 1);
							
						} else {
								
							DEBUGPRINT_NA(DEBUG_librfc1884, "This '0' block is not bigger than the last one - skip");

						};
					};
					tstart = -1;
					tend = -1;
				};
			};
		};
		/* cleanup */
		if ( tstart >= 0 ) {
			tend = w_max;
			/* trailing '0' block */
			if ( ( tend - tstart ) > 0 ) {
				/* ok, a block with 2 or more '0' */
				if ( zstart < 0 ) {
					/* no other block before, init */
					zstart = tstart;
					zend = tend;
				} else if ( ( zend - zstart ) < ( tend - tstart ) ) {
					/* ok, bigger block found */
					zstart = tstart;
					zend = tend;
				};
			};
		};

		if ( zstart != -1 ) {
			DEBUGPRINT_WA(DEBUG_librfc1884, "biggest '0' block is from word '%d' to '%d'",  zstart, zend);
		} else {
			DEBUGPRINT_NA(DEBUG_librfc1884, "no '0' block found");
		};

		/* create string */
		tempstring[0] = '\0';

		for ( i = 0; i <= w_max; i++ ) {
			if ( i == zstart ) {
		
				DEBUGPRINT_WA(DEBUG_librfc1884, "start of '0' at '%d'", i);
				
				snprintf(temp2string, sizeof(temp2string), "%s:", tempstring);
			} else if ( i == 0 ) {
				DEBUGPRINT_WA(DEBUG_librfc1884, "normal start value at '%d' (%x)", i, (unsigned int) ipv6addr_getword(ipv6addrp, (unsigned int) i));
				
				snprintf(temp2string, sizeof(temp2string), "%x", (unsigned int) ipv6addr_getword(ipv6addrp, (unsigned int) i));
			} else if ( ( i > zend ) || ( i < zstart ) ) {
				snprintf(temp2string, sizeof(temp2string), "%s:%x", tempstring, (unsigned int) ipv6addr_getword(ipv6addrp, (unsigned int) i));
			} else if ( ( i == 7 ) && ( zend == i )) {
				snprintf(temp2string, sizeof(temp2string), "%s:", tempstring);
			};
			snprintf(tempstring, sizeof(tempstring), "%s", temp2string);
		};
		
		if ( ((ipv6addrp->typeinfo & IPV6_ADDR_IID_32_63_HAS_IPV4) != 0) && ((ipv6addrp->typeinfo & IPV6_ADDR_ANONYMIZED_IID) == 0)) {
			/* append IPv4 address */
			snprintf(temp2string, sizeof(temp2string), "%s:%u.%u.%u.%u", \
				tempstring, \
				(unsigned int) ipv6addrp->in6_addr.s6_addr[12], \
				(unsigned int) ipv6addrp->in6_addr.s6_addr[13], \
				(unsigned int) ipv6addrp->in6_addr.s6_addr[14], \
				(unsigned int) ipv6addrp->in6_addr.s6_addr[15]  \
			);
			snprintf(tempstring, sizeof(tempstring), "%s", temp2string);
		};

		DEBUGPRINT_WA(DEBUG_librfc1884, "new method: '%s'", tempstring);

		retval = 0;
	};

	if ( ( retval == 0 ) && ( ipv6addrp->flag_prefixuse == 1 ) && ((formatoptions & FORMATOPTION_literal) == 0) ) {
		snprintf(resultstring, resultstring_length, "%s/%u", tempstring, (unsigned int) ipv6addrp->prefixlength);
	} else {
		if ((formatoptions & FORMATOPTION_literal) != 0) {
			/* replace : by - */
			for (s = 0; s < strlen(tempstring); s++) {
				if (tempstring[s] == ':') {
					tempstring[s] = '-';
				};
			};
			if (ipv6addrp->flag_scopeid) {
				snprintf(resultstring, resultstring_length, "%ss%s.ipv6-literal.net", tempstring, ipv6addrp->scopeid);
			} else {
				snprintf(resultstring, resultstring_length, "%s.ipv6-literal.net", tempstring);
			};
		} else {
			if (ipv6addrp->flag_scopeid) {
				snprintf(resultstring, resultstring_length, "%s%%%s", tempstring, ipv6addrp->scopeid);
			} else {
				snprintf(resultstring, resultstring_length, "%s", tempstring);
			};
		};
	};

	if ( (formatoptions & FORMATOPTION_printlowercase) != 0 ) {
		/* nothing to do */
	} else if ( (formatoptions & FORMATOPTION_printuppercase) != 0 ) {
		string_to_upcase(resultstring);
	};

	if ( (formatoptions & FORMATOPTION_machinereadable) != 0 ) {
		snprintf(temp2string, sizeof(temp2string), "IPV6=%s", resultstring);
		snprintf(resultstring, resultstring_length, "%s", temp2string);
	};

	DEBUGPRINT_WA(DEBUG_librfc1884, "Result: '%s'", resultstring);
	return (retval);
};