Patchwork [2/2] net/socket: allow ipv6 for net_socket_listen_init and net_socket_connect_init

login
register
mail settings
Submitter Juan Quintela
Date March 16, 2011, 9:01 p.m.
Message ID <4f8db30e533a8d9cd877a6fb56794ab0c35d1e1f.1300297944.git.quintela@redhat.com>
Download mbox | patch
Permalink /patch/87305/
State New
Headers show

Comments

Juan Quintela - March 16, 2011, 9:01 p.m.
Remove use of parse_host_port.

Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 net/socket.c |   60 ++++++++++-----------------------------------------------
 1 files changed, 11 insertions(+), 49 deletions(-)
Peter Maydell - March 16, 2011, 9:30 p.m.
On 16 March 2011 21:01, Juan Quintela <quintela@redhat.com> wrote:
> -    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
> +    ret = tcp_server_start(host_str, &fd);
>     if (ret < 0) {
> -        perror("bind");
> +        perror("tcp_server_start");
>         return -1;
>     }

It looks like tcp_server_start() returns an error code
rather than setting errno, so isn't perror() wrong here?

-- PMM
Juan Quintela - March 16, 2011, 10:22 p.m.
Peter Maydell <peter.maydell@linaro.org> wrote:
> On 16 March 2011 21:01, Juan Quintela <quintela@redhat.com> wrote:
>> -    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
>> +    ret = tcp_server_start(host_str, &fd);
>>     if (ret < 0) {
>> -        perror("bind");
>> +        perror("tcp_server_start");
>>         return -1;
>>     }
>
> It looks like tcp_server_start() returns an error code
> rather than setting errno, so isn't perror() wrong here?

You are right O:-)

I am not clear about what to do here.  Basically we have:
- migration code: uses DPRINTF()
- sockets: use perror() left and right.

Any good idea on what to do everywhere?

For this case, I will just put strerrno(), easy enough.

Thanks a lot, Juan.

Patch

diff --git a/net/socket.c b/net/socket.c
index 7337f4f..a28dfdd 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -392,28 +392,13 @@  static int net_socket_listen_init(VLANState *vlan,
                                   const char *host_str)
 {
     NetSocketListenState *s;
-    int fd, val, ret;
-    struct sockaddr_in saddr;
-
-    if (parse_host_port(&saddr, host_str) < 0)
-        return -1;
+    int fd, ret;

     s = qemu_mallocz(sizeof(NetSocketListenState));

-    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
-    if (fd < 0) {
-        perror("socket");
-        return -1;
-    }
-    socket_set_nonblock(fd);
-
-    /* allow fast reuse */
-    val = 1;
-    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
-
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+    ret = tcp_server_start(host_str, &fd);
     if (ret < 0) {
-        perror("bind");
+        perror("tcp_server_start");
         return -1;
     }
     ret = listen(fd, 0);
@@ -435,40 +420,17 @@  static int net_socket_connect_init(VLANState *vlan,
                                    const char *host_str)
 {
     NetSocketState *s;
-    int fd, connected, ret, err;
+    int fd, connected, ret;
     struct sockaddr_in saddr;

-    if (parse_host_port(&saddr, host_str) < 0)
-        return -1;
-
-    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
-    if (fd < 0) {
-        perror("socket");
+    ret = tcp_client_start(host_str, &fd);
+    if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
+        connected = 0;
+    } else if (ret < 0) {
+        closesocket(fd);
         return -1;
-    }
-    socket_set_nonblock(fd);
-
-    connected = 0;
-    for(;;) {
-        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
-        if (ret < 0) {
-            err = socket_error();
-            if (err == EINTR || err == EWOULDBLOCK) {
-            } else if (err == EINPROGRESS) {
-                break;
-#ifdef _WIN32
-            } else if (err == WSAEALREADY || err == WSAEINVAL) {
-                break;
-#endif
-            } else {
-                perror("connect");
-                closesocket(fd);
-                return -1;
-            }
-        } else {
-            connected = 1;
-            break;
-        }
+    } else {
+        connected = 1;
     }
     s = net_socket_fd_init(vlan, model, name, fd, connected);
     if (!s)