comparison common-runopts.c @ 1936:4528afefe45d

Fix IPv6 address parsing for dbclient -b Now can correctly handle '-b [ipv6address]:port' Code is shared with dropbear -p, though they handle colon-less arguments differently
author Matt Johnston <matt@ucc.asn.au>
date Fri, 01 Apr 2022 14:13:52 +0800
parents 70f05f7d4d11
children
comparison
equal deleted inserted replaced
1935:a7ad060707b6 1936:4528afefe45d
114 } else { 114 } else {
115 opts.recv_window = rw; 115 opts.recv_window = rw;
116 } 116 }
117 117
118 } 118 }
119
120 /* Splits addr:port. Handles IPv6 [2001:0011::4]:port style format.
121 Returns first/second parts as malloced strings, second will
122 be NULL if no separator is found.
123 :port -> (NULL, "port")
124 port -> (port, NULL)
125 addr:port (addr, port)
126 addr: -> (addr, "")
127 Returns DROPBEAR_SUCCESS/DROPBEAR_FAILURE */
128 int split_address_port(const char* spec, char **first, char ** second) {
129 char *spec_copy = NULL, *addr = NULL, *colon = NULL;
130 int ret = DROPBEAR_FAILURE;
131
132 *first = NULL;
133 *second = NULL;
134 spec_copy = m_strdup(spec);
135 addr = spec_copy;
136
137 if (*addr == '[') {
138 addr++;
139 colon = strchr(addr, ']');
140 if (!colon) {
141 dropbear_log(LOG_WARNING, "Bad address '%s'", spec);
142 goto out;
143 }
144 *colon = '\0';
145 colon++;
146 if (*colon == '\0') {
147 /* No port part */
148 colon = NULL;
149 } else if (*colon != ':') {
150 dropbear_log(LOG_WARNING, "Bad address '%s'", spec);
151 goto out;
152 }
153 } else {
154 /* search for ':', that separates address and port */
155 colon = strrchr(addr, ':');
156 }
157
158 /* colon points to ':' now, or is NULL */
159 if (colon) {
160 /* Split the address/port */
161 *colon = '\0';
162 colon++;
163 *second = m_strdup(colon);
164 }
165 if (strlen(addr)) {
166 *first = m_strdup(addr);
167 }
168 ret = DROPBEAR_SUCCESS;
169
170 out:
171 m_free(spec_copy);
172 return ret;
173 }