3062195e113465321ac1ec13ede7c0b742338222
[swifty.git] / src / lib_swift.h
1 #ifndef _LIB_SWIFT_
2 #define _LIB_SWIFT_
3
4 #define SWIFT_PORT              8080
5 #define PACKET_SIZE             4*1024
6 #define MAX_IPs                 10
7
8 #define DIE(s) \
9         do { \
10                 printf("%s:%d: ", __func__, __LINE__); \
11                 perror(s); \
12                 exit(EXIT_FAILURE); \
13         } while (0) \
14
15 #define CHECK(x) \
16         do { \
17                 if (!(x)) { \
18                         printf("%s:%d: ", __func__, __LINE__); \
19                         perror(#x); \
20                         exit(EXIT_FAILURE); \
21                 } \
22         } while (0) \
23
24 #ifdef DEBUG
25 #define Dprintf(msg,...) printf("[%s]:%d " msg, __FILE__, __LINE__, ##__VA_ARGS__)
26 #else
27 #define Dprintf(msg,...)                /* do nothing */
28 #endif
29
30 // swift interface
31 typedef struct swift {
32         int socketListener;
33         int sendChannel;
34         int *recvChannel;
35         
36         int usedChannels;
37         int maxChannels;
38 } *Swift;
39
40 // swift_addr structure similar with in_addr 
41 struct swift_addr {
42         unsigned short N;                                       // e.g. number of s_addr
43         unsigned long s_addr[MAX_IPs];          // i.p. ip list
44 };
45
46 // swift struct similar with sock_addr
47 typedef struct sockSwiftaddr {
48         short                           sin_family;             // e.g. AF_INET
49     unsigned short              sin_port;               // e.g. htons(3490)
50     struct swift_addr   sin_addr;               // see struct swift_addr, below
51 } *SockSwiftaddr;
52
53 // list of swift_addr
54 struct listsockaddr {
55         unsigned short N;
56         struct sockaddr_in sa[MAX_IPs];
57 };      
58
59 // Function to create a Swift socket
60 Swift socketSwift(int maxChannels);
61
62 // Function to close a Swift socket
63 void closeSwift(Swift);
64
65 // Function to listen to a port
66 int listenfromSwift (Swift s, void *buf, size_t len, int flags,
67                  struct sockSwiftaddr * __restrict__ from, socklen_t *fromlen);
68
69 // Function to bind a port for swift socket
70 int bindSwift(Swift s, const struct sockSwiftaddr *my_addr, socklen_t addrlen);
71
72 // Function to receive a message
73 ssize_t recvfromSwift(Swift s, void *buf, size_t len, int flags,
74                  struct sockSwiftaddr *from, socklen_t fromlen);
75                  
76 // Function to send a message
77 ssize_t sendToSwift(Swift s, const void *buf, size_t len, int flags, 
78                                         const struct sockSwiftaddr *to, socklen_t tolen);
79
80
81 // test function -- don't commit
82 struct sockSwiftaddr transformFromAddrToSwift(struct listsockaddr lsa);
83 struct listsockaddr transformFromSwiftToAddr(struct sockSwiftaddr ssa);
84
85 #endif