implements first function (recv, bind, socket, close)
authorDrutu Bogdan <bcristia@adobe.com>
Sat, 18 Dec 2010 16:25:34 +0000 (18:25 +0200)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sat, 2 Apr 2011 07:27:23 +0000 (10:27 +0300)
src/lib_swift.c
src/lib_swift.h

index 4ef146a..1ddd024 100644 (file)
@@ -1,18 +1,58 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
 #include "lib_swift.h"
 
+ssize_t recvfrom(int s, void *buf, size_t len, int flags,
+                 struct sockaddr *from, socklen_t *fromlen);
+
+int recvfromSwift (Swift s, void *buf, size_t len, int flags,
+                 struct sockaddr * __restrict__ from, socklen_t *fromlen)
+{
+       struct sockaddr_in si_other;
+       socklen_t slen=sizeof(si_other);
+
+       Dprintf("wait to receive messages");
+       return recvfrom(s->socketListener, buf, len, flags,
+               from, &slen);
+}
+
+int bindSwift(Swift s, const struct sockaddr *my_addr, socklen_t addrlen)
+{
+       Dprintf("bind swift socket");
+       return bind(s->socketListener, 
+               (const struct sockaddr *)&s->socketListenerAddr,
+               sizeof(s->socketListenerAddr));
+}
+
+Swift socketSwift()
+{
+       Dprintf("create swift socket"); 
+       Swift s = calloc(1,sizeof(*s));
 
-#ifdef DEBUG
-#define Dprintf(msg,...) printf("[%s]:%d" msg, __FILE__, __LINE__, ##__VA_ARGS__)
-#else
-#define Dprintf(msg,...)                /* do nothing */
-#endif
+       Dprintf("create swift listener");       
+       CHECK(s->socketListener = 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 f() 
+void closeSwift(Swift s)
 {
-       Dprintf("TESTARE in PLM");
+       Dprintf("close swift socket");
+       close(s->socketListener);
        
-       printf("Current file name %s\n", __FILE__);
-       printf("Current line %d in file %s\n", __LINE__, __FILE__);
+       free(s);
 }
index e3ce6ee..47967ad 100644 (file)
@@ -1,7 +1,41 @@
 #ifndef _LIB_SWIFT_
 #define _LIB_SWIFT_
 
-void f();
+#define SWIFT_PORT             8080
+#define PACKET_SIZE            4*1024
 
+#define DIE(s) \
+       do { \
+               printf("%s:%d: ", __func__, __LINE__); \
+               perror(s); \
+               exit(EXIT_FAILURE); \
+       } while (0) \
+
+#define CHECK(x) \
+       do { \
+               if (!(x)) { \
+                       printf("%s:%d: ", __func__, __LINE__); \
+                       perror(#x); \
+                       exit(EXIT_FAILURE); \
+               } \
+       } while (0) \
+
+#ifdef DEBUG
+#define Dprintf(msg,...) printf("[%s]:%d" msg, __FILE__, __LINE__, ##__VA_ARGS__)
+#else
+#define Dprintf(msg,...)                /* do nothing */
+#endif
+
+typedef struct swift {
+       int socketListener;
+       struct sockaddr_in socketListenerAddr;
+} *Swift;
+
+Swift socketSwift();
+void closeSwift(Swift);
+
+
+int recvfromSwift (Swift, void *, size_t, int, struct sockaddr *, socklen_t *);
+int bindSwift(Swift, const struct sockaddr *, socklen_t);
 
 #endif