Changeset 448 for trunk/src/sh_ipvx.c


Ignore:
Timestamp:
Mar 29, 2014, 7:59:38 AM (10 years ago)
Author:
katerina
Message:

Fic for ticket #350 (numeric hostnames).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_ipvx.c

    r422 r448  
    4141{
    4242  int j;
     43  int count = 0;
    4344  int len = sl_strlen(addr);
     45  unsigned int a, b, c, d;
    4446 
    45   for (j = 0; j < len; ++j)
     47  if (len > 15) /* 3*4 + 3 dots */
     48    return (1 == 0);
     49
     50  for (j = 0; j < len; ++j) {
    4651    if ( (addr[j] < '0' || addr[j] > '9') && addr[j] != '.')
    4752      return (1 == 0);
     53    if (addr[j] == '.') ++count;
     54  }
     55
     56  if (count != 3)
     57    return (1 == 0);
     58
     59  if (sscanf(addr, "%3u.%3u.%3u.%3u", &a, &b, &c, &d) != 4)
     60    return( 1 == 0 );
     61
     62  if ((a|b|c|d) > 255)
     63    return( 1 == 0 );
     64
    4865  return (1 == 1);
    4966}
     
    538555  return 0;
    539556}
     557
     558#ifdef SH_CUTEST
     559#include <stdlib.h>
     560#include "CuTest.h"
     561
     562void Test_ipvx (CuTest *tc) {
     563
     564  int result;
     565
     566  result = sh_ipvx_is_ipv4("123456789");
     567  CuAssertTrue(tc, result == FALSE);
     568
     569  result = sh_ipvx_is_ipv4("123456789123...");
     570  CuAssertTrue(tc, result == FALSE);
     571
     572  result = sh_ipvx_is_ipv4("123.456.789.123");
     573  CuAssertTrue(tc, result == FALSE);
     574
     575  result = sh_ipvx_is_ipv4("123.156.189.254");
     576  CuAssertTrue(tc, result == TRUE);
     577
     578  result = sh_ipvx_is_ipv4("255.255.255.255");
     579  CuAssertTrue(tc, result == TRUE);
     580
     581  result = sh_ipvx_is_ipv4("0.0.0.0");
     582  CuAssertTrue(tc, result == TRUE);
     583
     584  result = sh_ipvx_is_ipv4("0022.156.189.254");
     585  CuAssertTrue(tc, result == FALSE);
     586
     587  result = sh_ipvx_is_ipv4("999999999.1.1.2");
     588  CuAssertTrue(tc, result == FALSE);
     589
     590}
     591#endif
     592
Note: See TracChangeset for help on using the changeset viewer.