diff options
Diffstat (limited to 'libmailutils/sockaddr/ipaddr.c')
-rw-r--r-- | libmailutils/sockaddr/ipaddr.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/libmailutils/sockaddr/ipaddr.c b/libmailutils/sockaddr/ipaddr.c new file mode 100644 index 000000000..6b14b4c29 --- /dev/null +++ b/libmailutils/sockaddr/ipaddr.c | |||
@@ -0,0 +1,88 @@ | |||
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2011 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 3 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General | ||
15 | Public License along with this library. If not, see | ||
16 | <http://www.gnu.org/licenses/>. */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | |||
22 | #include <string.h> | ||
23 | #include <mailutils/sockaddr.h> | ||
24 | #include <mailutils/cctype.h> | ||
25 | |||
26 | int | ||
27 | mu_str_is_ipv4 (const char *addr) | ||
28 | { | ||
29 | int dot_count = 0; | ||
30 | int digit_count = 0; | ||
31 | |||
32 | for (; *addr; addr++) | ||
33 | { | ||
34 | if (!mu_isascii (*addr)) | ||
35 | return 0; | ||
36 | if (*addr == '.') | ||
37 | { | ||
38 | if (++dot_count > 3) | ||
39 | break; | ||
40 | digit_count = 0; | ||
41 | } | ||
42 | else if (!(mu_isdigit (*addr) && ++digit_count <= 3)) | ||
43 | return 0; | ||
44 | } | ||
45 | |||
46 | return (dot_count == 3); | ||
47 | } | ||
48 | |||
49 | int | ||
50 | mu_str_is_ipv6 (const char *addr) | ||
51 | { | ||
52 | int col_count = 0; /* Number of colons */ | ||
53 | int dcol = 0; /* Did we encounter a double-colon? */ | ||
54 | int dig_count = 0; /* Number of digits in the last group */ | ||
55 | |||
56 | for (; *addr; addr++) | ||
57 | { | ||
58 | if (!mu_isascii (*addr)) | ||
59 | return 0; | ||
60 | else if (mu_isxdigit (*addr)) | ||
61 | { | ||
62 | if (++dig_count > 4) | ||
63 | return 0; | ||
64 | } | ||
65 | else if (*addr == ':') | ||
66 | { | ||
67 | if (col_count && dig_count == 0 && ++dcol > 1) | ||
68 | return 0; | ||
69 | if (++col_count > 7) | ||
70 | return 0; | ||
71 | dig_count = 0; | ||
72 | } | ||
73 | else | ||
74 | return 0; | ||
75 | } | ||
76 | |||
77 | return (col_count == 7 || dcol); | ||
78 | } | ||
79 | |||
80 | int | ||
81 | mu_str_is_ipaddr (const char *addr) | ||
82 | { | ||
83 | if (strchr (addr, '.')) | ||
84 | return mu_str_is_ipv4(addr); | ||
85 | else if (strchr (addr, ':')) | ||
86 | return mu_str_is_ipv6(addr); | ||
87 | return 0; | ||
88 | } | ||