@@ -38,15 +38,15 @@ void idrnet_free(void* ptr) {
40// We call this from quite a few functions. Given a textual host and an int port,
40// We call this from quite a few functions. Given a textual host and an int port,
41−// populates a struct addrinfo.
41+// populates a struct addrinfo.
42−int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
42+int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
43 int family, int socket_type) {
43 int family, int socket_type) {
45 struct addrinfo hints;
45 struct addrinfo hints;
46 // Convert port into string
46 // Convert port into string
47 char str_port[8];
47 char str_port[8];
48 sprintf(str_port, "%d", port);
48 sprintf(str_port, "%d", port);
50 // Set up hints structure
50 // Set up hints structure
51 memset(&hints, 0, sizeof(hints)); // zero out hints
51 memset(&hints, 0, sizeof(hints)); // zero out hints
52 hints.ai_family = family;
52 hints.ai_family = family;
@@ -57,7 +57,7 @@ int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
57 if (strlen(host) == 0) {
57 if (strlen(host) == 0) {
58 hints.ai_flags = AI_PASSIVE; // fill in IP automatically
58 hints.ai_flags = AI_PASSIVE; // fill in IP automatically
59 return getaddrinfo(NULL, str_port, &hints, address_res);
59 return getaddrinfo(NULL, str_port, &hints, address_res);
61 return getaddrinfo(host, str_port, &hints, address_res);
61 return getaddrinfo(host, str_port, &hints, address_res);
@@ -75,7 +75,7 @@ int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
75 //freeaddrinfo(address_res);
75 //freeaddrinfo(address_res);
76 //printf("Lib err: bind\n");
76 //printf("Lib err: bind\n");
77 return -1;
77 return -1;
@@ -111,7 +111,7 @@ char* idrnet_sockaddr_ipv4(void* sockaddr) {
112int idrnet_sockaddr_ipv4_port(void* sockaddr) {
112int idrnet_sockaddr_ipv4_port(void* sockaddr) {
113 struct sockaddr_in* addr = (struct sockaddr_in*) sockaddr;
113 struct sockaddr_in* addr = (struct sockaddr_in*) sockaddr;
114− return ((int) ntohs(addr->sin_port));
114+ return ((int) ntohs(addr->sin_port));
117void* idrnet_create_sockaddr() {
117void* idrnet_create_sockaddr() {
@@ -141,13 +141,13 @@ int idrnet_send_buf(int sockfd, void* data, int len) {
143void* idrnet_recv(int sockfd, int len) {
143void* idrnet_recv(int sockfd, int len) {
144− idrnet_recv_result* res_struct =
144+ idrnet_recv_result* res_struct =
145 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
145 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
146 char* buf = malloc(len + 1);
146 char* buf = malloc(len + 1);
147 memset(buf, 0, len + 1);
147 memset(buf, 0, len + 1);
148 int recv_res = recv(sockfd, buf, len, 0);
148 int recv_res = recv(sockfd, buf, len, 0);
149 res_struct->result = recv_res;
149 res_struct->result = recv_res;
151 if (recv_res > 0) { // Data was received
151 if (recv_res > 0) { // Data was received
152 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
152 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
@@ -172,7 +172,7 @@ char* idrnet_get_recv_payload(void* res_struct) {
174void idrnet_free_recv_struct(void* res_struct) {
174void idrnet_free_recv_struct(void* res_struct) {
175− idrnet_recv_result* i_res_struct =
175+ idrnet_recv_result* i_res_struct =
176 (idrnet_recv_result*) res_struct;
176 (idrnet_recv_result*) res_struct;
177 if (i_res_struct->payload != NULL) {
177 if (i_res_struct->payload != NULL) {
178 free(i_res_struct->payload);
178 free(i_res_struct->payload);
@@ -191,9 +191,9 @@ int idrnet_sendto(int sockfd, char* data, char* host, int port, int family) {
191 int addr_res = idrnet_getaddrinfo(&remote_host, host, port, family, SOCK_DGRAM);
191 int addr_res = idrnet_getaddrinfo(&remote_host, host, port, family, SOCK_DGRAM);
192 if (addr_res == -1) {
192 if (addr_res == -1) {
193 return -1;
193 return -1;
196− int send_res = sendto(sockfd, data, strlen(data), 0,
196+ int send_res = sendto(sockfd, data, strlen(data), 0,
197 remote_host->ai_addr, remote_host->ai_addrlen);
197 remote_host->ai_addr, remote_host->ai_addrlen);
198 freeaddrinfo(remote_host);
198 freeaddrinfo(remote_host);
199 return send_res;
199 return send_res;
@@ -206,11 +206,11 @@ int idrnet_sendto_buf(int sockfd, void* buf, int buf_len, char* host, int port,
206 if (addr_res == -1) {
206 if (addr_res == -1) {
207 //printf("lib err: sendto getaddrinfo \n");
207 //printf("lib err: sendto getaddrinfo \n");
208 return -1;
208 return -1;
211 buf_htonl(buf, buf_len);
211 buf_htonl(buf, buf_len);
213− int send_res = sendto(sockfd, buf, buf_len, 0,
213+ int send_res = sendto(sockfd, buf, buf_len, 0,
214 remote_host->ai_addr, remote_host->ai_addrlen);
214 remote_host->ai_addr, remote_host->ai_addrlen);
215 if (send_res == -1) {
215 if (send_res == -1) {
216 perror("lib err: sendto \n");
216 perror("lib err: sendto \n");
@@ -224,13 +224,13 @@ int idrnet_sendto_buf(int sockfd, void* buf, int buf_len, char* host, int port,
224void* idrnet_recvfrom(int sockfd, int len) {
224void* idrnet_recvfrom(int sockfd, int len) {
226 * int recvfrom(int sockfd, void *buf, int len, unsigned int flags,
226 * int recvfrom(int sockfd, void *buf, int len, unsigned int flags,
227− struct sockaddr *from, int *fromlen);
227+ struct sockaddr *from, int *fromlen);
229 // Allocate the required structures, and nuke them
229 // Allocate the required structures, and nuke them
230− struct sockaddr_storage* remote_addr =
230+ struct sockaddr_storage* remote_addr =
231 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
231 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
232 char* buf = (char*) malloc(len + 1);
232 char* buf = (char*) malloc(len + 1);
233− idrnet_recvfrom_result* ret =
233+ idrnet_recvfrom_result* ret =
234 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
234 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
235 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
235 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
236 memset(buf, 0, len + 1);
236 memset(buf, 0, len + 1);
@@ -240,7 +240,7 @@ void* idrnet_recvfrom(int sockfd, int len) {
240 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
240 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
241 ret->result = recv_res;
241 ret->result = recv_res;
242 // Check for failure...
242 // Check for failure...
243− if (recv_res == -1) {
243+ if (recv_res == -1) {
244 free(buf);
244 free(buf);
245 free(remote_addr);
245 free(remote_addr);
@@ -255,12 +255,11 @@ void* idrnet_recvfrom(int sockfd, int len) {
255 return ret;
255 return ret;
259void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
258void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
260 // Allocate the required structures, and nuke them
259 // Allocate the required structures, and nuke them
261− struct sockaddr_storage* remote_addr =
260+ struct sockaddr_storage* remote_addr =
262 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
261 (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
263− idrnet_recvfrom_result* ret =
262+ idrnet_recvfrom_result* ret =
264 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
263 (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
265 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
264 memset(remote_addr, 0, sizeof(struct sockaddr_storage));
266 memset(ret, 0, sizeof(idrnet_recvfrom_result));
265 memset(ret, 0, sizeof(idrnet_recvfrom_result));
@@ -269,7 +268,7 @@ void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
269 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
268 int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
270 // Check for failure... But don't free the buffer! Not our job.
269 // Check for failure... But don't free the buffer! Not our job.
271 ret->result = recv_res;
270 ret->result = recv_res;
272− if (recv_res == -1) {
271+ if (recv_res == -1) {
273 free(remote_addr);
272 free(remote_addr);
275 // Payload will be NULL -- since it's been put into the user-specified buffer. We
274 // Payload will be NULL -- since it's been put into the user-specified buffer. We
@@ -298,7 +297,7 @@ void* idrnet_get_recvfrom_sockaddr(void* res_struct) {
298int idrnet_get_recvfrom_port(void* res_struct) {
297int idrnet_get_recvfrom_port(void* res_struct) {
299 idrnet_recvfrom_result* recv_struct = (idrnet_recvfrom_result*) res_struct;
298 idrnet_recvfrom_result* recv_struct = (idrnet_recvfrom_result*) res_struct;
300 if (recv_struct->remote_addr != NULL) {
299 if (recv_struct->remote_addr != NULL) {
301− struct sockaddr_in* remote_addr_in =
300+ struct sockaddr_in* remote_addr_in =
302 (struct sockaddr_in*) recv_struct->remote_addr;
301 (struct sockaddr_in*) recv_struct->remote_addr;
303 return ((int) ntohs(remote_addr_in->sin_port));
302 return ((int) ntohs(remote_addr_in->sin_port));
@@ -316,5 +315,3 @@ void idrnet_free_recvfrom_struct(void* res_struct) {