sockets: fix parsing of ipv4/ipv6 opts in parse_socket_addr
PATCH v2 1/1 sockets: fix parsing of ipv4/ipv6 opts in parse_socket_addr
From: "Daniel P. Berrange" <[email protected]>
The inet_parse() function looks for 'ipv4' and 'ipv6' flags, but only treats them as bare bool flags. The normal QemuOpts parsing would allow on/off values to be set too.
This updates inet_parse() so that its handling of the 'ipv4' and 'ipv6' flags matches that done by QemuOpts.
This impacts the NBD block driver parsing the legacy filename syntax and the migration code parsing the socket scheme.
--- util/qemu-sockets.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)
util/qemu-sockets.c +40 -4
@@ -554,6 +554,33 @@ err: }/* compatibility wrapper */ +static int inet_parse_flag (const char *flagname,const char *optstr,bool *val, +Error **errp) +{ +char *end; +size_t len; + + end =strstr (optstr,"," ); +if (end) { +if (end[1 ] ==',' ) {/* Reject 'ipv6=on,,foo' */ +error_setg (errp,"error parsing '%s' flag '%s'" , flagname, optstr); +return -1 ; + } + len = end - optstr; + }else { + len =strlen (optstr); + } +if (len ==0 || (len ==3 &&strncmp (optstr,"=on" , len) ==0 )) { + *val = true; + }else if ((len ==4 ) &&strncmp (optstr,"=off" , len) ==0 ) { + *val = false; + }else { +error_setg (errp,"error parsing '%s' flag '%s'" , flagname, optstr); +return -1 ; + } +return 0 ; +} +int inet_parse (InetSocketAddress *addr,const char *str,Error **errp) {const char *optstr, *h;
@@ -561,6 +588,7 @@ int inet_parse(InetSocketAddress *addr, const char *str, Error **errp)char port[33 ];int to;int pos; +char *begin;memset (addr,0 ,sizeof (*addr));
@@ -602,11 +630,19 @@ int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) addr->has_to = true; addr->to = to; } -if (strstr (optstr,",ipv4" )) { - addr->ipv4 = addr->has_ipv4 = true; + begin =strstr (optstr,",ipv4" ); +if (begin) { +if (inet_parse_flag ("ipv4" , begin +5 , &addr->ipv4, errp) <0 ) { +return -1 ; + } + addr->has_ipv4 = true; } -if (strstr (optstr,",ipv6" )) { - addr->ipv6 = addr->has_ipv6 = true; + begin =strstr (optstr,",ipv6" ); +if (begin) { +if (inet_parse_flag ("ipv6" , begin +5 , &addr->ipv6, errp) <0 ) { +return -1 ; + } + addr->has_ipv6 = true; }return 0 ; }
Signature · 1 line
2.14.3
Quoted 12 lines · Daniel P. Berrangé
From: "Daniel P. Berrange" <[email protected]>
The
inet_parse()function looks for 'ipv4' and 'ipv6' flags, but only treats them as bare bool flags. The normal QemuOpts parsing would allow on/off values to be set too.This updates
inet_parse()so that its handling of the 'ipv4' and 'ipv6' flags matches that done by QemuOpts.This impacts the NBD block driver parsing the legacy filename syntax and the migration code parsing the socket scheme.
Quoted 7 lines
Signed-off-by Daniel P. Berrange <[email protected]>--- util/qemu-sockets.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)+ if (len == 0 || (len == 3 &&
strncmp(optstr, "=on", len)== 0)) {
Good: no parens around 'len == 3'
util/qemu-sockets.c err: · line 575
Quoted 2 lines
util/qemu-sockets.c +2
-0+ *val = true; + }else if ((len ==4 ) &&strncmp (optstr,"=off" , len) ==0 ) {
Not so good: redundant parens around 'len == 4'
Bad: Inconsistency between the two forms.
With those made consistent, this matches the logic in the static util/qemu-option.c:parse_option_bool(), so it makes sense.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 78 lines · Daniel P. Berrangé
From: "Daniel P. Berrange" <[email protected]>
The
inet_parse()function looks for 'ipv4' and 'ipv6' flags, but only treats them as bare bool flags. The normal QemuOpts parsing would allow on/off values to be set too.This updates
inet_parse()so that its handling of the 'ipv4' and 'ipv6' flags matches that done by QemuOpts.This impacts the NBD block driver parsing the legacy filename syntax and the migration code parsing the socket scheme.
Signed-off-by Daniel P. Berrange <[email protected]>--- util/qemu-sockets.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)util/qemu-sockets.c +34
-3@@ -554,6 +554,33 @@ err: }/* compatibility wrapper */ +static int inet_parse_flag (const char *flagname,const char *optstr,bool *val, +Error **errp) +{ +char *end; +size_t len; + + end =strstr (optstr,"," ); +if (end) { +if (end[1 ] ==',' ) {/* Reject 'ipv6=on,,foo' */ +error_setg (errp,"error parsing '%s' flag '%s'" , flagname, optstr); +return -1 ; + } + len = end - optstr; + }else { + len =strlen (optstr); + } +if (len ==0 || (len ==3 &&strncmp (optstr,"=on" , len) ==0 )) { + *val = true; + }else if ((len ==4 ) &&strncmp (optstr,"=off" , len) ==0 ) { + *val = false; + }else { +error_setg (errp,"error parsing '%s' flag '%s'" , flagname, optstr); +return -1 ; + } +return 0 ; +} +int inet_parse (InetSocketAddress *addr,const char *str,Error **errp) {const char *optstr, *h;@@ -561,6 +588,7 @@ int inet_parse(InetSocketAddress *addr, const char *str, Error **errp)char port[33 ];int to;int pos; +char *begin;memset (addr,0 ,sizeof (*addr));@@ -602,11 +630,19 @@ int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) addr->has_to = true; addr->to = to; } -if (strstr (optstr,",ipv4" )) { - addr->ipv4 = addr->has_ipv4 = true; + begin =strstr (optstr,",ipv4" ); +if (begin) { +if (inet_parse_flag ("ipv4" , begin +5 , &addr->ipv4, errp) <0 ) { +return -1 ; + } + addr->has_ipv4 = true; } -if (strstr (optstr,",ipv6" )) {
util/qemu-sockets.c int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) · line 648
Quoted 10 lines
util/qemu-sockets.c +6
-1- addr->ipv6 = addr->has_ipv6 = true; + begin =strstr (optstr,",ipv6" ); +if (begin) { +if (inet_parse_flag ("ipv6" , begin +5 , &addr->ipv6, errp) <0 ) { +return -1 ; + } + addr->has_ipv6 = true; }return 0 ; }
Applied with Eric's suggested change.
Paolo