summary refs log tree commit diff stats
path: root/src/common/network.c
diff options
context:
space:
mode:
authorTingPing <tingping@tingping.se>2014-12-28 06:37:25 -0500
committerTingPing <tingping@tingping.se>2014-12-28 06:44:44 -0500
commit83032b1aa3c3e5910c5cfd3e0ea1d25827f56475 (patch)
tree9be32a04d3070eac82177e11d182dad40a63baa7 /src/common/network.c
parentc4cb1b25ec06a5b0cb718c6f8e74630df9a9bc64 (diff)
Use glib for all allocations
- Removes need to check for malloc failure
- Removes need for NULL checks on free
- Adds checks for integer overflows
- Removes some extra memset calls
- Removes chance of mixing libc and glib malloc/free
Diffstat (limited to 'src/common/network.c')
-rw-r--r--src/common/network.c43
1 files changed, 19 insertions, 24 deletions
diff --git a/src/common/network.c b/src/common/network.c
index d726bd24..f5cfce58 100644
--- a/src/common/network.c
+++ b/src/common/network.c
@@ -69,18 +69,13 @@ net_store_destroy (netstore * ns)
 	if (ns->ip6_hostent)
 		freeaddrinfo (ns->ip6_hostent);
 #endif
-	free (ns);
+	g_free (ns);
 }
 
 netstore *
 net_store_new (void)
 {
-	netstore *ns;
-
-	ns = malloc (sizeof (netstore));
-	memset (ns, 0, sizeof (netstore));
-
-	return ns;
+	return g_new0 (netstore, 1);
 }
 
 #ifndef USE_IPV6
@@ -120,8 +115,8 @@ net_resolve (netstore * ns, char *hostname, int port, char **real_host)
 	ns->addr.sin_port = htons (port);
 	ns->addr.sin_family = AF_INET;
 
-	*real_host = strdup (ns->ip4_hostent->h_name);
-	return strdup (inet_ntoa (ns->addr.sin_addr));
+	*real_host = g_strdup (ns->ip4_hostent->h_name);
+	return g_strdup (inet_ntoa (ns->addr.sin_addr));
 }
 
 int
@@ -232,11 +227,11 @@ net_resolve (netstore * ns, char *hostname, int port, char **real_host)
 					 ipstring, sizeof (ipstring), NULL, 0, NI_NUMERICHOST);
 
 	if (ns->ip6_hostent->ai_canonname)
-		*real_host = strdup (ns->ip6_hostent->ai_canonname);
+		*real_host = g_strdup (ns->ip6_hostent->ai_canonname);
 	else
-		*real_host = strdup (hostname);
+		*real_host = g_strdup (hostname);
 
-	return strdup (ipstring);
+	return g_strdup (ipstring);
 }
 
 /* the only thing making this interface unclean, this shitty sok4, sok6 business */
@@ -310,15 +305,15 @@ net_store_fill_any (netstore *ns)
 	struct sockaddr_in *sin;
 
 	ai = ns->ip6_hostent;
-	if (!ai) {
-		ai = malloc (sizeof (struct addrinfo));
-		memset (ai, 0, sizeof (struct addrinfo));
+	if (ai == NULL)
+	{
+		ai = g_new0 (struct addrinfo, 1);
 		ns->ip6_hostent = ai;
 	}
 	sin = (struct sockaddr_in *)ai->ai_addr;
-	if (!sin) {
-		sin = malloc (sizeof (struct sockaddr_in));
-		memset (sin, 0, sizeof (struct sockaddr_in));
+	if (sin == NULL)
+	{
+		sin = g_new0 (struct sockaddr_in, 1);
 		ai->ai_addr = (struct sockaddr *)sin;
 	}
 	ai->ai_family = AF_INET;
@@ -336,15 +331,15 @@ net_store_fill_v4 (netstore *ns, guint32 addr, int port)
 	struct sockaddr_in *sin;
 
 	ai = ns->ip6_hostent;
-	if (!ai) {
-		ai = malloc (sizeof (struct addrinfo));
-		memset (ai, 0, sizeof (struct addrinfo));
+	if (ai == NULL)
+	{
+		ai = g_new0 (struct addrinfo, 1);
 		ns->ip6_hostent = ai;
 	}
 	sin = (struct sockaddr_in *)ai->ai_addr;
-	if (!sin) {
-		sin = malloc (sizeof (struct sockaddr_in));
-		memset (sin, 0, sizeof (struct sockaddr_in));
+	if (sin == NULL)
+	{
+		sin = g_new0 (struct sockaddr_in, 1);
 		ai->ai_addr = (struct sockaddr *)sin;
 	}
 	ai->ai_family = AF_INET;