diff mbox

[10/11] libnet: Make sure to close sockets when we're done

Message ID 1473450729-19359-11-git-send-email-thuth@redhat.com
State Superseded
Headers show

Commit Message

Thomas Huth Sept. 9, 2016, 7:52 p.m. UTC
While libnet was linked to the net-snk, it did not matter
since the whole stack was completely reloaded each time.
But now that we've got libnet linked to Paflof, we've got
to make sure to properly release the resources that we've
allocated before.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 lib/libnet/netload.c | 6 ++++++
 lib/libnet/ping.c    | 4 ++++
 2 files changed, 10 insertions(+)
diff mbox

Patch

diff --git a/lib/libnet/netload.c b/lib/libnet/netload.c
index 2d1cea0..a62f040 100644
--- a/lib/libnet/netload.c
+++ b/lib/libnet/netload.c
@@ -10,6 +10,7 @@ 
  *     IBM Corporation - initial implementation
  *****************************************************************************/
 
+#include <unistd.h>
 #include <tftp.h>
 #include <ethernet.h>
 #include <dhcp.h>
@@ -566,6 +567,7 @@  netboot(int argc, char *argv[])
 		bootmsg_error(0x3001, &buf[7]);
 
 		write_mm_log(buf, strlen(buf), 0x91);
+		close(fn_ip.fd);
 		return -101;
 	}
 
@@ -590,6 +592,7 @@  netboot(int argc, char *argv[])
 		bootmsg_error(0x3002, &buf[7]);
 
 		write_mm_log(buf, strlen(buf), 0x91);
+		close(fn_ip.fd);
 		return -102;
 	}
 	if (rc == -4 || rc == -3) {
@@ -597,6 +600,7 @@  netboot(int argc, char *argv[])
 		bootmsg_error(0x3008, &buf[7]);
 
 		write_mm_log(buf, strlen(buf), 0x91);
+		close(fn_ip.fd);
 		return -107;
 	}
 
@@ -635,6 +639,8 @@  netboot(int argc, char *argv[])
 	if(obp_tftp_args.ip_init == IP_INIT_DHCP)
 		dhcp_send_release(fn_ip.fd);
 
+	close(fn_ip.fd);
+
 	if (rc > 0) {
 		printf("  TFTP: Received %s (%d KBytes)\n", fn_ip.filename,
 		       rc / 1024);
diff --git a/lib/libnet/ping.c b/lib/libnet/ping.c
index d35e221..4b7e95c 100644
--- a/lib/libnet/ping.c
+++ b/lib/libnet/ping.c
@@ -10,6 +10,7 @@ 
  *     IBM Corporation - initial implementation
  *****************************************************************************/
 
+#include <unistd.h>
 #include <ipv4.h>
 #include <dhcp.h>
 #include <ethernet.h>
@@ -164,6 +165,7 @@  int ping(char *args_fs, int alen)
 
 		if (arp_failed == -1) {
 			printf("\n  DHCP: Could not get ip address\n");
+			close(fn_ip.fd);
 			return -1;
 		}
 
@@ -210,10 +212,12 @@  int ping(char *args_fs, int alen)
 		receive_ether(fd_device);
 		if(pong_ipv4() == 0) {
 			printf("success\n");
+			close(fn_ip.fd);
 			return 0;
 		}
 	}
 
 	printf("failed\n");
+	close(fn_ip.fd);
 	return -1;
 }