1
0

fty_ipv4.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
  3. * You may freely copy it for use as a template for your own field types.
  4. * If you develop a field type that might be of general use, please send
  5. * it back to the ncurses maintainers for inclusion in the next version.
  6. */
  7. /***************************************************************************
  8. * *
  9. * Author : Per Foreby, [email protected] *
  10. * *
  11. ***************************************************************************/
  12. #include "form.priv.h"
  13. MODULE_ID("$Id$")
  14. /*---------------------------------------------------------------------------
  15. | Facility : libnform
  16. | Function : static bool Check_IPV4_Field(
  17. | FIELD * field,
  18. | const void * argp)
  19. |
  20. | Description : Validate buffer content to be a valid IP number (Ver. 4)
  21. |
  22. | Return Values : TRUE - field is valid
  23. | FALSE - field is invalid
  24. +--------------------------------------------------------------------------*/
  25. static bool Check_IPV4_Field(FIELD * field, const void * argp)
  26. {
  27. char *bp = field_buffer(field,0);
  28. int num = 0, len;
  29. unsigned int d1, d2, d3, d4;
  30. if(isdigit(*bp)) /* Must start with digit */
  31. {
  32. num = sscanf(bp, "%u.%u.%u.%u%n", &d1, &d2, &d3, &d4, &len);
  33. if (num == 4)
  34. {
  35. bp += len; /* Make bp point to what sscanf() left */
  36. while (*bp && isspace(*bp))
  37. bp++; /* Allow trailing whitespace */
  38. }
  39. }
  40. return ((num != 4 || *bp || d1 > 255 || d2 > 255
  41. || d3 > 255 || d4 > 255) ? FALSE : TRUE);
  42. }
  43. /*---------------------------------------------------------------------------
  44. | Facility : libnform
  45. | Function : static bool Check_IPV4_Character(
  46. | int c,
  47. | const void *argp )
  48. |
  49. | Description : Check a character for unsigned type or period.
  50. |
  51. | Return Values : TRUE - character is valid
  52. | FALSE - character is invalid
  53. +--------------------------------------------------------------------------*/
  54. static bool Check_IPV4_Character(int c, const void * argp)
  55. {
  56. return ((isdigit(c) || (c=='.')) ? TRUE : FALSE);
  57. }
  58. static FIELDTYPE typeIPV4 = {
  59. _RESIDENT,
  60. 1, /* this is mutable, so we can't be const */
  61. (FIELDTYPE *)0,
  62. (FIELDTYPE *)0,
  63. NULL,
  64. NULL,
  65. NULL,
  66. Check_IPV4_Field,
  67. Check_IPV4_Character,
  68. NULL,
  69. NULL
  70. };
  71. FIELDTYPE* TYPE_IPV4 = &typeIPV4;
  72. /* fty_ipv4.c ends here */