@@ -1,7 +1,7 @@
11// C-Side of the Idris network library
22// (C) Simon Fowler, 2014
33// MIT Licensed. Have fun!
4−#include "idris_net.h"
4+#include "../idris_net.h"
55
66#include <errno.h>
77#include <stdbool.h>
@@ -11,6 +11,12 @@
1111#include <Winsock2.h>
1212#include <Ws2tcpip.h>
1313
14+
15+// inet_ntop isn't defined in the old mingw delivered with GHC,
16+// so put the prototype here, cribbed from a newer version of it
17+WINSOCK_API_LINKAGE LPCSTR WSAAPI InetNtopA(INT Family, PVOID pAddr,
18+ LPSTR pStringBuf, size_t StringBufSize);
19+
1420static int socket_inited = 0;
1521static WSADATA wsa_data;
1622
@@ -22,12 +28,17 @@ void idrnet_free(void* ptr) {
2228 free(ptr);
2329}
2430
25−static int check_init()
31+static void clean_sockets(void) {
32+ WSACleanup();
33+}
34+
35+static int check_init(void)
2636{
2737 if (!socket_inited) {
2838 int result = WSAStartup(MAKEWORD(2, 2), &wsa_data);
2939 if (result == NO_ERROR) {
3040 socket_inited = 1;
41+ atexit(clean_sockets);
3142 }
3243 }
3344 return socket_inited;
@@ -104,7 +115,7 @@ int idrnet_sockaddr_family(void* sockaddr) {
104115char* idrnet_sockaddr_ipv4(void* sockaddr) {
105116 struct sockaddr_in* addr = (struct sockaddr_in*) sockaddr;
106117 char* ip_addr = (char*) malloc(sizeof(char) * INET_ADDRSTRLEN);
107− inet_ntop(AF_INET, &(addr->sin_addr), ip_addr, INET_ADDRSTRLEN);
118+ InetNtopA(AF_INET, &(addr->sin_addr), ip_addr, INET_ADDRSTRLEN);
108119 return ip_addr;
109120}
110121
@@ -139,7 +150,7 @@ int idrnet_send_buf(int sockfd, void* data, int len) {
139150
140151void* idrnet_recv(int sockfd, int len) {
141152 if (!check_init()) {
142− return -1;
153+ return NULL;
143154 }
144155 idrnet_recv_result* res_struct =
145156 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));