| 1 |
|
| /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */ |
| 2 |
|
| /* |
| 3 |
|
| * ***** BEGIN LICENSE BLOCK ***** |
| 4 |
|
| * Version: MIT |
| 5 |
|
| * |
| 6 |
|
| * Portions created by Alan Antonuk are Copyright (c) 2012-2013 |
| 7 |
|
| * Alan Antonuk. All Rights Reserved. |
| 8 |
|
| * |
| 9 |
|
| * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc. |
| 10 |
|
| * All Rights Reserved. |
| 11 |
|
| * |
| 12 |
|
| * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 |
| 13 |
|
| * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved. |
| 14 |
|
| * |
| 15 |
|
| * Permission is hereby granted, free of charge, to any person |
| 16 |
|
| * obtaining a copy of this software and associated documentation |
| 17 |
|
| * files (the "Software"), to deal in the Software without |
| 18 |
|
| * restriction, including without limitation the rights to use, copy, |
| 19 |
|
| * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 20 |
|
| * of the Software, and to permit persons to whom the Software is |
| 21 |
|
| * furnished to do so, subject to the following conditions: |
| 22 |
|
| * |
| 23 |
|
| * The above copyright notice and this permission notice shall be |
| 24 |
|
| * included in all copies or substantial portions of the Software. |
| 25 |
|
| * |
| 26 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 |
|
| * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 28 |
|
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 29 |
|
| * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 30 |
|
| * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 31 |
|
| * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 32 |
|
| * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 33 |
|
| * SOFTWARE. |
| 34 |
|
| * ***** END LICENSE BLOCK ***** |
| 35 |
|
| */ |
| 36 |
|
| |
| 37 |
|
| #ifdef HAVE_CONFIG_H |
| 38 |
|
| #include "config.h" |
| 39 |
|
| #endif |
| 40 |
|
| |
| 41 |
|
| #ifdef _MSC_VER |
| 42 |
|
| # define _CRT_SECURE_NO_WARNINGS |
| 43 |
|
| #endif |
| 44 |
|
| |
| 45 |
|
| #include "amqp_private.h" |
| 46 |
|
| #include <stdint.h> |
| 47 |
|
| #include <stdio.h> |
| 48 |
|
| #include <stdlib.h> |
| 49 |
|
| #include <string.h> |
| 50 |
|
| |
| 51 |
0 |
| void amqp_default_connection_info(struct amqp_connection_info *ci) |
| 52 |
|
| { |
| 53 |
|
| /* Apply defaults */ |
| 54 |
0 |
| ci->user = "guest"; |
| 55 |
0 |
| ci->password = "guest"; |
| 56 |
0 |
| ci->host = "localhost"; |
| 57 |
0 |
| ci->port = 5672; |
| 58 |
0 |
| ci->vhost = "/"; |
| 59 |
0 |
| ci->ssl = 0; |
| 60 |
0 |
| } |
| 61 |
|
| |
| 62 |
|
| /* Scan for the next delimiter, handling percent-encodings on the way. */ |
| 63 |
0 |
| static char find_delim(char **pp, int colon_and_at_sign_are_delims) |
| 64 |
|
| { |
| 65 |
0 |
| char *from = *pp; |
| 66 |
0 |
| char *to = from; |
| 67 |
|
| |
| 68 |
0 |
| for (;;) { |
| 69 |
0 |
| char ch = *from++; |
| 70 |
|
| |
| 71 |
0 |
| switch (ch) { |
| 72 |
0 |
| case ':': |
| 73 |
|
| case '@': |
| 74 |
0 |
▶ | if (!colon_and_at_sign_are_delims) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!colon_and_at_sign_are_delims) { |
0 |
0 |
0 |
|
| 75 |
0 |
| *to++ = ch; |
| 76 |
0 |
| break; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* fall through */ |
| 80 |
|
| case 0: |
| 81 |
|
| case '/': |
| 82 |
|
| case '?': |
| 83 |
|
| case '#': |
| 84 |
|
| case '[': |
| 85 |
|
| case ']': |
| 86 |
0 |
| *to = 0; |
| 87 |
0 |
| *pp = from; |
| 88 |
0 |
| return ch; |
| 89 |
|
| |
| 90 |
0 |
| case '%': { |
| 91 |
|
| unsigned int val; |
| 92 |
|
| int chars; |
| 93 |
0 |
| int res = sscanf(from, "%2x%n", &val, &chars); |
| 94 |
|
| |
| 95 |
0 |
▶ | if (res == EOF || res < 1 || chars != 2) |
Branch: 3 branches
| Branch | True | False | Total |
| if (res == EOF || res < 1 || chars != 2) |
0 |
0 |
0 |
| if (res == EOF || res < 1 || chars != 2) |
0 |
0 |
0 |
| if (res == EOF || res < 1 || chars != 2) |
0 |
0 |
0 |
|
| 96 |
|
| /* Return a surprising delimiter to |
| 97 |
|
| force an error. */ |
| 98 |
|
| { |
| 99 |
0 |
| return '%'; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
0 |
| *to++ = val; |
| 103 |
0 |
| from += 2; |
| 104 |
0 |
| break; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
0 |
| default: |
| 108 |
0 |
| *to++ = ch; |
| 109 |
0 |
| break; |
| 110 |
|
| } |
| 111 |
|
| } |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
|
| /* Parse an AMQP URL into its component parts. */ |
| 115 |
0 |
| int amqp_parse_url(char *url, struct amqp_connection_info *parsed) |
| 116 |
|
| { |
| 117 |
0 |
| int res = AMQP_STATUS_BAD_URL; |
| 118 |
|
| char delim; |
| 119 |
|
| char *start; |
| 120 |
|
| char *host; |
| 121 |
0 |
| char *port = NULL; |
| 122 |
|
| |
| 123 |
|
| /* check the prefix */ |
| 124 |
0 |
▶ | if (!strncmp(url, "amqp://", 7)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!strncmp(url, "amqp://", 7)) { |
0 |
0 |
0 |
|
| 125 |
|
| /* do nothing */ |
| 126 |
0 |
▶ | } else if (!strncmp(url, "amqps://", 8)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (!strncmp(url, "amqps://", 8)) { |
0 |
0 |
0 |
|
| 127 |
0 |
| parsed->port = 5671; |
| 128 |
0 |
| parsed->ssl = 1; |
| 129 |
|
| } else { |
| 130 |
0 |
| goto out; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
0 |
▶ | host = start = url += (parsed->ssl ? 8 : 7); |
Branch: 1 branch
| Branch | True | False | Total |
| host = start = url += (parsed->ssl ? 8 : 7); |
0 |
0 |
0 |
|
| 134 |
0 |
| delim = find_delim(&url, 1); |
| 135 |
|
| |
| 136 |
0 |
▶ | if (delim == ':') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (delim == ':') { |
0 |
0 |
0 |
|
| 137 |
|
| /* The colon could be introducing the port or the |
| 138 |
|
| password part of the userinfo. We don't know yet, |
| 139 |
|
| so stash the preceding component. */ |
| 140 |
0 |
| port = start = url; |
| 141 |
0 |
| delim = find_delim(&url, 1); |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
0 |
▶ | if (delim == '@') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (delim == '@') { |
0 |
0 |
0 |
|
| 145 |
|
| /* What might have been the host and port were in fact |
| 146 |
|
| the username and password */ |
| 147 |
0 |
| parsed->user = host; |
| 148 |
0 |
▶ | if (port) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (port) { |
0 |
0 |
0 |
|
| 149 |
0 |
| parsed->password = port; |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
0 |
| port = NULL; |
| 153 |
0 |
| host = start = url; |
| 154 |
0 |
| delim = find_delim(&url, 1); |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
0 |
▶ | if (delim == '[') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (delim == '[') { |
0 |
0 |
0 |
|
| 158 |
|
| /* IPv6 address. The bracket should be the first |
| 159 |
|
| character in the host. */ |
| 160 |
0 |
▶ | if (host != start || *host != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (host != start || *host != 0) { |
0 |
0 |
0 |
| if (host != start || *host != 0) { |
0 |
0 |
0 |
|
| 161 |
0 |
| goto out; |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
0 |
| start = url; |
| 165 |
0 |
| delim = find_delim(&url, 0); |
| 166 |
|
| |
| 167 |
0 |
▶ | if (delim != ']') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (delim != ']') { |
0 |
0 |
0 |
|
| 168 |
0 |
| goto out; |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
0 |
| parsed->host = start; |
| 172 |
0 |
| start = url; |
| 173 |
0 |
| delim = find_delim(&url, 1); |
| 174 |
|
| |
| 175 |
|
| /* Closing bracket should be the last character in the |
| 176 |
|
| host. */ |
| 177 |
0 |
▶ | if (*start != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*start != 0) { |
0 |
0 |
0 |
|
| 178 |
0 |
| goto out; |
| 179 |
|
| } |
| 180 |
|
| } else { |
| 181 |
|
| /* If we haven't seen the host yet, this is it. */ |
| 182 |
0 |
▶ | if (*host != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*host != 0) { |
0 |
0 |
0 |
|
| 183 |
0 |
| parsed->host = host; |
| 184 |
|
| } |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
0 |
▶ | if (delim == ':') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (delim == ':') { |
0 |
0 |
0 |
|
| 188 |
0 |
| port = start = url; |
| 189 |
0 |
| delim = find_delim(&url, 1); |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
0 |
▶ | if (port) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (port) { |
0 |
0 |
0 |
|
| 193 |
|
| char *end; |
| 194 |
0 |
| long portnum = strtol(port, &end, 10); |
| 195 |
|
| |
| 196 |
0 |
▶ | if (port == end || *end != 0 || portnum < 0 || portnum > 65535) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (port == end || *end != 0 || portnum < 0 || portnum > 65535) { |
0 |
0 |
0 |
| if (port == end || *end != 0 || portnum < 0 || portnum > 65535) { |
0 |
0 |
0 |
| if (port == end || *end != 0 || portnum < 0 || portnum > 65535) { |
0 |
0 |
0 |
| if (port == end || *end != 0 || portnum < 0 || portnum > 65535) { |
0 |
0 |
0 |
|
| 197 |
0 |
| goto out; |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
0 |
| parsed->port = portnum; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
0 |
▶ | if (delim == '/') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (delim == '/') { |
0 |
0 |
0 |
|
| 204 |
0 |
| start = url; |
| 205 |
0 |
| delim = find_delim(&url, 1); |
| 206 |
|
| |
| 207 |
0 |
▶ | if (delim != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (delim != 0) { |
0 |
0 |
0 |
|
| 208 |
0 |
| goto out; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
0 |
| parsed->vhost = start; |
| 212 |
0 |
| res = AMQP_STATUS_OK; |
| 213 |
0 |
▶ | } else if (delim == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (delim == 0) { |
0 |
0 |
0 |
|
| 214 |
0 |
| res = AMQP_STATUS_OK; |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
|
| /* Any other delimiter is bad, and we will return |
| 218 |
|
| AMQP_STATUS_BAD_AMQP_URL. */ |
| 219 |
|
| |
| 220 |
0 |
| out: |
| 221 |
0 |
| return res; |
| 222 |
|
| } |