Add counters for MPTP buffers and syscalls.
[swifty.git] / src / lib / 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 typedef int SOCKET;
31
32 // swift interface
33 typedef struct swift {
34         SOCKET socketListener;
35         SOCKET sendChannel;
36         SOCKET *recvChannel;
37         
38         int usedChannels;
39         int maxChannels;
40 } *Swift;
41
42 /*
43  * TODO: uniform structure names
44  */
45
46 // swift_addr structure similar with in_addr
47 struct swift_addr {
48         unsigned short N;                                       // e.g. number of s_addr
49         unsigned long s_addr[MAX_IPs];          // i.p. ip list
50 };
51
52 // swift struct similar with sock_addr
53 typedef struct sockSwiftaddr {
54         short                           sin_family;             // e.g. AF_INET
55         unsigned short          sin_port;               // e.g. htons(3490)
56         struct swift_addr   sin_addr;           // see struct swift_addr, below
57 } *SockSwiftaddr;
58
59 // list of swift_addr
60 struct listsockaddr {
61         unsigned short N;
62         struct sockaddr_in sa[MAX_IPs];
63 };
64
65 // Function to create a Swift socket
66 Swift socketSwift(int maxChannels);
67
68 // Function to close a Swift socket
69 void closeSwift(Swift);
70
71 // Function to listen to a port
72 int listenfromSwift (Swift s, void *buf, size_t len, int flags,
73                  struct sockSwiftaddr * __restrict__ from, socklen_t *fromlen);
74
75 // Function to bind a port for swift socket
76 int bindSwift(Swift s, const struct sockSwiftaddr *my_addr, socklen_t addrlen);
77
78 // Function to receive a message
79 ssize_t recvfromSwift(Swift s, void *buf, size_t len, int flags,
80                  struct sockSwiftaddr *from, socklen_t fromlen);
81
82 // Function to send a message
83 ssize_t sendToSwift(Swift s, const void *buf, size_t len, int flags,
84                 const struct sockSwiftaddr *to, socklen_t tolen);
85
86
87 // test function -- don't commit
88 void transformFromAddrToSwift(struct sockSwiftaddr *ssa, struct listsockaddr lsa);
89 void transformFromSwiftToAddr(struct listsockaddr *lsa, struct sockSwiftaddr ssa);
90
91 #endif