-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetutils.h
79 lines (57 loc) · 2.26 KB
/
netutils.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
#ifndef NETUTILS_H
#define NETUTILS_H
#include <arpa/inet.h>
#define EOLN "\n\r"
#define FIELDSEPERATOR ", "
#ifndef BUFLEN
#define BUFLEN 4096
#endif
#ifndef HBUFLEN
#define HBUFLEN BUFLEN
#endif
#define DEFAULTBACKLOG 100
#ifdef DEBUG
#define DEFAULTDEBUGLEVEL 1
#else
#define DEFAULTDEBUGLEVEL 0
#endif
extern short DEBUGLEVEL;
#ifndef CONNECT_TIMEOUT
#define CONNECT_TIMEOUT 5000
#endif
#ifndef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#endif
#ifndef MIN
#define MIN(x,y) ((x)>(y)?(y):(x))
#endif
// Skips the ws on the char*
const char* skip_ws(const char*);
const char* skip_nws(const char*);
int timeout_connect(int fd, const char * hostname, int port, int mstimeout);
// returns non-zero if select failed/timed out, otherwise returns zero and
// *status is the return from read()
int timeout_read(int * status, int fd, void *buf , int len , int mstimeout);
int timeout_readall(int * status, int fd, void *buf , int len , int mstimeout);
// same as timeout_read, but only reads up until NICE_TOKEN_SEPARATOR
int timeout_read_token(int * status, int fd, void *buf , int len , int mstimeout);
// returns non-zero if select failed/timed out, otherwise returns zero and
// *status is the return from write()
int timeout_write(int * status, int fd, const void *buf , int len , int mstimeout);
// iterate over timeout_write until everything is written
int timeout_writeall(int * status, int fd, const void *buf , int len , int mstimeout);
int timeout_read_line(int * status, int fd, char * vbuf, int len , int mstimeout);
int skip_to_eoln(int fd,int mstimeout);
// make a tcp server at port. If port is 0 then upon a valid return it will containg
// the port that the server is listening at
// if return <=0, it's an error, else it is a valid fd
int make_tcp_server(unsigned short* port);
// make a tcp connection to hostname:port
// if return <=0, it's an error, else it is a valid fd
int make_tcp_connection(const char * hostname, unsigned short port,int mstimeout);
int make_tcp_connection_from_port(const char * hostname, unsigned short port, unsigned short sport,int mstimeout);
unsigned int getLocalHostIP();
int do_utils_tests();
#define Dprintf(x...) Dprintf2(1,x)
#define Dprintf2(n,x...) do { if(DEBUGLEVEL>=n){ fprintf(stderr,"DEBUG: ");fprintf(stderr,x);} } while(0)
#endif