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