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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/* $Cambridge: hermes/src/prayer/lib/ipaddr.c,v 1.6 2009/08/20 12:37:55 dpc22 Exp $ */
/************************************************
* Prayer - a Webmail Interface *
************************************************/
/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */
#include "lib.h"
/* Class for recording and comparing IP addresses. At the moment IPv4
* only. Shouldn't be to hard to expand to IPv6 as well I hope */
/* ipaddr_create() *******************************************************
*
* Create a fresh ipaddr structure.
************************************************************************/
struct ipaddr *ipaddr_create(struct pool *pool)
{
struct ipaddr *result = pool_alloc(pool, sizeof(struct ipaddr));
return (result);
}
/* ipaddr_copy() *********************************************************
*
* Copy ip address.
* dst: Target
* src: Source
************************************************************************/
BOOL ipaddr_copy(struct ipaddr * dst, struct ipaddr * src)
{
memcpy(dst, src, sizeof(struct ipaddr));
return (T);
}
/* ipaddr_compare() ******************************************************
*
* Compare two ipaddr structures
* addr1: First ipaddr
* addr2: Second ipaddr
*
* Returns: T if ipaddrs match
************************************************************************/
BOOL ipaddr_compare(struct ipaddr * addr1, struct ipaddr * addr2)
{
if (addr1->version != addr2->version)
return (NIL);
return ((!memcmp(&addr1->addr, &addr2->addr,
(addr1->version == 6) ? 16 : 4)) ? T : NIL);
}
/* ====================================================================== */
/* ipaddr_text() *********************************************************
*
* Convert ipaddr to text representation using static buffer
************************************************************************/
char *ipaddr_text(struct ipaddr *addr)
{
static char buf[64];
os_inet_ntop(addr->addr, addr->version, buf, 64);
return (buf);
}
/* ipaddr_text() *********************************************************
*
* Convert ipaddr to canonical hostname, using named pool
************************************************************************/
char *ipaddr_name(struct ipaddr *addr)
{
char *result;
if ((result = os_gethostbyaddr(addr->addr, addr->version)))
return (result);
return (ipaddr_text(addr));
}
/* ====================================================================== */
/* ipaddr_parse() ********************************************************
*
* Parse text representation of IP address
* addr: Target ipaddr
* text: Text to parse
*
* Returns: T if text parsed as correct IP address.
************************************************************************/
BOOL ipaddr_parse(struct ipaddr *ipaddr, char *text)
{
if (text == NIL)
return (NIL);
return(os_inet_pton(text, ipaddr));
}
/* ====================================================================== */
/* ipaddr_compare_list() ************************************************
*
* Compare IP address to text list of form:
* ipaddr:
* text: Text string of form "131.111.0.0/16 : 192.168.0.0/24 : 2001:12cd:1::/48".
* (There has to be a space on either side of the colon for it to
* separate two networks)
*
* Returns: T if addr matches list.
************************************************************************/
BOOL ipaddr_compare_list(struct ipaddr * ipaddr, char *text)
{
char *next = NULL, *s, *alloc;
int i;
unsigned long bits, mask;
struct ipaddr parsed;
BOOL match;
alloc = text = pool_strdup(NIL, text);
while (text && *text) {
next = NULL;
s = text;
while ((s = strchr(s, ':'))) {
if ((s > text) && (*(s - 1) == ' ' || *(s + 1) == ' ')) {
*s++ = '\0';
next = s;
break;
}
s++;
}
text = string_trim_whitespace(text);
if ((s = strchr(text, '/'))) {
*s++ = '\0';
bits = atoi(s);
}
else
bits = 128; /* Doesn't matter if it's too big */
if (ipaddr_parse(&parsed, text)) {
match = T;
if (parsed.version != ipaddr->version) {
text = next; continue;
}
for (i = 0; i < (parsed.version == 6 ? 16 : 4); i++) {
if (bits == 0) mask = 0;
else if (bits < 8) {
mask = ((-1) << (8 - bits)) & 0xff;
bits = 0;
}
else {
mask = 0xff;
bits -= 8;
}
if ((parsed.addr[i] & mask) != (ipaddr->addr[i] & mask)) {
match = NIL;
break;
}
}
if (match) {
free(alloc);
return (T);
}
}
text = next;
}
free(alloc);
return (NIL);
}
/* ====================================================================== */
/* ipaddr_send_iostream() ************************************************
*
* Send contents an raw ip address into iostream
* addr: Address to send
* stream: Target stream
************************************************************************/
void ipaddr_send_iostream(struct ipaddr *addr, struct iostream *stream)
{
int i;
ioputc(addr->version, stream);
for (i = 0; i < (addr->version == 6 ? 16 : 4); i++) {
ioputc(addr->addr[i], stream);
}
}
/* ====================================================================== */
/* ipaddr_fetch_iostream() ***********************************************
*
* Parse raw ipaddr from iostream.
************************************************************************/
BOOL ipaddr_fetch_iostream(struct ipaddr *addr, struct iostream *stream)
{
BOOL rc = T;
int i, c;
memset(addr->addr, 0, 16);
if ((c = iogetc(stream)) != EOF) {
addr->version = (unsigned char) c;
for (i = 0; i < (addr->version == 6 ? 16 : 4); i++) {
if ((c = iogetc(stream)) == EOF) {
rc = NIL;
break;
}
addr->addr[i] = (unsigned char) c;
}
}
if (c == EOF) {
log_panic
("Unexpected disconnect receiving IP address from frontend");
rc = NIL;
}
return (rc);
}
/* ====================================================================== */
/* ipaddr_set() **********************************************************
*
* Set IP address in ipaddr structure
* ipaddr: Target ipaddr structure
* ipvers: IP version
* addr: Address string
************************************************************************/
void
ipaddr_set(struct ipaddr *ipaddr, unsigned long version,
unsigned char *addr)
{
unsigned char ipv4_prefix[] = {
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff
};
if (version != 4 && version != 6)
log_fatal("ipaddr_set(): IPv4 and IPv6 only supported!");
ipaddr->version = version;
memset(ipaddr->addr, 0, 16);
memcpy(ipaddr->addr, addr, version == 6 ? 16 : 4);
if ((ipaddr->version == 6) && !memcmp(ipaddr->addr, ipv4_prefix, 12)) {
memcpy(ipaddr->addr, &ipaddr->addr[12], 4);
memset(&ipaddr->addr[4], 0, 12);
ipaddr->version = 4;
}
}
|