COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
covise_connect.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_CONNECTION_H
9 #define EC_CONNECTION_H
10 
11 #include <iostream>
12 #include <vector>
13 
14 #include <fcntl.h>
15 #ifdef _WIN32
16 #include <io.h>
17 #else
18 #include <netinet/in.h>
19 
20 #endif
21 
22 #include <util/coExport.h>
23 #include "message.h"
24 
25 typedef struct ssl_st SSL;
26 typedef struct ssl_ctx_st SSL_CTX;
27 typedef struct ssl_method_st SSL_METHOD;
28 
29 /*
30  $Log: $
31  * Revision 1.9 1994/03/24 16:56:26 zrf30125
32  * ConnectionList completed
33  *
34  * Revision 1.8 94/02/18 14:31:52 zrfg0125
35  * ~Connection bug
36  *
37  * Revision 1.7 94/02/18 14:19:44 zrfg0125
38  * ~Connection bug solved
39  *
40 * Revision 1.6 94/02/18 14:08:18 zrfg0125
41  * read_buf no longer static
42  *
43  * Revision 1.5 93/12/10 14:19:06 zrfg0125
44  * modifications to speedup write and read calls
45  *
46  * Revision 1.4 93/10/20 15:14:18 zrhk0125
47  * socket cleanup improved
48  *
49  * Revision 1.3 93/10/08 19:26:59 zrhk0125
50  * shortened include filename
51  * conversion included as connection dependent
52  *
53  * Revision 1.2 93/09/30 17:09:04 zrhk0125
54  * basic modifications for CRAY
55  *
56  * Revision 1.1 93/09/25 20:39:31 zrhk0125
57  * Initial revision
58  *
59  */
60 
61 const int EC_SERVER = 0;
62 const int EC_CLIENT = 1;
63 
64 namespace covise
65 {
66 
67 class Host;
68 class SimpleServerConnection;
69 class SSLSocket;
70 
71 #ifdef CRAY
72 #define WRITE_BUFFER_SIZE 393216
73 #else
74 #define WRITE_BUFFER_SIZE 64000
75 #endif
76 #define READ_BUFFER_SIZE WRITE_BUFFER_SIZE
77 
78 /***********************************************************************\
79  ** **
80  ** Connection classes Version: 1.1 **
81  ** **
82  ** **
83  ** Description : These classes present the user-seeable part of the **
84  ** socket communications (if necessary). **
85  ** Connection is the base class, ServerConecction **
86  ** and ClientConnection are subclasses tuned for the **
87  ** server and the client part of a socket. **
88  ** ControllerConnection and DataManagerConnection **
89  ** are mere functional subclasses without additional **
90  ** data. **
91  ** ConnectionList provides the data structures **
92  ** necessary to use the select UNIX system call **
93  ** that allows to listen to many connections at once **
94  ** **
95  ** Classes : Connection, ServerConnection, ClientConnection, **
96  ** ControllerConnection, DataManagerConnection, **
97  ** ConnectionList **
98  ** **
99  ** Copyright (C) 1993 by University of Stuttgart **
100  ** Computer Center (RUS) **
101  ** Allmandring 30 **
102  ** 7000 Stuttgart 80 **
103  ** **
104  ** **
105  ** Author : A. Wierse (RUS) **
106  ** **
107  ** History : **
108  ** 15.04.93 Ver 1.0 **
109  ** 26.05.93 Ver 1.1 sender_type and sender_id **
110  ** introduced **
111  ** **
112  ** **
113 \***********************************************************************/
114 
116 {
117 protected:
118  friend class ServerConnection;
119  friend class ConnectionList;
120  class Socket *sock; // Socket for connection
121  int port; // port for connection
122  int sender_id; // id of the sending process
123  int send_type; // type of module for messages
124  int peer_id_; // id of the peer process
125  int peer_type_; // type of peer
126  int message_to_do; // if more than one message has been read
128  unsigned long tru;
129  char *read_buf;
131  int hostid; //hostid of remote host
132  void (*remove_socket)(int);
133  int get_id();
135 
136 public:
137  char convert_to; // to what format do we need to convert data?
138  Connection();
139  Connection(int sfd);
140  virtual ~Connection(); // close connection (for subclasses)
141 
143  {
144  return sock;
145  };
146  void set_peer(int id, int type);
147  int get_peer_id();
148  int get_peer_type();
149 
150  int is_connected() // return true if connection is established
151  {
152  if (sock == NULL)
153  return 0;
154  return (get_id() != -1);
155  };
156  virtual int receive(void *buf, unsigned nbyte); // receive from socket
157  virtual int send(const void *buf, unsigned nbyte); // send into socket
158  virtual int recv_msg(Message *msg); // receive Message
159  virtual int recv_msg_fast(Message *msg); // high-performace receive Message
160  virtual int send_msg(const Message *msg); // send Message
161  virtual int send_msg_fast(const Message *msg); // high-performance send Message
162  int check_for_input(float time = 0.0); // issue select call and return TRUE if there is an event or 0L otherwise
163  int get_port() // give port number
164  {
165  return port;
166  };
167  void set_hostid(int id);
169  {
170  return hostid;
171  };
173  {
174  return (send_type);
175  };
176  int get_id(void (*remove_func)(int)); // give socket id
178  {
179  return sender_id;
180  };
181  void close(); // send close msg for partner and delete socket
182  void close_inform(); // close without msg for partner
184  {
185  // if(message_to_do)
186  // LOGINFO( "message_to_do == 1");
187  // else
188  // LOGINFO( "message_to_do == 0");
189  return message_to_do; // message is already read
190  };
191  void print()
192  {
193  std::cerr << "port: " << port << std::endl;
194  };
195  Host *get_host();
196  const char *get_hostname();
197 };
198 
199 // Connection that acts as server
201 {
202 protected:
203  void get_dataformat();
204 
205 public:
206  // bind socket to port p
207  ServerConnection(int p, int id, int st);
208  // bind socket, port assigned automatically
209  ServerConnection(int *p, int id, int st);
211  {
212  sock = s;
213  };
214  virtual ~ServerConnection() // close connection
215  {};
216  int acceptOne(); // accept connection (after bind)
217  int acceptOne(int); // accept connection (after bind) and wait int seconds
218  int listen(); // listen for connection (after bind)
219  ServerConnection *spawn_connection(); // accept after select for open socket
220  // accept after select for open socket
221  SimpleServerConnection *spawnSimpleConnection();
222 };
223 
224 // Connection that acts as server
226 {
227  void get_dataformat();
228  char buffer[10001];
229  size_t buflen;
230 
231 public:
232  // bind socket to port p
233  SimpleServerConnection(int p, int id, int st);
234  // bind socket, port assigned automatically
235  SimpleServerConnection(int *p, int id, int st);
237  const char *readLine();
238 };
239 
240 // Connection that acts as client
242 {
245 
246 public:
247  // connect to server at port p on host h
248  // ClientConnection(Host *h, int p, int id, int st);
249  ClientConnection(Host *host, int port, int id, int senderType, int retries = 20, double timeout = 0.0);
250  ~ClientConnection(); // close connection
251 };
252 
253 // Connection that acts as client, does not send a byteorder Byte
255 {
258  char buffer[10001];
259  size_t buflen;
260 
261 public:
262  // connect to server at port p on host h
263  SimpleClientConnection(Host *, int p, int retries = 20);
264  ~SimpleClientConnection(); // close connection
265  const char *readLine();
266  void get_dataformat();
267 };
268 
270 {
271  // make connection to datamanager
272 public:
273  //DataManagerConnection(Host *hp, int pp, int my_id, int st): ClientConnection((Host*)hp, (int)pp, (int)my_id, (int) st) {};
274  DataManagerConnection(Host *hp, int pp, int my_id, int st)
275  : ClientConnection(hp, pp, my_id, st){};
276  // contact datamanager on port p at host h
277 
278  // !!! irgendwie mag die HP das nicht
279  // DataManagerConnection(int pp, int my_id, int st): ClientConnection(NULL,pp,my_id,st) {};
280  // !!! in der Version vor dem Linux Mergen war das so:
281  DataManagerConnection(int pp, int my_id, int st)
282  : ClientConnection((Host *)NULL, pp, my_id, st){};
283 
284  // contact local datamanager on port p
285  ~DataManagerConnection() // close connection
286  {};
287 };
288 
290 {
291  // make connection to controller
292 public:
293  ControllerConnection(Host *h, int p, int id, int st)
294  : ClientConnection(h, p, id, st){};
295  // contact controller on port p at host h
296  ~ControllerConnection() // close connection
297  {};
298 };
300 {
301 public:
302  UDPConnection(int id, int s_type, int p, char *address);
303 };
304 #ifdef MULTICAST
305 class NETEXPORT MulticastConnection : public Connection
306 {
307 public:
308  MulticastConnection(int id, int s_type, int p, char *MulticastGroup = "224.10.10.10", int ttl = 200);
309 };
310 #endif
311 
312 class NETEXPORT ConnectionList // list connections in a way that select can be used
313 {
314  long curidx = -1; // current index into vector
315  std::vector<Connection *> connlist; // list of connections
316  fd_set fdvar; // field for select call
317  int maxfd; // maximum socket id
318  ServerConnection *open_sock; // socket for listening
319 public:
320  ConnectionList(); // constructor
321  ConnectionList(ServerConnection *); // constructor (listens always at port)
322  ~ConnectionList(); // destructor
323  void add_open_conn(ServerConnection *c);
324  void add(Connection *c); // add connection
325  void remove(Connection *c); // remove connection
326  void deleteConnection(Connection *c);
327  Connection *get_last();
328  Connection *wait_for_input(); // issue select call and return the
329  // connection that shows the first event
330  // issue select call and return a
331  Connection *check_for_input(float time = 0.0);
332  // connection if there is an event or 0L otherwise
333  void reset();
334  Connection *next();
335  //Connection at(int index); // get specific entry from listpos i
336  int count(); // returns the number of current elements
337 };
338 
339 #ifdef HAVE_OPENSSL
340 class NETEXPORT SSLConnection : public Connection
341 {
342 public:
343  typedef int(PasswordCallback)(char *buf, int size, int rwflag, void *userdata);
344 
345  SSLConnection();
346  SSLConnection(int sfd);
347  ~SSLConnection();
348 
349  enum SSL_STATE
350  {
351  SSL_READY,
352  SSL_CLOSED
353  };
354 
355  int AttachSSLToSocket(Socket *sock);
356 
357  int receive(void *buf, unsigned nbyte); // receive from socket
358  int send(const void *buf, unsigned nbyte); // send into socket
359  int recv_msg(Message *msg); // receive Message
360  int send_msg(const Message *msg); // send Message
361  const char *readLine(); // Read line
362  int get_id(void (*remove_func)(int));
363  int get_id();
364  bool IsClosed();
365  std::string getPeerAddress();
366  SSL *mSSL;
367 
368 protected:
369  struct sockaddr_in mSA_client;
370  struct sockaddr_in mSA_server;
371 
372  static SSL_CTX *mCTX;
373  int mSFD;
374  int mClieDsc;
375  SSL_STATE mState;
376  size_t mBuflen;
377  char mBuffer[10001];
378 
379  void setPasswdCallback(PasswordCallback *, void *userData);
380  void validateConnState();
381 
382  PasswordCallback *mSSLCB;
383  void *mSSLUserData;
384 
385 private:
386 };
387 
388 class NETEXPORT SSLServerConnection : public SSLConnection
389 {
390 public:
391  SSLServerConnection(PasswordCallback *cb, void *userData);
392  SSLServerConnection(int *p, int id, int s_type, PasswordCallback *cb, void *userData);
393  SSLServerConnection(int p, int id, int st, PasswordCallback *cb, void *userData);
394  SSLServerConnection(SSLSocket *socket, PasswordCallback *cb, void *userData);
395  ~SSLServerConnection();
396 
397  int accept();
398  int sock_accept();
399  int listen();
400 
401  std::string getSSLSubjectUID();
402  std::string getSSLSubjectName();
403 
404  SSLServerConnection *spawnConnection();
405 
406 protected:
407 private:
408 };
409 
410 class NETEXPORT SSLClientConnection : public SSLConnection
411 {
412 public:
413  SSLClientConnection(Host *, int p, PasswordCallback *cb, void *userData /*,int retries = 20*/);
414  ~SSLClientConnection();
415 
416  int connect();
417  void evaluate();
418  std::string getSSLSubject();
419 
420  Host *host;
421  Host *lhost;
422 
423 protected:
424  void getErrorMessage(long result);
425 };
426 #endif
427 }
428 #endif
size_t buflen
Definition: covise_connect.h:259
Host * lhost
Definition: covise_connect.h:257
GLsizeiptr size
Definition: khronos-glext.h:6610
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: khronos-glext.h:8469
ServerConnection(Socket *s)
Definition: covise_connect.h:210
int get_port()
Definition: covise_connect.h:163
Definition: covise_connect.h:115
GLuint address
Definition: khronos-glext.h:10368
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
struct ssl_ctx_st SSL_CTX
Definition: covise_connect.h:26
int get_hostid()
Definition: covise_connect.h:168
GLuint GLuint GLsizei count
Definition: khronos-glext.h:6343
unsigned long tru
Definition: covise_connect.h:128
int get_sendertype()
Definition: covise_connect.h:172
GLuint buffer
Definition: khronos-glext.h:6606
GLfloat GLfloat GLfloat GLfloat h
Definition: khronos-glext.h:8441
const GLubyte * c
Definition: khronos-glext.h:9864
Definition: covise_host.h:18
const int EC_CLIENT
Definition: covise_connect.h:62
int sender_id
Definition: covise_connect.h:122
GLfloat GLfloat p
Definition: khronos-glext.h:9861
GLbitfield GLuint64 timeout
Definition: khronos-glext.h:7882
int hostid
Definition: covise_connect.h:131
virtual ~ServerConnection()
Definition: covise_connect.h:214
ServerConnection * open_sock
Definition: covise_connect.h:318
int bytes_to_process
Definition: covise_connect.h:127
int has_message()
Definition: covise_connect.h:183
char * read_buf
Definition: covise_connect.h:129
int send_type
Definition: covise_connect.h:123
int peer_type_
Definition: covise_connect.h:125
Definition: covise_socket.h:125
int maxfd
Definition: covise_connect.h:317
int port
Definition: covise_connect.h:121
std::vector< Connection * > connlist
Definition: covise_connect.h:315
Definition: covise_connect.h:200
struct ssl_st SSL
Definition: covise_connect.h:25
size_t buflen
Definition: covise_connect.h:229
GLboolean reset
Definition: khronos-glext.h:6369
DataManagerConnection(int pp, int my_id, int st)
Definition: covise_connect.h:281
Definition: covise_connect.h:254
GLuint64EXT * result
Definition: khronos-glext.h:12573
Host * lhost
Definition: covise_connect.h:244
Definition: covise_connect.h:241
ControllerConnection(Host *h, int p, int id, int st)
Definition: covise_connect.h:293
class Socket * sock
Definition: covise_connect.h:120
~DataManagerConnection()
Definition: covise_connect.h:285
Socket * getSocket()
Definition: covise_connect.h:142
GLdouble s
Definition: khronos-glext.h:6441
~ControllerConnection()
Definition: covise_connect.h:296
int message_to_do
Definition: covise_connect.h:126
char convert_to
Definition: covise_connect.h:137
int get_sender_id()
Definition: covise_connect.h:177
int peer_id_
Definition: covise_connect.h:124
#define NETEXPORT
Definition: coExport.h:361
Host * other_host
Definition: covise_connect.h:130
Definition: covise_connect.h:289
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
Definition: covise_connect.h:269
void print()
Definition: covise_connect.h:191
Definition: covise_connect.h:299
GLenum type
Definition: khronos-glext.h:6279
Definition: covise_connect.h:312
int is_connected()
Definition: covise_connect.h:150
Host * host
Definition: covise_connect.h:256
fd_set fdvar
Definition: covise_connect.h:316
struct ssl_method_st SSL_METHOD
Definition: covise_connect.h:27
const int EC_SERVER
Definition: covise_connect.h:61
#define NULL
Definition: covise_list.h:22
Host * host
Definition: covise_connect.h:243
int * header_int
Definition: covise_connect.h:134
Definition: message.h:111
Definition: covise_connect.h:225
DataManagerConnection(Host *hp, int pp, int my_id, int st)
Definition: covise_connect.h:274