Skip to content

Commit d0f34b5

Browse files
committed
Allow a "%scope" suffix on the client's ipv6 addr.
Hopefully fixes bug RsyncProject#239.
1 parent 8449810 commit d0f34b5

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

clientname.c

+17-9
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static char ipaddr_buf[100];
4949

5050
static void client_sockaddr(int fd, struct sockaddr_storage *ss, socklen_t *ss_len);
5151
static int check_name(const char *ipaddr, const struct sockaddr_storage *ss, char *name_buf, size_t name_buf_size);
52-
static int valid_ipaddr(const char *s);
52+
static int valid_ipaddr(const char *s, int allow_scope);
5353

5454
/* Return the IP addr of the client as a string. */
5555
char *client_addr(int fd)
@@ -73,7 +73,7 @@ char *client_addr(int fd)
7373
if ((p = strchr(ipaddr_buf, ' ')) != NULL)
7474
*p = '\0';
7575
}
76-
if (valid_ipaddr(ipaddr_buf))
76+
if (valid_ipaddr(ipaddr_buf, True))
7777
return ipaddr_buf;
7878
}
7979

@@ -213,13 +213,13 @@ int read_proxy_protocol_header(int fd)
213213
if (size != sizeof hdr.v2.addr.ip4)
214214
return 0;
215215
inet_ntop(AF_INET, hdr.v2.addr.ip4.src_addr, ipaddr_buf, sizeof ipaddr_buf);
216-
return valid_ipaddr(ipaddr_buf);
216+
return valid_ipaddr(ipaddr_buf, False);
217217
#ifdef INET6
218218
case PROXY_FAM_TCPv6:
219219
if (size != sizeof hdr.v2.addr.ip6)
220220
return 0;
221221
inet_ntop(AF_INET6, hdr.v2.addr.ip6.src_addr, ipaddr_buf, sizeof ipaddr_buf);
222-
return valid_ipaddr(ipaddr_buf);
222+
return valid_ipaddr(ipaddr_buf, False);
223223
#endif
224224
default:
225225
break;
@@ -276,15 +276,15 @@ int read_proxy_protocol_header(int fd)
276276
if ((sp = strchr(p, ' ')) == NULL)
277277
return 0;
278278
*sp = '\0';
279-
if (!valid_ipaddr(p))
279+
if (!valid_ipaddr(p, False))
280280
return 0;
281281
strlcpy(ipaddr_buf, p, sizeof ipaddr_buf); /* It will always fit when valid. */
282282

283283
p = sp + 1;
284284
if ((sp = strchr(p, ' ')) == NULL)
285285
return 0;
286286
*sp = '\0';
287-
if (!valid_ipaddr(p))
287+
if (!valid_ipaddr(p, False))
288288
return 0;
289289
/* Ignore destination address. */
290290

@@ -466,7 +466,7 @@ static int check_name(const char *ipaddr, const struct sockaddr_storage *ss, cha
466466
}
467467

468468
/* Returns 1 for a valid IPv4 or IPv6 addr, or 0 for a bad one. */
469-
static int valid_ipaddr(const char *s)
469+
static int valid_ipaddr(const char *s, int allow_scope)
470470
{
471471
int i;
472472

@@ -484,6 +484,11 @@ static int valid_ipaddr(const char *s)
484484
for (count = 0; count < 8; count++) {
485485
if (!*s)
486486
return saw_double_colon;
487+
if (allow_scope && *s == '%') {
488+
if (saw_double_colon)
489+
break;
490+
return 0;
491+
}
487492

488493
if (strchr(s, ':') == NULL && strchr(s, '.') != NULL) {
489494
if ((!saw_double_colon && count != 6) || (saw_double_colon && count > 6))
@@ -509,8 +514,11 @@ static int valid_ipaddr(const char *s)
509514
}
510515
}
511516

512-
if (!ipv4_at_end)
513-
return !*s;
517+
if (!ipv4_at_end) {
518+
if (allow_scope && *s == '%')
519+
for (s++; isAlNum(s); s++) { }
520+
return !*s && s[-1] != '%';
521+
}
514522
}
515523

516524
/* IPv4 */

itypes.h

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ isSpace(const char *ptr)
4040
return isspace(*(unsigned char *)ptr);
4141
}
4242

43+
static inline int
44+
isAlNum(const char *ptr)
45+
{
46+
return isalnum(*(unsigned char *)ptr);
47+
}
48+
4349
static inline int
4450
isLower(const char *ptr)
4551
{

0 commit comments

Comments
 (0)