Skip to contentSkip to conversation

Modify net/socket.c to use socket_* functions from include/qemu/sockets.h

PATCH In review Ashijeet Acharya 6 participants 16 messages May 12, 2016 – Jun 23, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Modify net/socket.c to use socket_* functions from include/qemu/sockets.h

Changed the listen(),connect(),parse_host_port() in net/socket.c with the socket_*()functions in include/qemu/sockets.h.

---
 net/socket.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
net/socket.c +19 -19
index 9fa2cd8..b6e2f3e 100644
@@ -522,10 +522,12 @@ static int net_socket_listen_init(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
     int fd, ret;
+    Error *local_error = NULL;
+    saddr = g_new0(SocketAddress, 1);
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
         return -1;
 
     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
@@ -536,14 +538,9 @@ static int net_socket_listen_init(NetClientState *peer,
     qemu_set_nonblock(fd);
 
     socket_set_fast_reuse(fd);
+    saddr = socket_local_address(fd, &local_error);
 
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
-    if (ret < 0) {
-        perror("bind");
-        closesocket(fd);
-        return -1;
-    }
-    ret = listen(fd, 0);
+    ret = socket_listen(saddr, &local_error);
     if (ret < 0) {
         perror("listen");
         closesocket(fd);
@@ -557,6 +554,7 @@ static int net_socket_listen_init(NetClientState *peer,
     s->nc.link_down = true;
 
     qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+    g_free(saddr);
     return 0;
 }
 
@@ -567,9 +565,11 @@ static int net_socket_connect_init(NetClientState *peer,
 {
     NetSocketState *s;
     int fd, connected, ret;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
+    Error *local_error = NULL;
+    saddr = g_new0(SocketAddress, 1);
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
         return -1;
 
     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
@@ -578,10 +578,10 @@ static int net_socket_connect_init(NetClientState *peer,
         return -1;
     }
     qemu_set_nonblock(fd);
-
+    saddr = socket_local_address(fd, &local_error);
     connected = 0;
     for(;;) {
-        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+        ret = socket_connect(saddr, &local_error, NULL, NULL);
         if (ret < 0) {
             if (errno == EINTR || errno == EWOULDBLOCK) {
                 /* continue */
@@ -602,9 +602,7 @@ static int net_socket_connect_init(NetClientState *peer,
     s = net_socket_fd_init(peer, model, name, fd, connected);
     if (!s)
         return -1;
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: connect to %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+    g_free(saddr);
     return 0;
 }
 
@@ -618,8 +616,9 @@ static int net_socket_mcast_init(NetClientState *peer,
     int fd;
     struct sockaddr_in saddr;
     struct in_addr localaddr, *param_localaddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
         return -1;
 
     if (localaddr_str != NULL) {
@@ -656,12 +655,13 @@ static int net_socket_udp_init(NetClientState *peer,
     NetSocketState *s;
     int fd, ret;
     struct sockaddr_in laddr, raddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&laddr, lhost) < 0) {
+    if (socket_parse(lhost, &local_error) < 0) {
         return -1;
     }
 
-    if (parse_host_port(&raddr, rhost) < 0) {
+    if (socket_parse(rhost, &local_error) < 0) {
         return -1;
     }
 
Signature · 1 line
1.9.1
#1 lore raw
Quoted 1 line · Ashijeet Acharya

Changed the listen(),connect(),parse_host_port() in net/socket.c with the socket_*()functions in include/qemu/sockets.h.

What is the rationale for this change? Please explain why this is necessary or a good idea.

Please summarize the address syntax changes in this patch and update the QEMU man page.

Quoted 13 lines
---
 net/socket.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
net/socket.c +0 -0
index 9fa2cd8..b6e2f3e 100644
@@ -522,10 +522,12 @@ static int net_socket_listen_init(NetClientState *peer,
 {
     NetClientState *nc;

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 531

Quoted 10 lines
net/socket.c +4 -2
     NetSocketState *s;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
     int fd, ret;
+    Error *local_error = NULL;
+    saddr = g_new0(SocketAddress, 1);
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
         return -1;

saddr is leaked. Please check all return code paths and avoid memory leaks.

I'm not sure if it makes sense to allocate a new SocketAddress object since it is assigned a different object further down in the patch:

saddr = socket_local_address(fd, &local_error);

#2 lore raw

PATCH 1/1 Modify net/socket.c to use socket_* functions from include/qemu/sockets.h

Changed the listen(),connect(),parse_host_port() in net/socket.c with the socket_*()functions in include/qemu/sockets.h.

---
 net/socket.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
net/socket.c +17 -19
index 9fa2cd8..b6e2f3e 100644
@@ -522,10 +522,12 @@ static int net_socket_listen_init(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
     int fd, ret;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
         return -1;
 
     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
@@ -536,14 +538,9 @@ static int net_socket_listen_init(NetClientState *peer,
     qemu_set_nonblock(fd);
 
     socket_set_fast_reuse(fd);
+    saddr = socket_local_address(fd, &local_error);
 
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
-    if (ret < 0) {
-        perror("bind");
-        closesocket(fd);
-        return -1;
-    }
-    ret = listen(fd, 0);
+    ret = socket_listen(saddr, &local_error);
     if (ret < 0) {
         perror("listen");
         closesocket(fd);
@@ -557,6 +554,7 @@ static int net_socket_listen_init(NetClientState *peer,
     s->nc.link_down = true;
 
     qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+    g_free(saddr);
     return 0;
 }
 
@@ -567,9 +565,11 @@ static int net_socket_connect_init(NetClientState *peer,
 {
     NetSocketState *s;
     int fd, connected, ret;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
         return -1;
 
     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
@@ -578,10 +578,10 @@ static int net_socket_connect_init(NetClientState *peer,
         return -1;
     }
     qemu_set_nonblock(fd);
-
+    saddr = socket_local_address(fd, &local_error);
     connected = 0;
     for(;;) {
-        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+        ret = socket_connect(saddr, &local_error, NULL, NULL);
         if (ret < 0) {
             if (errno == EINTR || errno == EWOULDBLOCK) {
                 /* continue */
@@ -602,9 +602,7 @@ static int net_socket_connect_init(NetClientState *peer,
     s = net_socket_fd_init(peer, model, name, fd, connected);
     if (!s)
         return -1;
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: connect to %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+    g_free(saddr);
     return 0;
 }
 
@@ -618,8 +616,9 @@ static int net_socket_mcast_init(NetClientState *peer,
     int fd;
     struct sockaddr_in saddr;
     struct in_addr localaddr, *param_localaddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
         return -1;
 
     if (localaddr_str != NULL) {
@@ -656,12 +655,13 @@ static int net_socket_udp_init(NetClientState *peer,
     NetSocketState *s;
     int fd, ret;
     struct sockaddr_in laddr, raddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&laddr, lhost) < 0) {
+    if (socket_parse(lhost, &local_error) < 0) {
         return -1;
     }
 
-    if (parse_host_port(&raddr, rhost) < 0) {
+    if (socket_parse(rhost, &local_error) < 0) {
         return -1;
     }
 
Signature · 1 line
1.9.1
author
#14 lore raw
In reply to Stefan Hajnoczi
Quoted 5 lines · Stefan Hajnoczi
Quoted 1 line · Ashijeet Acharya

Changed the listen(),connect(),parse_host_port() in net/socket.c with the socket_*()functions in include/qemu/sockets.h.

What is the rationale for this change? Please explain why this is necessary or a good idea.

This patch consists of basic api conversion since i guess everything will be using QAPI based socket_* functions in the future and the same task was listed on this http://wiki.qemu.org/BiteSizedTasks page too.

Quoted 3 lines

Please summarize the address syntax changes in this patch and update the QEMU man page.

Syntax changes:

1. connect() -> socket_connect()
2. listen() -> socket_listen()
3. parse_host_port() -> socket_parse()
4, delete bind as it is automatically done inside socket_listen.
5. use SocketAddress as data-type of socket variable saddr.

Quoted 28 lines
Quoted 14 lines
---
  net/socket.c | 38 +++++++++++++++++++-------------------
  1 file changed, 19 insertions(+), 19 deletions(-)
net/socket.c +0 -0
index 9fa2cd8..b6e2f3e 100644
@@ -522,10 +522,12 @@ static int net_socket_listen_init(NetClientState *peer,
  {
      NetClientState *nc;

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 531

Quoted 10 lines
net/socket.c +4 -2
      NetSocketState *s;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
      int fd, ret;
+    Error *local_error = NULL;
+    saddr = g_new0(SocketAddress, 1);
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
          return -1;

saddr is leaked. Please check all return code paths and avoid memory leaks.

Quoted 5 lines

I'm not sure if it makes sense to allocate a new SocketAddress object since it is assigned a different object further down in the patch:

saddr = socket_local_address(fd, &local_error);

I have looked into it and hopefully solved the memory leakage problem as you can see in the new patch.

Thanks
Ashijeet Acharya

#3 lore raw
In reply to Ashijeet Acharya
Quoted 12 lines · Ashijeet Acharya

Changed the listen(),connect(),parse_host_port() in net/socket.c with the socket_*()functions in include/qemu/sockets.h.

---
 net/socket.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
net/socket.c +0 -0
index 9fa2cd8..b6e2f3e 100644

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 529

Quoted 10 lines
net/socket.c +3 -2
@@ -522,10 +522,12 @@ static int net_socket_listen_init(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
     int fd, ret;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)

This leaks the return address.

The result of socket_parse should be stored in saddr.

Also, the right comparison is "!= NULL", not "< 0".

Finally, you're not printing the error (with error_report_err).

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 541

Quoted 8 lines
net/socket.c +1 -0
         return -1;
 
     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
@@ -536,14 +538,9 @@ static int net_socket_listen_init(NetClientState *peer,
     qemu_set_nonblock(fd);
 
     socket_set_fast_reuse(fd);
+    saddr = socket_local_address(fd, &local_error);

This is incorrect too. You're adding a call to getsockname that wasn't in the original code. You're also ignoring possible failures from socket_local_address.

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 545

Quoted 10 lines
net/socket.c +1 -7
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
-    if (ret < 0) {
-        perror("bind");
-        closesocket(fd);
-        return -1;
-    }
-    ret = listen(fd, 0);
+    ret = socket_listen(saddr, &local_error);
     if (ret < 0) {
         perror("listen");

You should use error_report_err instead of perror, since that's how socket_listen returns errors. You are also leaking saddr.

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 557

Quoted 6 lines
net/socket.c +1 -0
         closesocket(fd);
@@ -557,6 +554,7 @@ static int net_socket_listen_init(NetClientState *peer,
     s->nc.link_down = true;
 
     qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+    g_free(saddr);

Use qapi_free_SocketAddress instead of g_free, because SocketAddress includes pointers to other data structures that have to be freed.

net/socket.c static int net_socket_connect_init(NetClientState *peer, · line 571

Quoted 13 lines
net/socket.c +3 -2
     return 0;
 }
 
@@ -567,9 +565,11 @@ static int net_socket_connect_init(NetClientState *peer,
 {
     NetSocketState *s;
     int fd, connected, ret;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)

Same as above.

net/socket.c static int net_socket_connect_init(NetClientState *peer, · line 581

Quoted 9 lines
net/socket.c +1 -1
         return -1;
 
     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
@@ -578,10 +578,10 @@ static int net_socket_connect_init(NetClientState *peer,
         return -1;
     }
     qemu_set_nonblock(fd);
-
+    saddr = socket_local_address(fd, &local_error);

Same as above. You probably haven't tested this patch well enough, because otherwise you would have noticed the problem here.

Quoted 8 lines
net/socket.c +1 -1
     connected = 0;
     for(;;) {
-        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+        ret = socket_connect(saddr, &local_error, NULL, NULL);
         if (ret < 0) {
             if (errno == EINTR || errno == EWOULDBLOCK) {
                 /* continue */

net/socket.c static int net_socket_connect_init(NetClientState *peer, · line 607

Quoted 6 lines
net/socket.c +0 -3
@@ -602,9 +602,7 @@ static int net_socket_connect_init(NetClientState *peer,
     s = net_socket_fd_init(peer, model, name, fd, connected);
     if (!s)
         return -1;
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: connect to %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));

Here you should have created a new function socket_address_to_string in util/qemu-sockets.c. The function should return a new string corresponding to the address. The address can be IPv4/IPv6 (then printing is done via inet_ntop), a Unix socket or a file descriptor; you have to handle all three cases.

The return value of the function can be used together with snprintf to form s->nc.info_str.

net/socket.c static int net_socket_mcast_init(NetClientState *peer, · line 622

Quoted 13 lines
net/socket.c +3 -1
+    g_free(saddr);
     return 0;
 }
 
@@ -618,8 +616,9 @@ static int net_socket_mcast_init(NetClientState *peer,
     int fd;
     struct sockaddr_in saddr;
     struct in_addr localaddr, *param_localaddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
         return -1;

Same problem as above. In addition, saddr is being passed uninitialized to net_socket_mcast_create.

Quoted 6 lines
net/socket.c +1 -0
     if (localaddr_str != NULL) {
@@ -656,12 +655,13 @@ static int net_socket_udp_init(NetClientState *peer,
     NetSocketState *s;
     int fd, ret;
     struct sockaddr_in laddr, raddr;
+    Error *local_error = NULL;

net/socket.c static int net_socket_udp_init(NetClientState *peer, · line 666

Quoted 10 lines
net/socket.c +2 -2
 
-    if (parse_host_port(&laddr, lhost) < 0) {
+    if (socket_parse(lhost, &local_error) < 0) {
         return -1;
     }
 
-    if (parse_host_port(&raddr, rhost) < 0) {
+    if (socket_parse(rhost, &local_error) < 0) {
         return -1;
     }

Same problem as above. In addition, laddr and raddr are being used uninitialized. At least in the case of raddr, the compiler should have noticed this and issued a warning.

Thanks,

Paolo

#4 lore raw
Quoted 28 lines · Paolo Bonzini
Quoted 12 lines · Ashijeet Acharya

Changed the listen(),connect(),parse_host_port() in net/socket.c with the socket_*()functions in include/qemu/sockets.h.

---
  net/socket.c | 38 +++++++++++++++++++-------------------
  1 file changed, 19 insertions(+), 19 deletions(-)
net/socket.c +0 -0
index 9fa2cd8..b6e2f3e 100644

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 529

Quoted 10 lines
net/socket.c +3 -2
@@ -522,10 +522,12 @@ static int net_socket_listen_init(NetClientState *peer,
  {
      NetClientState *nc;
      NetSocketState *s;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
      int fd, ret;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)

This leaks the return address.

Quoted 6 lines

The result of socket_parse should be stored in saddr.

Also, the right comparison is "!= NULL", not "< 0".

Finally, you're not printing the error (with error_report_err).

Solved this....although I think the comparison will be "== NULL".

Quoted 24 lines

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 541

Quoted 8 lines
net/socket.c +1 -0
          return -1;
 
      fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
@@ -536,14 +538,9 @@ static int net_socket_listen_init(NetClientState *peer,
      qemu_set_nonblock(fd);
 
      socket_set_fast_reuse(fd);
+    saddr = socket_local_address(fd, &local_error);

This is incorrect too. You're adding a call to getsockname that wasn't in the original code. You're also ignoring possible failures from socket_local_address.

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 545

Quoted 10 lines
net/socket.c +1 -7
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
-    if (ret < 0) {
-        perror("bind");
-        closesocket(fd);
-        return -1;
-    }
-    ret = listen(fd, 0);
+    ret = socket_listen(saddr, &local_error);
      if (ret < 0) {
          perror("listen");
Quoted 3 lines

You should use error_report_err instead of perror, since that's how socket_listen returns errors. You are also leaking saddr.

Done. I am not sure how saddr is getting leaked here.

Quoted 9 lines

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 557

Quoted 6 lines
net/socket.c +1 -0
          closesocket(fd);
@@ -557,6 +554,7 @@ static int net_socket_listen_init(NetClientState *peer,
      s->nc.link_down = true;
 
      qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+    g_free(saddr);

Use qapi_free_SocketAddress instead of g_free, because SocketAddress includes pointers to other data structures that have to be freed.

Done.

Quoted 51 lines
Quoted 5 lines
net/socket.c +0 -0
      return 0;
  }
 

net/socket.c static int net_socket_connect_init(NetClientState *peer, · line 571

Quoted 9 lines
net/socket.c +3 -2
@@ -567,9 +565,11 @@ static int net_socket_connect_init(NetClientState *peer,
  {
      NetSocketState *s;
      int fd, connected, ret;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)

Same as above.

net/socket.c static int net_socket_connect_init(NetClientState *peer, · line 581

Quoted 9 lines
net/socket.c +1 -1
          return -1;
 
      fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
@@ -578,10 +578,10 @@ static int net_socket_connect_init(NetClientState *peer,
          return -1;
      }
      qemu_set_nonblock(fd);
-
+    saddr = socket_local_address(fd, &local_error);

Same as above. You probably haven't tested this patch well enough, because otherwise you would have noticed the problem here.

Quoted 8 lines
net/socket.c +1 -1
      connected = 0;
      for(;;) {
-        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+        ret = socket_connect(saddr, &local_error, NULL, NULL);
          if (ret < 0) {
              if (errno == EINTR || errno == EWOULDBLOCK) {
                  /* continue */

net/socket.c static int net_socket_connect_init(NetClientState *peer, · line 607

Quoted 6 lines
net/socket.c +0 -3
@@ -602,9 +602,7 @@ static int net_socket_connect_init(NetClientState *peer,
      s = net_socket_fd_init(peer, model, name, fd, connected);
      if (!s)
          return -1;
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: connect to %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));

Here you should have created a new function socket_address_to_string in util/qemu-sockets.c. The function should return a new string corresponding to the address. The address can be IPv4/IPv6 (then printing is done via inet_ntop), a Unix socket or a file descriptor; you have to handle all three cases.

Quoted 2 lines

The return value of the function can be used together with snprintf to form s->nc.info_str.

I created the new function in util/qemu-sockets.c, will it be right to use sprintf() instead of inet_ntop() like the way its done in inet_ntoa()?

Quoted 11 lines
Quoted 11 lines
net/socket.c +2 -0
+    g_free(saddr);
      return 0;
  }
 
@@ -618,8 +616,9 @@ static int net_socket_mcast_init(NetClientState *peer,
      int fd;
      struct sockaddr_in saddr;
      struct in_addr localaddr, *param_localaddr;
+    Error *local_error = NULL;
 
Quoted 6 lines

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 530

Quoted 3 lines
net/socket.c +1 -1
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
          return -1;

Same problem as above. In addition, saddr is being passed uninitialized to net_socket_mcast_create.

Here net_socket_mcast_create() takes argument of the type struct sockaddr_in, but if i change saddr to the type struct SocketAddress to use it with socket_parse() the whole thing becomes a mess. How do i tackle this?? Please bear with me...I am a newbie and this is my first patch.

Quoted 22 lines
Quoted 7 lines
net/socket.c +1 -0
      if (localaddr_str != NULL) {
@@ -656,12 +655,13 @@ static int net_socket_udp_init(NetClientState *peer,
      NetSocketState *s;
      int fd, ret;
      struct sockaddr_in laddr, raddr;
+    Error *local_error = NULL;

net/socket.c static int net_socket_udp_init(NetClientState *peer, · line 666

Quoted 10 lines
net/socket.c +2 -2
 
-    if (parse_host_port(&laddr, lhost) < 0) {
+    if (socket_parse(lhost, &local_error) < 0) {
          return -1;
      }
 
-    if (parse_host_port(&raddr, rhost) < 0) {
+    if (socket_parse(rhost, &local_error) < 0) {
          return -1;
      }

Same problem as above. In addition, laddr and raddr are being used uninitialized. At least in the case of raddr, the compiler should have noticed this and issued a warning.

Quoted 4 lines

Thanks,

Paolo

Thanks
Ashijeet

#5 lore raw
Quoted 33 lines · Ashijeet Acharya
Quoted 30 lines · Paolo Bonzini
Quoted 14 lines · Ashijeet Acharya

Changed the listen(),connect(),parse_host_port() in net/socket.c with the socket_*()functions in include/qemu/sockets.h.

---
  net/socket.c | 38 +++++++++++++++++++-------------------
  1 file changed, 19 insertions(+), 19 deletions(-)
net/socket.c +0 -0
index 9fa2cd8..b6e2f3e 100644
@@ -522,10 +522,12 @@ static int
 net_socket_listen_init(NetClientState *peer,

net/socket.c static int · line 530

Quoted 10 lines
net/socket.c +3 -2
  {
      NetClientState *nc;
      NetSocketState *s;
-    struct sockaddr_in saddr;
+    SocketAddress *saddr;
      int fd, ret;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)

This leaks the return address.

Quoted 7 lines
Quoted 6 lines

The result of socket_parse should be stored in saddr.

Also, the right comparison is "!= NULL", not "< 0".

Finally, you're not printing the error (with error_report_err).

Solved this....although I think the comparison will be "== NULL".

Right.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
net/socket.c +1 -7
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
-    if (ret < 0) {
-        perror("bind");
-        closesocket(fd);
-        return -1;
-    }
-    ret = listen(fd, 0);
+    ret = socket_listen(saddr, &local_error);
      if (ret < 0) {
          perror("listen");
Quoted 4 lines
Quoted 2 lines

You should use error_report_err instead of perror, since that's how socket_listen returns errors. You are also leaking saddr.

Done. I am not sure how saddr is getting leaked here.

It was allocated in socket_parse, and you're returning without freeing it.

Quoted 15 lines
Quoted 15 lines

net/socket.c static int net_socket_connect_init(NetClientState · line 608

Quoted 8 lines
net/socket.c +0 -3
@@ -602,9 +602,7 @@ static int net_socket_connect_init(NetClientState
 *peer,
      s = net_socket_fd_init(peer, model, name, fd, connected);
      if (!s)
          return -1;
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: connect to %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));

Here you should have created a new function socket_address_to_string in util/qemu-sockets.c. The function should return a new string corresponding to the address. The address can be IPv4/IPv6 (then printing is done via inet_ntop), a Unix socket or a file descriptor; you have to handle all three cases.

Quoted 5 lines
Quoted 2 lines

The return value of the function can be used together with snprintf to form s->nc.info_str.

I created the new function in util/qemu-sockets.c, will it be right to use sprintf() instead of inet_ntop() like the way its done in inet_ntoa()?

I'm not sure I understand... To print an address with address family AF_INET6 you need inet_ntop.

Quoted 14 lines
Quoted 7 lines
Quoted 7 lines
net/socket.c +1 -0
@@ -618,8 +616,9 @@ static int net_socket_mcast_init(NetClientState
 *peer,
      int fd;
      struct sockaddr_in saddr;
      struct in_addr localaddr, *param_localaddr;
+    Error *local_error = NULL;
 
Quoted 6 lines

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 530

Quoted 3 lines
net/socket.c +1 -1
-    if (parse_host_port(&saddr, host_str) < 0)
+    if (socket_parse(host_str, &local_error) < 0)
          return -1;

Same problem as above. In addition, saddr is being passed uninitialized to net_socket_mcast_create.

Quoted 4 lines

Here net_socket_mcast_create() takes argument of the type struct sockaddr_in, but if i change saddr to the type struct SocketAddress to use it with socket_parse() the whole thing becomes a mess. How do i tackle this??

You can leave net_socket_mcast_init and net_socket_mcast_create aside for now.

Thanks,

Paolo

#6 lore raw

PATCH 1/1 Change net/socket.c to use socket_*() functions

In reply to Ashijeet Acharya

Use socket_*() functions from include/qemu/sockets.h instead of listen()/bind()/ connect()/parse_host_port(). socket_*() fucntions are QAPI based and this patch performs this api conversion since everything will be using QAPI based sockets in the future. Also add a helper function socket_address_to_string() in util/qemu-sockets.c which returns the string representation of socket address. The task was listed on http://wiki.qemu.org/BiteSizedTasks page.

---
 include/qemu/sockets.h | 16 ++++++++++++++-
 net/socket.c           | 56 +++++++++++++++++++++++++-------------------------
 util/qemu-sockets.c    | 25 ++++++++++++++++++++++
 3 files changed, 68 insertions(+), 29 deletions(-)
include/qemu/sockets.h +15 -1
index 1bd9218..3a1a887 100644
@@ -110,4 +110,18 @@ SocketAddress *socket_remote_address(int fd, Error **errp);
 void qapi_copy_SocketAddress(SocketAddress **p_dest,
                              SocketAddress *src);
 
-#endif /* QEMU_SOCKET_H */
+/**
+ * socket_address_to_string:
+ * @addr: the socket address struct
+ * @errp: pointer to uninitialized error object
+ *
+ * Get the string representation of the socket
+ * address. A pointer to the char array containing
+ * string format will be returned, the caller is
+ * required to release the returned value when no
+ * longer required with g_free.
+ *
+ * Returns: the socket address in string format, or NULL on error
+ */
+char *socket_address_to_string(struct SocketAddress *addr, Error **errp);
+#endif /* QEMU_SOCKET_H */
\ No newline at end of file
net/socket.c +28 -28

105 lines not on this page · raw message

util/qemu-sockets.c +25 -0
index 0d6cd1f..191e0e0 100644
@@ -1151,3 +1151,28 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest,
     qmp_input_visitor_cleanup(qiv);
     qobject_decref(obj);
 }
+
+char *socket_address_to_string(struct SocketAddress *addr, Error **errp)
+{
+    char *buf;
+
+    switch (addr->type) {
+    case SOCKET_ADDRESS_KIND_INET:
+        buf = g_strdup(addr->u.inet.data->host);
+        break;
+
+    case SOCKET_ADDRESS_KIND_UNIX:
+        buf = g_strdup(addr->u.q_unix.data->path);
+        break;
+
+    case SOCKET_ADDRESS_KIND_FD:
+        buf = g_strdup(addr->u.fd.data->str);
+        break;
+
+    default:
+        error_setg(errp, "socket family %d unsupported",
+                   addr->type);
+        return NULL;
+    }
+    return buf;
+}
Signature · 1 line
2.6.2
#7 lore raw

Re: [Qemu-devel] [PATCH] Change net/socket.c to use socket_*() functions

Thanks, much better! Just one thing:

util/qemu-sockets.c void qapi_copy_SocketAddress(SocketAddress **p_dest, · line 1161

Quoted 2 lines · Ashijeet Acharya
util/qemu-sockets.c +2 -0
+    case SOCKET_ADDRESS_KIND_INET:
+        buf = g_strdup(addr->u.inet.data->host);

Please include the port too here. Also, if the host contains a colon (which you can check with strchr), please print it as [HOST]:PORT instead of HOST:PORT. This will help with IPv6.

Thanks,

Paolo

Quoted 14 lines
util/qemu-sockets.c +14 -0
+        break;
+
+    case SOCKET_ADDRESS_KIND_UNIX:
+        buf = g_strdup(addr->u.q_unix.data->path);
+        break;
+
+    case SOCKET_ADDRESS_KIND_FD:
+        buf = g_strdup(addr->u.fd.data->str);
+        break;
+
+    default:
+        error_setg(errp, "socket family %d unsupported",
+                   addr->type);
+        return NULL;
#8 lore raw

PATCH v2 1/1 Change net/socket.c to use socket_*() functions

In reply to Ashijeet Acharya

Use socket_*() functions from include/qemu/sockets.h instead of listen()/bind()/ connect()/parse_host_port(). socket_*() fucntions are QAPI based and this patch performs this api conversion since everything will be using QAPI based sockets in the future. Also add a helper function socket_address_to_string() in util/qemu-sockets.c which returns the string representation of socket address. Thetask was listed on http://wiki.qemu.org/BiteSizedTasks page.

---
 include/qemu/sockets.h | 16 ++++++++++++++-
 net/socket.c           | 55 +++++++++++++++++++++++++-------------------------
 util/qemu-sockets.c    | 36 +++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 29 deletions(-)
include/qemu/sockets.h +15 -1
index 1bd9218..3a1a887 100644
@@ -110,4 +110,18 @@ SocketAddress *socket_remote_address(int fd, Error **errp);
 void qapi_copy_SocketAddress(SocketAddress **p_dest,
                              SocketAddress *src);
 
-#endif /* QEMU_SOCKET_H */
+/**
+ * socket_address_to_string:
+ * @addr: the socket address struct
+ * @errp: pointer to uninitialized error object
+ *
+ * Get the string representation of the socket
+ * address. A pointer to the char array containing
+ * string format will be returned, the caller is
+ * required to release the returned value when no
+ * longer required with g_free.
+ *
+ * Returns: the socket address in string format, or NULL on error
+ */
+char *socket_address_to_string(struct SocketAddress *addr, Error **errp);
+#endif /* QEMU_SOCKET_H */
\ No newline at end of file
net/socket.c +27 -28

104 lines not on this page · raw message

util/qemu-sockets.c +36 -0
index 0d6cd1f..771b7db 100644
@@ -1151,3 +1151,39 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest,
     qmp_input_visitor_cleanup(qiv);
     qobject_decref(obj);
 }
+
+char *socket_address_to_string(struct SocketAddress *addr, Error **errp)
+{
+    char *buf;
+    InetSocketAddress *inet;
+    char host_port[INET6_ADDRSTRLEN + 5 + 4];
+
+    switch (addr->type) {
+    case SOCKET_ADDRESS_KIND_INET:
+        inet = addr->u.inet.data;
+        if (strchr(inet->host, ':') == NULL) {
+            snprintf(host_port, sizeof(host_port), "%s:%s", inet->host,
+                    inet->port);
+            buf = g_strdup(host_port);
+        } else {
+            snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host,
+                    inet->port);
+            buf = g_strdup(host_port);
+        }
+        break;
+
+    case SOCKET_ADDRESS_KIND_UNIX:
+        buf = g_strdup(addr->u.q_unix.data->path);
+        break;
+
+    case SOCKET_ADDRESS_KIND_FD:
+        buf = g_strdup(addr->u.fd.data->str);
+        break;
+
+    default:
+        error_setg(errp, "socket family %d unsupported",
+                   addr->type);
+        return NULL;
+    }
+    return buf;
+}
Signature · 1 line
2.6.2
Reviewed-by on v2 1/1
#9 lore raw

Re: [Qemu-devel] [PATCH v2] Change net/socket.c to use socket_*() functions

Quoted 9 lines · Ashijeet Acharya

Use socket_*() functions from include/qemu/sockets.h instead of listen()/bind()/ connect()/parse_host_port(). socket_*() fucntions are QAPI based and this patch performs this api conversion since everything will be using QAPI based sockets in the future. Also add a helper function socket_address_to_string() in util/qemu-sockets.c which returns the string representation of socket address. Thetask was listed on
http://wiki.qemu.org/BiteSizedTasks page.

Jason, are you going to take this through the net tree?

Thanks,

Paolo

Quoted 189 lines
---
 include/qemu/sockets.h | 16 ++++++++++++++-
 net/socket.c           | 55 +++++++++++++++++++++++++-------------------------
 util/qemu-sockets.c    | 36 +++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 29 deletions(-)
include/qemu/sockets.h +15 -1
index 1bd9218..3a1a887 100644
@@ -110,4 +110,18 @@ SocketAddress *socket_remote_address(int fd, Error **errp);
 void qapi_copy_SocketAddress(SocketAddress **p_dest,
                              SocketAddress *src);
 
-#endif /* QEMU_SOCKET_H */
+/**
+ * socket_address_to_string:
+ * @addr: the socket address struct
+ * @errp: pointer to uninitialized error object
+ *
+ * Get the string representation of the socket
+ * address. A pointer to the char array containing
+ * string format will be returned, the caller is
+ * required to release the returned value when no
+ * longer required with g_free.
+ *
+ * Returns: the socket address in string format, or NULL on error
+ */
+char *socket_address_to_string(struct SocketAddress *addr, Error **errp);
+#endif /* QEMU_SOCKET_H */
\ No newline at end of file
net/socket.c +27 -28
index 333fb9e..ae6f921 100644
@@ -489,41 +489,30 @@ static int net_socket_listen_init(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
-    struct sockaddr_in saddr;
-    int fd, ret;
+    SocketAddress *saddr;
+    int ret;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
-        return -1;
-
-    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
-    if (fd < 0) {
-        perror("socket");
+    saddr = socket_parse(host_str, &local_error);
+    if (saddr == NULL) {
+        error_report_err(local_error);
         return -1;
     }
-    qemu_set_nonblock(fd);
 
-    socket_set_fast_reuse(fd);
-
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+    ret = socket_listen(saddr, &local_error);
     if (ret < 0) {
-        perror("bind");
-        closesocket(fd);
-        return -1;
-    }
-    ret = listen(fd, 0);
-    if (ret < 0) {
-        perror("listen");
-        closesocket(fd);
+        error_report_err(local_error);
         return -1;
     }
 
     nc = qemu_new_net_client(&net_socket_info, peer, model, name);
     s = DO_UPCAST(NetSocketState, nc, nc);
     s->fd = -1;
-    s->listen_fd = fd;
+    s->listen_fd = ret;
     s->nc.link_down = true;
 
     qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+    qapi_free_SocketAddress(saddr);
     return 0;
 }
 
@@ -534,10 +523,15 @@ static int net_socket_connect_init(NetClientState *peer,
 {
     NetSocketState *s;
     int fd, connected, ret;
-    struct sockaddr_in saddr;
+    char *addr_str;
+    SocketAddress *saddr;
+    Error *local_error = NULL;
 
-    if (parse_host_port(&saddr, host_str) < 0)
+    saddr = socket_parse(host_str, &local_error);
+    if (saddr == NULL) {
+        error_report_err(local_error);
         return -1;
+    }
 
     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
     if (fd < 0) {
@@ -545,10 +539,9 @@ static int net_socket_connect_init(NetClientState *peer,
         return -1;
     }
     qemu_set_nonblock(fd);
-
     connected = 0;
     for(;;) {
-        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+        ret = socket_connect(saddr, &local_error, NULL, NULL);
         if (ret < 0) {
             if (errno == EINTR || errno == EWOULDBLOCK) {
                 /* continue */
@@ -557,7 +550,7 @@ static int net_socket_connect_init(NetClientState *peer,
                        errno == EINVAL) {
                 break;
             } else {
-                perror("connect");
+                error_report_err(local_error);
                 closesocket(fd);
                 return -1;
             }
@@ -569,9 +562,15 @@ static int net_socket_connect_init(NetClientState *peer,
     s = net_socket_fd_init(peer, model, name, fd, connected);
     if (!s)
         return -1;
+
+    addr_str = socket_address_to_string(saddr, &local_error);
+    if (addr_str == NULL)
+        return -1;
+
     snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: connect to %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+             "socket: connect to %s", addr_str);
+    qapi_free_SocketAddress(saddr);
+    g_free(addr_str);
     return 0;
 }
 
util/qemu-sockets.c +36 -0
index 0d6cd1f..771b7db 100644
@@ -1151,3 +1151,39 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest,
     qmp_input_visitor_cleanup(qiv);
     qobject_decref(obj);
 }
+
+char *socket_address_to_string(struct SocketAddress *addr, Error **errp)
+{
+    char *buf;
+    InetSocketAddress *inet;
+    char host_port[INET6_ADDRSTRLEN + 5 + 4];
+
+    switch (addr->type) {
+    case SOCKET_ADDRESS_KIND_INET:
+        inet = addr->u.inet.data;
+        if (strchr(inet->host, ':') == NULL) {
+            snprintf(host_port, sizeof(host_port), "%s:%s", inet->host,
+                    inet->port);
+            buf = g_strdup(host_port);
+        } else {
+            snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host,
+                    inet->port);
+            buf = g_strdup(host_port);
+        }
+        break;
+
+    case SOCKET_ADDRESS_KIND_UNIX:
+        buf = g_strdup(addr->u.q_unix.data->path);
+        break;
+
+    case SOCKET_ADDRESS_KIND_FD:
+        buf = g_strdup(addr->u.fd.data->str);
+        break;
+
+    default:
+        error_setg(errp, "socket family %d unsupported",
+                   addr->type);
+        return NULL;
+    }
+    return buf;
+}
#10 lore raw

Re: [Qemu-devel] [PATCH v2] Change net/socket.c to use socket_*() functions

Quoted 11 lines · Paolo Bonzini
Quoted 8 lines · Ashijeet Acharya

Use socket_*() functions from include/qemu/sockets.h instead of listen()/bind()/ connect()/parse_host_port(). socket_*() fucntions are QAPI based and this patch performs this api conversion since everything will be using QAPI based sockets in the future. Also add a helper function socket_address_to_string() in util/qemu-sockets.c which returns the string representation of socket address. Thetask was listed on
http://wiki.qemu.org/BiteSizedTasks page.

Quoted 5 lines
Quoted 1 line

Jason, are you going to take this through the net tree?

Can you fix up the long lines/space issues in the commit message if you do, please?

thanks
-- PMM

#11 lore raw

Re: [Qemu-devel] [PATCH v2] Change net/socket.c to use socket_*() functions

Quoted 12 lines · Peter Maydell
Quoted 11 lines · Paolo Bonzini
Quoted 9 lines · Ashijeet Acharya

Use socket_*() functions from include/qemu/sockets.h instead of listen()/bind()/ connect()/parse_host_port(). socket_*() fucntions are QAPI based and this patch performs this api conversion since everything will be using QAPI based sockets in the future. Also add a helper function socket_address_to_string() in util/qemu-sockets.c which returns the string representation of socket address. Thetask was listed on
http://wiki.qemu.org/BiteSizedTasks page.

Quoted 8 lines
Quoted 3 lines

Jason, are you going to take this through the net tree?

Can you fix up the long lines/space issues in the commit message if you do, please?

thanks
-- PMM

Fixed and apply in -net.

Thanks

#12 lore raw

Re: [Qemu-devel] [PATCH v2] Change net/socket.c to use socket_*() functions

Quoted 26 lines · Jason Wang
Quoted 23 lines · Peter Maydell
Quoted 12 lines · Paolo Bonzini
Quoted 9 lines · Ashijeet Acharya

Use socket_*() functions from include/qemu/sockets.h instead of listen()/bind()/ connect()/parse_host_port(). socket_*() fucntions are QAPI based and this patch performs this api conversion since everything will be using QAPI based sockets in the future. Also add a helper function socket_address_to_string() in util/qemu-sockets.c which returns the string representation of socket address. Thetask was listed on
http://wiki.qemu.org/BiteSizedTasks page.

Quoted 5 lines
Quoted 1 line

Jason, are you going to take this through the net tree?

Can you fix up the long lines/space issues in the commit message if you do, please?

Quoted 7 lines
Quoted 2 lines

thanks
-- PMM

Fixed and apply in -net.

Thanks

Thanks a lot!

Ashijeet

#13 lore raw

Re: [Qemu-devel] [PATCH v2] Change net/socket.c to use socket_*() functions

In reply to Ashijeet Acharya
Quoted 27 lines · Ashijeet Acharya

Use socket_*() functions from include/qemu/sockets.h instead of listen()/bind()/ connect()/parse_host_port(). socket_*() fucntions are QAPI based and this patch performs this api conversion since everything will be using QAPI based sockets in the future. Also add a helper function socket_address_to_string() in util/qemu-sockets.c which returns the string representation of socket address. Thetask was listed on http://wiki.qemu.org/BiteSizedTasks page.

---
 include/qemu/sockets.h | 16 ++++++++++++++-
 net/socket.c           | 55 +++++++++++++++++++++++++-------------------------
 util/qemu-sockets.c    | 36 +++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 29 deletions(-)
util/qemu-sockets.c +8 -0
index 0d6cd1f..771b7db 100644
@@ -1151,3 +1151,39 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest,
     qmp_input_visitor_cleanup(qiv);
     qobject_decref(obj);
 }
+
+char *socket_address_to_string(struct SocketAddress *addr, Error **errp)
+{
+    char *buf;
+    InetSocketAddress *inet;
+    char host_port[INET6_ADDRSTRLEN + 5 + 4];
+
+    switch (addr->type) {

util/qemu-sockets.c void qapi_copy_SocketAddress(SocketAddress **p_dest, · line 1171

Quoted 10 lines
util/qemu-sockets.c +10 -0
+    case SOCKET_ADDRESS_KIND_INET:
+        inet = addr->u.inet.data;
+        if (strchr(inet->host, ':') == NULL) {
+            snprintf(host_port, sizeof(host_port), "%s:%s", inet->host,
+                    inet->port);
+            buf = g_strdup(host_port);
+        } else {
+            snprintf(host_port, sizeof(host_port), "[%s]:%s", inet->host,
+                    inet->port);
+            buf = g_strdup(host_port);

I see the patch is already queued, but really this should be changed to not use a preallocated buffer. It should instead use g_strdup_printf()

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|