Patchwork [v3,09/12] net: socket: use info_dict instead of info_str

login
register
mail settings
Submitter Miguel Di Ciurcio Filho
Date April 15, 2010, 2:07 p.m.
Message ID <1271340427-12579-10-git-send-email-miguel.filho@gmail.com>
Download mbox | patch
Permalink /patch/50268/
State New
Headers show

Comments

Miguel Di Ciurcio Filho - April 15, 2010, 2:07 p.m.
Signed-off-by: Miguel Di Ciurcio Filho <miguel.filho@gmail.com>
---
 net/socket.c |   47 ++++++++++++++++++++++++++++++++---------------
 1 files changed, 32 insertions(+), 15 deletions(-)
Luiz Capitulino - April 23, 2010, 9:10 p.m.
On Thu, 15 Apr 2010 11:07:04 -0300
Miguel Di Ciurcio Filho <miguel.filho@gmail.com> wrote:

> Signed-off-by: Miguel Di Ciurcio Filho <miguel.filho@gmail.com>
> ---
>  net/socket.c |   47 ++++++++++++++++++++++++++++++++---------------
>  1 files changed, 32 insertions(+), 15 deletions(-)
> 
> diff --git a/net/socket.c b/net/socket.c
> index 1c4e153..fa5d24d 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -28,6 +28,10 @@
>  #include "net.h"
>  #include "qemu-char.h"
>  #include "qemu-common.h"
> +#include "qdict.h"
> +#include "qstring.h"
> +#include "qbool.h"
> +#include "qint.h"
>  #include "qemu-error.h"
>  #include "qemu-option.h"
>  #include "qemu_socket.h"
> @@ -266,11 +270,14 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
>      }
>  
>      nc = qemu_new_net_client(&net_dgram_socket_info, vlan, NULL, model, name);
> -
> -    snprintf(nc->info_str, sizeof(nc->info_str),
> -	    "socket: fd=%d (%s mcast=%s:%d)",
> -	    fd, is_connected ? "cloned" : "",
> -	    inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
> +    
> +    nc->info_dict = qdict_new();
> +    qdict_put(nc->info_dict, "fd", qint_from_int(fd));


> +    qdict_put(nc->info_dict, "host",
> +        qstring_from_str(inet_ntoa(saddr.sin_addr)));
> +    qdict_put(nc->info_dict, "service", qint_from_int(ntohs(saddr.sin_port)));
> +    qdict_put(nc->info_dict, "family", qstring_from_str("ipv4"));

 Please, move the block above to a function, it's really duplicated in
the other hunks.

 Also, I think you identify the type of the socket here, eg. listen, connect,
etc.

> +    qdict_put(nc->info_dict, "cloned", qbool_from_int(is_connected));
>  
>      s = DO_UPCAST(NetSocketState, nc, nc);
>  
> @@ -306,8 +313,9 @@ static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
>      NetSocketState *s;
>  
>      nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name);
> +    nc->info_dict = qdict_new();
>  
> -    snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
> +    qdict_put(nc->info_dict, "fd", qint_from_int(fd));
>  
>      s = DO_UPCAST(NetSocketState, nc, nc);
>  
> @@ -366,9 +374,12 @@ static void net_socket_accept(void *opaque)
>      if (!s1) {
>          closesocket(fd);
>      } else {
> -        snprintf(s1->nc.info_str, sizeof(s1->nc.info_str),
> -                 "socket: connection from %s:%d",
> -                 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
> +        s1->nc.info_dict = qdict_new();
> +
> +        qdict_put(s1->nc.info_dict, "host",
> +            qstring_from_str(inet_ntoa(saddr.sin_addr)));
> +        qdict_put(s1->nc.info_dict, "service", qint_from_int(ntohs(saddr.sin_port)));
> +        qdict_put(s1->nc.info_dict, "family", qstring_from_str("ipv4"));
>      }
>  }
>  
> @@ -459,9 +470,12 @@ static int net_socket_connect_init(VLANState *vlan,
>      s = net_socket_fd_init(vlan, 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));
> +
> +    s->nc.info_dict = qdict_new();
> +    qdict_put(s->nc.info_dict, "host",
> +        qstring_from_str(inet_ntoa(saddr.sin_addr)));
> +    qdict_put(s->nc.info_dict, "service", qint_from_int(ntohs(saddr.sin_port)));
> +    qdict_put(s->nc.info_dict, "family", qstring_from_str("ipv4"));
>      return 0;
>  }
>  
> @@ -487,10 +501,13 @@ static int net_socket_mcast_init(VLANState *vlan,
>          return -1;
>  
>      s->dgram_dst = saddr;
> +    
> +    s->nc.info_dict = qdict_new();
> +    qdict_put(s->nc.info_dict, "host",
> +        qstring_from_str(inet_ntoa(saddr.sin_addr)));
> +    qdict_put(s->nc.info_dict, "service", qint_from_int(ntohs(saddr.sin_port)));
> +    qdict_put(s->nc.info_dict, "family", qstring_from_str("ipv4"));
>  
> -    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> -             "socket: mcast=%s:%d",
> -             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
>      return 0;
>  
>  }

Patch

diff --git a/net/socket.c b/net/socket.c
index 1c4e153..fa5d24d 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -28,6 +28,10 @@ 
 #include "net.h"
 #include "qemu-char.h"
 #include "qemu-common.h"
+#include "qdict.h"
+#include "qstring.h"
+#include "qbool.h"
+#include "qint.h"
 #include "qemu-error.h"
 #include "qemu-option.h"
 #include "qemu_socket.h"
@@ -266,11 +270,14 @@  static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
     }
 
     nc = qemu_new_net_client(&net_dgram_socket_info, vlan, NULL, model, name);
-
-    snprintf(nc->info_str, sizeof(nc->info_str),
-	    "socket: fd=%d (%s mcast=%s:%d)",
-	    fd, is_connected ? "cloned" : "",
-	    inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+    
+    nc->info_dict = qdict_new();
+    qdict_put(nc->info_dict, "fd", qint_from_int(fd));
+    qdict_put(nc->info_dict, "host",
+        qstring_from_str(inet_ntoa(saddr.sin_addr)));
+    qdict_put(nc->info_dict, "service", qint_from_int(ntohs(saddr.sin_port)));
+    qdict_put(nc->info_dict, "family", qstring_from_str("ipv4"));
+    qdict_put(nc->info_dict, "cloned", qbool_from_int(is_connected));
 
     s = DO_UPCAST(NetSocketState, nc, nc);
 
@@ -306,8 +313,9 @@  static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
     NetSocketState *s;
 
     nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name);
+    nc->info_dict = qdict_new();
 
-    snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
+    qdict_put(nc->info_dict, "fd", qint_from_int(fd));
 
     s = DO_UPCAST(NetSocketState, nc, nc);
 
@@ -366,9 +374,12 @@  static void net_socket_accept(void *opaque)
     if (!s1) {
         closesocket(fd);
     } else {
-        snprintf(s1->nc.info_str, sizeof(s1->nc.info_str),
-                 "socket: connection from %s:%d",
-                 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+        s1->nc.info_dict = qdict_new();
+
+        qdict_put(s1->nc.info_dict, "host",
+            qstring_from_str(inet_ntoa(saddr.sin_addr)));
+        qdict_put(s1->nc.info_dict, "service", qint_from_int(ntohs(saddr.sin_port)));
+        qdict_put(s1->nc.info_dict, "family", qstring_from_str("ipv4"));
     }
 }
 
@@ -459,9 +470,12 @@  static int net_socket_connect_init(VLANState *vlan,
     s = net_socket_fd_init(vlan, 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));
+
+    s->nc.info_dict = qdict_new();
+    qdict_put(s->nc.info_dict, "host",
+        qstring_from_str(inet_ntoa(saddr.sin_addr)));
+    qdict_put(s->nc.info_dict, "service", qint_from_int(ntohs(saddr.sin_port)));
+    qdict_put(s->nc.info_dict, "family", qstring_from_str("ipv4"));
     return 0;
 }
 
@@ -487,10 +501,13 @@  static int net_socket_mcast_init(VLANState *vlan,
         return -1;
 
     s->dgram_dst = saddr;
+    
+    s->nc.info_dict = qdict_new();
+    qdict_put(s->nc.info_dict, "host",
+        qstring_from_str(inet_ntoa(saddr.sin_addr)));
+    qdict_put(s->nc.info_dict, "service", qint_from_int(ntohs(saddr.sin_port)));
+    qdict_put(s->nc.info_dict, "family", qstring_from_str("ipv4"));
 
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: mcast=%s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
     return 0;
 
 }