6 #include <netinet/in.h>
8 #include <sys/socket.h>
12 #include "lib_swift.h"
15 * TODO: send ssa as pointer
18 void transformFromAddrToSwift(struct sockSwiftaddr *ssa, struct listsockaddr lsa)
22 Dprintf("transform in %d address\n", lsa.N);
23 ssa->sin_addr.N = lsa.N;
25 ssa->sin_family = lsa.sa[0].sin_family;
26 ssa->sin_port = lsa.sa[0].sin_port;
29 for (i = 0; i < lsa.N; i++)
31 ssa->sin_addr.s_addr[i] = lsa.sa[i].sin_addr.s_addr;
36 * TODO: send ssa as pointer
39 void transformFromSwiftToAddr(struct listsockaddr *lsa, struct sockSwiftaddr ssa)
43 Dprintf("transform %d address\n", ssa.sin_addr.N);
44 lsa->N = ssa.sin_addr.N;
46 for ( i = 0; i < lsa->N; i++) {
47 lsa->sa[i].sin_family = ssa.sin_family;
48 lsa->sa[i].sin_port = ssa.sin_port;
49 lsa->sa[i].sin_addr.s_addr = ssa.sin_addr.s_addr[i];
53 // Function to receive a message
54 ssize_t recvfromSwift(Swift s, void *buf, size_t len, int flags,
55 struct sockSwiftaddr *from, socklen_t fromlen)
57 struct sockaddr s_other;
58 socklen_t slen=sizeof(s_other);
59 ssize_t rec = -1, send;
60 char *command = "test";
61 struct listsockaddr lsa;
64 transformFromSwiftToAddr(&lsa, *from);
65 Dprintf("create recv channel\n");
67 if (s->usedChannels < s->maxChannels)
69 channel = s->usedChannels++;
70 CHECK(s->recvChannel[channel] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
72 for ( i = 0 ; i < lsa.N ; i++)
74 Dprintf("send information to %s:%d\n", inet_ntoa(lsa.sa[i].sin_addr), ntohs(lsa.sa[i].sin_port));
75 send = sendto(s->recvChannel[channel], command, 4, 0, (const struct sockaddr *)&lsa.sa[i], sizeof(lsa.sa[i]));
78 Dprintf("receive data\n");
79 // TODO make a non-blocant select
80 // I'm waiting for response
81 rec = recvfrom(s->recvChannel[channel], buf, len, flags, (struct sockaddr * __restrict__)&s_other, &slen);
83 close(s->recvChannel[channel]);
89 // Function to send a message
90 ssize_t sendToSwift(Swift s, const void *buf, size_t len, int flags,
91 const struct sockSwiftaddr *to, socklen_t tolen)
93 struct listsockaddr lsa;
97 transformFromSwiftToAddr(&lsa, *to);
99 for ( i = 0 ; i < lsa.N ; i++)
101 Dprintf("send data to %s:%d\n", inet_ntoa(lsa.sa[i].sin_addr), ntohs(lsa.sa[i].sin_port));
102 send = sendto(s->sendChannel, buf, len, flags, (const struct sockaddr *)&lsa.sa[i], sizeof(lsa.sa[i]));
107 // Function to listen to a port
108 int listenfromSwift (Swift s, void *buf, size_t len, int flags,
109 struct sockSwiftaddr * from, socklen_t *fromlen)
111 struct sockaddr s_other;
112 struct listsockaddr lsa;
113 socklen_t slen=sizeof(s_other);
116 Dprintf("wait to receive messages\n");
118 rec = recvfrom(s->socketListener, buf, len, flags, (struct sockaddr * __restrict__)&s_other, &slen);
121 memcpy(&lsa.sa[0], &s_other, sizeof(s_other));
124 transformFromAddrToSwift(from, lsa);
129 // Function to bind a port for swift socket
130 int bindSwift(Swift s, const struct sockSwiftaddr *my_addr, socklen_t addrlen)
132 Dprintf("bind swift socket\n");
133 struct listsockaddr lsa;
134 transformFromSwiftToAddr(&lsa, *my_addr);
136 return bind(s->socketListener, (const struct sockaddr *)&lsa.sa[0], sizeof(lsa.sa[0]));
139 // Function to create a Swift socket
140 Swift socketSwift(int maxChannels)
142 Dprintf("create struct swift\n");
143 Swift s = calloc(1,sizeof(*s));
145 Dprintf("create swift socket listener\n");
146 CHECK(s->socketListener = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
148 Dprintf("create swift send channel\n");
149 CHECK(s->sendChannel = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
151 Dprintf("create swift send channel\n");
152 CHECK(s->recvChannel = calloc(100,sizeof(SOCKET)));
155 s->maxChannels = maxChannels;
161 void closeSwift(Swift s)
163 Dprintf("close swift socket\n");
164 close(s->socketListener);
165 close(s->sendChannel);