fty_ipv4.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /****************************************************************************
  2. * Copyright (c) 1998-2004,2006 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /***************************************************************************
  29. * *
  30. * Author : Per Foreby, perf@efd.lth.se *
  31. * *
  32. ***************************************************************************/
  33. #include "form.priv.h"
  34. MODULE_ID("$Id: fty_ipv4.c,v 1.8 2006/12/02 19:33:02 tom Exp $")
  35. /*---------------------------------------------------------------------------
  36. | Facility : libnform
  37. | Function : static bool Check_IPV4_Field(
  38. | FIELD * field,
  39. | const void * argp)
  40. |
  41. | Description : Validate buffer content to be a valid IP number (Ver. 4)
  42. |
  43. | Return Values : TRUE - field is valid
  44. | FALSE - field is invalid
  45. +--------------------------------------------------------------------------*/
  46. static bool
  47. Check_IPV4_Field(FIELD *field, const void *argp GCC_UNUSED)
  48. {
  49. char *bp = field_buffer(field, 0);
  50. int num = 0, len;
  51. unsigned int d1, d2, d3, d4;
  52. if (isdigit(UChar(*bp))) /* Must start with digit */
  53. {
  54. num = sscanf(bp, "%u.%u.%u.%u%n", &d1, &d2, &d3, &d4, &len);
  55. if (num == 4)
  56. {
  57. bp += len; /* Make bp point to what sscanf() left */
  58. while (isspace(UChar(*bp)))
  59. bp++; /* Allow trailing whitespace */
  60. }
  61. }
  62. return ((num != 4 || *bp || d1 > 255 || d2 > 255
  63. || d3 > 255 || d4 > 255) ? FALSE : TRUE);
  64. }
  65. /*---------------------------------------------------------------------------
  66. | Facility : libnform
  67. | Function : static bool Check_IPV4_Character(
  68. | int c,
  69. | const void *argp )
  70. |
  71. | Description : Check a character for unsigned type or period.
  72. |
  73. | Return Values : TRUE - character is valid
  74. | FALSE - character is invalid
  75. +--------------------------------------------------------------------------*/
  76. static bool
  77. Check_IPV4_Character(int c, const void *argp GCC_UNUSED)
  78. {
  79. return ((isdigit(UChar(c)) || (c == '.')) ? TRUE : FALSE);
  80. }
  81. static FIELDTYPE typeIPV4 =
  82. {
  83. _RESIDENT,
  84. 1, /* this is mutable, so we can't be const */
  85. (FIELDTYPE *)0,
  86. (FIELDTYPE *)0,
  87. NULL,
  88. NULL,
  89. NULL,
  90. Check_IPV4_Field,
  91. Check_IPV4_Character,
  92. NULL,
  93. NULL
  94. };
  95. NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_IPV4 = &typeIPV4;
  96. /* fty_ipv4.c ends here */