summaryrefslogtreecommitdiff
path: root/src/ip4.c
blob: c9ab15333be8a3eddf79fe90cc76d2d2c856be2c (plain)
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
#include "fmt.h"
#include "ip.h"
#include "scan.h"
#include "str.h"

/**
	@file ip4.c
	@author djb, fefe, feh, kp
	@source ucspi-tcp, ucspi-tcp6 
	@brief handling of IPv4 addresses
*/

/**
 @brief ip4_fmt
        converts IPv4 address to dotted decimal string format
 @param input:  IPv4 char array
        output: IPv4 address string
 @return int length of address (ok > 0)
 */

unsigned int ip4_fmt(char *s, char ip[4])
{
  unsigned int len;
  unsigned int i;

  len = 0;
  // clang-format off
  i = fmt_ulong(s,(unsigned long) (unsigned char) ip[0]); len += i; if (s) s += i;
  if (s) { *s++ = '.'; } ++len;
  i = fmt_ulong(s,(unsigned long) (unsigned char) ip[1]); len += i; if (s) s += i;
  if (s) { *s++ = '.'; } ++len;
  i = fmt_ulong(s,(unsigned long) (unsigned char) ip[2]); len += i; if (s) s += i;
  if (s) { *s++ = '.'; } ++len;
  i = fmt_ulong(s,(unsigned long) (unsigned char) ip[3]); len += i; if (s) s += i;
  // clang-format on
  return len;
}

/**
 @brief ia4_fmt
        converts IPv4 address into DNS inverse nibble format
 @param input:  IPv4 char array
        output: IPv4 address string
 @return int length of address (ok > 0)
 */

unsigned int ia4_fmt(char *s, char ip[4])
{
  unsigned int i;
  unsigned int len;

  len = 0;
  // clang-format off
  i = fmt_ulong(s,(unsigned long) ip[3]); len += i; if (s) s += i;
  i = fmt_str(s,"."); len += i; if (s) s += i;
  i = fmt_ulong(s,(unsigned long) ip[2]); len += i; if (s) s += i;
  i = fmt_str(s,"."); len += i; if (s) s += i;
  i = fmt_ulong(s,(unsigned long) ip[1]); len += i; if (s) s += i;
  i = fmt_str(s,"."); len += i; if (s) s += i;
  i = fmt_ulong(s,(unsigned long) ip[0]); len += i; if (s) s += i;
  i = fmt_str(s,".in-addr.arpa."); len += i; if (s) s += i;
  // clang-format on
  return len;
}

/**
 @brief ip4_scan
        parse IPv4 address string and convert to IP address array
 @param input:  IPv4 address string
        output: IPv4 char array
 @return int length of ip_address (ok > 0)
 */

unsigned int ip4_scan(const char *s, char ip[4])
{
  unsigned int i;
  unsigned int len;
  unsigned long u;

  byte_zero(ip, 4);
  len = 0;
  // clang-format off
  i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[0] = u; s += i; len += i;
  if (*s != '.') { return 0; } ++s; ++len;
  i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[1] = u; s += i; len += i;
  if (*s != '.') { return 0; } ++s; ++len;
  i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[2] = u; s += i; len += i;
  if (*s != '.') { return 0; } ++s; ++len;
  i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[3] = u; s += i; len += i;
  // clang-format on
  return len;
}

/**
 @brief ip4_scanbracket
        parse IPv4 address string enclosed in brackets and convert to IP address array
 @param input:  IPv4 char array
        output: IPv4 char array
 @return int length of ip_address (ok > 0)
 */

unsigned int ip4_scanbracket(const char *s, char ip[4])
{
  unsigned int len;

  if (*s != '[') return 0;
  len = ip4_scan(s + 1, ip);
  if (!len) return 0;
  if (s[len + 1] != ']') return 0;
  return len + 2;
}

/**
 @brief ip4_cidr
        parse IPv4 address string
        concatinated with the prefix length:  192.168.1/24
 @param input:  IPv6 char array
        output: IPv6 char array, long plen
 @return int length of ip6_address/ip
 */

unsigned int ip4_cidr(char *s, char ip[4], unsigned long *plen)
{
  unsigned int j = 0;
  *plen = 32UL;

  j = str_chr(s, '/');
  if (s[j] == '/') {
    s[j] = 0;
    j = scan_ulong(s + j + 1, plen);
  }
  return ip4_scan((const char *)s, ip);
}

/**
 @brief ip4_bytestring 
        parse IPv4 address and represent as char string with length prefix
 @param input:  IPv4 char array, prefix length
        output: pointer to stralloc bytestring
 @return n: number of bytes, if ok; -1: memory shortage; -2: input error
 */

unsigned int ip4_bytestring(stralloc *ipstring, char ip[4], int prefix)
{
  int i, j, n = 0;
  unsigned char number;

  if (!stralloc_readyplus(ipstring, 32)) return -1;
  if (!stralloc_copys(ipstring, "")) return -1;

  for (i = 0; i < 4; i++) {
    number = (unsigned char)ip[i];
    if (number > 255) return -2;

    for (j = 7; j >= 0; j--) {
      if (number & (1 << j)) {
        n++;
        if (!stralloc_cats(ipstring, "1")) return -1;
      } else {
        n++;
        if (!stralloc_cats(ipstring, "0")) return -1;
      }
      prefix--;
      if (!prefix) goto DONE;
    }
  }

DONE:
  if (!stralloc_0(ipstring)) return 1;

  return n;
}