fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
ip4.c
Go to the documentation of this file.
1#include "fmt.h"
2#include "scan.h"
3#include "str.h"
4#include "ip.h"
5
20unsigned int ip4_fmt(char *s,char ip[4])
21{
22 unsigned int len;
23 unsigned int i;
24
25 len = 0;
26 i = fmt_ulong(s,(unsigned long) (unsigned char) ip[0]); len += i; if (s) s += i;
27 if (s) { *s++ = '.'; } ++len;
28 i = fmt_ulong(s,(unsigned long) (unsigned char) ip[1]); len += i; if (s) s += i;
29 if (s) { *s++ = '.'; } ++len;
30 i = fmt_ulong(s,(unsigned long) (unsigned char) ip[2]); len += i; if (s) s += i;
31 if (s) { *s++ = '.'; } ++len;
32 i = fmt_ulong(s,(unsigned long) (unsigned char) ip[3]); len += i; if (s) s += i;
33 return len;
34}
35
43unsigned int ia4_fmt(char *s,char ip[4])
44{
45 unsigned int i;
46 unsigned int len;
47
48 len = 0;
49 i = fmt_ulong(s,(unsigned long) ip[3]); len += i; if (s) s += i;
50 i = fmt_str(s,"."); len += i; if (s) s += i;
51 i = fmt_ulong(s,(unsigned long) ip[2]); len += i; if (s) s += i;
52 i = fmt_str(s,"."); len += i; if (s) s += i;
53 i = fmt_ulong(s,(unsigned long) ip[1]); len += i; if (s) s += i;
54 i = fmt_str(s,"."); len += i; if (s) s += i;
55 i = fmt_ulong(s,(unsigned long) ip[0]); len += i; if (s) s += i;
56 i = fmt_str(s,".in-addr.arpa."); len += i; if (s) s += i;
57 return len;
58}
59
67unsigned int ip4_scan(const char *s,char ip[4])
68{
69 unsigned int i;
70 unsigned int len;
71 unsigned long u;
72
73 byte_zero(ip,4);
74 len = 0;
75 i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[0] = u; s += i; len += i;
76 if (*s != '.') { return 0; } ++s; ++len;
77 i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[1] = u; s += i; len += i;
78 if (*s != '.') { return 0; } ++s; ++len;
79 i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[2] = u; s += i; len += i;
80 if (*s != '.') { return 0; } ++s; ++len;
81 i = scan_ulong((char *)s,&u); if (!i) { return 0; } ip[3] = u; s += i; len += i;
82 return len;
83}
84
92unsigned int ip4_scanbracket(const char *s,char ip[4])
93{
94 unsigned int len;
95
96 if (*s != '[') return 0;
97 len = ip4_scan(s + 1,ip);
98 if (!len) return 0;
99 if (s[len + 1] != ']') return 0;
100 return len + 2;
101}
102
111unsigned int ip4_cidr(char *s,char ip[4],unsigned long *plen)
112{
113 unsigned int j = 0;
114 *plen = 32UL;
115
116 j = str_chr(s,'/');
117 if (s[j] == '/') {
118 s[j] = 0;
119 j = scan_ulong(s + j + 1,plen);
120 }
121 return ip4_scan((const char *)s,ip);
122}
123
131unsigned int ip4_bytestring(stralloc *ipstring,char ip[4],int prefix)
132{
133 int i, j, n = 0;
134 unsigned char number;
135
136 if (!stralloc_readyplus(ipstring,32)) return -1;
137 if (!stralloc_copys(ipstring,"")) return -1;
138
139 for (i = 0; i < 4; i++) {
140 number = (unsigned char) ip[i];
141 if (number > 255) return -2;
142
143 for (j = 7; j >= 0; j--) {
144 if (number & (1 << j)) {
145 n++;
146 if (!stralloc_cats(ipstring,"1")) return -1;
147 } else {
148 n++;
149 if (!stralloc_cats(ipstring,"0")) return -1;
150 }
151 prefix--;
152 if (!prefix) goto DONE;
153 }
154 }
155
156DONE:
157 if (!stralloc_0(ipstring)) return 1;
158
159 return n;
160}
unsigned int ip4_fmt(char *s, char ip[4])
ip4_fmt converts IPv4 address to dotted decimal string format
Definition: ip4.c:20
unsigned int ip4_cidr(char *s, char ip[4], unsigned long *plen)
ip4_cidr parse IPv4 address string concatinated with the prefix length: 192.168.1/24
Definition: ip4.c:111
unsigned int ip4_bytestring(stralloc *ipstring, char ip[4], int prefix)
ip4_bytestring parse IPv4 address and represent as char string with length prefix
Definition: ip4.c:131
unsigned int ip4_scanbracket(const char *s, char ip[4])
ip4_scanbracket parse IPv4 address string enclosed in brackets and convert to IP address array
Definition: ip4.c:92
unsigned int ia4_fmt(char *s, char ip[4])
ia4_fmt converts IPv4 address into DNS inverse nibble format
Definition: ip4.c:43
unsigned int ip4_scan(const char *s, char ip[4])
ip4_scan parse IPv4 address string and convert to IP address array
Definition: ip4.c:67
unsigned int scan_ulong(const char *, unsigned long *)
Definition: scan.c:79
conversion function declarations
unsigned int fmt_ulong(char *, unsigned long)
Definition: fmt.c:44
unsigned int fmt_str(char *, const char *)
Definition: fmt.c:10
void byte_zero(void *, unsigned int)
Definition: byte.c:65
unsigned int str_chr(const char *, int)
Definition: str.c:81
#define stralloc_0(sa)
Definition: stralloc.h:37
int stralloc_cats(stralloc *, const char *)
Definition: stralloc.c:36
int stralloc_readyplus(stralloc *sa, size_t len)
Definition: stralloc.c:61
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79