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 
13 #include <fcntl.h>
14 #ifdef _WIN32
15 #include <io.h>
16 #else
17 #include <netinet/in.h>
18 
19 #endif
20 
21 #include <util/coExport.h>
22 #include <util/covise_list.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 accept(); // accept connection (after bind)
217  int accept(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  int 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 *, int, int, int, 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  int 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  List<Connection> *connlist; // list of connections
315  fd_set fdvar; // field for select call
316  int maxfd; // maximum socket id
317  ServerConnection *open_sock; // socket for listening
318 public:
319  ConnectionList(); // constructor
320  ConnectionList(ServerConnection *); // constructor (listens always at port)
321  ~ConnectionList(); // destructor
322  void add_open_conn(ServerConnection *c);
323  void add(Connection *c); // add connection
324  void remove(Connection *c); // remove connection
325  void deleteConnection(Connection *c);
326  Connection *get_last() // get connection made recently
327  {
328  return connlist->get_last();
329  };
330  Connection *wait_for_input(); // issue select call and return the
331  // connection that shows the first event
332  // issue select call and return a
333  Connection *check_for_input(float time = 0.0);
334  // connection if there is an event or 0L otherwise
335  void reset() //
336  {
337  connlist->reset();
338  };
340  {
341  return connlist->next();
342  };
343  //Connection at(int index); // get specific entry from listpos i
344  int count(); // returns the number of current elements
345 };
346 
347 #ifdef HAVE_OPENSSL
348 class NETEXPORT SSLConnection : public Connection
349 {
350 public:
351  typedef int(PasswordCallback)(char *buf, int size, int rwflag, void *userdata);
352 
353  SSLConnection();
354  SSLConnection(int sfd);
355  ~SSLConnection();
356 
357  enum SSL_STATE
358  {
359  SSL_READY,
360  SSL_CLOSED
361  };
362 
363  int AttachSSLToSocket(Socket *sock);
364 
365  int receive(void *buf, unsigned nbyte); // receive from socket
366  int send(const void *buf, unsigned nbyte); // send into socket
367  int recv_msg(Message *msg); // receive Message
368  int send_msg(const Message *msg); // send Message
369  const char *readLine(); // Read line
370  int get_id(void (*remove_func)(int));
371  int get_id();
372  bool IsClosed();
373  std::string getPeerAddress();
374  SSL *mSSL;
375 
376 protected:
377  struct sockaddr_in mSA_client;
378  struct sockaddr_in mSA_server;
379 
380  static SSL_CTX *mCTX;
381  int mSFD;
382  int mClieDsc;
383  SSL_STATE mState;
384  int mBuflen;
385  char mBuffer[10001];
386 
387  void setPasswdCallback(PasswordCallback *, void *userData);
388  void validateConnState();
389 
390  PasswordCallback *mSSLCB;
391  void *mSSLUserData;
392 
393 private:
394 };
395 
396 class NETEXPORT SSLServerConnection : public SSLConnection
397 {
398 public:
399  SSLServerConnection(PasswordCallback *cb, void *userData);
400  SSLServerConnection(int *p, int id, int s_type, PasswordCallback *cb, void *userData);
401  SSLServerConnection(int p, int id, int st, PasswordCallback *cb, void *userData);
402  SSLServerConnection(SSLSocket *socket, PasswordCallback *cb, void *userData);
403  ~SSLServerConnection();
404 
405  int accept();
406  int sock_accept();
407  int listen();
408 
409  std::string getSSLSubjectUID();
410  std::string getSSLSubjectName();
411 
412  SSLServerConnection *spawnConnection();
413 
414 protected:
415 private:
416 };
417 
418 class NETEXPORT SSLClientConnection : public SSLConnection
419 {
420 public:
421  SSLClientConnection(Host *, int p, PasswordCallback *cb, void *userData /*,int retries = 20*/);
422  ~SSLClientConnection();
423 
424  int connect();
425  void evaluate();
426  std::string getSSLSubject();
427 
428  Host *host;
429  Host *lhost;
430 
431 protected:
432  void getErrorMessage(long result);
433 };
434 #endif
435 }
436 #endif
Definition: covise_host.h:17
int buflen
Definition: covise_connect.h:229
int get_port()
Definition: covise_connect.h:163
GLdouble s
Definition: khronos-glext.h:6441
Definition: covise_connect.h:115
int get_hostid()
Definition: covise_connect.h:168
unsigned long tru
Definition: covise_connect.h:128
int get_sendertype()
Definition: covise_connect.h:172
Definition: covise_connect.h:200
Definition: covise_connect.h:269
Connection * next()
Definition: covise_connect.h:339
struct ssl_method_st SSL_METHOD
Definition: covise_connect.h:27
#define NULL
Definition: covise_list.h:22
int sender_id
Definition: covise_connect.h:122
Definition: message.h:111
Definition: covise_socket.h:125
void reset()
Definition: covise_connect.h:335
int hostid
Definition: covise_connect.h:131
ServerConnection * open_sock
Definition: covise_connect.h:317
int bytes_to_process
Definition: covise_connect.h:127
List< Connection > * connlist
Definition: covise_connect.h:314
int has_message()
Definition: covise_connect.h:183
char * read_buf
Definition: covise_connect.h:129
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
int send_type
Definition: covise_connect.h:123
int peer_type_
Definition: covise_connect.h:125
int maxfd
Definition: covise_connect.h:316
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: khronos-glext.h:8469
Definition: covise_connect.h:299
int port
Definition: covise_connect.h:121
ServerConnection(Socket *s)
Definition: covise_connect.h:210
DataManagerConnection(int pp, int my_id, int st)
Definition: covise_connect.h:281
GLenum type
Definition: khronos-glext.h:6279
const int EC_SERVER
Definition: covise_connect.h:61
GLuint address
Definition: khronos-glext.h:10368
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
~DataManagerConnection()
Definition: covise_connect.h:285
struct ssl_ctx_st SSL_CTX
Definition: covise_connect.h:26
GLuint GLuint GLsizei count
Definition: khronos-glext.h:6343
Connection * get_last()
Definition: covise_connect.h:326
Definition: covise_list.h:53
Host * lhost
Definition: covise_connect.h:244
#define NETEXPORT
Definition: coExport.h:343
Definition: covise_connect.h:241
ControllerConnection(Host *h, int p, int id, int st)
Definition: covise_connect.h:293
GLsizeiptr size
Definition: khronos-glext.h:6610
class Socket * sock
Definition: covise_connect.h:120
Socket * getSocket()
Definition: covise_connect.h:142
~ControllerConnection()
Definition: covise_connect.h:296
int message_to_do
Definition: covise_connect.h:126
char convert_to
Definition: covise_connect.h:137
const GLubyte * c
Definition: khronos-glext.h:9864
int get_sender_id()
Definition: covise_connect.h:177
int peer_id_
Definition: covise_connect.h:124
GLfloat GLfloat p
Definition: khronos-glext.h:9861
Host * host
Definition: covise_connect.h:256
Host * other_host
Definition: covise_connect.h:130
Definition: covise_connect.h:289
GLuint buffer
Definition: khronos-glext.h:6606
GLfloat GLfloat GLfloat GLfloat h
Definition: khronos-glext.h:8441
void print()
Definition: covise_connect.h:191
Host * lhost
Definition: covise_connect.h:257
Definition: covise_connect.h:312
int is_connected()
Definition: covise_connect.h:150
fd_set fdvar
Definition: covise_connect.h:315
DataManagerConnection(Host *hp, int pp, int my_id, int st)
Definition: covise_connect.h:274
struct ssl_st SSL
Definition: covise_connect.h:25
Definition: covise_connect.h:225
const int EC_CLIENT
Definition: covise_connect.h:62
Definition: covise_connect.h:254
GLbitfield GLuint64 timeout
Definition: khronos-glext.h:7882
Host * host
Definition: covise_connect.h:243
int buflen
Definition: covise_connect.h:259
virtual ~ServerConnection()
Definition: covise_connect.h:214
int * header_int
Definition: covise_connect.h:134
GLuint64EXT * result
Definition: khronos-glext.h:12573