@@ -1,200 +0,0 @@
1−// C-Side of the Idris network library
2−// (C) Simon Fowler, 2014
3−// MIT Licensed. Have fun!
4−#include "../idris_net.h"
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);
20−static int socket_inited = 0;
21−static WSADATA wsa_data;
23−void* idrnet_malloc(int size) {
27−void idrnet_free(void* ptr) {
31−static void clean_sockets(void) {
35−static int check_init(void)
37− if (!socket_inited) {
38− int result = WSAStartup(MAKEWORD(2, 2), &wsa_data);
39− if (result == NO_ERROR) {
41− atexit(clean_sockets);
44− return socket_inited;
47−int idrnet_bind(int sockfd, int family, int socket_type, char* host, int port) {
48− struct addrinfo hints;
49− struct addrinfo* address_res;
50− // Convert port into string
52− sprintf(str_port, "%d", port);
57− // Set up hints structure
58− memset(&hints, 0, sizeof(hints)); // zero out hints
59− hints.ai_family = family;
60− hints.ai_socktype = socket_type;
62− // If the length of the hostname is 0 (i.e, it was set to Nothing in Idris)
63− // then we want to instruct the C library to fill in the IP automatically
64− if (strlen(host) == 0) {
65− hints.ai_flags = AI_PASSIVE; // fill in IP automatically
68− int addr_res = getaddrinfo(host, str_port, &hints, &address_res);
69− if (addr_res == -1) {
73− int bind_res = bind(sockfd, address_res->ai_addr, address_res->ai_addrlen);
74− if (bind_res == -1) {
81−int idrnet_connect(int sockfd, int family, int socket_type, char* host, int port) {
83− sprintf(str_port, "%d", port);
84− struct addrinfo hints;
85− struct addrinfo* remote_host;
90− // Set up hints structure for getaddrinfo
91− memset(&hints, 0, sizeof(hints));
92− hints.ai_family = family;
93− hints.ai_socktype = socket_type;
95− // Get info about the remote host (DNS lookup etc)
96− int addr_res = getaddrinfo(host, str_port, &hints, &remote_host);
101− int connect_res = connect(sockfd, remote_host->ai_addr, remote_host->ai_addrlen);
102− if (connect_res == -1) {
110−int idrnet_sockaddr_family(void* sockaddr) {
111− struct sockaddr* addr = (struct sockaddr*) sockaddr;
112− return (int) addr->sa_family;
115−char* idrnet_sockaddr_ipv4(void* sockaddr) {
116− struct sockaddr_in* addr = (struct sockaddr_in*) sockaddr;
117− char* ip_addr = (char*) malloc(sizeof(char) * INET_ADDRSTRLEN);
118− InetNtopA(AF_INET, &(addr->sin_addr), ip_addr, INET_ADDRSTRLEN);
122−void* idrnet_create_sockaddr() {
123− return malloc(sizeof(struct sockaddr_storage));
127−int idrnet_accept(int sockfd, void* sockaddr) {
128− struct sockaddr* addr = (struct sockaddr*) sockaddr;
129− socklen_t addr_size = 0;
130− if (!check_init()) {
133− return accept(sockfd, addr, &addr_size);
136−int idrnet_send(int sockfd, char* data) {
137− int len = strlen(data); // For now.
138− if (!check_init()) {
141− return send(sockfd, (void*) data, len, 0);
144−int idrnet_send_buf(int sockfd, void* data, int len) {
145− if (!check_init()) {
148− return send(sockfd, data, len, 0);
151−void* idrnet_recv(int sockfd, int len) {
152− if (!check_init()) {
155− idrnet_recv_result* res_struct =
156− (idrnet_recv_result*) malloc(sizeof(idrnet_recv_result));
158− char* buf = malloc(len + 1);
159− int recv_res = recv(sockfd, buf, len, 0);
160− res_struct->result = recv_res;
162− if (recv_res > 0) { // Data was received
163− buf[recv_res + 1] = 0x00; // Null-term, so Idris can interpret it
165− res_struct->payload = (void*) buf;
166− return (void*) res_struct;
169−int idrnet_recv_buf(int sockfd, void* buf, int len) {
170− if (!check_init()) {
173− return recv(sockfd, buf, len, 0);
176−int idrnet_get_recv_res(void* res_struct) {
177− return (((idrnet_recv_result*) res_struct)->result);
180−char* idrnet_get_recv_payload(void* res_struct) {
181− return (((idrnet_recv_result*) res_struct)->payload);
184−void idrnet_free_recv_struct(void* res_struct) {
185− idrnet_recv_result* i_res_struct =
186− (idrnet_recv_result*) res_struct;
187− if (i_res_struct->payload != NULL) {
188− free(i_res_struct->payload);
197−int idrnet_geteagain() {