1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.50 2009/04/17 11:38:28 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
6 * Purpose : Declares functions to match URLs against URL
9 * Copyright : Written by and Copyright (C) 2001-2009
10 * the Privoxy team. http://www.privoxy.org/
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 *********************************************************************/
41 #include <sys/types.h>
49 #if !defined(_WIN32) && !defined(__OS2__)
59 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
61 enum regex_anchoring {NO_ANCHORING, LEFT_ANCHORED, RIGHT_ANCHORED};
62 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern);
64 /*********************************************************************
66 * Function : free_http_request
68 * Description : Freez a http_request structure
71 * 1 : http = points to a http_request structure to free
75 *********************************************************************/
76 void free_http_request(struct http_request *http)
85 freez(http->hostport);
88 freez(http->host_ip_addr_str);
95 /*********************************************************************
97 * Function : init_domain_components
99 * Description : Splits the domain name so we can compare it
100 * against wildcards. It used to be part of
101 * parse_http_url, but was separated because the
102 * same code is required in chat in case of
103 * intercepted requests.
106 * 1 : http = pointer to the http structure to hold elements.
108 * Returns : JB_ERR_OK on success
109 * JB_ERR_MEMORY on out of memory
110 * JB_ERR_PARSE on malformed command/URL
111 * or >100 domains deep.
113 *********************************************************************/
114 jb_err init_domain_components(struct http_request *http)
116 char *vec[BUFFER_SIZE];
120 http->dbuffer = strdup(http->host);
121 if (NULL == http->dbuffer)
123 return JB_ERR_MEMORY;
126 /* map to lower case */
127 for (p = http->dbuffer; *p ; p++)
129 *p = (char)tolower((int)(unsigned char)*p);
132 /* split the domain name into components */
133 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
135 if (http->dcount <= 0)
138 * Error: More than SZ(vec) components in domain
139 * or: no components in domain
141 log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
145 /* save a copy of the pointers in dvec */
146 size = (size_t)http->dcount * sizeof(*http->dvec);
148 http->dvec = (char **)malloc(size);
149 if (NULL == http->dvec)
151 return JB_ERR_MEMORY;
154 memcpy(http->dvec, vec, size);
160 /*********************************************************************
162 * Function : parse_http_url
164 * Description : Parse out the host and port from the URL. Find the
165 * hostname & path, port (if ':'), and/or password (if '@')
168 * 1 : url = URL (or is it URI?) to break down
169 * 2 : http = pointer to the http structure to hold elements.
170 * Must be initialized with valid values (like NULLs).
171 * 3 : require_protocol = Whether or not URLs without
172 * protocol are acceptable.
174 * Returns : JB_ERR_OK on success
175 * JB_ERR_MEMORY on out of memory
176 * JB_ERR_PARSE on malformed command/URL
177 * or >100 domains deep.
179 *********************************************************************/
180 jb_err parse_http_url(const char *url, struct http_request *http, int require_protocol)
182 int host_available = 1; /* A proxy can dream. */
185 * Save our initial URL
187 http->url = strdup(url);
188 if (http->url == NULL)
190 return JB_ERR_MEMORY;
195 * Check for * URI. If found, we're done.
197 if (*http->url == '*')
199 if ( NULL == (http->path = strdup("*"))
200 || NULL == (http->hostport = strdup("")) )
202 return JB_ERR_MEMORY;
204 if (http->url[1] != '\0')
213 * Split URL into protocol,hostport,path.
223 return JB_ERR_MEMORY;
226 /* Find the start of the URL in our scratch space */
228 if (strncmpic(url_noproto, "http://", 7) == 0)
232 else if (strncmpic(url_noproto, "https://", 8) == 0)
235 * Should only happen when called from cgi_show_url_info().
240 else if (*url_noproto == '/')
243 * Short request line without protocol and host.
244 * Most likely because the client's request
245 * was intercepted and redirected into Privoxy.
250 else if (require_protocol)
256 url_path = strchr(url_noproto, '/');
257 if (url_path != NULL)
262 * NOTE: The following line ignores the path for HTTPS URLS.
263 * This means that you get consistent behaviour if you type a
264 * https URL in and it's parsed by the function. (When the
265 * URL is actually retrieved, SSL hides the path part).
267 http->path = strdup(http->ssl ? "/" : url_path);
269 http->hostport = strdup(url_noproto);
274 * Repair broken HTTP requests that don't contain a path,
275 * or CONNECT requests
277 http->path = strdup("/");
278 http->hostport = strdup(url_noproto);
283 if ( (http->path == NULL)
284 || (http->hostport == NULL))
286 return JB_ERR_MEMORY;
292 /* Without host, there is nothing left to do here */
297 * Split hostport into user/password (ignored), host, port.
304 buf = strdup(http->hostport);
307 return JB_ERR_MEMORY;
310 /* check if url contains username and/or password */
311 host = strchr(buf, '@');
314 /* Contains username/password, skip it and the @ sign. */
319 /* No username or password. */
323 /* Move after hostname before port number */
326 /* Numeric IPv6 address delimited by brackets */
328 port = strchr(host, ']');
332 /* Missing closing bracket */
343 else if (*port != ':')
345 /* Garbage after closing bracket */
352 /* Plain non-escaped hostname */
353 port = strchr(host, ':');
356 /* check if url contains port */
360 /* Terminate hostname and point to start of port string */
362 http->port = atoi(port);
366 /* No port specified. */
367 http->port = (http->ssl ? 443 : 80);
370 http->host = strdup(host);
374 if (http->host == NULL)
376 return JB_ERR_MEMORY;
381 * Split domain name so we can compare it against wildcards
383 return init_domain_components(http);
388 /*********************************************************************
390 * Function : unknown_method
392 * Description : Checks whether a method is unknown.
395 * 1 : method = points to a http method
397 * Returns : TRUE if it's unknown, FALSE otherwise.
399 *********************************************************************/
400 static int unknown_method(const char *method)
402 static const char *known_http_methods[] = {
403 /* Basic HTTP request type */
404 "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
405 /* webDAV extensions (RFC2518) */
406 "PROPFIND", "PROPPATCH", "MOVE", "COPY", "MKCOL", "LOCK", "UNLOCK",
408 * Microsoft webDAV extension for Exchange 2000. See:
409 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
410 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
412 "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
414 * Another Microsoft webDAV extension for Exchange 2000. See:
415 * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
416 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
417 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
419 "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
421 * Yet another WebDAV extension, this time for
422 * Web Distributed Authoring and Versioning (RFC3253)
424 "VERSION-CONTROL", "REPORT", "CHECKOUT", "CHECKIN", "UNCHECKOUT",
425 "MKWORKSPACE", "UPDATE", "LABEL", "MERGE", "BASELINE-CONTROL", "MKACTIVITY",
429 for (i = 0; i < SZ(known_http_methods); i++)
431 if (0 == strcmpic(method, known_http_methods[i]))
442 /*********************************************************************
444 * Function : parse_http_request
446 * Description : Parse out the host and port from the URL. Find the
447 * hostname & path, port (if ':'), and/or password (if '@')
450 * 1 : req = HTTP request line to break down
451 * 2 : http = pointer to the http structure to hold elements
453 * Returns : JB_ERR_OK on success
454 * JB_ERR_MEMORY on out of memory
455 * JB_ERR_CGI_PARAMS on malformed command/URL
456 * or >100 domains deep.
458 *********************************************************************/
459 jb_err parse_http_request(const char *req, struct http_request *http)
462 char *v[10]; /* XXX: Why 10? We should only need three. */
466 memset(http, '\0', sizeof(*http));
471 return JB_ERR_MEMORY;
474 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
482 * Fail in case of unknown methods
483 * which we might not handle correctly.
485 * XXX: There should be a config option
486 * to forward requests with unknown methods
487 * anyway. Most of them don't need special
490 if (unknown_method(v[0]))
492 log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
497 if (strcmpic(v[2], "HTTP/1.1") && strcmpic(v[2], "HTTP/1.0"))
499 log_error(LOG_LEVEL_ERROR, "The only supported HTTP "
500 "versions are 1.0 and 1.1. This rules out: %s", v[2]);
505 http->ssl = !strcmpic(v[0], "CONNECT");
507 err = parse_http_url(v[1], http, !http->ssl);
515 * Copy the details into the structure
517 http->cmd = strdup(req);
518 http->gpc = strdup(v[0]);
519 http->ver = strdup(v[2]);
523 if ( (http->cmd == NULL)
524 || (http->gpc == NULL)
525 || (http->ver == NULL) )
527 return JB_ERR_MEMORY;
535 /*********************************************************************
537 * Function : compile_pattern
539 * Description : Compiles a host, domain or TAG pattern.
542 * 1 : pattern = The pattern to compile.
543 * 2 : anchoring = How the regex should be anchored.
544 * Can be either one of NO_ANCHORING,
545 * LEFT_ANCHORED or RIGHT_ANCHORED.
546 * 3 : url = In case of failures, the spec member is
547 * logged and the structure freed.
548 * 4 : regex = Where the compiled regex should be stored.
550 * Returns : JB_ERR_OK - Success
551 * JB_ERR_MEMORY - Out of memory
552 * JB_ERR_PARSE - Cannot parse regex
554 *********************************************************************/
555 static jb_err compile_pattern(const char *pattern, enum regex_anchoring anchoring,
556 struct url_spec *url, regex_t **regex)
559 char rebuf[BUFFER_SIZE];
560 const char *fmt = NULL;
563 assert(strlen(pattern) < sizeof(rebuf) - 2);
565 if (pattern[0] == '\0')
583 log_error(LOG_LEVEL_FATAL,
584 "Invalid anchoring in compile_pattern %d", anchoring);
587 *regex = zalloc(sizeof(**regex));
591 return JB_ERR_MEMORY;
594 snprintf(rebuf, sizeof(rebuf), fmt, pattern);
596 errcode = regcomp(*regex, rebuf, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
600 size_t errlen = regerror(errcode, *regex, rebuf, sizeof(rebuf));
601 if (errlen > (sizeof(rebuf) - (size_t)1))
603 errlen = sizeof(rebuf) - (size_t)1;
605 rebuf[errlen] = '\0';
606 log_error(LOG_LEVEL_ERROR, "error compiling %s from %s: %s",
607 pattern, url->spec, rebuf);
618 /*********************************************************************
620 * Function : compile_url_pattern
622 * Description : Compiles the three parts of an URL pattern.
625 * 1 : url = Target url_spec to be filled in.
626 * 2 : buf = The url pattern to compile. Will be messed up.
628 * Returns : JB_ERR_OK - Success
629 * JB_ERR_MEMORY - Out of memory
630 * JB_ERR_PARSE - Cannot parse regex
632 *********************************************************************/
633 static jb_err compile_url_pattern(struct url_spec *url, char *buf)
637 p = strchr(buf, '/');
641 * Only compile the regex if it consists of more than
642 * a single slash, otherwise it wouldn't affect the result.
647 * XXX: does it make sense to compile the slash at the beginning?
649 jb_err err = compile_pattern(p, LEFT_ANCHORED, url, &url->preg);
651 if (JB_ERR_OK != err)
660 * IPv6 numeric hostnames can contain colons, thus we need
661 * to delimit the hostname before the real port separator.
662 * As brackets are already used in the hostname pattern,
663 * we use angle brackets ('<', '>') instead.
665 if ((buf[0] == '<') && (NULL != (p = strchr(buf + 1, '>'))))
672 /* IPv6 address without port number */
677 /* Garbage after address delimiter */
683 p = strchr(buf, ':');
689 url->port_list = strdup(p);
690 if (NULL == url->port_list)
692 return JB_ERR_MEMORY;
697 url->port_list = NULL;
702 return compile_host_pattern(url, buf);
710 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
711 /*********************************************************************
713 * Function : compile_host_pattern
715 * Description : Parses and compiles a host pattern..
718 * 1 : url = Target url_spec to be filled in.
719 * 2 : host_pattern = Host pattern to compile.
721 * Returns : JB_ERR_OK - Success
722 * JB_ERR_MEMORY - Out of memory
723 * JB_ERR_PARSE - Cannot parse regex
725 *********************************************************************/
726 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
728 return compile_pattern(host_pattern, RIGHT_ANCHORED, url, &url->host_regex);
733 /*********************************************************************
735 * Function : compile_host_pattern
737 * Description : Parses and "compiles" an old-school host pattern.
740 * 1 : url = Target url_spec to be filled in.
741 * 2 : host_pattern = Host pattern to parse.
743 * Returns : JB_ERR_OK - Success
744 * JB_ERR_MEMORY - Out of memory
745 * JB_ERR_PARSE - Cannot parse regex
747 *********************************************************************/
748 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
757 if (host_pattern[strlen(host_pattern) - 1] == '.')
759 url->unanchored |= ANCHOR_RIGHT;
761 if (host_pattern[0] == '.')
763 url->unanchored |= ANCHOR_LEFT;
767 * Split domain into components
769 url->dbuffer = strdup(host_pattern);
770 if (NULL == url->dbuffer)
773 return JB_ERR_MEMORY;
779 for (p = url->dbuffer; *p ; p++)
781 *p = (char)tolower((int)(unsigned char)*p);
785 * Split the domain name into components
787 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
792 return JB_ERR_MEMORY;
794 else if (url->dcount != 0)
797 * Save a copy of the pointers in dvec
799 size = (size_t)url->dcount * sizeof(*url->dvec);
801 url->dvec = (char **)malloc(size);
802 if (NULL == url->dvec)
805 return JB_ERR_MEMORY;
808 memcpy(url->dvec, v, size);
811 * else dcount == 0 in which case we needn't do anything,
812 * since dvec will never be accessed and the pattern will
819 /*********************************************************************
821 * Function : simplematch
823 * Description : String matching, with a (greedy) '*' wildcard that
824 * stands for zero or more arbitrary characters and
825 * character classes in [], which take both enumerations
829 * 1 : pattern = pattern for matching
830 * 2 : text = text to be matched
832 * Returns : 0 if match, else nonzero
834 *********************************************************************/
835 static int simplematch(const char *pattern, const char *text)
837 const unsigned char *pat = (const unsigned char *)pattern;
838 const unsigned char *txt = (const unsigned char *)text;
839 const unsigned char *fallback = pat;
842 unsigned char lastchar = 'a';
844 unsigned char charmap[32];
849 /* EOF pattern but !EOF text? */
862 /* '*' in the pattern? */
866 /* The pattern ends afterwards? Speed up the return. */
872 /* Else, set wildcard mode and remember position after '*' */
877 /* Character range specification? */
880 memset(charmap, '\0', sizeof(charmap));
882 while (*++pat != ']')
888 else if (*pat == '-')
890 if ((*++pat == ']') || *pat == '\0')
894 for (i = lastchar; i <= *pat; i++)
896 charmap[i / 8] |= (unsigned char)(1 << (i % 8));
901 charmap[*pat / 8] |= (unsigned char)(1 << (*pat % 8));
905 } /* -END- if Character range specification */
909 * Char match, or char range match?
913 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
923 * No match && no wildcard: No luck
927 else if (pat != fallback)
930 * Increment text pointer if in char range matching
937 * Wildcard mode && nonmatch beyond fallback: Rewind pattern
941 * Restart matching from current text pointer
948 /* Cut off extra '*'s */
949 if(*pat == '*') pat++;
951 /* If this is the pattern's end, fine! */
957 /*********************************************************************
959 * Function : simple_domaincmp
961 * Description : Domain-wise Compare fqdn's. The comparison is
962 * both left- and right-anchored. The individual
963 * domain names are compared with simplematch().
964 * This is only used by domain_match.
967 * 1 : pv = array of patterns to compare
968 * 2 : fv = array of domain components to compare
969 * 3 : len = length of the arrays (both arrays are the
970 * same length - if they weren't, it couldn't
971 * possibly be a match).
973 * Returns : 0 => domains are equivalent, else no match.
975 *********************************************************************/
976 static int simple_domaincmp(char **pv, char **fv, int len)
980 for (n = 0; n < len; n++)
982 if (simplematch(pv[n], fv[n]))
993 /*********************************************************************
995 * Function : domain_match
997 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
998 * pattern->unachored, the comparison is un-, left-,
999 * right-anchored, or both.
1000 * The individual domain names are compared with
1004 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1005 * 2 : fqdn = domain name against which the patterns are compared.
1007 * Returns : 0 => domains are equivalent, else no match.
1009 *********************************************************************/
1010 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
1012 char **pv, **fv; /* vectors */
1014 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1016 plen = pattern->dcount;
1017 flen = fqdn->dcount;
1021 /* fqdn is too short to match this pattern */
1028 if (unanchored == ANCHOR_LEFT)
1033 * Convert this into a fully anchored pattern with
1034 * the fqdn and pattern the same length
1036 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1037 return simple_domaincmp(pv, fv, plen);
1039 else if (unanchored == 0)
1041 /* Fully anchored, check length */
1046 return simple_domaincmp(pv, fv, plen);
1048 else if (unanchored == ANCHOR_RIGHT)
1050 /* Left anchored, ignore all extra in fqdn */
1051 return simple_domaincmp(pv, fv, plen);
1057 int maxn = flen - plen;
1058 for (n = 0; n <= maxn; n++)
1060 if (!simple_domaincmp(pv, fv, plen))
1065 * Doesn't match from start of fqdn
1066 * Try skipping first part of fqdn
1074 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
1077 /*********************************************************************
1079 * Function : create_url_spec
1081 * Description : Creates a "url_spec" structure from a string.
1082 * When finished, free with free_url_spec().
1085 * 1 : url = Target url_spec to be filled in. Will be
1086 * zeroed before use.
1087 * 2 : buf = Source pattern, null terminated. NOTE: The
1088 * contents of this buffer are destroyed by this
1089 * function. If this function succeeds, the
1090 * buffer is copied to url->spec. If this
1091 * function fails, the contents of the buffer
1094 * Returns : JB_ERR_OK - Success
1095 * JB_ERR_MEMORY - Out of memory
1096 * JB_ERR_PARSE - Cannot parse regex (Detailed message
1097 * written to system log)
1099 *********************************************************************/
1100 jb_err create_url_spec(struct url_spec *url, char *buf)
1105 memset(url, '\0', sizeof(*url));
1107 /* Remember the original specification for the CGI pages. */
1108 url->spec = strdup(buf);
1109 if (NULL == url->spec)
1111 return JB_ERR_MEMORY;
1114 /* Is it tag pattern? */
1115 if (0 == strncmpic("TAG:", url->spec, 4))
1117 /* The pattern starts with the first character after "TAG:" */
1118 const char *tag_pattern = buf + 4;
1119 return compile_pattern(tag_pattern, NO_ANCHORING, url, &url->tag_regex);
1122 /* If it isn't a tag pattern it must be a URL pattern. */
1123 return compile_url_pattern(url, buf);
1127 /*********************************************************************
1129 * Function : free_url_spec
1131 * Description : Called from the "unloaders". Freez the url
1132 * structure elements.
1135 * 1 : url = pointer to a url_spec structure.
1139 *********************************************************************/
1140 void free_url_spec(struct url_spec *url)
1142 if (url == NULL) return;
1145 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1146 if (url->host_regex)
1148 regfree(url->host_regex);
1149 freez(url->host_regex);
1152 freez(url->dbuffer);
1155 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
1156 freez(url->port_list);
1164 regfree(url->tag_regex);
1165 freez(url->tag_regex);
1170 /*********************************************************************
1172 * Function : url_match
1174 * Description : Compare a URL against a URL pattern.
1177 * 1 : pattern = a URL pattern
1178 * 2 : url = URL to match
1180 * Returns : Nonzero if the URL matches the pattern, else 0.
1182 *********************************************************************/
1183 int url_match(const struct url_spec *pattern,
1184 const struct http_request *http)
1186 /* XXX: these should probably be functions. */
1187 #define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, http->port))
1188 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1189 #define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)))
1191 #define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)))
1193 #define PATH_MATCHES ((NULL == pattern->preg) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0)))
1195 if (pattern->tag_regex != NULL)
1197 /* It's a tag pattern and shouldn't be matched against URLs */
1201 return (PORT_MATCHES && DOMAIN_MATCHES && PATH_MATCHES);
1206 /*********************************************************************
1208 * Function : match_portlist
1210 * Description : Check if a given number is covered by a comma
1211 * separated list of numbers and ranges (a,b-c,d,..)
1214 * 1 : portlist = String with list
1215 * 2 : port = port to check
1217 * Returns : 0 => no match
1220 *********************************************************************/
1221 int match_portlist(const char *portlist, int port)
1223 char *min, *max, *next, *portlist_copy;
1225 min = next = portlist_copy = strdup(portlist);
1228 * Zero-terminate first item and remember offset for next
1230 if (NULL != (next = strchr(portlist_copy, (int) ',')))
1236 * Loop through all items, checking for match
1240 if (NULL == (max = strchr(min, (int) '-')))
1243 * No dash, check for equality
1245 if (port == atoi(min))
1247 freez(portlist_copy);
1254 * This is a range, so check if between min and max,
1255 * or, if max was omitted, between min and 65K
1258 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
1260 freez(portlist_copy);
1272 * Zero-terminate next item and remember offset for n+1
1274 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
1280 freez(portlist_copy);
1286 /*********************************************************************
1288 * Function : parse_forwarder_address
1290 * Description : Parse out the host and port from a forwarder address.
1293 * 1 : address = The forwarder address to parse.
1294 * 2 : hostname = Used to return the hostname. NULL on error.
1295 * 3 : port = Used to return the port. Untouched if no port
1298 * Returns : JB_ERR_OK on success
1299 * JB_ERR_MEMORY on out of memory
1300 * JB_ERR_PARSE on malformed address.
1302 *********************************************************************/
1303 jb_err parse_forwarder_address(char *address, char **hostname, int *port)
1307 if ((*address == '[') && (NULL == strchr(address, ']')))
1309 /* XXX: Should do some more validity checks here. */
1310 return JB_ERR_PARSE;
1313 *hostname = strdup(address);
1314 if (NULL == *hostname)
1316 return JB_ERR_MEMORY;
1319 if ((**hostname == '[') && (NULL != (p = strchr(*hostname, ']'))))
1322 memmove(*hostname, (*hostname + 1), (size_t)(p - *hostname));
1325 *port = (int)strtol(++p, NULL, 0);
1328 else if (NULL != (p = strchr(*hostname, ':')))
1331 *port = (int)strtol(p, NULL, 0);