bnf.c 21.1 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3
/*
 * This file is part of the Sofia-SIP package
 *
4
 * Copyright (C) 2005,2006 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
5 6 7
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE bnf.c
 * @brief Character syntax table for HTTP-like protocols.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
29
 * @author Kai Vehmanen <Kai.Vehmanen@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
30 31 32 33 34 35
 *
 * @date Created: Thu Jun  8 19:28:55 2000 ppessi
 */

#include "config.h"

36
#include "sofia-sip/bnf.h"
Pekka Pessi's avatar
Pekka Pessi committed
37

38 39
#include <assert.h>

Pekka Pessi's avatar
Pekka Pessi committed
40 41 42 43 44 45 46 47 48 49 50 51
#define ws    bnf_ws
#define crlf  bnf_crlf
#define alpha bnf_alpha
#define digit bnf_mark|bnf_token0|bnf_safe
#define sep   bnf_separator
#define msep  bnf_mark|bnf_separator
#define psep  bnf_param0|bnf_separator
#define tok   bnf_token0
#define mtok  bnf_mark|bnf_token0
#define smtok bnf_mark|bnf_token0|bnf_safe
#define safe  bnf_safe

52
/** Table for determining class of a character */
Pekka Pessi's avatar
Pekka Pessi committed
53 54 55 56 57 58 59 60 61 62
unsigned char const _bnf_table[256] = {
  0,     0,     0,     0,     0,     0,     0,     0,
  0,     ws,    crlf,  0,     0,     crlf,  0,     0,
  0,     0,     0,     0,     0,     0,     0,     0,
  0,     0,     0,     0,     0,     0,     0,     0,
  ws,    mtok,  sep,   0,     safe,  mtok,  0,     mtok,  /*  !"#$%&' */
  msep,  msep,  mtok,  tok,   sep,   smtok, smtok, psep,  /* ()*+,-./ */
  digit, digit, digit, digit, digit, digit, digit, digit, /* 01234567 */
  digit, digit, psep,  sep,   sep,   sep,   sep,   sep,   /* 89:;<=>? */
  sep,   alpha, alpha, alpha, alpha, alpha, alpha, alpha, /* @ABCDEFG */
63
  alpha, alpha, alpha, alpha, alpha, alpha, alpha, alpha, /* HIJKLMNO */
Pekka Pessi's avatar
Pekka Pessi committed
64 65 66
  alpha, alpha, alpha, alpha, alpha, alpha, alpha, alpha, /* PQRSTUVW */
  alpha, alpha, alpha, psep,  sep,   psep,  0,     smtok, /* XYZ[\]^_ */
  tok,   alpha, alpha, alpha, alpha, alpha, alpha, alpha, /* `abcdefg */
67
  alpha, alpha, alpha, alpha, alpha, alpha, alpha, alpha, /* hijklmno */
Pekka Pessi's avatar
Pekka Pessi committed
68 69 70
  alpha, alpha, alpha, alpha, alpha, alpha, alpha, alpha, /* pqrstuvw */
  alpha, alpha, alpha, sep,   0,     sep,   mtok,  0,     /* xyz{|}~  */
};
71

72
#if 0				/* This escaped lab */
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

#define BM(c, m00, m32, m64, m96)			   \
  ((c < 64)						   \
   ? ((c < 32)						   \
      ? (m00 & (1 << (31 - c)))				   \
      : (m32 & (1 << (63 - c))))			   \
   : ((c < 96)						   \
      ? (m64 & (1 << (95 - c)))				   \
      : (m96 & (1 << (127 - c)))))

/** Span of a token */
size_t bnf_span_token(char const *s)
{
  char const *e = s;
  unsigned const m32 = 0x4536FFC0U, m64 = 0x7FFFFFE1U, m96 = 0xFFFFFFE2U;

  while (BM(*e, 0, m32, m64, m96))
    e++;

  return e - s;
}

/** Span of a token */
size_t bnf_span_token4(char const *s)
{
  char const *e = s; 
  while (_bnf_table[(unsigned char)(*e)] & bnf_token)
    e++; 
  return e - s; 
}

char * bnf_span_token_end(char const *s)
{
  return (char *)s;
}
108
#endif
109

110
/** Return length of decimal-octet */
111
su_inline int span_ip4_octet(char const *host)
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
{
  /*
      decimal-octet =       DIGIT
                            / DIGIT DIGIT
                            / (("0"/"1") 2*(DIGIT))
                            / ("2" ("0"/"1"/"2"/"3"/"4") DIGIT)
                            / ("2" "5" ("0"/"1"/"2"/"3"/"4"/"5"))
  */

  if (!IS_DIGIT(host[0]))
    return 0;

  /* DIGIT */
  if (!IS_DIGIT(host[1]))
    return 1;

  if (host[0] == '2') {
    /* ("2" "5" ("0"/"1"/"2"/"3"/"4"/"5")) */
    if (host[1] == '5' && host[2] >= '0' && host[2] <= '5')
      return 3;

    /* ("2" ("0"/"1"/"2"/"3"/"4") DIGIT) */
    if (host[1] >= '0' && host[1] <= '4' &&
	host[2] >= '0' && host[2] <= '9')
      return 3;
  }
  else if (host[0] == '0' || host[0] == '1') {
    if (IS_DIGIT(host[2]))
      /* ("1" 2*(DIGIT)) ... or "0" 2*(DIGIT) */
      return 3;
  }

  /* POS-DIGIT DIGIT */
  return 2;
}

/** Return length of valid IP4 address */
static
int span_canonic_ip4_address(char const *host, int *return_canonize)
{
  int n, len, canonize = 0;

  if (host == NULL)
    return 0;

  /* IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet */
  len = span_ip4_octet(host);
  if (len == 0 || host[len] != '.')
    return 0;
  if (len > 1 && host[0] == '0')
    canonize = 1;
  n = len + 1;

  len = span_ip4_octet(host + n);
  if (len == 0 || host[n + len] != '.')
    return 0;
  if (len > 1 && host[n] == '0')
    canonize = 1;
  n += len + 1;

  len = span_ip4_octet(host + n);
  if (len == 0 || host[n + len] != '.')
    return 0;
  if (len > 1 && host[n] == '0')
    canonize = 1;
  n += len + 1;

  len = span_ip4_octet(host + n);
  if (len == 0 || IS_DIGIT(host[n + len]) || host[n + len] == '.')
    return 0;
  if (len > 1 && host[n] == '0')
    canonize = 1;
  n += len;

  if (canonize && return_canonize)
    *return_canonize = 1;

  return n;
}

/** Return length of valid IP4 address.
 *
 * Note that we accept here up to two leading zeroes 
 * which makes "dotted decimal" notation ambiguous:
 * 127.000.000.001 is interpreted same as 127.0.0.1 
 *
 * Note that traditionally IP address octets starting
 * with zero have been interpreted as octal:
 * 172.055.055.001 has been same as 172.45.45.1
 *
 * @b However, we interpret them as @b decimal,
 * 172.055.055.001 is same as 172.55.55.1.
 */
int span_ip4_address(char const *host)
{
  return span_canonic_ip4_address(host, NULL);
}

210
/** Scan and canonize a valid IP4 address. */
211 212 213
int scan_ip4_address(char **inout_host)
{
  char *src = *inout_host, *dst = src;
214 215
  issize_t n;
  int canonize = 0;
216

217 218 219
  if (src == NULL)
    return -1;

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
  n = span_canonic_ip4_address(src, &canonize);
  if (n == 0)
    return -1;

  *inout_host += n;

  if (!canonize)
    return n;

  for (;;) {
    char c = *dst++ = *src++;

    if (IS_DIGIT(*src)) {
      if (canonize && c == '0')
	dst--;
      else if (c == '.')
	canonize = 1;
      else
	canonize = 0;
    }
    else if (*src != '.') {
      break;
    }
  }
  *dst = '\0';

  return n;
}

/** Return length of hex4 */
250
su_inline int span_hex4(char const *host)
251 252 253 254 255 256 257 258 259 260 261 262 263
{
  if (!IS_HEX(host[0]))
    return 0;
  if (!IS_HEX(host[1]))
    return 1;
  if (!IS_HEX(host[2]))
    return 2;
  if (!IS_HEX(host[3]))
    return 3;
  return 4;
}

/** Return length of valid IP6 address */
264
su_inline
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
int span_canonic_ip6_address(char const *host,
			     int *return_canonize,
			     char *hexparts[9])
{
  int n = 0, len, hex4, doublecolon = 0, canonize = 0;

  /*
     IPv6address    =  hexpart [ ":" IPv4address ]
     hexpart        =  hexseq / hexseq "::" [ hexseq ] / "::" [ hexseq ]
     hexseq         =  hex4 *( ":" hex4)
     hex4           =  1*4HEXDIG

     There is at most 8 hex4, 6 hex4 if IPv4address is included.
  */

  if (host == NULL)
    return 0;

  for (hex4 = 0; hex4 < 8; ) {
    len = span_hex4(host + n);

    if (return_canonize) {
      if ((len > 1 && host[n + 1] == '0') || host[n] == '0')
	canonize = 1;
      if (hexparts)
	hexparts[hex4 + doublecolon] = (char *)(host + n);
    }

    if (host[n + len] == ':') {
      if (len != 0) {
	hex4++;
	n += len + 1;
	if (!doublecolon && host[n] == ':') {
	  if (return_canonize && hexparts) {
	    hexparts[hex4] = (char *)(host + n - 1);
	  }
	  doublecolon++, n++;
	}
      }
      else if (n == 0 && host[1] == ':') {
	doublecolon++, n = 2;
      }
      else
	break;
    }
    else if (host[n + len] == '.') {
      len = span_canonic_ip4_address(host + n, return_canonize);

      if (len == 0 || hex4 > 6 || !(doublecolon || hex4 == 6))
	return 0;

      if (canonize && return_canonize)
	*return_canonize = 1;

      return n + len;
    }
    else {
      if (len != 0)
	hex4++;
      n += len;
      break;
    }
  }

  if (hex4 != 8 && !doublecolon)
    return 0;

  if (IS_HEX(host[n]) || host[n] == ':')
    return 0;

  if (canonize && return_canonize)
    *return_canonize = canonize;

  return n;
}

/** Canonize scanned IP6 address.
 *
 * @retval Length of canonized IP6 address.
 */
345
su_inline
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
int canonize_ip6_address(char *host, char *hexparts[9])
{
  char *dst, *hex, *ip4 = NULL;
  int i, doublecolon, j, maxparts, maxspan, span, len;

  char buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];

  /*
    Canonic representation has fewest chars
     - except for mapped/compatible IP4 addresses, like
       ::15.21.117.42 or ::ffff:15.21.117.42 which have non-canonic forms of
       ::f15:752a or ::ffff:f15:752a
    => we just canonize hexparts and ip4part separately
       and select optimal place for doublecolon
       (with expection of ::1 and ::, which are canonized)
  */
  for (i = 0, doublecolon = -1; i < 9; i++) {
    hex = hexparts[i];
    if (!hex)
      break;
    if (hex[0] == ':')
      doublecolon = i;
    while (hex[0] == '0' && IS_HEX(hex[1]))
      hex++;
    hexparts[i] = hex;
  }
  assert(i > 0);

  if (hexparts[i - 1][span_hex4(hexparts[i - 1])] == '.')
    ip4 = hexparts[--i];

  maxparts = ip4 ? 6 : 8;

  if (doublecolon >= 0) {
    /* Order at most 8 (or 6) hexparts */
    assert(i <= maxparts + 1);

    if (i == maxparts + 1) {
      /* There is an extra doublecolon */
385
      for (j = doublecolon; j + 1 < i; j++)
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	hexparts[j] = hexparts[j + 1];
      i--;
    }
    else {
      for (j = maxparts; i > doublecolon + 1; )
	hexparts[--j] = hexparts[--i];
      for (;j > doublecolon;)
	hexparts[--j] = "0:";
      i = maxparts;
    }
  }
  assert(i == maxparts);

  /* Scan for optimal place for "::" */
  for (i = 0, maxspan = 0, span = 0, doublecolon = 0; i < maxparts; i++) {
    if (hexparts[i][0] == '0')
      span++;
    else if (span > maxspan)
      doublecolon = i - span, maxspan = span, span = 0;
    else
      span = 0;
  }

  if (span > maxspan)
    doublecolon = i - span, maxspan = span;

  dst = buf;

  for (i = 0; i < maxparts; i++) {
    if (i == doublecolon)
      hex = i == 0 ? "::" : ":", len = 1;
    else if (i > doublecolon && i < doublecolon + maxspan)
      continue;
    else
      hex = hexparts[i], len = span_hex4(hex);
    if (hex[len] == ':')
      len++;
    memcpy(dst, hex, len);
    dst += len;
  }

  if (ip4) {
    hex = ip4;
    len = scan_ip4_address(&hex); assert(len > 0);

    /* Canonize :: and ::1 */
    if (doublecolon == 0 && maxspan == 6) {
      if (len == 7 && strncmp(ip4, "0.0.0.0", len) == 0)
	ip4 = "", len = 0;
      else if (len == 7 && strncmp(ip4, "0.0.0.1", len) == 0)
	ip4 = "1", len = 1;
    }

    memcpy(dst, ip4, len);
    dst += len;
  }

  len = dst - buf;

  memcpy(host, buf, len);

  return len;
}

/** Return length of valid IP6 address */
int span_ip6_address(char const *host)
{
  return span_canonic_ip6_address(host, NULL, NULL);
}

/** Scan and canonize valid IP6 address.
 *
 * @param inout_host input pointer to string to scan
 *                   output pointer to first character after valid IP6 address
 *
 * @retval Length of valid IP6 address or -1 upon an error.
 *
463
 * @note Scanned IP6 is not always NUL-terminated.
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
 */
int scan_ip6_address(char **inout_host)
{
  int n, canonize = 0;
  char *host = *inout_host;
  char *hexparts[9] = { NULL };

  n = span_canonic_ip6_address(host, &canonize, hexparts);

  if (n == 0)
    return -1;

  *inout_host += n;

  if (canonize) {
    int len = canonize_ip6_address(host, hexparts);
    assert(len <= n);
    if (len < n)
      host[len] = '\0';
  }

  return n;
}

/** Return length of valid IP6 reference. */
int span_ip6_reference(char const *host)
{
  /* IPv6reference  =  "[" IPv6address "]" */

493
  if (host && host[0] == '[') {
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    int n = span_ip6_address(host + 1);
    if (n > 0 && host[n + 1] == ']')
      return n + 2;
  }

  return 0;
}

/** Scan valid IP6 reference. */
int scan_ip6_reference(char **inout_host)
{
  int n, canonize = 0;
  char *host = *inout_host;
  char *hexparts[9] = { NULL };

  /* IPv6reference  =  "[" IPv6address "]" */

  if (host == NULL ||
      host[0] != '[' ||
      (n = span_canonic_ip6_address(host + 1, &canonize, hexparts)) == 0 ||
      host[n + 1] != ']')
    return -1;

  *inout_host += n + 2;

  if (canonize) {
    int len = canonize_ip6_address(host + 1, hexparts);

    assert(len <= n);

    host[len + 1] = ']';
    if (len + 2 < n + 2)
      host[len + 2] = '\0';
  }

  return n + 2;
}

/** Return length of valid IP4 or IP6 address. */
int span_ip_address(char const *host)
{
  if (!host || !host[0])
    return 0;

  /* IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet */
  if (IS_DIGIT(host[0])) {
    int n = span_ip4_address(host);
    if (n)
      return n;
  }

  if (host[0] == '[')
    return span_ip6_reference(host);
  else
    return span_ip6_address(host);
}

/** Scan valid IP4/IP6 address. */
int scan_ip_address(char **inout_host)
{
  char *host = *inout_host;

  if (host == NULL)
    return -1;

  /* IPv6reference  =  "[" IPv6address "]" */
  if (host[0] == '[')
    return scan_ip6_reference(inout_host);

  if (IS_DIGIT(host[0])) {
    int n = scan_ip4_address(inout_host);
    if (n > 0)
      return n;
  }

  return scan_ip6_address(inout_host);
}

/** Return length of a valid domain label */
573
su_inline
574
size_t span_domain_label(char const *label)
575 576 577
{
  /* domainlabel =  alphanum / alphanum *( alphanum / "-" ) alphanum */
  if (IS_ALPHANUM(*label)) {
578
    size_t n;
579 580 581 582 583 584 585 586 587 588
    for (n = 1; IS_ALPHANUM(label[n]) || label[n] == '-'; n++)
      ;
    if (IS_ALPHANUM(label[n - 1]))
      return n;
  }

  return 0;
}

/** Scan valid domain name and count number of labels in it. */
589
su_inline
590
size_t span_domain_labels(char const *host, size_t *return_labels)
591
{
592 593
  size_t len, n, labels;
  int c;
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

  if (!host || !host[0])
    return 0;

  for (n = 0, labels = 0; ; n += len) {
    len = span_domain_label(host + n);
    if (len == 0)
      return 0;

    labels++;

    if (host[n + len] != '.')
      break;
    len++;
    if (!IS_ALPHANUM(host[n + len]))
      break;
  }

  /* Check that last label does not start with number */
  if (!IS_ALPHA(host[n]))
    return 0;

  c = host[n + len];
  if (IS_ALPHANUM(c) || c == '-' || c == '.')
    return 0;

  if (return_labels)
    *return_labels = labels;

  return n + len;
}

/** Return length of a valid domain name.
 *
 * @code
 * hostname         =  *( domainlabel "." ) toplabel [ "." ]
 * domainlabel      =  alphanum
 *                     / alphanum *( alphanum / "-" ) alphanum
 * toplabel         =  ALPHA / ALPHA *( alphanum / "-" ) alphanum
 * @endcode
 */
635
isize_t span_domain(char const *host)
636 637 638 639 640
{
  return span_domain_labels(host, NULL);
}

/** Scan valid domain name. */
641
issize_t scan_domain(char **inout_host)
642 643
{
  char *host;
644
  size_t n, labels;
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

  n = span_domain_labels(host = *inout_host, &labels);
  if (n == 0)
    return -1;

  /* Remove extra dot at the end of hostname */
  if (labels > 1 && host[n - 1] == '.')
    host[n - 1] = '\0';

  *inout_host += n;

  return n;
}

/** Return length of a valid domain name or IP address. */
660
isize_t span_host(char const *host)
661 662 663 664 665 666 667 668 669 670
{
 if (!host || !host[0])
    return 0;

  if (host[0] == '[')
    return span_ip6_reference(host);

  if (IS_DIGIT(host[0])) {
    int n = span_ip4_address(host);
    if (n)
671
      return (isize_t)n;
672 673 674 675 676 677
  }

  return span_domain(host);
}

/** Scan valid domain name or IP address. */
678
issize_t scan_host(char **inout_host)
679 680 681 682 683 684 685 686 687 688 689 690 691
{
  char *host = *inout_host;

  if (host == NULL)
    return -1;

  /* IPv6reference  =  "[" IPv6address "]" */
  if (host[0] == '[')
    return scan_ip6_reference(inout_host);

  if (IS_DIGIT(host[0])) {
    int n = scan_ip4_address(inout_host);
    if (n > 0)
692
      return (issize_t)n;
693 694 695 696 697
  }

  return scan_domain(inout_host);
}

698 699 700 701 702 703 704 705 706 707 708 709 710
#include <sofia-sip/hostdomain.h>

/** Return true if @a string is valid IP4 address in dot-notation.
 *
 * @note Only 4-octet form is accepted, e.g., @c 127.1 is not considered
 * valid IP4 address.
 */
int host_is_ip4_address(char const *string)
{
  int n = span_ip4_address(string);
  return n > 0 && string[n] == '\0';
}

711
/** Return true if @a string is valid IP6 address in hex notation.
712
 *
713
 * E.g., fe80::1 is a valid IP6 address. 
714 715 716 717 718 719 720
 */
int host_is_ip6_address(char const *string)
{
  int n = span_ip6_address(string);
  return n > 0 && string[n] == '\0';
}

721 722 723 724 725
int host_ip6_reference(char const *string)
{
  return host_is_ip6_reference(string);
}

726 727 728 729 730
/** Return true if @a string is valid IP6 reference, 
 *  i.e. hex notation in square brackets.
 *
 * E.g., [::1] is a valid IP6 reference.
 */
731
int host_is_ip6_reference(char const *string)
732 733 734 735 736
{
  int n = span_ip6_reference(string);
  return n > 0 && string[n] == '\0';
}

737 738 739 740 741
/** Return true if @a string is valid IP address.
 *
 * Valid IP address is either a IP4 adddress in quad-octet notation, 
 * IP6 hex address or IP6 reference in square brackets ([]).
 */
742 743 744 745 746 747
int host_is_ip_address(char const *string)
{
  int n = span_ip_address(string);
  return n > 0 && string[n] == '\0';
}

748 749 750 751 752 753 754 755 756 757 758 759 760
/** Return true if @a string is valid a domain name.
 *
 * Valid domain name consists of alphanumeric labels separated with 
 * dot ("."). There can be a "-" in the middle of label.
 * The last label must start with a letter.
 *
 * @code
 * hostname         =  *( domainlabel "." ) toplabel [ "." ]
 * domainlabel      =  alphanum
 *                     / alphanum *( alphanum / "-" ) alphanum
 * toplabel         =  ALPHA / ALPHA *( alphanum / "-" ) alphanum
 * @endcode
 */
761
int host_is_domain(char const *string)
762
{
763
  size_t n = string ? span_domain(string) : 0;
764
  return string && n > 0 && string[n] == '\0';
765 766
}

767 768 769 770
/** Return true if @a string is valid a host name.
 *
 * Check if the @a string is a domain name, IP address or IP6 reference.
 */
771 772
int host_is_valid(char const *string)
{
773
  size_t n = span_host(string);
774 775
  return n > 0 && string[n] == '\0';
}
776

777 778 779 780 781 782 783
/** Returns true if @a string is describing a local address.
 *
 * Uses the definitions of local addresses found in RFC1700 and 
 * RFC4291. 
 */
int host_is_local(char const *host)
{
784 785 786
  size_t n;

  if (host_is_ip6_reference(host))
787 788 789 790 791
    return (strcmp(host, "[::1]") == 0);
  else if (host_is_ip6_address(host))
    return (strcmp(host, "::1") == 0);
  else if (host_is_ip4_address(host))
    return (strncmp(host, "127.", 4) == 0);
792 793 794 795 796 797 798 799 800 801 802

  n = span_domain(host);

  return 
    n >= 9 /* strlen("localhost") */ &&
    strncasecmp(host, "localhost", 9) == 0 &&
    (n == 9 || 
     ((n == 10 || /* localhost. */
       n == 21 || /* strlen("localhost.localdomain") */
       n == 22) && /* strlen("localhost.localdomain.") */
      strncasecmp(host + 9, ".localdomain.", n - 9) == 0));
803 804
}

805 806 807 808 809
/** Return true if @a string has domain name in "invalid." domain.
 *
 */
int host_has_domain_invalid(char const *string)
{
810
  size_t n = span_domain(string);
811 812 813 814 815 816 817 818 819 820 821 822 823

  if (n >= 7 && string[n] == '\0') {
    static char const invalid[] = ".invalid";
    if (string[n - 1] == '.')	/* .invalid. perhaps? */
      n--;
    if (n == 7 /* strlen("invalid") */)
      return strncasecmp(string, invalid + 1, 7) == 0;
    else
      return strncasecmp(string + n - 8, invalid, 8) == 0;
  }

  return 0;
}
Pekka Pessi's avatar
Pekka Pessi committed
824 825 826 827 828 829 830 831

#include <sofia-sip/su.h>

static size_t convert_ip_address(char const *s,
				 uint8_t addr[16],
				 size_t *return_addrlen)
{
  size_t len;
832
  int canonize = 0;
833
  char buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
Pekka Pessi's avatar
Pekka Pessi committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

#if SU_HAVE_IN6

  len = span_ip6_reference(s);
  if (len) {
    assert(len - 2 < sizeof buf); assert(len > 2);

    if (s[len])
      return 0;

    len = len - 2;
    s = memcpy(buf, s + 1, len), buf[len] = '\0';
  }
  else
    len = span_ip6_address(s);

  if (len) {
    if (s[len] == '\0' && inet_pton(AF_INET6, s, addr) == 1) {
      if (SU_IN6_IS_ADDR_V4MAPPED(addr) ||
	  SU_IN6_IS_ADDR_V4COMPAT(addr)) {
	memcpy(addr, addr + 12, 4);
	return (void)(*return_addrlen = 4), len;
      }
      return (void)(*return_addrlen = 16), len;
    }
  }
860
  else
Pekka Pessi's avatar
Pekka Pessi committed
861
#endif
862
  len = span_canonic_ip4_address(s, &canonize);
863

Pekka Pessi's avatar
Pekka Pessi committed
864
  if (len) {
865 866 867 868 869
    if (canonize) {
      char *tmp = buf;
      s = memcpy(tmp, s, len + 1);
      scan_ip4_address(&tmp);      
    }
Pekka Pessi's avatar
Pekka Pessi committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    if (s[len] == '\0' && inet_pton(AF_INET, s, addr) == 1)
      return (void)(*return_addrlen = 4), len;
  }

  return 0;
}

/** Compare two host names or IP addresses
 *
 * Converts valid IP addresses to the binary format before comparing them. 
 * Note that IP6-mapped IP4 addresses and IP6-compatible IP4 addresses are
 * compared as IP4 addresses; that is, ::ffff:127.0.0.1, ::127.0.0.1 and
 * 127.0.0.1 all are all equal.
 *
 * @param a IP address or domain name
 * @param b IP address or domain name
 * 
 * @retval -1 if a < b
 * @retval 0 if a == b
 * @retval 1 if a > b
 *
 * @since New in @VERSION_1_12_4.
 */
int host_cmp(char const *a, char const *b)
{
  uint8_t a6[16], b6[16];
  size_t alen, blen, asize = 0, bsize = 0;
897
  int retval;
Pekka Pessi's avatar
Pekka Pessi committed
898

899 900
  if (a == NULL || b == NULL) {
    retval = (a != NULL) - (b != NULL);
Pekka Pessi's avatar
Pekka Pessi committed
901 902
  }
  else {
903 904 905 906 907 908 909 910 911 912 913 914 915 916
    alen = convert_ip_address(a, a6, &asize);
    blen = convert_ip_address(b, b6, &bsize);

    if (alen > 0 && blen > 0) {
      if (asize < bsize)
	retval = -1;
      else if (asize > bsize)
	retval = 1;
      else
	retval = memcmp(a6, b6, asize);
    }
    else {
      retval = strcasecmp(a, b);
    }
Pekka Pessi's avatar
Pekka Pessi committed
917
  }
918 919

  return retval;
Pekka Pessi's avatar
Pekka Pessi committed
920
}