COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
covise_socket.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef EC_SOCKET_H
9 #define EC_SOCKET_H
10 #include <iostream>
11 
12 #include <errno.h>
13 #include <math.h>
14 #include <fcntl.h>
15 
16 #ifdef _WIN32
17 #include <io.h>
18 #endif
19 
20 #include <util/coExport.h>
21 #ifdef _WIN32
22 #include <windows.h>
23 #include <time.h>
24 #endif
25 
26 #include <net/covise_connect.h>
27 
28 typedef struct ssl_st SSL;
29 
30 #define HAVEMULTICAST
31 
32 #ifndef _WIN32
33 #ifndef _SX
34 extern "C" {
35 #include <netinet/in.h>
36 }
37 #endif
38 #include <netdb.h>
39 #endif
40 
41 #include <util/coExport.h>
42 
43 #ifdef HAVEMULTICAST
44 #ifndef _WIN32
45 #include <netinet/in.h>
46 #include <sys/stat.h>
47 #include <netdb.h>
48 #include <arpa/inet.h>
49 #endif
50 #endif
51 
52 namespace covise
53 {
54 
55 extern void coPerror(const char *prefix);
56 
57 class Host;
58 
59 /*
60  $Log: covise_socket.h,v $
61  * Revision 1.3 1993/10/08 19:10:08 zrhk0125
62  * modifications for correct conversion handling
63  *
64  * Revision 1.2 93/09/30 17:08:08 zrhk0125
65  * basic modifications for CRAY
66  *
67  * Revision 1.1 93/09/25 20:51:06 zrhk0125
68  * Initial revision
69  *
70 */
71 
72 /***********************************************************************\
73  ** **
74  ** Socket Classes Version: 1.0 **
75  ** **
76  ** **
77  ** Description : The Socket class handles the operating system **
78  ** part of the socket handling. **
79  ** **
80  ** Classes : Socket **
81  ** **
82  ** Copyright (C) 1993 by University of Stuttgart **
83  ** Computer Center (RUS) **
84  ** Allmandring 30 **
85  ** 7000 Stuttgart 80 **
86  ** **
87  ** **
88  ** Author : A. Wierse (RUS) **
89  ** **
90  ** History : **
91  ** 15.04.93 Ver 1.0 **
92  ** **
93  ** **
94  ** **
95 \***********************************************************************/
96 
97 const char DF_NONE = 0;
98 const char DF_IEEE = 1;
99 const char DF_CRAY = 2;
100 const int COVISE_SOCKET_INVALID = -2;
101 
102 #if defined(CRAY) && !defined(_WIN32)
103 const char df_local_machine = DF_CRAY;
104 #else
106 #endif
107 
109 {
111  FirewallConfig();
112  ~FirewallConfig();
113 
114 public:
118  static FirewallConfig *the();
119 };
120 
121 class ServerConnection;
123 class SSLServerConnection;
124 
126 {
127 protected:
128  static int stport;
129  static char **ip_alias_list;
131  static bool bInitialised;
132  struct sockaddr_in s_addr_in;
133  Host *host; // host on the other end of the socket
134  int sock_id;
135  int port;
136  int setTCPOptions();
137  bool connected;
138 
139 public:
140  // connect as client
141  Socket(Host *h, int p, int retries = 20, double timeout = 0.0);
142  Socket(int p); // initiate as server
143  Socket(int *p); // initiate as server and use free port
144  Socket() // initialize with descriptor
145  {
146  sock_id = -1;
147  host = NULL;
148  };
149  Socket(int, int sfd); // initialize with descriptor
150  Socket(const Socket &); // initiate as server and use free port
151  Socket(int socket_id, sockaddr_in *sockaddr);
152  virtual ~Socket(); // NIL
153  static void initialize();
154  static void uninitialize();
155  static void set_start_port(int stp)
156  {
157  stport = stp;
158  };
160  {
161  return stport;
162  };
163  ServerConnection *copy_and_accept(); // after select/accept
164  // after select/accept
165  SimpleServerConnection *copySimpleAndAccept();
166  int available(void);
167  int listen(); // listen for actual connection (server)
168  virtual int accept(); // listen and wait for and accept a connection (server)
169  int acceptOnly(); // accept a connection (server)
170  int accept(int); // listen and wait for and accept a connection (server) wait max secondes
171  int acceptOnly(int); // wait for and accept a connection (server) wait max secondes
172  // returns -1 when no accept, 0 otherwise
173  virtual int read(void *buf, unsigned nbyte);
174  virtual int Read(void *buf, unsigned nbyte); // does not exit when read failes but returns -1
175 
176  int setNonBlocking(bool on);
177  //int read_non_blocking(void *buf, unsigned nbyte);
178  virtual int write(const void *buf, unsigned nbyte);
179 #ifdef CRAY
180  int writea(const void *buf, unsigned nbyte);
181 #endif
182  int get_id()
183  {
184  return sock_id;
185  };
186  int get_port()
187  {
188  return port;
189  };
190  Host *get_ip_alias(Host *);
192  {
193  return host;
194  };
195  const char *get_hostname();
196  void print();
197  bool isConnected();
198 #ifdef WIN32
199  static int getErrno()
200  {
201  return WSAGetLastError();
202  };
203 #else
204  static int getErrno()
205  {
206  return errno;
207  };
208 #endif
209  static const char *coStrerror(int err);
210 };
211 
212 #ifdef HAVE_OPENSSL
213 class NETEXPORT SSLSocket : public Socket
214 {
215 public:
216  SSLSocket();
217  SSLSocket(int p, SSL *ssl);
218  SSLSocket(int *p, SSL *ssl);
219  SSLSocket(Host *h, int p, int retries, double timeout, SSL *ssl);
220  SSLSocket(int socket_id, sockaddr_in *sockaddr, SSL *ssl);
221  ~SSLSocket();
222 
223  int read(void *buf, unsigned int nbyte);
224  int Read(void *buf, unsigned int nbyte);
225  //int accept(SSLSocket* sock);
226 
227  int write(const void *buf, unsigned int nbyte);
228  int connect(sockaddr_in addr /*, int retries, double timeout*/);
229 
230  SSLServerConnection *spawnConnection(SSLConnection::PasswordCallback *cb, void *userData);
231 
232  void setSSL(SSL *pSSL);
233 
234  std::string getPeerAddress();
235 
236 protected:
237  struct sockaddr_in mPeer;
238  SSL *mSSLObject;
239 };
240 #endif
241 
242 class NETEXPORT UDPSocket : public Socket
243 {
244 public:
245  UDPSocket(char *address, int p);
246  ~UDPSocket();
247  int read(void *buf, unsigned nbyte);
248  int write(const void *buf, unsigned nbyte);
249 };
250 
251 #ifdef HAVEMULTICAST
253 {
254  int ttl;
255 
256 public:
257  MulticastSocket(char *MulticastGroup, int p, int ttl);
258  ~MulticastSocket();
259  int read(void *buf, unsigned nbyte);
260  int write(const void *buf, unsigned nbyte);
261  int get_ttl()
262  {
263  return ttl;
264  };
265 };
266 
267 #endif
268 
269 bool NETEXPORT checkSSLError(SSL *ssl, int error);
270 void NETEXPORT resolveError();
271 }
272 #endif
GLfloat GLfloat GLfloat GLfloat h
Definition: khronos-glext.h:8441
Definition: covise_host.h:17
static FirewallConfig * theFirewallConfig
Definition: covise_socket.h:110
Definition: covise_socket.h:242
const int COVISE_SOCKET_INVALID
Definition: covise_socket.h:100
struct ssl_st SSL
Definition: covise_connect.h:25
Definition: covise_socket.h:252
GLbitfield GLuint64 timeout
Definition: khronos-glext.h:7882
Host * get_host()
Definition: covise_socket.h:191
bool connected
Definition: covise_socket.h:137
Host * host
Definition: covise_socket.h:133
int sock_id
Definition: covise_socket.h:134
Definition: covise_connect.h:200
int ttl
Definition: covise_socket.h:254
int destinationPort
Definition: covise_socket.h:116
int port
Definition: covise_socket.h:135
static bool bInitialised
Definition: covise_socket.h:131
#define NULL
Definition: covise_list.h:22
int get_start_port()
Definition: covise_socket.h:159
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
Definition: covise_socket.h:125
static int getErrno()
Definition: covise_socket.h:204
int sourcePort
Definition: covise_socket.h:115
Definition: covise_socket.h:108
int get_ttl()
Definition: covise_socket.h:261
static Host ** host_alias_list
Definition: covise_socket.h:130
bool setSourcePort
Definition: covise_socket.h:117
GLenum const GLvoid * addr
Definition: khronos-glext.h:10596
GLfloat GLfloat p
Definition: khronos-glext.h:9861
static void set_start_port(int stp)
Definition: covise_socket.h:155
int get_port()
Definition: covise_socket.h:186
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: khronos-glext.h:8469
void resolveError()
Definition: covise_socket.cpp:1653
GLuint address
Definition: khronos-glext.h:10368
#define NETEXPORT
Definition: coExport.h:343
Socket()
Definition: covise_socket.h:144
static char ** ip_alias_list
Definition: covise_socket.h:129
const char DF_CRAY
Definition: covise_socket.h:99
const char df_local_machine
Definition: covise_socket.h:105
const char DF_IEEE
Definition: covise_socket.h:98
void coPerror(const char *prefix)
Definition: covise_socket.cpp:1898
Definition: covise_connect.h:225
bool NETEXPORT checkSSLError(SSL *ssl, int error)
int get_id()
Definition: covise_socket.h:182
static int stport
Definition: covise_socket.h:128
const char DF_NONE
Definition: covise_socket.h:97