File: libipaddr.c

package info (click to toggle)
ipv6calc 4.1.0-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,676 kB
  • sloc: ansic: 101,567; sh: 6,796; perl: 3,867; xml: 1,475; makefile: 909
file content (111 lines) | stat: -rw-r--r-- 3,000 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
/*
 * Project    : ipv6calc
 * File       : libipaddr.c
 * Version    : $Id: 1f2029f3bffb64ecc38cef6881702578e8ad8532 $
 * Copyright  : 2014-2021 by Peter Bieringer <pb (at) bieringer.de>
 *
 * Information:
 *  Function library for generic IPv4/6 address handling
 */

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

#include "config.h"
#include "libipaddr.h"
#include "libipv6addr.h"
#include "libipv4addr.h"
#include "ipv6calctypes.h"
#include "libipv6calc.h"
#include "libipv6calcdebug.h"
#include "librfc1884.h"


/*
 * stores the ipaddr structure in an IPv4/6 string
 *
 * in:  ipaddr = IPv4/6 address structure
 * out: *resultstring = IPv4/6 address (modified)
 * ret: ==0: ok, !=0: error
 */
int libipaddr_ipaddrstruct_to_string(const ipv6calc_ipaddr *ipaddrp, char *resultstring, const size_t resultstring_length, const uint32_t formatoptions) {
	int retval = 1;

	ipv6calc_ipv4addr ipv4addr;
	ipv6calc_ipv6addr ipv6addr;

	switch (ipaddrp->proto) {
	    case IPV6CALC_PROTO_IPV4:
		ipv4addr_clearall(&ipv4addr);
		ipv4addr_setdword(&ipv4addr, ipaddrp->addr[0]);

		ipv4addr.prefixlength   = ipaddrp->prefixlength;
		ipv4addr.flag_valid     = ipaddrp->flag_valid;
		ipv4addr.flag_prefixuse = ipaddrp->flag_prefixuse;

		libipv4addr_ipv4addrstruct_to_string(&ipv4addr, resultstring, resultstring_length, formatoptions);
		break;

	    case IPV6CALC_PROTO_IPV6:
		ipv6addr_clearall(&ipv6addr);
		ipv6addr_setdword(&ipv6addr, 0, ipaddrp->addr[0]);
		ipv6addr_setdword(&ipv6addr, 1, ipaddrp->addr[1]);
		ipv6addr_setdword(&ipv6addr, 2, ipaddrp->addr[2]);
		ipv6addr_setdword(&ipv6addr, 3, ipaddrp->addr[3]);

		ipv6addr.prefixlength   = ipaddrp->prefixlength;
		ipv6addr.flag_valid     = ipaddrp->flag_valid;
		ipv6addr.flag_prefixuse = ipaddrp->flag_prefixuse;

		if ((formatoptions & (FORMATOPTION_printuncompressed | FORMATOPTION_printfulluncompressed)) != 0) {
			libipv6addr_ipv6addrstruct_to_uncompaddr(&ipv6addr, resultstring, resultstring_length, formatoptions);
		} else {
			librfc1884_ipv6addrstruct_to_compaddr(&ipv6addr, resultstring, resultstring_length, formatoptions);
		};
		break;

	    default:
		ERRORPRINT_WA("unsupported proto=%d (FIX CODE)", ipaddrp->proto);
		exit(EXIT_FAILURE);
		break;
	};

	DEBUGPRINT_WA(DEBUG_libipaddr, "result string: %s", resultstring);
	
	retval = 0;	
	return (retval);
};


/*
 * function clears the IPv6 structure
 *
 * mod: ipaddrp = pointer to IP address structure
 */
extern void libipaddr_clearall(ipv6calc_ipaddr *ipaddrp) {
        /* Clear IP address */
	ipaddrp->addr[0] = 0;
	ipaddrp->addr[1] = 0;
	ipaddrp->addr[2] = 0;
	ipaddrp->addr[3] = 0;

        /* Clear IP address scope */
        ipaddrp->scope = 0;

	/* Clear proto */
	ipaddrp->proto = 0;

        /* Clear valid flag */
        ipaddrp->flag_valid = 0;

        /* Clear prefix */
        ipaddrp->prefixlength = 0;
        ipaddrp->flag_prefixuse = 0;

        /* Clear typeinfo */
        ipaddrp->typeinfo1 = 0;
        ipaddrp->typeinfo2 = 0;

	return;
};