[ccan] freeaddrinfo() in ccan/net

Rusty Russell rusty at rustcorp.com.au
Thu Jul 24 09:54:56 EST 2014


Jeremy Visser <jeremy at visser.name> writes:

> Hi Rusty,
>
> First off, can I just say I'm a terrible C programmer, so there's probably something obvious I've missed.
>
> But I was just reading your code at:
>
> <http://git.ozlabs.org/?p=ccan;a=blob_plain;f=ccan/net/net.c;hb=HEAD>
> <http://git.ozlabs.org/?p=ccan;a=blob_plain;f=ccan/net/_info;hb=HEAD>
>
> In particular, the example program supplied in the comments of the
> _info file assigns the output of net_client_lookup() to a "struct
> addrinfo *", but later on it never actually calls freeaddrinfo() to
> free that structure.

Hi Jeremy,

It doesn't, no.  It's a trivial example, but it would be a better
example if it did.

As the documentation for net_client_lookup() says, you should use
freeaddrinfo(): I've added that.

Thanks!
Rusty.

===
From: Rusty Russell <rusty at rustcorp.com.au>
Date: Thu, 24 Jul 2014 09:20:21 +0930
Subject: [PATCH] net: use freeaddrinfo() in _info example.

In fact, almost everyone will want to do this, so include the required
headers in net.h.  This makes usage simpler.

Reported-by: Jeremy Visser <jeremy at visser.name>
Signed-off-by: Rusty Russell <rusty at rustcorp.com.au>

diff --git a/ccan/net/_info b/ccan/net/_info
index 5ac83f9..cfa58f8 100644
--- a/ccan/net/_info
+++ b/ccan/net/_info
@@ -15,8 +15,6 @@
  *
  * Example:
  *	#include <ccan/net/net.h>
- *	#include <sys/types.h>
- *	#include <sys/socket.h>
  *	#include <netinet/in.h>
  *	#include <stdio.h>
  *	#include <err.h>
@@ -49,6 +47,7 @@
  *		fd = net_connect(addr);
  *		if (fd < 0)
  *			err(1, "Failed to connect to %s", dest);
+ *		freeaddrinfo(addr);
  *	
  *		if (getsockname(fd, &u.s, &slen) == 0)
  *			printf("Connected via %s\n",
diff --git a/ccan/net/net.c b/ccan/net/net.c
index 7867054..61efd5b 100644
--- a/ccan/net/net.c
+++ b/ccan/net/net.c
@@ -1,10 +1,7 @@
 /* Licensed under BSD-MIT - see LICENSE file for details */
 #include <ccan/net/net.h>
 #include <ccan/noerr/noerr.h>
-#include <sys/types.h>
-#include <sys/socket.h>
 #include <poll.h>
-#include <netdb.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
diff --git a/ccan/net/net.h b/ccan/net/net.h
index 5e02fc3..05d552d 100644
--- a/ccan/net/net.h
+++ b/ccan/net/net.h
@@ -1,6 +1,9 @@
 /* Licensed under BSD-MIT - see LICENSE file for details */
 #ifndef CCAN_NET_H
 #define CCAN_NET_H
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
 #include <stdbool.h>
 
 struct pollfd;
@@ -16,10 +19,7 @@ struct pollfd;
  * of results, or NULL on error.  You should use freeaddrinfo() to free it.
  *
  * Example:
- *	#include <sys/types.h>
- *	#include <sys/socket.h>
  *	#include <stdio.h>
- *	#include <netdb.h>
  *	#include <poll.h>
  *	#include <err.h>
  *	...
@@ -124,10 +124,7 @@ void net_connect_abort(struct pollfd *pfds);
  * of results, or NULL on error.  You should use freeaddrinfo() to free it.
  *
  * Example:
- *	#include <sys/types.h>
- *	#include <sys/socket.h>
  *	#include <stdio.h>
- *	#include <netdb.h>
  *	#include <err.h>
  *	...
  *	struct addrinfo *addr;


More information about the ccan mailing list