1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.55 2009/06/03 16:43:16 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);
89 #ifndef FEATURE_EXTENDED_HOST_PATTERNS
97 #ifndef FEATURE_EXTENDED_HOST_PATTERNS
98 /*********************************************************************
100 * Function : init_domain_components
102 * Description : Splits the domain name so we can compare it
103 * against wildcards. It used to be part of
104 * parse_http_url, but was separated because the
105 * same code is required in chat in case of
106 * intercepted requests.
109 * 1 : http = pointer to the http structure to hold elements.
111 * Returns : JB_ERR_OK on success
112 * JB_ERR_MEMORY on out of memory
113 * JB_ERR_PARSE on malformed command/URL
114 * or >100 domains deep.
116 *********************************************************************/
117 jb_err init_domain_components(struct http_request *http)
119 char *vec[BUFFER_SIZE];
123 http->dbuffer = strdup(http->host);
124 if (NULL == http->dbuffer)
126 return JB_ERR_MEMORY;
129 /* map to lower case */
130 for (p = http->dbuffer; *p ; p++)
132 *p = (char)tolower((int)(unsigned char)*p);
135 /* split the domain name into components */
136 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
138 if (http->dcount <= 0)
141 * Error: More than SZ(vec) components in domain
142 * or: no components in domain
144 log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
148 /* save a copy of the pointers in dvec */
149 size = (size_t)http->dcount * sizeof(*http->dvec);
151 http->dvec = (char **)malloc(size);
152 if (NULL == http->dvec)
154 return JB_ERR_MEMORY;
157 memcpy(http->dvec, vec, size);
161 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
164 /*********************************************************************
166 * Function : parse_http_url
168 * Description : Parse out the host and port from the URL. Find the
169 * hostname & path, port (if ':'), and/or password (if '@')
172 * 1 : url = URL (or is it URI?) to break down
173 * 2 : http = pointer to the http structure to hold elements.
174 * Must be initialized with valid values (like NULLs).
175 * 3 : require_protocol = Whether or not URLs without
176 * protocol are acceptable.
178 * Returns : JB_ERR_OK on success
179 * JB_ERR_MEMORY on out of memory
180 * JB_ERR_PARSE on malformed command/URL
181 * or >100 domains deep.
183 *********************************************************************/
184 jb_err parse_http_url(const char *url, struct http_request *http, int require_protocol)
186 int host_available = 1; /* A proxy can dream. */
189 * Save our initial URL
191 http->url = strdup(url);
192 if (http->url == NULL)
194 return JB_ERR_MEMORY;
199 * Check for * URI. If found, we're done.
201 if (*http->url == '*')
203 if ( NULL == (http->path = strdup("*"))
204 || NULL == (http->hostport = strdup("")) )
206 return JB_ERR_MEMORY;
208 if (http->url[1] != '\0')
217 * Split URL into protocol,hostport,path.
227 return JB_ERR_MEMORY;
230 /* Find the start of the URL in our scratch space */
232 if (strncmpic(url_noproto, "http://", 7) == 0)
236 else if (strncmpic(url_noproto, "https://", 8) == 0)
239 * Should only happen when called from cgi_show_url_info().
244 else if (*url_noproto == '/')
247 * Short request line without protocol and host.
248 * Most likely because the client's request
249 * was intercepted and redirected into Privoxy.
254 else if (require_protocol)
260 url_path = strchr(url_noproto, '/');
261 if (url_path != NULL)
266 * NOTE: The following line ignores the path for HTTPS URLS.
267 * This means that you get consistent behaviour if you type a
268 * https URL in and it's parsed by the function. (When the
269 * URL is actually retrieved, SSL hides the path part).
271 http->path = strdup(http->ssl ? "/" : url_path);
273 http->hostport = strdup(url_noproto);
278 * Repair broken HTTP requests that don't contain a path,
279 * or CONNECT requests
281 http->path = strdup("/");
282 http->hostport = strdup(url_noproto);
287 if ( (http->path == NULL)
288 || (http->hostport == NULL))
290 return JB_ERR_MEMORY;
296 /* Without host, there is nothing left to do here */
301 * Split hostport into user/password (ignored), host, port.
308 buf = strdup(http->hostport);
311 return JB_ERR_MEMORY;
314 /* check if url contains username and/or password */
315 host = strchr(buf, '@');
318 /* Contains username/password, skip it and the @ sign. */
323 /* No username or password. */
327 /* Move after hostname before port number */
330 /* Numeric IPv6 address delimited by brackets */
332 port = strchr(host, ']');
336 /* Missing closing bracket */
347 else if (*port != ':')
349 /* Garbage after closing bracket */
356 /* Plain non-escaped hostname */
357 port = strchr(host, ':');
360 /* check if url contains port */
364 /* Terminate hostname and point to start of port string */
366 http->port = atoi(port);
370 /* No port specified. */
371 http->port = (http->ssl ? 443 : 80);
374 http->host = strdup(host);
378 if (http->host == NULL)
380 return JB_ERR_MEMORY;
384 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
387 /* Split domain name so we can compare it against wildcards */
388 return init_domain_components(http);
389 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
394 /*********************************************************************
396 * Function : unknown_method
398 * Description : Checks whether a method is unknown.
401 * 1 : method = points to a http method
403 * Returns : TRUE if it's unknown, FALSE otherwise.
405 *********************************************************************/
406 static int unknown_method(const char *method)
408 static const char *known_http_methods[] = {
409 /* Basic HTTP request type */
410 "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
411 /* webDAV extensions (RFC2518) */
412 "PROPFIND", "PROPPATCH", "MOVE", "COPY", "MKCOL", "LOCK", "UNLOCK",
414 * Microsoft webDAV extension for Exchange 2000. See:
415 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
416 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
418 "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
420 * Another Microsoft webDAV extension for Exchange 2000. See:
421 * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
422 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
423 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
425 "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
427 * Yet another WebDAV extension, this time for
428 * Web Distributed Authoring and Versioning (RFC3253)
430 "VERSION-CONTROL", "REPORT", "CHECKOUT", "CHECKIN", "UNCHECKOUT",
431 "MKWORKSPACE", "UPDATE", "LABEL", "MERGE", "BASELINE-CONTROL", "MKACTIVITY",
435 for (i = 0; i < SZ(known_http_methods); i++)
437 if (0 == strcmpic(method, known_http_methods[i]))
448 /*********************************************************************
450 * Function : parse_http_request
452 * Description : Parse out the host and port from the URL. Find the
453 * hostname & path, port (if ':'), and/or password (if '@')
456 * 1 : req = HTTP request line to break down
457 * 2 : http = pointer to the http structure to hold elements
459 * Returns : JB_ERR_OK on success
460 * JB_ERR_MEMORY on out of memory
461 * JB_ERR_CGI_PARAMS on malformed command/URL
462 * or >100 domains deep.
464 *********************************************************************/
465 jb_err parse_http_request(const char *req, struct http_request *http)
468 char *v[10]; /* XXX: Why 10? We should only need three. */
472 memset(http, '\0', sizeof(*http));
477 return JB_ERR_MEMORY;
480 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
488 * Fail in case of unknown methods
489 * which we might not handle correctly.
491 * XXX: There should be a config option
492 * to forward requests with unknown methods
493 * anyway. Most of them don't need special
496 if (unknown_method(v[0]))
498 log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
503 if (strcmpic(v[2], "HTTP/1.1") && strcmpic(v[2], "HTTP/1.0"))
505 log_error(LOG_LEVEL_ERROR, "The only supported HTTP "
506 "versions are 1.0 and 1.1. This rules out: %s", v[2]);
511 http->ssl = !strcmpic(v[0], "CONNECT");
513 err = parse_http_url(v[1], http, !http->ssl);
521 * Copy the details into the structure
523 http->cmd = strdup(req);
524 http->gpc = strdup(v[0]);
525 http->ver = strdup(v[2]);
529 if ( (http->cmd == NULL)
530 || (http->gpc == NULL)
531 || (http->ver == NULL) )
533 return JB_ERR_MEMORY;
541 /*********************************************************************
543 * Function : compile_pattern
545 * Description : Compiles a host, domain or TAG pattern.
548 * 1 : pattern = The pattern to compile.
549 * 2 : anchoring = How the regex should be anchored.
550 * Can be either one of NO_ANCHORING,
551 * LEFT_ANCHORED or RIGHT_ANCHORED.
552 * 3 : url = In case of failures, the spec member is
553 * logged and the structure freed.
554 * 4 : regex = Where the compiled regex should be stored.
556 * Returns : JB_ERR_OK - Success
557 * JB_ERR_MEMORY - Out of memory
558 * JB_ERR_PARSE - Cannot parse regex
560 *********************************************************************/
561 static jb_err compile_pattern(const char *pattern, enum regex_anchoring anchoring,
562 struct url_spec *url, regex_t **regex)
565 char rebuf[BUFFER_SIZE];
566 const char *fmt = NULL;
569 assert(strlen(pattern) < sizeof(rebuf) - 2);
571 if (pattern[0] == '\0')
589 log_error(LOG_LEVEL_FATAL,
590 "Invalid anchoring in compile_pattern %d", anchoring);
593 *regex = zalloc(sizeof(**regex));
597 return JB_ERR_MEMORY;
600 snprintf(rebuf, sizeof(rebuf), fmt, pattern);
602 errcode = regcomp(*regex, rebuf, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
606 size_t errlen = regerror(errcode, *regex, rebuf, sizeof(rebuf));
607 if (errlen > (sizeof(rebuf) - (size_t)1))
609 errlen = sizeof(rebuf) - (size_t)1;
611 rebuf[errlen] = '\0';
612 log_error(LOG_LEVEL_ERROR, "error compiling %s from %s: %s",
613 pattern, url->spec, rebuf);
624 /*********************************************************************
626 * Function : compile_url_pattern
628 * Description : Compiles the three parts of an URL pattern.
631 * 1 : url = Target url_spec to be filled in.
632 * 2 : buf = The url pattern to compile. Will be messed up.
634 * Returns : JB_ERR_OK - Success
635 * JB_ERR_MEMORY - Out of memory
636 * JB_ERR_PARSE - Cannot parse regex
638 *********************************************************************/
639 static jb_err compile_url_pattern(struct url_spec *url, char *buf)
643 p = strchr(buf, '/');
647 * Only compile the regex if it consists of more than
648 * a single slash, otherwise it wouldn't affect the result.
653 * XXX: does it make sense to compile the slash at the beginning?
655 jb_err err = compile_pattern(p, LEFT_ANCHORED, url, &url->preg);
657 if (JB_ERR_OK != err)
666 * IPv6 numeric hostnames can contain colons, thus we need
667 * to delimit the hostname before the real port separator.
668 * As brackets are already used in the hostname pattern,
669 * we use angle brackets ('<', '>') instead.
671 if ((buf[0] == '<') && (NULL != (p = strchr(buf + 1, '>'))))
678 /* IPv6 address without port number */
683 /* Garbage after address delimiter */
689 p = strchr(buf, ':');
695 url->port_list = strdup(p);
696 if (NULL == url->port_list)
698 return JB_ERR_MEMORY;
703 url->port_list = NULL;
708 return compile_host_pattern(url, buf);
716 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
717 /*********************************************************************
719 * Function : compile_host_pattern
721 * Description : Parses and compiles a host pattern..
724 * 1 : url = Target url_spec to be filled in.
725 * 2 : host_pattern = Host pattern to compile.
727 * Returns : JB_ERR_OK - Success
728 * JB_ERR_MEMORY - Out of memory
729 * JB_ERR_PARSE - Cannot parse regex
731 *********************************************************************/
732 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
734 return compile_pattern(host_pattern, RIGHT_ANCHORED, url, &url->host_regex);
739 /*********************************************************************
741 * Function : compile_host_pattern
743 * Description : Parses and "compiles" an old-school host pattern.
746 * 1 : url = Target url_spec to be filled in.
747 * 2 : host_pattern = Host pattern to parse.
749 * Returns : JB_ERR_OK - Success
750 * JB_ERR_MEMORY - Out of memory
751 * JB_ERR_PARSE - Cannot parse regex
753 *********************************************************************/
754 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
763 if (host_pattern[strlen(host_pattern) - 1] == '.')
765 url->unanchored |= ANCHOR_RIGHT;
767 if (host_pattern[0] == '.')
769 url->unanchored |= ANCHOR_LEFT;
773 * Split domain into components
775 url->dbuffer = strdup(host_pattern);
776 if (NULL == url->dbuffer)
779 return JB_ERR_MEMORY;
785 for (p = url->dbuffer; *p ; p++)
787 *p = (char)tolower((int)(unsigned char)*p);
791 * Split the domain name into components
793 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
798 return JB_ERR_MEMORY;
800 else if (url->dcount != 0)
803 * Save a copy of the pointers in dvec
805 size = (size_t)url->dcount * sizeof(*url->dvec);
807 url->dvec = (char **)malloc(size);
808 if (NULL == url->dvec)
811 return JB_ERR_MEMORY;
814 memcpy(url->dvec, v, size);
817 * else dcount == 0 in which case we needn't do anything,
818 * since dvec will never be accessed and the pattern will
825 /*********************************************************************
827 * Function : simplematch
829 * Description : String matching, with a (greedy) '*' wildcard that
830 * stands for zero or more arbitrary characters and
831 * character classes in [], which take both enumerations
835 * 1 : pattern = pattern for matching
836 * 2 : text = text to be matched
838 * Returns : 0 if match, else nonzero
840 *********************************************************************/
841 static int simplematch(const char *pattern, const char *text)
843 const unsigned char *pat = (const unsigned char *)pattern;
844 const unsigned char *txt = (const unsigned char *)text;
845 const unsigned char *fallback = pat;
848 unsigned char lastchar = 'a';
850 unsigned char charmap[32];
855 /* EOF pattern but !EOF text? */
868 /* '*' in the pattern? */
872 /* The pattern ends afterwards? Speed up the return. */
878 /* Else, set wildcard mode and remember position after '*' */
883 /* Character range specification? */
886 memset(charmap, '\0', sizeof(charmap));
888 while (*++pat != ']')
894 else if (*pat == '-')
896 if ((*++pat == ']') || *pat == '\0')
900 for (i = lastchar; i <= *pat; i++)
902 charmap[i / 8] |= (unsigned char)(1 << (i % 8));
907 charmap[*pat / 8] |= (unsigned char)(1 << (*pat % 8));
911 } /* -END- if Character range specification */
915 * Char match, or char range match?
919 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
929 * No match && no wildcard: No luck
933 else if (pat != fallback)
936 * Increment text pointer if in char range matching
943 * Wildcard mode && nonmatch beyond fallback: Rewind pattern
947 * Restart matching from current text pointer
954 /* Cut off extra '*'s */
955 if(*pat == '*') pat++;
957 /* If this is the pattern's end, fine! */
963 /*********************************************************************
965 * Function : simple_domaincmp
967 * Description : Domain-wise Compare fqdn's. The comparison is
968 * both left- and right-anchored. The individual
969 * domain names are compared with simplematch().
970 * This is only used by domain_match.
973 * 1 : pv = array of patterns to compare
974 * 2 : fv = array of domain components to compare
975 * 3 : len = length of the arrays (both arrays are the
976 * same length - if they weren't, it couldn't
977 * possibly be a match).
979 * Returns : 0 => domains are equivalent, else no match.
981 *********************************************************************/
982 static int simple_domaincmp(char **pv, char **fv, int len)
986 for (n = 0; n < len; n++)
988 if (simplematch(pv[n], fv[n]))
999 /*********************************************************************
1001 * Function : domain_match
1003 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1004 * pattern->unachored, the comparison is un-, left-,
1005 * right-anchored, or both.
1006 * The individual domain names are compared with
1010 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1011 * 2 : fqdn = domain name against which the patterns are compared.
1013 * Returns : 0 => domains are equivalent, else no match.
1015 *********************************************************************/
1016 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
1018 char **pv, **fv; /* vectors */
1020 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1022 plen = pattern->dcount;
1023 flen = fqdn->dcount;
1027 /* fqdn is too short to match this pattern */
1034 if (unanchored == ANCHOR_LEFT)
1039 * Convert this into a fully anchored pattern with
1040 * the fqdn and pattern the same length
1042 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1043 return simple_domaincmp(pv, fv, plen);
1045 else if (unanchored == 0)
1047 /* Fully anchored, check length */
1052 return simple_domaincmp(pv, fv, plen);
1054 else if (unanchored == ANCHOR_RIGHT)
1056 /* Left anchored, ignore all extra in fqdn */
1057 return simple_domaincmp(pv, fv, plen);
1063 int maxn = flen - plen;
1064 for (n = 0; n <= maxn; n++)
1066 if (!simple_domaincmp(pv, fv, plen))
1071 * Doesn't match from start of fqdn
1072 * Try skipping first part of fqdn
1080 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
1083 /*********************************************************************
1085 * Function : create_url_spec
1087 * Description : Creates a "url_spec" structure from a string.
1088 * When finished, free with free_url_spec().
1091 * 1 : url = Target url_spec to be filled in. Will be
1092 * zeroed before use.
1093 * 2 : buf = Source pattern, null terminated. NOTE: The
1094 * contents of this buffer are destroyed by this
1095 * function. If this function succeeds, the
1096 * buffer is copied to url->spec. If this
1097 * function fails, the contents of the buffer
1100 * Returns : JB_ERR_OK - Success
1101 * JB_ERR_MEMORY - Out of memory
1102 * JB_ERR_PARSE - Cannot parse regex (Detailed message
1103 * written to system log)
1105 *********************************************************************/
1106 jb_err create_url_spec(struct url_spec *url, char *buf)
1111 memset(url, '\0', sizeof(*url));
1113 /* Remember the original specification for the CGI pages. */
1114 url->spec = strdup(buf);
1115 if (NULL == url->spec)
1117 return JB_ERR_MEMORY;
1120 /* Is it a tag pattern? */
1121 if (0 == strncmpic("TAG:", url->spec, 4))
1123 /* The pattern starts with the first character after "TAG:" */
1124 const char *tag_pattern = buf + 4;
1125 return compile_pattern(tag_pattern, NO_ANCHORING, url, &url->tag_regex);
1128 /* If it isn't a tag pattern it must be an URL pattern. */
1129 return compile_url_pattern(url, buf);
1133 /*********************************************************************
1135 * Function : free_url_spec
1137 * Description : Called from the "unloaders". Freez the url
1138 * structure elements.
1141 * 1 : url = pointer to a url_spec structure.
1145 *********************************************************************/
1146 void free_url_spec(struct url_spec *url)
1148 if (url == NULL) return;
1151 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1152 if (url->host_regex)
1154 regfree(url->host_regex);
1155 freez(url->host_regex);
1158 freez(url->dbuffer);
1161 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
1162 freez(url->port_list);
1170 regfree(url->tag_regex);
1171 freez(url->tag_regex);
1176 /*********************************************************************
1178 * Function : port_matches
1180 * Description : Compares a port against a port list.
1183 * 1 : port = The port to check.
1184 * 2 : port_list = The list of port to compare with.
1186 * Returns : TRUE for yes, FALSE otherwise.
1188 *********************************************************************/
1189 static int port_matches(const int port, const char *port_list)
1191 return ((NULL == port_list) || match_portlist(port_list, port));
1195 /*********************************************************************
1197 * Function : url_match
1199 * Description : Compare a URL against a URL pattern.
1202 * 1 : pattern = a URL pattern
1203 * 2 : url = URL to match
1205 * Returns : Nonzero if the URL matches the pattern, else 0.
1207 *********************************************************************/
1208 int url_match(const struct url_spec *pattern,
1209 const struct http_request *http)
1211 /* XXX: these should probably be functions. */
1212 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1213 #define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)))
1215 #define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)))
1217 #define PATH_MATCHES ((NULL == pattern->preg) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0)))
1219 if (pattern->tag_regex != NULL)
1221 /* It's a tag pattern and shouldn't be matched against URLs */
1225 return (port_matches(http->port, pattern->port_list) && DOMAIN_MATCHES && PATH_MATCHES);
1230 /*********************************************************************
1232 * Function : match_portlist
1234 * Description : Check if a given number is covered by a comma
1235 * separated list of numbers and ranges (a,b-c,d,..)
1238 * 1 : portlist = String with list
1239 * 2 : port = port to check
1241 * Returns : 0 => no match
1244 *********************************************************************/
1245 int match_portlist(const char *portlist, int port)
1247 char *min, *max, *next, *portlist_copy;
1249 min = portlist_copy = strdup(portlist);
1252 * Zero-terminate first item and remember offset for next
1254 if (NULL != (next = strchr(portlist_copy, (int) ',')))
1260 * Loop through all items, checking for match
1264 if (NULL == (max = strchr(min, (int) '-')))
1267 * No dash, check for equality
1269 if (port == atoi(min))
1271 freez(portlist_copy);
1278 * This is a range, so check if between min and max,
1279 * or, if max was omitted, between min and 65K
1282 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
1284 freez(portlist_copy);
1296 * Zero-terminate next item and remember offset for n+1
1298 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
1304 freez(portlist_copy);
1310 /*********************************************************************
1312 * Function : parse_forwarder_address
1314 * Description : Parse out the host and port from a forwarder address.
1317 * 1 : address = The forwarder address to parse.
1318 * 2 : hostname = Used to return the hostname. NULL on error.
1319 * 3 : port = Used to return the port. Untouched if no port
1322 * Returns : JB_ERR_OK on success
1323 * JB_ERR_MEMORY on out of memory
1324 * JB_ERR_PARSE on malformed address.
1326 *********************************************************************/
1327 jb_err parse_forwarder_address(char *address, char **hostname, int *port)
1331 if ((*address == '[') && (NULL == strchr(address, ']')))
1333 /* XXX: Should do some more validity checks here. */
1334 return JB_ERR_PARSE;
1337 *hostname = strdup(address);
1338 if (NULL == *hostname)
1340 return JB_ERR_MEMORY;
1343 if ((**hostname == '[') && (NULL != (p = strchr(*hostname, ']'))))
1346 memmove(*hostname, (*hostname + 1), (size_t)(p - *hostname));
1349 *port = (int)strtol(++p, NULL, 0);
1352 else if (NULL != (p = strchr(*hostname, ':')))
1355 *port = (int)strtol(p, NULL, 0);