#include <sys/socket.h>
#include <unistd.h>
+#define DEBUG
#include "lib_swift.h"
struct sockSwiftaddr transformFromAddrToSwift(struct listsockaddr lsa)
return lsa;
}
+// Function to receive a message
+ssize_t recvfromSwift(Swift s, void *buf, size_t len, int flags,
+ struct sockSwiftaddr *from, socklen_t fromlen)
+{
+ struct sockaddr s_other;
+ socklen_t slen=sizeof(s_other);
+ ssize_t rec = -1, send;
+ char *command = "test";
+ struct listsockaddr lsa = transformFromSwiftToAddr(*from);
+ int i, channel;
+
+ Dprintf("create recv channel\n");
+ // TODO make pool
+ if (s->usedChannels < s->maxChannels)
+ {
+ channel = s->usedChannels++;
+ CHECK(s->recvChannel[channel] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
+
+ for ( i = 0 ; i < lsa.N ; i++)
+ {
+ Dprintf("send information to %s\n", inet_ntoa(lsa.sa[0].sin_addr));
+ send = sendto(s->recvChannel[channel], command, strlen(command), 0, (const struct sockaddr *)&lsa.sa[i], sizeof(lsa.sa[i]));
+ }
+
+ Dprintf("receive data\n");
+ // I'm waiting for response
+ rec = recvfrom(s->recvChannel[channel], buf, len, flags, (struct sockaddr * __restrict__)&s_other, &slen);
+
+ close(s->recvChannel[channel]);
+ s->usedChannels--;
+ }
+ return rec;
+}
+
+// Function to send a message
+ssize_t sendToSwift(Swift s, const void *buf, size_t len, int flags,
+ const struct sockSwiftaddr *to, socklen_t tolen)
+{
+ struct listsockaddr lsa = transformFromSwiftToAddr(*to);
+ int i;
+ ssize_t send = -1;
+
+ Dprintf("send data\n");
+ for ( i = 0 ; i < lsa.N ; i++)
+ {
+ send = sendto(s->sendChannel, buf, len, flags, (const struct sockaddr *)&lsa.sa[i], sizeof(lsa.sa[i]));
+ }
+
+ return send;
+}
+// Function to listen to a port
int listenfromSwift (Swift s, void *buf, size_t len, int flags,
- struct sockSwiftaddr * __restrict__ from, socklen_t *fromlen)
+ struct sockSwiftaddr * __restrict__ from, socklen_t *fromlen)
{
struct sockaddr s_other;
struct listsockaddr lsa;
- socklen_t slen=sizeof(s_other);
-
- Dprintf("wait to receive messages");
+ socklen_t slen=sizeof(s_other);
+ ssize_t rec;
- int rec = recvfrom(s->socketListener, buf, len, flags,
- (struct sockaddr * __restrict__)&s_other, &slen);
+ Dprintf("wait to receive messages\n");
+
+ rec = recvfrom(s->socketListener, buf, len, flags, (struct sockaddr * __restrict__)&s_other, &slen);
// fill listsockaddr
memcpy(&lsa.sa[0], &s_other, sizeof(s_other));
return rec;
}
+// Function to bind a port for swift socket
int bindSwift(Swift s, const struct sockSwiftaddr *my_addr, socklen_t addrlen)
{
- Dprintf("bind swift socket");
- return bind(s->socketListener,
- (const struct sockaddr *)&s->socketListenerAddr,
- sizeof(s->socketListenerAddr));
+ Dprintf("bind swift socket\n");
+ struct listsockaddr lsa = transformFromSwiftToAddr(*my_addr);
+
+ return bind(s->socketListener, (const struct sockaddr *)&lsa.sa[0], sizeof(lsa.sa[0]));
}
-Swift socketSwift()
+// Function to create a Swift socket
+Swift socketSwift(int maxChannels)
{
- Dprintf("create swift socket");
+ Dprintf("create struct swift\n");
Swift s = calloc(1,sizeof(*s));
- Dprintf("create swift socket listener");
+ Dprintf("create swift socket listener\n");
CHECK(s->socketListener = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
- Dprintf("create swift socket data");
- CHECK(s->socketData = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
+ Dprintf("create swift send channel\n");
+ CHECK(s->sendChannel = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
- memset((char *) &s->socketListenerAddr, 0, sizeof(s->socketListenerAddr));
-
- s->socketListenerAddr.sin_family = AF_INET;
- s->socketListenerAddr.sin_port = htons(SWIFT_PORT);
- s->socketListenerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
return s;
}
+
void closeSwift(Swift s)
{
- Dprintf("close swift socket");
+ Dprintf("close swift socket\n");
close(s->socketListener);
+ close(s->sendChannel);
+
free(s);
}
} while (0) \
#ifdef DEBUG
-#define Dprintf(msg,...) printf("[%s]:%d" msg, __FILE__, __LINE__, ##__VA_ARGS__)
+#define Dprintf(msg,...) printf("[%s]:%d " msg, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define Dprintf(msg,...) /* do nothing */
#endif
// swift interface
typedef struct swift {
- int socketListener, socketData;
- struct sockaddr_in socketListenerAddr;
+ int socketListener;
+ int sendChannel;
+ int *recvChannel;
+
+ int usedChannels;
+ int maxChannels;
} *Swift;
// swift_addr structure similar with in_addr
};
// Function to create a Swift socket
-Swift socketSwift();
+Swift socketSwift(int maxChannels);
// Function to close a Swift socket
void closeSwift(Swift);
int bindSwift(Swift s, const struct sockSwiftaddr *my_addr, socklen_t addrlen);
// Function to receive a message
-ssize_t recvfrom(Swift s, void *buf, size_t len, int flags,
- struct sockSwiftaddr *from, socklen_t *fromlen);
+ssize_t recvfromSwift(Swift s, void *buf, size_t len, int flags,
+ struct sockSwiftaddr *from, socklen_t fromlen);
// Function to send a message
-ssize_t sendto(Swift s, const void *buf, size_t len, int flags,
- const struct sockSwiftaddr *to, socklen_t tolen);
+ssize_t sendToSwift(Swift s, const void *buf, size_t len, int flags,
+ const struct sockSwiftaddr *to, socklen_t tolen);
+
+
+// test function -- don't commit
+struct sockSwiftaddr transformFromAddrToSwift(struct listsockaddr lsa);
+struct listsockaddr transformFromSwiftToAddr(struct sockSwiftaddr ssa);
#endif
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "lib_swift.h"
+
+int main()
+{
+ Swift s = socketSwift(1);
+ struct sockSwiftaddr my_addr, from;
+ char buf[100];
+ socklen_t slen;
+ struct listsockaddr lsa;
+
+ // populate sockSwiftaddr
+ my_addr.sin_family = AF_INET;
+ my_addr.sin_port = htons(SWIFT_PORT);
+ my_addr.sin_addr.N = 1;
+ my_addr.sin_addr.s_addr[0] = htonl(INADDR_ANY);
+
+ //recvfromSwift(s, buf, 100, 0, (struct sockaddr *)&si_other, &slen);
+
+ bindSwift(s, &my_addr, sizeof(my_addr));
+ listenfromSwift(s, buf, 100, 0, &from, &slen);
+
+ lsa = transformFromSwiftToAddr(from);
+ printf("Received packet from %s:%d with data: %s\n", inet_ntoa(lsa.sa[0].sin_addr), ntohs(lsa.sa[0].sin_port), buf);
+
+
+ closeSwift(s);
+ return 0;
+}