Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added network server files
[palacios.git] / misc / network_servers / vtl / socks.h
1 #ifndef _socks
2 #define _socks
3
4 #include "util.h"
5
6 #if defined(WIN32) && !defined(__CYGWIN__)
7 #include <ws2tcpip.h>
8 #include <Iphlpapi.h>
9 #include <winsock2.h>
10 #include <windows.h>
11 #else
12 extern "C" {
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <netdb.h>
20 #include <sys/time.h>
21 #include <netinet/tcp.h>
22 #include <unistd.h>
23 #include <sys/ioctl.h>
24 #include <net/if.h>
25 #include <netinet/if_ether.h>
26
27 #if defined(USE_SSL)
28 #define OPENSSL_NO_KRB5
29 #include <openssl/ssl.h>
30 #endif // USE_SSL
31
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <errno.h>
35 }
36 #endif
37
38 #include <string>
39
40 using namespace std;
41
42
43 #define TCP 0
44 #define UDP 1
45
46 #define SND_RCV_SOCKET_BUF_SIZE 65536
47 #ifndef INADDR_NONE
48 #define INADDR_NONE             0xffffffff
49 #endif
50
51
52 #if defined(WIN32) && !defined(__CYGWIN__)
53 #include <io.h>
54 #define WRITE(fd,buf,len) send(fd,buf,len,0)
55 #define READ(fd,buf,len) recv(fd,buf,len,0)
56 //#define WRITE(fd,buf,len) _write(_open_osfhandle(fd, 0),buf,len)
57 //#define READ(fd,buf,len) _read(_open_osfhandle(fd, 0),buf,len)
58
59 #define SOCK SOCKET
60
61 #define CLOSE(x) closesocket(x)
62 #define IOCTL(x,y,z) ioctlsocket((SOCKET)x,(long)y,(unsigned long *)z)
63 #else 
64
65 #define SOCK int
66
67 #if defined(USE_SSL)
68 #define WRITE(ssl,buf,len) write(ssl, buf, len)
69 #else 
70 #define WRITE(fd,buf,len) write(fd, buf, len)
71 #endif 
72
73 #define READ(fd,buf,len) read(fd, buf, len)
74 #define CLOSE(x) close(x)
75 #define IOCTL(x,y,z) ioctl(x, y, z)
76 #endif
77
78
79 SOCK CreateAndSetupTcpSocket(const int bufsize=SND_RCV_SOCKET_BUF_SIZE, 
80                             const bool nodelay=true,
81                             const bool nonblocking=false);
82
83 SOCK CreateAndSetupUdpSocket(const int bufsize=SND_RCV_SOCKET_BUF_SIZE, 
84                             const bool nonblocking=false);
85
86 SOCK CreateAndSetupUnixDomainSocket(const int bufsize=SND_RCV_SOCKET_BUF_SIZE,
87                                    const bool nonblocking=false);
88
89 int SetNoDelaySocket(const SOCK fd, const bool nodelay=true);
90
91 int IsSocket(const SOCK fd);
92 int IsStreamSocket(const SOCK fd);
93 int IsDatagramSocket(const SOCK fd);
94
95 #ifdef linux
96 int IsVirtualSocket(const int fd);
97 #endif
98
99 int BindSocket(const SOCK mysocket, const int myport);
100 int BindSocket(const SOCK mysocket, const unsigned adx, const int myport);
101 int BindSocket(const SOCK mysocket, const char *host_or_ip, const int myport);
102 int BindSocket(const SOCK mysocket, const char *pathname);
103
104 int ListenSocket(const SOCK mysocket, const int max=SOMAXCONN);
105
106 int ConnectToHost(const SOCK mysocket, const int hostip, const int port);
107 int ConnectToHost(const SOCK mysocket, const char *host, const int port);
108 int ConnectToPath(const SOCK mysocket, const char *pathname);
109
110 #if defined(USE_SSL) 
111 int Send(const SOCK fd, SSL *ssl, const char *buf, const int len, bool sendall=true);
112 int Receive(const SOCK fd, SSL *ssl, char *buf, const int len, bool recvall=true);
113 #endif 
114 int Send(const SOCK fd, const char *buf, const int len, bool sendall=true);
115 int Receive(const SOCK fd, char *buf, const int len, bool recvall=true);
116
117
118
119 int SendTo(const SOCK mysocket, 
120            const unsigned ip, const int port, 
121            const char *buf, const int len, bool sendall=true);
122 int ReceiveFrom(const SOCK mysocket, 
123                 const unsigned ip, const int port, 
124                 char *buf, const int len, const bool recvall=true);
125 int SendTo(const SOCK mysocket, 
126            const char *host_or_ip, const int port, 
127            const char *buf, const int len, const bool sendall=true);
128 int ReceiveFrom(const SOCK mysocket, 
129                 const char *host_or_ip, const int port, 
130                 char *buf, const int len, const bool recvall=true);
131
132
133 int JoinMulticastGroup(const SOCK mysocket, const char *IP);
134 int JoinMulticastGroup(const SOCK mysocket, const unsigned adx);
135 int LeaveMulticastGroup(const SOCK mysocket, const char *IP);
136 int LeaveMulticastGroup(const SOCK mysocket, const unsigned adx);
137 int SetMulticastTimeToLive(const SOCK mysocket, const unsigned char ttl);
138
139
140
141 unsigned long GetRemoteSockAddress(SOCK sock);
142 int GetRemoteSockAddress(SOCK sock, struct sockaddr * addr);
143
144 unsigned long GetLocalSockAddress(SOCK sock);
145 int GetLocalSockAddress(SOCK sock, struct sockaddr * addr);
146 int GetLocalMacAddress(const string dev_name, char * buf);
147 int GetLocalMacAddress(const char * dev_name, char * buf);
148 unsigned GetMyIPAddress();
149 unsigned ToIPAddress(const char *hostname);
150 void     PrintIPAddress(const unsigned adx, FILE *out=stderr);
151 void     IPToHostname(const unsigned ip, char *name, const int namesize);
152 int      IsValidIPMulticastAddress(const unsigned ipadx);
153
154
155 int GetOpenTcpPorts(int ** ports);
156 int GetOpenUdpPorts(int ** ports);
157 int IsPortOpen(int port_num, int proto = TCP);
158
159
160 int SetSignalHandler(const int signum, void (*handler)(int), const bool oneshot=false);
161 int IgnoreSignal(const int signum);
162 int ListenToSignal(const int signum);
163
164 #if defined(USE_SSL) 
165 int GetLine(SOCK fd, SSL *ssl, string &s);
166 int PutLine(SOCK fd, SSL *ssl, const string &s);
167 #endif
168  
169 int GetLine(SOCK fd, string &s);
170 int PutLine(SOCK fd, const string &s);
171
172
173 #endif