• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

sst/elements/genericProc/programs/libcprops/socket.h

Go to the documentation of this file.
00001 #ifndef _CP_SOCKET_H
00002 #define _CP_SOCKET_H
00003 
00004 /**
00005  * @addtogroup cp_socket
00006  */
00007 /** @{ */
00008 /**
00009  * @file
00010  * definitions for socket abstraction api
00011  */
00012 
00013 #include "common.h"
00014 
00015 #ifdef _WINDOWS
00016 #include <Winsock2.h>
00017 #endif
00018 
00019 __BEGIN_DECLS
00020 
00021 #include "config.h"
00022 
00023 #include "hashlist.h"
00024 #include "vector.h"
00025 #include "thread.h"
00026 
00027 #if defined(unix) || defined(__unix__) || defined(__MACH__)
00028 #include <sys/time.h>
00029 #include <netinet/in.h>
00030 #endif
00031 
00032 #ifdef CP_USE_SSL
00033 #include <openssl/ssl.h>
00034 #endif
00035 
00036 #if defined(sun) || defined(__OpenBSD__)
00037 #include <sys/types.h>
00038 #include <sys/socket.h>
00039 #endif
00040 
00041 #ifdef _WINDOWS
00042 #include "Winsock2.h"
00043 #endif 
00044 
00045 #define CPSOCKET_DEFAULT_BACKLOG                 50
00046 #define CPSOCKET_DEFAULT_DELAY_SEC                1
00047 #define CPSOCKET_DEFAULT_DELAY_USEC               0 
00048 #define CPSOCKET_THREADPOOL_DEFAULT_SIZE_MIN      5
00049 #define CPSOCKET_THREADPOOL_DEFAULT_SIZE_MAX     50
00050 
00051 /** recommended to call before using socket functions */
00052 CPROPS_DLL
00053 void cp_socket_init();
00054 /** 
00055  * call from signal handler to stop sockets in waiting select() and close all 
00056  * connections
00057  */
00058 CPROPS_DLL
00059 void cp_socket_stop_all();
00060 /** performs cleanup */
00061 CPROPS_DLL
00062 void cp_socket_shutdown();
00063 
00064 #ifdef CP_USE_SSL
00065 CPROPS_DLL
00066 void cp_socket_ssl_init();
00067 CPROPS_DLL
00068 void cp_socket_ssl_shutdown();
00069 
00070 CPROPS_DLL
00071 SSL_CTX *get_ssl_ctx(char *certificate_file, char *key_file);
00072 CPROPS_DLL
00073 SSL_CTX *get_client_ssl_ctx(char *CA_file, char *CA_path);
00074 #endif
00075 
00076 /**
00077  * add callback to be made on tcp layer shutdown
00078  */
00079 CPROPS_DLL
00080 void cp_tcp_add_shutdown_callback(void (*cb)(void *), void *prm);
00081 
00082 
00083 /**
00084  * cp_socket connection handling strategy
00085  */
00086 typedef CPROPS_DLL enum 
00087 { 
00088         CPSOCKET_STRATEGY_CALLBACK,
00089         CPSOCKET_STRATEGY_THREADPOOL
00090 } cp_socket_strategy;
00091 
00092 CPROPS_DLL 
00093 struct _cp_socket; /* fwd, uh, declaration */
00094 
00095 /** thread function for threadpool implementation */
00096 typedef void *(*cp_socket_thread_function)(void *);
00097 /** function prototype for callback implementation */
00098 typedef int (*cp_socket_callback)(struct _cp_socket *, int fd);
00099 #ifdef CP_USE_SSL
00100 /** function prototype for callback implementation with SSL */
00101 typedef int (*cp_socket_ssl_callback)(struct _cp_socket *, SSL *ssl);
00102 #endif
00103 
00104 /**
00105  * cp_socket is a 'server socket'. create a cp_socket to listen on a port, 
00106  * specify a callback to implemet your communication protocol (libcprops 
00107  * provides a basic http implementation you could plug in) and the strategy
00108  * you want for handling handling multiple connections - use a thread pool
00109  * to serve each connection on its own thread or a more select()-like approach. 
00110  * <p>
00111  * If you create a socket with the CPSOCKET_STRATEGY_THREADPOOL, supply a 
00112  * thread function to handle communication once a connection has been 
00113  * accept()-ed. Your cp_thread function should close the connection when done 
00114  * and check the 'closing' flag, which is set to 1 on shutdown. 
00115  * <p>
00116  * If you create a socket with the CPSOCKET_STRATEGY_CALLBACK, supply a callback
00117  * to process incremental feeds as they come along.
00118  */
00119 typedef CPROPS_DLL struct _cp_socket
00120 {
00121         int id;                                           /**< socket id */
00122         int port;                     /**< the port this socket listens on */
00123         int backlog;                  /**< max backlog */
00124         struct timeval delay;         /**< max blocking time */
00125         int connections_served;           /**< number of connections accepted */
00126         struct timeval created;       /**< creation time */
00127 #if defined(unix) || defined(__unix__) || defined(__MACH__)
00128         int fd;                       /**< listener */
00129 #else
00130 #ifdef _WINDOWS
00131         SOCKET fd;
00132 #endif /* _WINDOWS */
00133 #endif /* unix */
00134         cp_hashlist *fds;             /**< all available file descriptors */
00135         cp_thread_pool *tpool;        /**< cp_thread pool (if requested) */
00136         int poolsize_min;             /**< min cp_thread count for cp_thread pool */
00137         int poolsize_max;             /**< max cp_thread count for cp_thread pool */
00138         unsigned int fdn;             /**< largest fd - needed for select() */
00139         cp_socket_strategy strategy;  /**< threadpool or callback */    
00140         union
00141         {
00142                 cp_socket_thread_function thread_fn;
00143                 cp_socket_callback callback;
00144 #ifdef CP_USE_SSL
00145                 cp_socket_ssl_callback ssl_callback;
00146 #endif
00147         } action;                                         /**< placeholder for action */
00148         int closing;                              /**< shutdown flag */
00149         void *owner;
00150 #ifdef CP_USE_SSL
00151         int use_ssl;                              /**< ssl connection */
00152         SSL_CTX *ctx;                             /**< ssl context */
00153 #endif
00154         cp_vector *shutdown_callback; /**< shutdown callback list */
00155 } cp_socket;
00156 
00157 /** set number of outstanding requests before accept() fails new connections */
00158 CPROPS_DLL
00159 void cp_socket_set_backlog(cp_socket *socket, int backlog);
00160 /** delay time before re-accept()ing */
00161 CPROPS_DLL
00162 void cp_socket_set_delay(cp_socket *socket, struct timeval delay);
00163 /** seconds before re-accept()ing */
00164 CPROPS_DLL
00165 void cp_socket_set_delay_sec(cp_socket *socket, long sec);
00166 /** microseconds before re-accept()ing */
00167 CPROPS_DLL
00168 void cp_socket_set_delay_usec(cp_socket *socket, long usec);
00169 /** lower size limit for threadpool implementation */
00170 CPROPS_DLL
00171 void cp_socket_set_poolsize_min(cp_socket *socket, int min);
00172 /** upper size limit for threadpool implementation */
00173 CPROPS_DLL
00174 void cp_socket_set_poolsize_max(cp_socket *socket, int max);
00175 /** useful free pointer for client code */
00176 CPROPS_DLL
00177 void cp_socket_set_owner(cp_socket *socket, void *owner);
00178 
00179 /** create a new cp_socket struct */
00180 CPROPS_DLL
00181 cp_socket *cp_socket_create(int port, cp_socket_strategy strategy, void *fn);
00182 #ifdef CP_USE_SSL
00183 /** create a new ssl enabled cp_socket struct */
00184 CPROPS_DLL
00185 cp_socket *cp_socket_create_ssl(int port, 
00186                                         cp_socket_strategy strategy, 
00187                                                                 void *fn,
00188                                                                 char *certificate_file,
00189                                                                 char *key_file,
00190                                                                 int verification_mode);
00191 /** close an ssl connection */
00192 CPROPS_DLL
00193 int cp_socket_ssl_connection_close(cp_socket *sock, SSL *ssl);
00194 #endif
00195 /** deallocate a socket descriptor */
00196 CPROPS_DLL
00197 void cp_socket_delete(cp_socket *sock);
00198 /** bind socket */
00199 CPROPS_DLL
00200 int cp_socket_listen(cp_socket *sock);
00201 /** block and wait for connections */
00202 CPROPS_DLL
00203 int cp_socket_select(cp_socket *sock);
00204 /** close a (non ssl) connection */
00205 CPROPS_DLL
00206 int cp_socket_connection_close(cp_socket *sock, int fd);
00207 /** add a callback to be made on socket shutdown */
00208 CPROPS_DLL
00209 void *cp_socket_add_shutdown_callback(cp_socket *sock, 
00210                                                                           void (*cb)(void *),
00211                                                                           void *prm);
00212 
00213 /** connection descriptor structure */
00214 typedef CPROPS_DLL struct _cp_connection_descriptor
00215 {
00216         cp_socket *sock;                        /**< socket the connection was made on */
00217         struct sockaddr_in *addr;       /**< client address */
00218 #if defined(unix) || defined(__unix__) || defined(__MACH__)
00219         int fd;                                         /**< file descriptor */
00220 #else
00221 #ifdef _WINDOWS
00222         SOCKET fd;
00223 #endif /* _WINDOWS */
00224 #endif /* unix */
00225         void *owner;                            /**< placeholder for client identifier */
00226 #ifdef CP_USE_SSL
00227         SSL *ssl;                                       /**< ssl object */
00228 #endif
00229         struct timeval created;         /**< creation time */
00230         unsigned long bytes_read;   /**< bytes read */
00231         unsigned long bytes_sent;   /**< bytes written */
00232 } cp_connection_descriptor;
00233 
00234 /** internal: create a new connection descriptor */
00235 CPROPS_DLL
00236 cp_connection_descriptor *cp_connection_descriptor_create(cp_socket *sock, 
00237                                                                                                         struct sockaddr_in *addr, 
00238                                                                                                         int fd);
00239 #ifdef CP_USE_SSL
00240 /** internal: create a new ssl connection descriptor */
00241 CPROPS_DLL
00242 cp_connection_descriptor *cp_connection_descriptor_create_ssl(cp_socket *sock, 
00243                                                                                                         struct sockaddr_in *addr, 
00244                                                                                                         int fd, 
00245                                                                                                         SSL *ssl);
00246 #endif
00247 /** deallocate a connection descriptor */
00248 CPROPS_DLL
00249 void cp_connection_descriptor_destroy(cp_connection_descriptor *conn_desc);
00250 
00251 /** return cp_socket struct for given connection descriptor */
00252 #define cp_connection_descriptor_get_socket(cd) ((cd)->sock)
00253 /** return client address for given connection descriptor */
00254 #define cp_connection_descriptor_get_addr(cd) ((cd)->addr)
00255 /** return file descriptor for given connection descriptor */
00256 #define cp_connection_descriptor_get_fd(cd) ((cd)->fd)
00257 
00258 /** read from a connection descriptor */
00259 CPROPS_DLL
00260 int cp_connection_descrpitor_read(cp_connection_descriptor *desc, 
00261                                           char *buf, int len);
00262 
00263 /** write on a connection descriptor */
00264 CPROPS_DLL
00265 int cp_connection_descriptor_write(cp_connection_descriptor *desc, 
00266                                                                    char *buf, int len);
00267 
00268 __END_DECLS
00269 
00270 /** @} */
00271 
00272 #endif /* _CP_SOCKET_H */

Generated on Fri Oct 22 2010 11:02:23 for SST by  doxygen 1.7.1