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