ETSI STF525 / Internal Testing test suite
This project provides an internal testing test suite and its associated Test Adapter/Codec
Abstract_Socket.hh
Go to the documentation of this file.
1 /******************************************************************************
2 * Copyright (c) 2004, 2014 Ericsson AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Zoltan Bibo - initial implementation and initial documentation
10 * Gergely Futo
11 * Oliver Ferenc Czerman
12 * Balasko Jeno
13 * Zoltan Bibo
14 * Eduard Czimbalmos
15 * Kulcsár Endre
16 * Gabor Szalai
17 * Jozsef Gyurusi
18 * Csöndes Tibor
19 * Zoltan Jasz
20 ******************************************************************************/
21 //
22 // File: Abstract_Socket.hh
23 // Description: Abstract_Socket header file
24 // Rev: R9B
25 // Prodnr: CNL 113 384
26 //
27 
28 
29 #ifndef Abstract_Socket_HH
30 #define Abstract_Socket_HH
31 
32 #ifdef AS_USE_SSL
33 #include <openssl/ssl.h>
34 #include <openssl/rand.h>
35 #include <openssl/err.h>
36 #endif
37 
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <TTCN3.hh>
41 
42 // to support systems not supporting IPv6 define AF_INET6 to some dummy value:
43 #ifndef AF_INET6
44 #define AF_INET6 (-255)
45 #endif
46 
48 public:
49  // Byte order in the header
51 private:
52  unsigned long length_offset;
53  unsigned long nr_bytes_in_length;
56  unsigned long length_multiplier;
57 public:
58  PacketHeaderDescr(unsigned long p_length_offset,
59  unsigned long p_nr_bytes_in_length, HeaderByteOrder p_byte_order,
60  long p_value_offset = 0, unsigned long p_length_multiplier = 1)
61  : length_offset(p_length_offset), nr_bytes_in_length(p_nr_bytes_in_length),
62  byte_order(p_byte_order), value_offset(p_value_offset),
63  length_multiplier(p_length_multiplier) { }
64 
65  // returns the message length
66  unsigned long Get_Message_Length(const unsigned char* buffer_pointer) const;
67  // returns the number of bytes needed to have a valid message length
68  inline unsigned long Get_Valid_Header_Length() const
69  { return length_offset + nr_bytes_in_length; }
70 };
71 
73 {
74 protected:
75  enum TCP_STATES {CLOSED, LISTEN, ESTABLISHED, CLOSE_WAIT, FIN_WAIT};
76  enum READING_STATES {STATE_DONT_RECEIVE, STATE_WAIT_FOR_RECEIVE_CALLBACK, STATE_BLOCK_FOR_SENDING, STATE_DONT_CLOSE, STATE_NORMAL};
77  // client data
79  void *user_data; // pointer to any additional data needed by the user
80  TTCN_Buffer *fd_buff; // pointer to the data buffer
81  struct sockaddr_storage clientAddr;// client address
82 #if defined LINUX || defined FREEBSD || defined SOLARIS8
83  socklen_t
84 #else /* SOLARIS or WIN32 */
85  int
86 #endif
88  TCP_STATES tcp_state; // TCP state
89  READING_STATES reading_state; //used when SSL_write returns SSL_ERROR_WANT_READ an we are using non-blocking socket
90  };
91 
93  Abstract_Socket(const char *testport_type, const char *testport_name);
94  virtual ~Abstract_Socket();
95 
96  // Shall be called from set_parameter()
97  bool parameter_set(const char *parameter_name, const char *parameter_value);
98  // Shall be called from user_map()
99  void map_user();
100  // Shall be called from user_unmap()
101  void unmap_user();
102 
103  // puts the IP address in the addr
104  void get_host_id(const char* hostName, struct sockaddr_in *addr); /* This function should not be used! Use getaddrinfo instead! */
105 
106  // Closes the current listening port and opens the specified one
107  int open_listen_port(const struct sockaddr_in & localAddr); /* This function should be removed! Deprecated by: */
108  int open_listen_port(const char* localHostname, const char* localServicename);
109  // Closes the current listening port
110  void close_listen_port();
111 
112  virtual void listen_port_opened(int port_number);
113 
114  // Opens a new client connection
115  int open_client_connection(const struct sockaddr_in & new_remote_addr, const struct sockaddr_in & new_local_addr); /* This function should be removed! Deprecated by: */
116  int open_client_connection(const char* remoteHostname, const char* remoteService, const char* localHostname, const char* localService);
117 
118  virtual void client_connection_opened(int client_id);
119 
120  // Shall be called from Handle_Fd_Event()
121  void Handle_Socket_Event(int fd, boolean is_readable, boolean is_writable, boolean is_error);
122  // Shall be called from Handle_Timeout() - for possible future development
123  void Handle_Timeout_Event(double /*time_since_last_call*/) {};
124 
125  // Shall be called from outgoing_send()
126  void send_outgoing(const unsigned char* message_buffer, int length, int client_id = -1);
127  void send_shutdown(int client_id = -1);
128 
129  // Access to private variables
130  bool get_nagling() const {return nagling;}
131  bool get_use_non_blocking_socket() const {return use_non_blocking_socket;};
132  bool get_server_mode() const {return server_mode;}
133  bool get_socket_debugging() const {return socket_debugging;}
134  bool get_halt_on_connection_reset() const {return halt_on_connection_reset;}
135  bool get_use_connection_ASPs() const {return use_connection_ASPs;}
136  bool get_handle_half_close() const {return handle_half_close;}
137  int get_socket_fd() const;
138  int get_listen_fd() const {return listen_fd;}
139 
140  //set non-blocking mode
141  int set_non_block_mode(int fd, bool enable_nonblock);
142 
143  //increase buffer size
144  bool increase_send_buffer(int fd, int &old_size, int& new_size);
145 
146  const char* get_local_host_name(){return local_host_name; };
147  unsigned int get_local_port_number(){return local_port_number; };
148  const char* get_remote_host_name(){return remote_host_name; };
149  unsigned int get_remote_port_number(){return remote_port_number; };
150  const struct sockaddr_in & get_remote_addr() {return remoteAddr; }; /* FIXME: This function is deprecated and should be removed! */
151  const struct sockaddr_in & get_local_addr() {return localAddr; }; /* FIXME: This function is deprecated and should be removed! */
152  const int& get_ai_family() const {return ai_family;}
153  void set_ai_family(int parameter_value) {ai_family=parameter_value;}
154  bool get_ttcn_buffer_usercontrol() const {return ttcn_buffer_usercontrol; }
155  void set_nagling(bool parameter_value) {nagling=parameter_value;}
156  void set_server_mode(bool parameter_value) {server_mode=parameter_value;}
157  void set_handle_half_close(bool parameter_value) {handle_half_close=parameter_value;}
158  void set_socket_debugging(bool parameter_value) {socket_debugging=parameter_value;}
159  void set_halt_on_connection_reset(bool parameter_value) {halt_on_connection_reset=parameter_value;}
160  void set_ttcn_buffer_usercontrol(bool parameter_value) {ttcn_buffer_usercontrol=parameter_value;}
161  const char *test_port_type;
162  const char *test_port_name;
163 
164  // Called when a message is received
165  virtual void message_incoming(const unsigned char* message_buffer, int length, int client_id = -1) = 0;
166 
167  virtual void Add_Fd_Read_Handler(int fd) = 0;
168  virtual void Add_Fd_Write_Handler(int fd) = 0;
169  virtual void Remove_Fd_Read_Handler(int fd) = 0;
170  virtual void Remove_Fd_Write_Handler(int fd) = 0;
171  virtual void Remove_Fd_All_Handlers(int fd) = 0;
172  virtual void Handler_Uninstall() = 0;
173  virtual void Timer_Set_Handler(double call_interval, boolean is_timeout = TRUE,
174  boolean call_anyway = TRUE, boolean is_periodic = TRUE) = 0; // unused - for possible future development
175  virtual const PacketHeaderDescr* Get_Header_Descriptor() const;
176 
177  // Logging functions
178  void log_debug(const char *fmt, ...) const
179  __attribute__ ((__format__ (__printf__, 2, 3)));
180  void log_warning(const char *fmt, ...) const
181  __attribute__ ((__format__ (__printf__, 2, 3)));
182  void log_error(const char *fmt, ...) const
183  __attribute__ ((__format__ (__printf__, 2, 3), __noreturn__));
184  void log_hex(const char *prompt, const unsigned char *msg, size_t length) const;
185 
186  // Called when a message is to be received (an event detected)
187  virtual int receive_message_on_fd(int client_id);
188  // Called when a message is to be sent
189  virtual int send_message_on_fd(int client_id, const unsigned char* message_buffer, int message_length);
190  virtual int send_message_on_nonblocking_fd(int client_id, const unsigned char *message_buffer, int message_length);
191  // Called after a peer is connected
192  virtual void peer_connected(int client_id, sockaddr_in& remote_addr); /* This function should be removed! deprecated by: */
193  virtual void peer_connected(int /*client_id*/, const char * /*host*/, const int /*port*/) {};
194  // Called after a peer is disconnected
195  virtual void peer_disconnected(int client_id);
196  // Called when a peer shut down its fd for writing
197  virtual void peer_half_closed(int client_id);
198  // Called after a send error
199  virtual void report_error(int client_id, int msg_length, int sent_length, const unsigned char* msg, const char* error_text);
200  // Called after a unsent message
201  virtual void report_unsent(int client_id, int msg_length, int sent_length, const unsigned char* msg, const char* error_text);
202 
203  // Test port parameters
204  virtual const char* local_port_name();
205  virtual const char* remote_address_name();
206  virtual const char* local_address_name();
207  virtual const char* remote_port_name();
208  virtual const char* ai_family_name();
209  virtual const char* use_connection_ASPs_name();
210  virtual const char* halt_on_connection_reset_name();
211  virtual const char* client_TCP_reconnect_name();
212  virtual const char* TCP_reconnect_attempts_name();
213  virtual const char* TCP_reconnect_delay_name();
214  virtual const char* server_mode_name();
215  virtual const char* socket_debugging_name();
216  virtual const char* nagling_name();
217  virtual const char* use_non_blocking_socket_name();
218  virtual const char* server_backlog_name();
219 
220  // Fetch/Set user data pointer
221  void* get_user_data(int client_id) {return get_peer(client_id)->user_data;}
222  void set_user_data(int client_id, void *uptr) {get_peer(client_id)->user_data = uptr;}
223  // Called after a TCP connection is established
224  virtual bool add_user_data(int client_id);
225  // Called before the TCP connection is drop down
226  virtual bool remove_user_data(int client_id);
227  // Called when a client shall be removed
228  virtual void remove_client(int client_id);
229  // Called when all clients shall be removed
230  virtual void remove_all_clients();
231  // Called at the beginning of map() to check mandatory parameter presence
232  virtual bool user_all_mandatory_configparameters_present();
233  TTCN_Buffer *get_buffer(int client_id) {return get_peer(client_id)->fd_buff; }
234 
235  // Client data management functions
236  // add peer to the list
237  as_client_struct *peer_list_add_peer(int client_id);
238  // remove peer from list
239  void peer_list_remove_peer(int client_id);
240  // remove all peers from list
241  void peer_list_reset_peer();
242  // returns back the structure of the peer
243  as_client_struct *get_peer(int client_id, bool no_error=false) const;
244  // length of the list
245  int peer_list_get_length() const { return peer_list_length; }
246  // number of peers in the list
247  int peer_list_get_nr_of_peers() const;
248  // fd of the last peer in the list
249  int peer_list_get_last_peer() const;
250  // fd of the first peer in the list
251  int peer_list_get_first_peer() const;
252 
253 
254 private:
255  void handle_message(int client_id = -1);
256  void all_mandatory_configparameters_present();
266  bool nagling;
270  unsigned int local_port_number;
272  unsigned int remote_port_number;
273  int ai_family; // address family to use
274  // remoteAddr and localAddr is filled when map_user is called
275  struct sockaddr_in remoteAddr; /* FIXME: not used! should be removed */
276  struct sockaddr_in localAddr; /* FIXME: not used! should be removed */
281 
282  // Client data management functions
284  void peer_list_resize_list(int client_id);
285 };
286 
287 
288 
289 #ifdef AS_USE_SSL
290 
291 class SSL_Socket: public Abstract_Socket
292 {
293 
294 protected:
295  SSL_Socket();
296  SSL_Socket(const char *tp_type, const char *tp_name);
297  virtual ~SSL_Socket();
298 
299  bool parameter_set(const char * parameter_name, const char * parameter_value);
300  // Called after a TCP connection is established (client side or server accepted a connection).
301  // It will create a new SSL conenction on the top of the TCP connection.
302  virtual bool add_user_data(int client_id);
303  // Called after a TCP connection is closed.
304  // It will delete the SSL conenction.
305  virtual bool remove_user_data(int client_id);
306  // Called from all_mandatory_configparameters_present() function
307  // during map() operation to check mandatory parameter presents.
308  virtual bool user_all_mandatory_configparameters_present();
309  // Called after an SSL connection is established (handshake finished) for further
310  // authentication. Shall return 'true' if verification
311  // is OK, otherwise 'false'. If return value was 'true', the connection is kept, otherwise
312  // the connection will be shutted down.
313  virtual bool ssl_verify_certificates();
314  // Call during SSL handshake (and rehandshake as well) by OpenSSL
315  // Return values:
316  // ==1: user authentication is passed, go on with handshake
317  // ==0: user authentication failed, refuse the connection to the other peer
318  // <0 : user don't care, go on with default basic checks
319  virtual int ssl_verify_certificates_at_handshake(int preverify_ok, X509_STORE_CTX *ssl_ctx);
320  // Called to receive from the socket if data is available (select()).
321  // Shall return with 0 if the peer is disconnected or with the number of bytes read.
322  // If error occured, execution shall stop in the function by calling log_error()
323  virtual int receive_message_on_fd(int client_id);
324  // Called to send a message on the socket.
325  // Shall return with 0 if the peer is disconnected or with the number of bytes written.
326  // If error occured, execution shall stop in the function by calling log_error()
327  virtual int send_message_on_fd(int client_id, const unsigned char * message_buffer, int length_of_message);
328  virtual int send_message_on_nonblocking_fd(int client_id, const unsigned char * message_buffer, int length_of_message);
329 
330  // The following members can be called to fetch the current values
331  bool get_ssl_use_ssl() const {return ssl_use_ssl;}
332  bool get_ssl_verifycertificate() const {return ssl_verify_certificate;}
333  bool get_ssl_use_session_resumption() const {return ssl_use_session_resumption;}
334  bool get_ssl_initialized() const {return ssl_initialized;}
335  char * get_ssl_key_file() const {return ssl_key_file;}
336  char * get_ssl_certificate_file() const {return ssl_certificate_file;}
337  char * get_ssl_trustedCAlist_file() const {return ssl_trustedCAlist_file;}
338  char * get_ssl_cipher_list() const {return ssl_cipher_list;}
339  char * get_ssl_password() const;
340  const unsigned char * get_ssl_server_auth_session_id_context() const {return ssl_server_auth_session_id_context;}
341 // const SSL_METHOD * get_current_ssl_method() const {return ssl_method;}
342 // const SSL_CIPHER * get_current_ssl_cipher() const {return ssl_cipher;}
343  SSL_SESSION* get_current_ssl_session() const {return ssl_session;}
344  SSL_CTX * get_current_ssl_ctx() const {return ssl_ctx;}
345  SSL * get_current_ssl() const {return ssl_current_ssl;}
346 
347  // The following members can be called to set the current values
348  // NOTE that in case the parameter_value is a char *pointer, the old character
349  // array is deleted by these functions automatically.
350  void set_ssl_use_ssl(bool parameter_value);
351  void set_ssl_verifycertificate(bool parameter_value);
352  void set_ssl_use_session_resumption(bool parameter_value);
353  void set_ssl_key_file(char * parameter_value);
354  void set_ssl_certificate_file(char * parameter_value);
355  void set_ssl_trustedCAlist_file(char * parameter_value);
356  void set_ssl_cipher_list(char * parameter_value);
357  void set_ssl_server_auth_session_id_context(const unsigned char * parameter_value);
358 
359  // The following members can be called to fetch the default test port parameter names
360  virtual const char* ssl_use_ssl_name();
361  virtual const char* ssl_use_session_resumption_name();
362  virtual const char* ssl_private_key_file_name();
363  virtual const char* ssl_trustedCAlist_file_name();
364  virtual const char* ssl_certificate_file_name();
365  virtual const char* ssl_password_name();
366  virtual const char* ssl_cipher_list_name();
367  virtual const char* ssl_verifycertificate_name();
368  virtual const char* ssl_disable_SSLv2();
369  virtual const char* ssl_disable_SSLv3();
370  virtual const char* ssl_disable_TLSv1();
371  virtual const char* ssl_disable_TLSv1_1();
372  virtual const char* ssl_disable_TLSv1_2();
373 
374 private:
375  bool ssl_verify_certificate; // verify other part's certificate or not
376  bool ssl_use_ssl; // whether to use SSL
377  bool ssl_initialized; // whether SSL already initialized or not
378  bool ssl_use_session_resumption; // use SSL sessions or not
379 
380  bool SSLv2;
381  bool SSLv3;
382  bool TLSv1;
383  bool TLSv1_1;
384  bool TLSv1_2;
385 
386 
387  char *ssl_key_file; // private key file
388  char *ssl_certificate_file; // own certificate file
389  char *ssl_trustedCAlist_file; // trusted CA list file
390  char *ssl_cipher_list; // ssl_cipher list restriction to apply
391  char *ssl_password; // password to decode the private key
392  static const unsigned char * ssl_server_auth_session_id_context;
393 
394 // const SSL_METHOD *ssl_method; // SSL context method
395  SSL_CTX *ssl_ctx; // SSL context
396 // const SSL_CIPHER *ssl_cipher; // used SSL ssl_cipher
397  SSL_SESSION *ssl_session; // SSL ssl_session
398  SSL *ssl_current_ssl; // currently used SSL object
399  static void *ssl_current_client; // current SSL object, used only during authentication
400 
401  void ssl_actions_to_seed_PRNG(); // Seed the PRNG with enough random data
402  void ssl_init_SSL(); // Initialize SSL libraries and create the SSL context
403  void ssl_log_SSL_info(); // Log the currently used SSL setting (debug)
404  int ssl_getresult(int result_code); // Fetch and log the SSL error code from I/O operation result codes
405  // Callback function to pass the password to OpenSSL. Called by OpenSSL
406  // during SSL handshake.
407  static int ssl_password_cb(char * password_buffer, int length_of_password, int rw_flag, void * user_data);
408  // Callback function to perform authentication during SSL handshake. Called by OpenSSL.
409  // NOTE: for further authentication, use ssl_verify_certificates().
410  static int ssl_verify_callback(int preverify_status, X509_STORE_CTX * ssl_context);
411 };
412 #endif
413 
414 #endif
void set_ttcn_buffer_usercontrol(bool parameter_value)
Definition: Abstract_Socket.hh:160
bool halt_on_connection_reset
Definition: Abstract_Socket.hh:258
bool get_ttcn_buffer_usercontrol() const
Definition: Abstract_Socket.hh:154
unsigned int get_remote_port_number()
Definition: Abstract_Socket.hh:149
Definition: Abstract_Socket.hh:76
Definition: Abstract_Socket.hh:47
void set_socket_debugging(bool parameter_value)
Definition: Abstract_Socket.hh:158
unsigned long Get_Valid_Header_Length() const
Definition: Abstract_Socket.hh:68
bool use_non_blocking_socket
Definition: Abstract_Socket.hh:267
bool get_server_mode() const
Definition: Abstract_Socket.hh:132
void set_ai_family(int parameter_value)
Definition: Abstract_Socket.hh:153
unsigned long length_offset
Definition: Abstract_Socket.hh:52
unsigned int local_port_number
Definition: Abstract_Socket.hh:270
long value_offset
Definition: Abstract_Socket.hh:55
void * get_user_data(int client_id)
Definition: Abstract_Socket.hh:221
bool handle_half_close
Definition: Abstract_Socket.hh:264
bool get_handle_half_close() const
Definition: Abstract_Socket.hh:136
unsigned long nr_bytes_in_length
Definition: Abstract_Socket.hh:53
void * user_data
Definition: Abstract_Socket.hh:79
const struct sockaddr_in & get_remote_addr()
Definition: Abstract_Socket.hh:150
Definition: Abstract_Socket.hh:78
const char * test_port_type
Definition: Abstract_Socket.hh:161
char * local_host_name
Definition: Abstract_Socket.hh:269
bool socket_debugging
Definition: Abstract_Socket.hh:265
Definition: Abstract_Socket.hh:50
TTCN_Buffer * get_buffer(int client_id)
Definition: Abstract_Socket.hh:233
Definition: Abstract_Socket.hh:50
READING_STATES
Definition: Abstract_Socket.hh:76
void set_server_mode(bool parameter_value)
Definition: Abstract_Socket.hh:156
int listen_fd
Definition: Abstract_Socket.hh:279
bool get_nagling() const
Definition: Abstract_Socket.hh:130
void Handle_Timeout_Event(double)
Definition: Abstract_Socket.hh:123
unsigned int get_local_port_number()
Definition: Abstract_Socket.hh:147
PacketHeaderDescr(unsigned long p_length_offset, unsigned long p_nr_bytes_in_length, HeaderByteOrder p_byte_order, long p_value_offset=0, unsigned long p_length_multiplier=1)
Definition: Abstract_Socket.hh:58
unsigned long Get_Message_Length(const unsigned char *buffer_pointer) const
Definition: Abstract_Socket.cc:68
void set_halt_on_connection_reset(bool parameter_value)
Definition: Abstract_Socket.hh:159
void set_user_data(int client_id, void *uptr)
Definition: Abstract_Socket.hh:222
unsigned int remote_port_number
Definition: Abstract_Socket.hh:272
TCP_STATES tcp_state
Definition: Abstract_Socket.hh:88
int ai_family
Definition: Abstract_Socket.hh:273
bool get_halt_on_connection_reset() const
Definition: Abstract_Socket.hh:134
READING_STATES reading_state
Definition: Abstract_Socket.hh:89
void set_handle_half_close(bool parameter_value)
Definition: Abstract_Socket.hh:157
Definition: Abstract_Socket.hh:72
int peer_list_get_length() const
Definition: Abstract_Socket.hh:245
bool use_connection_ASPs
Definition: Abstract_Socket.hh:263
char * remote_host_name
Definition: Abstract_Socket.hh:271
bool ttcn_buffer_usercontrol
Definition: Abstract_Socket.hh:268
TTCN_Buffer * fd_buff
Definition: Abstract_Socket.hh:80
TCP_STATES
Definition: Abstract_Socket.hh:75
bool server_mode
Definition: Abstract_Socket.hh:262
const char * get_remote_host_name()
Definition: Abstract_Socket.hh:148
unsigned long length_multiplier
Definition: Abstract_Socket.hh:56
void set_nagling(bool parameter_value)
Definition: Abstract_Socket.hh:155
int clientAddrlen
Definition: Abstract_Socket.hh:87
bool halt_on_connection_reset_set
Definition: Abstract_Socket.hh:257
HeaderByteOrder
Definition: Abstract_Socket.hh:50
int TCP_reconnect_attempts
Definition: Abstract_Socket.hh:260
int deadlock_counter
Definition: Abstract_Socket.hh:278
bool nagling
Definition: Abstract_Socket.hh:266
bool get_socket_debugging() const
Definition: Abstract_Socket.hh:133
bool get_use_non_blocking_socket() const
Definition: Abstract_Socket.hh:131
int TCP_reconnect_delay
Definition: Abstract_Socket.hh:261
int get_listen_fd() const
Definition: Abstract_Socket.hh:138
bool client_TCP_reconnect
Definition: Abstract_Socket.hh:259
const char * test_port_name
Definition: Abstract_Socket.hh:162
int peer_list_length
Definition: Abstract_Socket.hh:280
int server_backlog
Definition: Abstract_Socket.hh:277
const char * get_local_host_name()
Definition: Abstract_Socket.hh:146
const int & get_ai_family() const
Definition: Abstract_Socket.hh:152
HeaderByteOrder byte_order
Definition: Abstract_Socket.hh:54
const struct sockaddr_in & get_local_addr()
Definition: Abstract_Socket.hh:151
bool get_use_connection_ASPs() const
Definition: Abstract_Socket.hh:135
as_client_struct ** peer_list_root
Definition: Abstract_Socket.hh:283