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(); // wait for and accept a connection (server)
169  int accept(int); // wait for and accept a connection (server) wait max secondes
170  // returns -1 when no accept, 0 otherwise
171  virtual int read(void *buf, unsigned nbyte);
172  virtual int Read(void *buf, unsigned nbyte); // does not exit when read failes but returns -1
173 
174  int setNonBlocking(bool on);
175  //int read_non_blocking(void *buf, unsigned nbyte);
176  virtual int write(const void *buf, unsigned nbyte);
177 #ifdef CRAY
178  int writea(const void *buf, unsigned nbyte);
179 #endif
180  int get_id()
181  {
182  return sock_id;
183  };
184  int get_port()
185  {
186  return port;
187  };
188  Host *get_ip_alias(Host *);
190  {
191  return host;
192  };
193  const char *get_hostname();
194  void print();
195  bool isConnected();
196 #ifdef WIN32
197  static int getErrno()
198  {
199  return WSAGetLastError();
200  };
201 #else
202  static int getErrno()
203  {
204  return errno;
205  };
206 #endif
207  static const char *coStrerror(int err);
208 };
209 
210 #ifdef HAVE_OPENSSL
211 class NETEXPORT SSLSocket : public Socket
212 {
213 public:
214  SSLSocket();
215  SSLSocket(int p, SSL *ssl);
216  SSLSocket(int *p, SSL *ssl);
217  SSLSocket(Host *h, int p, int retries, double timeout, SSL *ssl);
218  SSLSocket(int socket_id, sockaddr_in *sockaddr, SSL *ssl);
219  ~SSLSocket();
220 
221  int read(void *buf, unsigned int nbyte);
222  int Read(void *buf, unsigned int nbyte);
223  //int accept(SSLSocket* sock);
224 
225  int write(const void *buf, unsigned int nbyte);
226  int connect(sockaddr_in addr /*, int retries, double timeout*/);
227 
228  SSLServerConnection *spawnConnection(SSLConnection::PasswordCallback *cb, void *userData);
229 
230  void setSSL(SSL *pSSL);
231 
232  std::string getPeerAddress();
233 
234 protected:
235  struct sockaddr_in mPeer;
236  SSL *mSSLObject;
237 };
238 #endif
239 
240 class NETEXPORT UDPSocket : public Socket
241 {
242 public:
243  UDPSocket(char *address, int p);
244  ~UDPSocket();
245  int read(void *buf, unsigned nbyte);
246  int write(const void *buf, unsigned nbyte);
247 };
248 
249 #ifdef HAVEMULTICAST
251 {
252  int ttl;
253 
254 public:
255  MulticastSocket(char *MulticastGroup, int p, int ttl);
256  ~MulticastSocket();
257  int read(void *buf, unsigned nbyte);
258  int write(const void *buf, unsigned nbyte);
259  int get_ttl()
260  {
261  return ttl;
262  };
263 };
264 
265 #endif
266 
267 bool NETEXPORT checkSSLError(SSL *ssl, int error);
268 void NETEXPORT resolveError();
269 }
270 #endif
Definition: covise_host.h:17
static FirewallConfig * theFirewallConfig
Definition: covise_socket.h:110
const char DF_CRAY
Definition: covise_socket.h:99
Definition: covise_socket.h:240
Definition: covise_socket.h:250
const int COVISE_SOCKET_INVALID
Definition: covise_socket.h:100
Host * get_host()
Definition: covise_socket.h:189
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:252
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
Definition: covise_socket.h:125
static int getErrno()
Definition: covise_socket.h:202
int sourcePort
Definition: covise_socket.h:115
Definition: covise_socket.h:108
int get_ttl()
Definition: covise_socket.h:259
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: khronos-glext.h:8469
static Host ** host_alias_list
Definition: covise_socket.h:130
bool setSourcePort
Definition: covise_socket.h:117
GLuint address
Definition: khronos-glext.h:10368
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
const char DF_IEEE
Definition: covise_socket.h:98
static void set_start_port(int stp)
Definition: covise_socket.h:155
#define NETEXPORT
Definition: coExport.h:343
int get_port()
Definition: covise_socket.h:184
const char DF_NONE
Definition: covise_socket.h:97
bool NETEXPORT checkSSLError(SSL *ssl, int error)
void coPerror(const char *prefix)
Definition: covise_socket.cpp:1887
GLenum const GLvoid * addr
Definition: khronos-glext.h:10596
void resolveError()
Definition: covise_socket.cpp:1642
GLfloat GLfloat p
Definition: khronos-glext.h:9861
Socket()
Definition: covise_socket.h:144
GLfloat GLfloat GLfloat GLfloat h
Definition: khronos-glext.h:8441
static char ** ip_alias_list
Definition: covise_socket.h:129
const char df_local_machine
Definition: covise_socket.h:105
struct ssl_st SSL
Definition: covise_connect.h:25
Definition: covise_connect.h:225
GLbitfield GLuint64 timeout
Definition: khronos-glext.h:7882
int get_id()
Definition: covise_socket.h:180
static int stport
Definition: covise_socket.h:128