@@ -1,19 +1,22 @@
11// C-Side of the Idris network library
22// (C) Simon Fowler, 2014
33// MIT Licensed. Have fun!
4−#include <errno.h>
5−#include <netdb.h>
6−#include <stdbool.h>
7−#include <stdlib.h>
8−#include <stdio.h>
9−#include <string.h>
10−#include <sys/types.h>
11−#include <sys/socket.h>
12−#include <netinet/in.h>
13−#include <arpa/inet.h>
14−
154#include "idris_net.h"
165
6+void buf_htonl(void* buf, int len) {
7+ int* buf_i = (int*) buf;
8+ for (int i = 0; i < (len / sizeof(int)) + 1; i++) {
9+ buf_i[i] = htonl(buf_i[i]);
10+ }
11+}
12+
13+void buf_ntohl(void* buf, int len) {
14+ int* buf_i = (int*) buf;
15+ for (int i = 0; i < (len / sizeof(int)) + 1; i++) {
16+ buf_i[i] = ntohl(buf_i[i]);
17+ }
18+}
19+
1720void* idrnet_malloc(int size) {
1821 return malloc(size);
1922}
@@ -22,9 +25,12 @@ void idrnet_free(void* ptr) {
2225 free(ptr);
2326}
2427
25−int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
28+// We call this from quite a few functions. Given a textual host and an int port,
29+// populates a struct addrinfo.
30+int idrnet_getaddrinfo(struct addrinfo** address_res, char* host, int port,
31+ int family, int socket_type) {
32+
2633 struct addrinfo hints;
27− struct addrinfo* address_res;
2834 // Convert port into string
2935 char str_port[8];
3036 sprintf(str_port, "%d", port);
@@ -38,43 +44,42 @@ int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
3844 // then we want to instruct the C library to fill in the IP automatically
3945 if (strlen(host) == 0) {
4046 hints.ai_flags = AI_PASSIVE; // fill in IP automatically
41− }
47+ return getaddrinfo(NULL, str_port, &hints, address_res);
48+ }
49+ return getaddrinfo(host, str_port, &hints, address_res);
4250
43− int addr_res = getaddrinfo(host, str_port, &hints, &address_res);
51+}
52+
53+int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
54+ struct addrinfo* address_res;
55+ int addr_res = idrnet_getaddrinfo(&address_res, host, port, family, socket_type);
4456 if (addr_res == -1) {
57+ //printf("Lib err: bind getaddrinfo\n");
4558 return -1;
4659 }
4760
4861 int bind_res = bind(sockfd, address_res->ai_addr, address_res->ai_addrlen);
4962 if (bind_res == -1) {
63+ //freeaddrinfo(address_res);
64+ //printf("Lib err: bind\n");
5065 return -1;
5166 }
52−
5367 return 0;
5468}
5569
5670int idrnet_connect(int sockfd, int family, int socket_type, char* host, int port) {
57− char str_port[8];
58− sprintf(str_port, "%d", port);
59− struct addrinfo hints;
6071 struct addrinfo* remote_host;
61−
62− // Set up hints structure for getaddrinfo
63− memset(&hints, 0, sizeof(hints));
64− hints.ai_family = family;
65− hints.ai_socktype = socket_type;
66−
67− // Get info about the remote host (DNS lookup etc)
68− int addr_res = getaddrinfo(host, str_port, &hints, &remote_host);
72+ int addr_res = idrnet_getaddrinfo(&remote_host, host, port, family, socket_type);
6973 if (addr_res == -1) {
7074 return -1;
7175 }
7276
7377 int connect_res = connect(sockfd, remote_host->ai_addr, remote_host->ai_addrlen);
7478 if (connect_res == -1) {
79+ freeaddrinfo(remote_host);
7580 return -1;
7681 }
77−
82+ freeaddrinfo(remote_host);
7883 return 0;
7984}
8085
@@ -88,9 +93,15 @@ char* idrnet_sockaddr_ipv4(void* sockaddr) {
8893 struct sockaddr_in* addr = (struct sockaddr_in*) sockaddr;
8994 char* ip_addr = (char*) malloc(sizeof(char) * INET_ADDRSTRLEN);
9095 inet_ntop(AF_INET, &(addr->sin_addr), ip_addr, INET_ADDRSTRLEN);
96+ //printf("Lib: ip_addr: %s\n", ip_addr);
9197 return ip_addr;
9298}
9399
100+int idrnet_sockaddr_ipv4_port(void* sockaddr) {
101+ struct sockaddr_in* addr = (struct sockaddr_in*) addr;
102+ return ((int) ntohs(addr->sin_port));
103+}
104+
94105void* idrnet_create_sockaddr() {
95106 return malloc(sizeof(struct sockaddr_storage));
96107}
@@ -108,33 +119,43 @@ int idrnet_send(int sockfd, char* data) {
108119}
109120
110121int idrnet_send_buf(int sockfd, void* data, int len) {
111− return send(sockfd, data, len, 0);
122+ void* buf_cpy = malloc(len);
123+ memset(buf_cpy, 0, len);
124+ memcpy(buf_cpy, data, len);
125+ buf_htonl(buf_cpy, len);
126+ int res = send(sockfd, buf_cpy, len, 0);
127+ free(buf_cpy);
128+ return res;
112129}
113130
114131void* idrnet_recv(int sockfd, int len) {
115132 idrnet_recv_result* res_struct =
116133 (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
117−
118134 char* buf = malloc(len + 1);
135+ memset(buf, 0, len + 1);
119136 int recv_res = recv(sockfd, buf, len, 0);
120137 res_struct->result = recv_res;
121138
122139 if (recv_res > 0) { // Data was received
123140 buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
124141 }
125− res_struct->payload = (void*) buf;
142+ res_struct->payload = buf;
126143 return (void*) res_struct;
127144}
128145
129146int idrnet_recv_buf(int sockfd, void* buf, int len) {
130− return recv(sockfd, buf, len, 0);
147+ int recv_res = recv(sockfd, buf, len, 0);
148+ if (recv_res != -1) {
149+ buf_ntohl(buf, len);
150+ }
151+ return recv_res;
131152}
132153
133154int idrnet_get_recv_res(void* res_struct) {
134155 return (((idrnet_recv_result*) res_struct)->result);
135156}
136157
137−void* idrnet_get_recv_payload(void* res_struct) {
158+char* idrnet_get_recv_payload(void* res_struct) {
138159 return (((idrnet_recv_result*) res_struct)->payload);
139160}
140161
@@ -151,3 +172,137 @@ int idrnet_errno() {
151172 return errno;
152173}
153174
175+
176+int idrnet_sendto(int sockfd, char* data, char* host, int port, int family) {
177+
178+ struct addrinfo* remote_host;
179+ int addr_res = idrnet_getaddrinfo(&remote_host, host, port, family, SOCK_DGRAM);
180+ if (addr_res == -1) {
181+ return -1;
182+ }
183+
184+ int send_res = sendto(sockfd, data, strlen(data), 0,
185+ remote_host->ai_addr, remote_host->ai_addrlen);
186+ freeaddrinfo(remote_host);
187+ return send_res;
188+}
189+
190+int idrnet_sendto_buf(int sockfd, void* buf, int buf_len, char* host, int port, int family) {
191+
192+ struct addrinfo* remote_host;
193+ int addr_res = idrnet_getaddrinfo(&remote_host, host, port, family, SOCK_DGRAM);
194+ if (addr_res == -1) {
195+ //printf("lib err: sendto getaddrinfo \n");
196+ return -1;
197+ }
198+
199+ buf_htonl(buf, buf_len);
200+
201+ int send_res = sendto(sockfd, buf, buf_len, 0,
202+ remote_host->ai_addr, remote_host->ai_addrlen);
203+ if (send_res == -1) {
204+ perror("lib err: sendto \n");
205+ }
206+ //freeaddrinfo(remote_host);
207+ return send_res;
208+}
209+
210+
211+
212+void* idrnet_recvfrom(int sockfd, int len) {
213+/*
214+ * int recvfrom(int sockfd, void *buf, int len, unsigned int flags,
215+ struct sockaddr *from, int *fromlen);
216+*/
217+ // Allocate the required structures, and nuke them
218+ struct sockaddr_storage* remote_addr =
219+ (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
220+ char* buf = (char*) malloc(len + 1);
221+ idrnet_recvfrom_result* ret =
222+ (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
223+ memset(remote_addr, 0, sizeof(struct sockaddr_storage));
224+ memset(buf, 0, len + 1);
225+ memset(ret, 0, sizeof(idrnet_recvfrom_result));
226+ socklen_t fromlen = sizeof(struct sockaddr_storage);
227+
228+ int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
229+ ret->result = recv_res;
230+ // Check for failure...
231+ if (recv_res == -1) {
232+ free(buf);
233+ free(remote_addr);
234+ } else {
235+ // If data was received, process and populate
236+ ret->result = recv_res;
237+ ret->remote_addr = remote_addr;
238+ // Ensure the last byte is NULL, since in this mode we're sending strings
239+ buf[len] = 0x00;
240+ ret->payload = (void*) buf;
241+ }
242+
243+ return ret;
244+}
245+
246+
247+void* idrnet_recvfrom_buf(int sockfd, void* buf, int len) {
248+ // Allocate the required structures, and nuke them
249+ struct sockaddr_storage* remote_addr =
250+ (struct sockaddr_storage*) malloc(sizeof(struct sockaddr_storage));
251+ idrnet_recvfrom_result* ret =
252+ (idrnet_recvfrom_result*) malloc(sizeof(idrnet_recvfrom_result));
253+ memset(remote_addr, 0, sizeof(struct sockaddr_storage));
254+ memset(ret, 0, sizeof(idrnet_recvfrom_result));
255+ socklen_t fromlen = 0;
256+
257+ int recv_res = recvfrom(sockfd, buf, len, 0, (struct sockaddr*) remote_addr, &fromlen);
258+ // Check for failure... But don't free the buffer! Not our job.
259+ ret->result = recv_res;
260+ if (recv_res == -1) {
261+ free(remote_addr);
262+ }
263+ // Payload will be NULL -- since it's been put into the user-specified buffer. We
264+ // still need the return struct to get our hands on the remote address, though.
265+ if (recv_res > 0) {
266+ buf_ntohl(buf, len);
267+ ret->payload = NULL;
268+ ret->remote_addr = remote_addr;
269+ }
270+ return ret;
271+}
272+
273+int idrnet_get_recvfrom_res(void* res_struct) {
274+ return (((idrnet_recvfrom_result*) res_struct)->result);
275+}
276+
277+char* idrnet_get_recvfrom_payload(void* res_struct) {
278+ return (((idrnet_recvfrom_result*) res_struct)->payload);
279+}
280+
281+void* idrnet_get_recvfrom_sockaddr(void* res_struct) {
282+ idrnet_recvfrom_result* recv_struct = (idrnet_recvfrom_result*) res_struct;
283+ return recv_struct->remote_addr;
284+}
285+
286+int idrnet_get_recvfrom_port(void* res_struct) {
287+ idrnet_recvfrom_result* recv_struct = (idrnet_recvfrom_result*) res_struct;
288+ if (recv_struct->remote_addr != NULL) {
289+ struct sockaddr_in* remote_addr_in =
290+ (struct sockaddr_in*) recv_struct->remote_addr;
291+ return ((int) ntohs(remote_addr_in->sin_port));
292+ }
293+ return -1;
294+}
295+
296+void idrnet_free_recvfrom_struct(void* res_struct) {
297+ idrnet_recvfrom_result* recv_struct = (idrnet_recvfrom_result*) res_struct;
298+ if (recv_struct != NULL) {
299+ if (recv_struct->payload != NULL) {
300+ free(recv_struct->payload);
301+ }
302+ if (recv_struct->remote_addr != NULL) {
303+ free(recv_struct->remote_addr);
304+ }
305+ }
306+}
307+
308+