]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: prepare str2sa_range() to return an error message
authorWilly Tarreau <w@1wt.eu>
Fri, 1 Mar 2013 19:22:54 +0000 (20:22 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 8 Mar 2013 13:04:53 +0000 (14:04 +0100)
We'll need str2sa_range() to return address parsing errors if we want to
extend its functionalities. Let's do that now eventhough it's not used
yet.

include/common/standard.h
src/cfgparse.c
src/standard.c

index 0178757e9c99a4dcab08256a8c520c46d0da1b7a..e476b901a5fc4db837d06a22e532018848f839a3 100644 (file)
@@ -240,7 +240,7 @@ struct sockaddr_storage *str2ip(const char *str);
  * The IPv6 '::' address is IN6ADDR_ANY, so in order to bind to a given port on
  * IPv6, use ":::port". NULL is returned if the host part cannot be resolved.
  */
-struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high);
+struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err);
 
 /* converts <str> to a struct in_addr containing a network mask. It can be
  * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
index 5c3d7fab965dda5015c2ecff731f875747b20889..c6f4abfffb45828bc5971da20b449bb86927afda 100644 (file)
@@ -239,7 +239,7 @@ int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf,
                else {
                        struct sockaddr_storage *ss2;
 
-                       ss2 = str2sa_range(str, &port, &end);
+                       ss2 = str2sa_range(str, &port, &end, NULL);
                        if (!ss2) {
                                memprintf(err, "invalid listening address: '%s'\n", str);
                                goto fail;
@@ -1156,7 +1156,7 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                        struct sockaddr_storage *sk;
                        int port1, port2;
 
-                       sk = str2sa_range(args[1], &port1, &port2);
+                       sk = str2sa_range(args[1], &port1, &port2, NULL);
                        if (!sk) {
                                Alert("parsing [%s:%d] : '%s' : unknown host in '%s'\n", file, linenum, args[0], args[1]);
                                err_code |= ERR_ALERT | ERR_FATAL;
@@ -1530,7 +1530,7 @@ int cfg_parse_peers(const char *file, int linenum, char **args, int kwm)
                newpeer->last_change = now.tv_sec;
                newpeer->id = strdup(args[1]);
 
-               sk = str2sa_range(args[2], &port1, &port2);
+               sk = str2sa_range(args[2], &port1, &port2, NULL);
                if (!sk) {
                        Alert("parsing [%s:%d] : '%s %s' : unknown host in '%s'\n", file, linenum, args[0], args[1], args[2]);
                        err_code |= ERR_ALERT | ERR_FATAL;
@@ -3986,7 +3986,7 @@ stats_error_parsing:
                else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
                        err_code |= ERR_WARN;
 
-               sk = str2sa_range(args[1], &port1, &port2);
+               sk = str2sa_range(args[1], &port1, &port2, NULL);
                if (!sk) {
                        Alert("parsing [%s:%d] : '%s' : unknown host in '%s'\n", file, linenum, args[0], args[1]);
                        err_code |= ERR_ALERT | ERR_FATAL;
@@ -4102,7 +4102,7 @@ stats_error_parsing:
                         *  - IP:+N => port=+N, relative
                         *  - IP:-N => port=-N, relative
                         */
-                       sk = str2sa_range(args[2], &port1, &port2);
+                       sk = str2sa_range(args[2], &port1, &port2, NULL);
                        if (!sk) {
                                Alert("parsing [%s:%d] : Unknown host in '%s'\n", file, linenum, args[2]);
                                err_code |= ERR_ALERT | ERR_FATAL;
@@ -4269,7 +4269,7 @@ stats_error_parsing:
                                struct sockaddr_storage *sk;
                                int port1, port2;
 
-                               sk = str2sa_range(args[cur_arg + 1], &port1, &port2);
+                               sk = str2sa_range(args[cur_arg + 1], &port1, &port2, NULL);
                                if (!sk) {
                                        Alert("parsing [%s:%d] : '%s' : unknown host in '%s'\n",
                                              file, linenum, args[cur_arg], args[cur_arg + 1]);
@@ -4461,7 +4461,7 @@ stats_error_parsing:
                                        goto out;
                                }
                                newsrv->conn_src.opts |= CO_SRC_BIND;
-                               sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high);
+                               sk = str2sa_range(args[cur_arg + 1], &port_low, &port_high, NULL);
                                if (!sk) {
                                        Alert("parsing [%s:%d] : Unknown host in '%s'\n", file, linenum, args[cur_arg + 1]);
                                        err_code |= ERR_ALERT | ERR_FATAL;
@@ -4559,7 +4559,7 @@ stats_error_parsing:
                                                        struct sockaddr_storage *sk;
                                                        int port1, port2;
 
-                                                       sk = str2sa_range(args[cur_arg + 1], &port1, &port2);
+                                                       sk = str2sa_range(args[cur_arg + 1], &port1, &port2, NULL);
                                                        if (!sk) {
                                                                Alert("parsing [%s:%d] : '%s' : unknown host in '%s'\n",
                                                                      file, linenum, args[cur_arg], args[cur_arg + 1]);
@@ -4887,7 +4887,7 @@ stats_error_parsing:
                                struct sockaddr_storage *sk;
                                int port1, port2;
 
-                               sk = str2sa_range(args[1], &port1, &port2);
+                               sk = str2sa_range(args[1], &port1, &port2, NULL);
                                if (!sk) {
                                        Alert("parsing [%s:%d] : '%s' : unknown host in '%s'\n",
                                              file, linenum, args[0], args[1]);
@@ -4937,7 +4937,7 @@ stats_error_parsing:
                curproxy->conn_src.iface_name = NULL;
                curproxy->conn_src.iface_len = 0;
 
-               sk = str2sa_range(args[1], &port1, &port2);
+               sk = str2sa_range(args[1], &port1, &port2, NULL);
                if (!sk) {
                        Alert("parsing [%s:%d] : '%s' : unknown host in '%s'\n",
                              file, linenum, args[0], args[1]);
@@ -5020,7 +5020,7 @@ stats_error_parsing:
                                                goto out;
                                        }
                                } else {
-                                       struct sockaddr_storage *sk = str2sa_range(args[cur_arg + 1], &port1, &port2);
+                                       struct sockaddr_storage *sk = str2sa_range(args[cur_arg + 1], &port1, &port2, NULL);
 
                                        if (!sk) {
                                                Alert("parsing [%s:%d] : '%s' : unknown host in '%s'\n",
index be6b91190ff5932d697eff2a572f0fc6600b9664..857d9b4f807ceb0bfc00af33a10d22352bbaa4ae 100644 (file)
@@ -638,7 +638,7 @@ struct sockaddr_storage *str2ip(const char *str)
  * returned if the address cannot be parsed. The <low> and <high> ports are
  * always initialized if non-null.
  */
-struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high)
+struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err)
 {
        struct sockaddr_storage *ret = NULL;
        char *str2;
@@ -648,8 +648,10 @@ struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high)
        portl = porth = porta = 0;
 
        str2 = strdup(str);
-       if (str2 == NULL)
+       if (str2 == NULL) {
+               memprintf(err, "out of memory in '%s'\n", __FUNCTION__);
                goto out;
+       }
 
        port1 = strrchr(str2, ':');
        if (port1)
@@ -658,8 +660,10 @@ struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high)
                port1 = "";
 
        ret = str2ip(str2);
-       if (!ret)
+       if (!ret) {
+               memprintf(err, "invalid address: '%s' in '%s'\n", str2, str);
                goto out;
+       }
 
        if (isdigit(*port1)) {  /* single port or range */
                port2 = strchr(port1, '-');
@@ -680,6 +684,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high)
                porta = porth;
        }
        else if (*port1) { /* other any unexpected char */
+               memprintf(err, "invalid character '%c' in port number '%s'\n", *port1, port1);
                ret = NULL;
                goto out;
        }