fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
ip.h
Go to the documentation of this file.
1#ifndef IP_H
2#define IP_H
3
4/*
5 * Revision 20200603, Erwin Hoffmann
6 * - added V46loopback address
7 * Revision 20190414, Erwin Hoffmann
8 * - removed ip_scan and ip_scanbracket (comp. versions).
9 * - added ia6_fmt and ia4_fmt (for qmail)
10 * Revision 20180314, Erwin Hoffmann
11 * - ip4_cidr added and argument list changed (also for ip6_cidr)
12 * Revision 20180213, Erwin Hoffmann
13 * - ip4_scan/ip6_scan/ip4_scanbracket/ip6_fmt_flat revised
14 * Revision 20180206, Erwin Hoffmann
15 * - added V6linklocal address
16 * Revision 20170319, Kai Peter
17 * - rewrite (consolidation and compatibility)
18 * Revision 20170210, Kai Peter
19 * - added definition 'V4loopback' and redefinition 'ip4loopback'
20 * Revision 20170908, Erwin Hoffmann
21 * - added some definitions from s/qmail + required for djbdns
22*/
23
24/* Consolidated header files ip.h from *qmail (with IPv6) and ip4.h/ip6.h
25 from libowfat. Thus it could be used with 'older' and 'newer' code.
26*/
27
28#include "byte.h"
29#include "stralloc.h"
30
31#define V4MAPPREFIX "::ffff:"
32#define HOSTNAMELEN 1025
33#define IP4MLMTU 512
34#define IP6MLMTU 1280 /* RFC 8200 */
35#define MTUSIZE IP6MLMTU
36
37#define IP4_FMT 20 /* backwards compatibility */
38#define IP6_FMT 40 /* " */
39#define IPFMT 72 /* used in qmail-remote only (temp?) */
40/* may be better: */
41//#define IP4_FMT IPFMT /* backwards compatibility */
42//#define IP6_FMT IPFMT /* " */
43/* deprecated: */
44#define FMT_IP4 IP4_FMT /* more backwards compatibility */
45#define FMT_IP6 IP6_FMT /* " */
46
47/* these structs are going deprecated (should replaced by socket lib) */
48struct ip4_address { unsigned char d[4]; }; /* 4 decimal pieces */
49struct ip6_address { unsigned char d[16]; }; /* 16 hex pieces */
50#define ip_address ip4_address /* backward compatibility */
51
52unsigned int ip4_bytestring(stralloc *,char [4],int);
53unsigned int ip4_cidr(char *,char [4],unsigned long *);
54unsigned int ip4_fmt(char *,char [4]);
55unsigned int ip4_scan(const char *,char [4]);
56unsigned int ip4_scanbracket(const char *,char [4]);
57unsigned int ia4_fmt(char *,char [4]);
58
59const static char V4loopback[4] = {127,0,0,1};
60const static char V4localnet[4] = {0,0,0,0};
61const static char V4broadcast[4] = {-1,-1,-1,-1}; // all bits 1
62
63/***
64 * Compactified IPv6 addresses are really ugly to parse.
65 * Syntax: (h = hex digit) [RFC 5952]
66 * 1. hhhh:hhhh:hhhh:hhhh:hhhh:hhhh:hhhh:hhhh
67 * 2. leading 0s in any octet can be suppressed
68 * 3. any number of 0000 may be abbreviated as "::"
69 * a) but only once;
70 * b) the longest piece or on equal match the first piece,
71 * c) a single instance of 0000 has to displayed as 0
72 * 4. The last two words may be written as IPv4 address
73 *
74 * Flat ip6 address syntax:
75 * hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh (32 hex digits)
76 *
77 * struct ip6_address format:
78 * cccccccccccccccc (16 chars; d[16]) -- each char swallows two hex values
79 *
80 * Bitstring representation with length prefix:
81 * bbbbbbb.........bbbb (max 128 bits); stralloc(ip6string); b = 0 || 1
82 *
83 */
84
85unsigned int ip6_bytestring(stralloc *,char *,int);
86unsigned int ip6_cidr(char *,char [16],unsigned long *);
87unsigned int ip6_fmt(char *,char [16]);
88unsigned int ip6_fmt_flat(char *,char [16]);
89unsigned int ip6_ifscan(char *,char [16],stralloc *);
90unsigned int ip6_scan(const char *,char [16]);
91unsigned int ip6_scanbracket(const char *,char [16]);
92unsigned int ip6_scan_flat(const char *,char [16]);
93unsigned int ia6_fmt(char *,char [16]);
94
95static const unsigned char V4mappedprefix[12] = {0,0,0,0, 0,0,0,0, 0,0,0xff,0xff};
96static const unsigned char V46loopback[16] = {0,0,0,0, 0,0,0,0, 0,0,0xff,0xff, 127,0,0,1};
97static const unsigned char V6loopback[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1};
98static const unsigned char V6localnet[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
99static const unsigned char V6linklocal[2] = {0xfe,0x80};
100
101int fromhex(unsigned char);
102char tohex(char);
103
104#define V6any V6localnet /* backward compatibility */
105#define ip6_isv4mapped(ip) (byte_equal(ip,12,V4mappedprefix))
106
107#endif
unsigned int ip4_scanbracket(const char *, char[4])
ip4_scanbracket parse IPv4 address string enclosed in brackets and convert to IP address array
Definition: ip4.c:92
unsigned int ip4_fmt(char *, char[4])
ip4_fmt converts IPv4 address to dotted decimal string format
Definition: ip4.c:20
unsigned int ip4_scan(const char *, char[4])
ip4_scan parse IPv4 address string and convert to IP address array
Definition: ip4.c:67
unsigned int ip6_scan(const char *, char[16])
ip6_scan parse compactified IPv6 address string and convert to IPv6 address array
Definition: ip6.c:151
unsigned int ip4_bytestring(stralloc *, char[4], int)
ip4_bytestring parse IPv4 address and represent as char string with length prefix
Definition: ip4.c:131
unsigned int ip6_scanbracket(const char *, char[16])
ip6_scanbracket parse IPv6 string address enclosed in brackets
Definition: ip6.c:243
unsigned int ip6_ifscan(char *, char[16], stralloc *)
ip6_ifscan parse compactified IPv6 address string concatinated with the interface name: fe80::1eth0
Definition: ip6.c:262
unsigned int ia6_fmt(char *, char[16])
ia6_fmt convert IPv6 address to inverse DNS nibble format 1.2.3.4.5.6.7.8.9.a.b.c....
Definition: ip6.c:102
unsigned int ip6_cidr(char *, char[16], unsigned long *)
ip6_cidr parse compactified IPv6 address string concatinated with the prefix length: fe80::1/64
Definition: ip6.c:290
unsigned int ip4_cidr(char *, char[4], unsigned long *)
ip4_cidr parse IPv4 address string concatinated with the prefix length: 192.168.1/24
Definition: ip4.c:111
unsigned int ip6_fmt(char *, char[16])
ip6_fmt convert IPv6 address to compactified IPv6 address string
Definition: ip6.c:21
int fromhex(unsigned char)
Definition: fmt.c:79
unsigned int ip6_bytestring(stralloc *, char *, int)
unsigned int ia4_fmt(char *, char[4])
ia4_fmt converts IPv4 address into DNS inverse nibble format
Definition: ip4.c:43
unsigned int ip6_scan_flat(const char *, char[16])
ip6_scan_flat convert IPv6 address string to IPv6 address array
Definition: ip6.c:129
unsigned int ip6_fmt_flat(char *, char[16])
ip6_fmt_flat convert IPv6 address to IPv6 address string
Definition: ip6.c:84
char tohex(char)
Definition: fmt.c:70
Definition: ip.h:48
unsigned char d[4]
Definition: ip.h:48
Definition: ip.h:49
unsigned char d[16]
Definition: ip.h:49