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