This repository has been archived by the owner on May 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
socklib.h
223 lines (174 loc) · 5.87 KB
/
socklib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#ifndef SOCKLIB_H
#define SOCKLIB_H
#ifndef UDPCAST_CONFIG_H
# define UDPCAST_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <string.h>
#ifdef HAVE_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#endif
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#ifdef __MINGW32__
#define WINDOWS
#undef USE_SYSLOG
#endif /* __MINGW32__ */
#ifdef __CYGWIN__
/* Untested so far ... */
#define WINDOWS
#endif
#define RECEIVER_PORT(x) (x)
#define SENDER_PORT(x) ((x)+1)
#define loseSendPacket udpc_loseSendPacket
#define loseRecvPacket udpc_loseRecvPacket
#define setWriteLoss udpc_setWriteLoss
#define setReadLoss udpc_setReadLoss
#define setReadSwap udpc_setReadSwap
#define srandomTime udpc_srandomTime
#define RecvMsg udpc_RecvMsg
#define doAutoRateLimit udpc_doAutoRateLimit
#define makeSockAddr udpc_makeSockAddr
#define getMyAddress udpc_getMyAddress
#define getBroadCastAddress udpc_getBroadCastAddress
#define getMcastAllAddress udpc_getMcastAllAddress
#define doSend udpc_doSend
#define doReceive udpc_doReceive
#define printMyIp udpc_printMyIp
#define makeSocket udpc_makeSocket
#define setSocketToBroadcast udpc_setSocketToBroadcast
#define setTtl udpc_setTtl
#define setMcastDestination udpc_setMcastDestination
#define isFullDuplex udpc_isFullDuplex
#define getNetIf udpc_getNetIf
#define getCurrentQueueLength udpc_getCurrentQueueLength
#define getSendBuf udpc_getSendBuf
#define setSendBuf udpc_setSendBuf
#define getRcvBuf udpc_getRcvBuf
#define setRcvBuf udpc_setRcvBuf
#define getPort udpc_getPort
#define setPort udpc_setPort
#define getIpString udpc_getIpString
#define ipIsEqual udpc_ipIsEqual
#define ipIsZero udpc_ipIsZero
#define clearIp udpc_clearIp
#define setIpFromString udpc_setIpFromString
#define copyIpFrom udpc_copyIpFrom
#define getDefaultMcastAddress udpc_getDefaultMcastAddress
#define copyToMessage udpc_copyToMessage
#define copyFromMessage udpc_copyFromMessage
#define isAddressEqual udpc_isAddressEqual
#define parseSize udpc_parseSize
#define parseSpeed udpc_parseSpeed
#define zeroSockArray udpc_zeroSockArray
#define selectSock udpc_selectSock
#define prepareForSelect udpc_prepareForSelect
#define getSelectedSock udpc_getSelectedSock
#define closeSock udpc_closeSock
#ifdef LOSSTEST
int loseSendPacket(void);
void loseRecvPacket(int s);
void setWriteLoss(char *l);
void setReadLoss(char *l);
void setReadSwap(char *l);
void srandomTime(int printSeed);
int RecvMsg(int s, struct msghdr *msg, int flags);
#endif
struct net_if {
struct in_addr addr;
struct in_addr bcast;
char *name;
#ifdef SIOCGIFINDEX
int index;
#endif
};
typedef struct net_if net_if_t;
typedef enum addr_type_t {
ADDR_TYPE_UCAST,
ADDR_TYPE_MCAST,
ADDR_TYPE_BCAST
} addr_type_t;
void doAutoRateLimit(int sock, int dir, int qsize, int size);
int makeSockAddr(char *hostname, short port, struct sockaddr_in *addr);
int getMyAddress(net_if_t *net_if, struct sockaddr_in *addr);
int getBroadCastAddress(net_if_t *net_if, struct sockaddr_in *addr, short port);
int getMcastAllAddress(struct sockaddr_in *addr, char *address, short port);
int doSend(int s, void *message, size_t len, struct sockaddr_in *to);
int doReceive(int s, void *message, size_t len,
struct sockaddr_in *from, int portBase);
void printMyIp(net_if_t *net_if, int s);
int makeSocket(addr_type_t addr_type, net_if_t *net_if,
struct sockaddr_in *tmpl, int port);
int setSocketToBroadcast(int sock);
int setTtl(int sock, int ttl);
int setMcastDestination(int,net_if_t *,struct sockaddr_in *);
int isFullDuplex(int sock, const char *ifName);
net_if_t *getNetIf(const char *ifName);
int getCurrentQueueLength(int sock);
int getSendBuf(int sock);
void setSendBuf(int sock, unsigned int bufsize);
unsigned int getRcvBuf(int sock);
void setRcvBuf(int sock, unsigned int bufsize);
#define SEND(s, msg, to) \
doSend(s, &msg, sizeof(msg), &to)
#define RECV(s, msg, from, portBase ) \
doReceive((s), &msg, sizeof(msg), &from, (portBase) )
#define BCAST_CONTROL(s, msg) \
doSend(s, &msg, sizeof(msg), &net_config->controlMcastAddr)
unsigned short getPort(struct sockaddr_in *addr);
void setPort(struct sockaddr_in *addr, unsigned short port);
char *getIpString(struct sockaddr_in *addr, char *buffer);
int ipIsEqual(struct sockaddr_in *left, struct sockaddr_in *right);
int ipIsZero(struct sockaddr_in *ip);
void clearIp(struct sockaddr_in *addr);
void setIpFromString(struct sockaddr_in *addr, char *ip);
void copyIpFrom(struct sockaddr_in *dst, struct sockaddr_in *src);
void getDefaultMcastAddress(net_if_t *net_if, struct sockaddr_in *mcast);
void copyToMessage(unsigned char *dst, struct sockaddr_in *src);
void copyFromMessage(struct sockaddr_in *dst, unsigned char *src);
int isAddressEqual(struct sockaddr_in *a, struct sockaddr_in *b);
unsigned long parseSize(char *sizeString);
unsigned long parseSpeed(char *speedString);
void zeroSockArray(int *socks, int nr);
int selectSock(int *socks, int nr, int startTimeout);
int prepareForSelect(int *socks, int nr, fd_set *read_set);
int getSelectedSock(int *socks, int nr, fd_set *read_set);
void closeSock(int *socks, int nr, int target);
int isMcastAddress(struct sockaddr_in *addr);
int udpc_socklibFatal(int code);
#ifdef __MINGW32__ /* __MINGW32__ */
struct iovec {
void *iov_base;
int iov_len;
};
struct msghdr {
void *msg_name;
int msg_namelen;
struct iovec *msg_iov;
int msg_iovlen;
};
ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
ssize_t recvmsg (int fd, struct msghdr *msg, int flags);
void gettimeofday(struct timeval* p, void* tz /* IGNORED */);
#define usleep(x) Sleep((x)/1000)
#define sleep(x) Sleep(1000L*(x))
#endif /* __MINGW32__ */
static inline void initMsgHdr(struct msghdr *hdr) {
#ifndef WINDOWS
hdr->msg_control = 0;
hdr->msg_controllen = 0;
hdr->msg_flags = 0;
#endif
}
#endif