cmdline: big rework and clean of cmdline library
[libcmdline.git] / src / lib / cmdline_parse_ipaddr.c
index 2ffbf0c..eab2319 100644 (file)
 #include "cmdline_parse.h"
 #include "cmdline_parse_ipaddr.h"
 
-struct cmdline_token_ops cmdline_token_ipaddr_ops = {
-       .parse = cmdline_parse_ipaddr,
-       .complete_get_nb = NULL,
-       .complete_get_elt = NULL,
-       .get_help = cmdline_get_help_ipaddr,
-};
-
 #define INADDRSZ 4
 #define IN6ADDRSZ 16
 
@@ -108,8 +101,8 @@ struct cmdline_token_ops cmdline_token_ipaddr_ops = {
  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
  */
 
-static int inet_pton4(const char *src, u_char *dst);
-static int inet_pton6(const char *src, u_char *dst);
+static int inet_pton4(const char *src, unsigned char *dst);
+static int inet_pton6(const char *src, unsigned char *dst);
 
 /* int
  * inet_pton(af, src, dst)
@@ -148,11 +141,11 @@ my_inet_pton(int af, const char *src, void *dst)
  *      Paul Vixie, 1996.
  */
 static int
-inet_pton4(const char *src, u_char *dst)
+inet_pton4(const char *src, unsigned char *dst)
 {
        static const char digits[] = "0123456789";
        int saw_digit, octets, ch;
-       u_char tmp[INADDRSZ], *tp;
+       unsigned char tmp[INADDRSZ], *tp;
 
        saw_digit = 0;
        octets = 0;
@@ -161,7 +154,7 @@ inet_pton4(const char *src, u_char *dst)
                const char *pch;
 
                if ((pch = strchr(digits, ch)) != NULL) {
-                       u_int new = *tp * 10 + (pch - digits);
+                       unsigned int new = *tp * 10 + (pch - digits);
 
                        if (new > 255)
                                return (0);
@@ -170,7 +163,7 @@ inet_pton4(const char *src, u_char *dst)
                                        return (0);
                                saw_digit = 1;
                        }
-                       *tp = new;
+                       *tp = (unsigned char)new;
                } else if (ch == '.' && saw_digit) {
                        if (octets == 4)
                                return (0);
@@ -200,14 +193,14 @@ inet_pton4(const char *src, u_char *dst)
  *      Paul Vixie, 1996.
  */
 static int
-inet_pton6(const char *src, u_char *dst)
+inet_pton6(const char *src, unsigned char *dst)
 {
        static const char xdigits_l[] = "0123456789abcdef",
                xdigits_u[] = "0123456789ABCDEF";
-       u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
+       unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
        const char *xdigits, *curtok;
        int ch, saw_xdigit, count_xdigit;
-       u_int val;
+       unsigned int val;
 
        memset((tp = tmp), '\0', IN6ADDRSZ);
        endp = tp + IN6ADDRSZ;
@@ -247,8 +240,8 @@ inet_pton6(const char *src, u_char *dst)
                        }
                        if (tp + sizeof(int16_t) > endp)
                                return (0);
-                       *tp++ = (u_char) (val >> 8) & 0xff;
-                       *tp++ = (u_char) val & 0xff;
+                       *tp++ = (unsigned char) ((val >> 8) & 0xff);
+                       *tp++ = (unsigned char) (val & 0xff);
                        saw_xdigit = 0;
                        count_xdigit = 0;
                        val = 0;
@@ -266,8 +259,8 @@ inet_pton6(const char *src, u_char *dst)
        if (saw_xdigit) {
                if (tp + sizeof(int16_t) > endp)
                        return (0);
-               *tp++ = (u_char) (val >> 8) & 0xff;
-               *tp++ = (u_char) val & 0xff;
+               *tp++ = (unsigned char) ((val >> 8) & 0xff);
+               *tp++ = (unsigned char) (val & 0xff);
        }
        if (colonp != NULL) {
                /*
@@ -289,8 +282,9 @@ inet_pton6(const char *src, u_char *dst)
        return (1);
 }
 
-int
-cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
+static int
+cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
+                    unsigned ressize)
 {
        struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
        unsigned int token_len = 0;
@@ -299,18 +293,14 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
        char *prefix, *prefix_end;
        long prefixlen;
 
-       if (! *buf)
+       if (res && ressize < sizeof(cmdline_ipaddr_t))
                return -1;
 
-       while (!cmdline_isendoftoken(buf[token_len]))
-               token_len++;
-
        /* if token is too big... */
-       if (token_len >= INET6_ADDRSTRLEN+4)
+       token_len = snprintf(ip_str, sizeof(ip_str), "%s", buf);
+       if (token_len >= sizeof(ip_str))
                return -1;
 
-       snprintf(ip_str, token_len+1, "%s", buf);
-
        /* convert the network prefix */
        if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
                prefix = strrchr(ip_str, '/');
@@ -343,8 +333,9 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
 
 }
 
-int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
-                           unsigned int size)
+static int
+cmdline_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
+                   unsigned int size)
 {
        struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
 
@@ -373,3 +364,12 @@ int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
        }
        return 0;
 }
+
+struct cmdline_token_ops cmdline_token_ipaddr_ops = {
+       .parse = cmdline_parse_ipaddr,
+       .complete_start = NULL,
+       .complete_iterate = NULL,
+       .complete_end = NULL,
+       .help = cmdline_help_ipaddr,
+};
+