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