@@ -0,0 +1,260 @@
3+#include <sys/socket.h>
4+#include <netinet/in.h>
14+int isReadable(int sd, int timeOut) { // milliseconds
15+ fd_set socketReadSet;
16+ FD_ZERO(&socketReadSet);
17+ FD_SET(sd,&socketReadSet);
20+ tv.tv_sec = timeOut / 1000;
21+ tv.tv_usec = (timeOut % 1000) * 1000;
26+ if (select(sd+1,&socketReadSet,0,0,&tv) == -1) {
27+ printf("READ ERROR!\n");
30+ return FD_ISSET(sd,&socketReadSet) != 0;
33+void* net_UDP_clientSocket(char* server, int port) {
34+ srand(12345); //time(NULL));
36+ Connection* con = (Connection*)EMALLOC(sizeof(Connection));
38+ struct sockaddr_in si_me, si_other;
40+ if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
45+ memset((char *) &si_other, 0, sizeof(si_other));
46+ si_other.sin_family = AF_INET;
47+ si_other.sin_port = htons(port);
49+ if (inet_aton(server, &si_other.sin_addr)==0) {
54+ // Bind, so we can receive replies.
55+ si_me.sin_family = AF_INET;
56+ si_me.sin_port = htons(0);
57+ si_me.sin_addr.s_addr = htonl(INADDR_ANY);
59+ if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me))<0) {
64+ con->addr = si_other;
66+// printf("Opened client socket %d\n", s);
70+void* net_UDP_serverSocket(int port) {
73+ Connection* con = (Connection*)EMALLOC(sizeof(Connection));
75+ struct sockaddr_in si_me;
77+ if ((s=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
81+ memset((char *) &si_me, 0, sizeof(si_me));
82+ si_me.sin_family = AF_INET;
83+ si_me.sin_port = htons(port);
84+ si_me.sin_addr.s_addr = htonl(INADDR_ANY);
85+ if (bind(s, (struct sockaddr*)&si_me, sizeof(si_me))==-1) {
92+// printf("Opened server socket %d\n", s);
96+int net_sendUDP(void* conn, char* server, int port, char* stuff) {
97+// printf("Socket is %d\n", conn);
98+ if (0) { //rand()%50000 == 0) {
104+ Connection* c = (Connection*)conn;
106+ struct sockaddr_in other = c->addr;
107+ memset((char *) &other, 0, sizeof(other));
108+ other.sin_family = AF_INET;
109+ other.sin_port = htons(port);
110+ if (inet_aton(server, &other.sin_addr)==0) {
115+ if (sendto(s, stuff, strlen(stuff), 0, (struct sockaddr*)&other,
116+ sizeof(other))==-1) {
117+ printf("Send FAIL\n");
120+// printf("Send WIN, to %s %d %d\n", inet_ntoa(other.sin_addr),
121+// ntohs(other.sin_port), port);
125+void* net_recvUDP(void* conn) {
126+ struct sockaddr_in other;
127+ Connection* c = (Connection*)conn;
129+ socklen_t slen = sizeof(other);
130+ struct sockaddr_in me = c->addr;
132+ // TMP HACK: 512 should be enough for the purposes of this experiment...
133+ // Do it properly some time.
134+ char* buf = (char*)EMALLOC(512*sizeof(char));
136+// printf("Waiting to receive\n");
139+// while (!isReadable(s,100)) printf(".");
141+ if (!isReadable(s, 1000)) {
142+ printf("Nothing\n");
146+ if (recvfrom(s, buf, 512, 0, (struct sockaddr *)&other, &slen)==-1) {
147+ printf("Nothing\n");
151+/* printf("Received %s from %s:UDP%u\n",
152+ buf,inet_ntoa(other.sin_addr),
153+ ntohs(other.sin_port));
155+ // return a structure with the received value, and where it came from.
156+ Recv* r = (Recv*)EMALLOC(sizeof(Recv));
158+ r->server = inet_ntoa(other.sin_addr);
159+ r->port = ntohs(other.sin_port);
164+char* get_recvBuf(void* recv) {
165+ return ((Recv*)recv)->buf;
168+char* get_recvServer(void* recv) {
169+ return ((Recv*)recv)->server;
172+int get_recvPort(void* recv) {
173+ return ((Recv*)recv)->port;
176+int nullPtr(void *ptr) {
180+// Just to see if the idris FFI is working, and happily talking to .o files...
181+int netTest(char* strTest, int num) {
183+ for(i=0;i<num;++i) {
184+ printf("%s\n",strTest);
190+void net_sendRaw(void* conn, char* server, int port, VAL pkt)
192+ int seq = GETINT(PROJECT(pkt, 0));
193+ VAL ty = PROJECT(pkt, 1);
204+ pty = TAG(PROJECT(ty,0)) + 2;
207+ char* buf = (char*)EMALLOC(512*sizeof(char));
208+ sprintf(buf, "%d %d", seq, pty);
210+ net_sendUDP(conn, server, port, buf);
213+VAL net_recvRaw(void* conn)
217+ void* dat = net_recvUDP(conn);
219+ return CONSTRUCTOR(1,0,NULL); // Nothing
222+ char* recvBuf = get_recvBuf(dat);
224+ sscanf(recvBuf, "%d %d", &seq, &pty);
227+ ptyc = CONSTRUCTOR(pty,0,NULL);
229+ ptyc = CONSTRUCTOR1(2, CONSTRUCTOR(pty-2,0,NULL));
231+ VAL pkt = CONSTRUCTOR2(0, MKINT(seq), ptyc);
232+ printf("Received %d %d\n", seq, pty);
234+ VAL recvpkt = CONSTRUCTOR3(0, pkt, MKSTR(get_recvServer(dat)),
235+ MKINT(get_recvPort(dat)));
237+ return CONSTRUCTOR1(0, recvpkt); // Just pkt
242+void dumpPkt(VAL pkt)
244+ int seq = GETINT(PROJECT(pkt, 0));
245+ VAL ty = PROJECT(pkt, 1);
256+ pty = TAG(PROJECT(ty,0)) + 2;
259+ printf("%d %d\n", seq, pty);