00001 /* ==================================================================== 00002 * The Kannel Software License, Version 1.0 00003 * 00004 * Copyright (c) 2001-2008 Kannel Group 00005 * Copyright (c) 1998-2001 WapIT Ltd. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in 00017 * the documentation and/or other materials provided with the 00018 * distribution. 00019 * 00020 * 3. The end-user documentation included with the redistribution, 00021 * if any, must include the following acknowledgment: 00022 * "This product includes software developed by the 00023 * Kannel Group (http://www.kannel.org/)." 00024 * Alternately, this acknowledgment may appear in the software itself, 00025 * if and wherever such third-party acknowledgments normally appear. 00026 * 00027 * 4. The names "Kannel" and "Kannel Group" must not be used to 00028 * endorse or promote products derived from this software without 00029 * prior written permission. For written permission, please 00030 * contact org@kannel.org. 00031 * 00032 * 5. Products derived from this software may not be called "Kannel", 00033 * nor may "Kannel" appear in their name, without prior written 00034 * permission of the Kannel Group. 00035 * 00036 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00039 * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 00040 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00041 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00042 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00043 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00044 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00045 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00046 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00047 * ==================================================================== 00048 * 00049 * This software consists of voluntary contributions made by many 00050 * individuals on behalf of the Kannel Group. For more information on 00051 * the Kannel Group, please see <http://www.kannel.org/>. 00052 * 00053 * Portions of this software are based upon software originally written at 00054 * WapIT Ltd., Helsinki, Finland for the Kannel project. 00055 */ 00056 00057 /* 00058 * conn.h - declare Connection type to wrap a file descriptor 00059 * 00060 * This file defines operations on the Connection type, which provides 00061 * input and output buffers for a two-way file descriptor, such as a 00062 * socket or a serial device. 00063 * 00064 * The operations are designed for non-blocking use. Blocking can be 00065 * done explicitly with conn_wait() or conn_flush(). A thread that 00066 * blocks in these functions can be woken up with gwthread_wakeup. 00067 * 00068 * The write operations will queue the data for sending. They will 00069 * try to send whatever data can be sent immediately, if there's enough 00070 * of it queued. "Enough" is defined by a value which can be set with 00071 * conn_set_output_buffering. The caller must call either conn_wait 00072 * or conn_flush to actually send the data. 00073 * 00074 * The read operations will return whatever data is immediately 00075 * available. If none is, then the caller should not simply re-try 00076 * the request (that would cause a busy-loop); instead, it should 00077 * wait for more data with conn_wait(). 00078 * 00079 * The Connection structure has internal locks, so it can be shared 00080 * safely between threads. There is a race condition in the interface, 00081 * however, that can cause threads to wait unnecessarily if there are 00082 * multiple readers. But in that case there will always be at least one 00083 * thread busy reading. 00084 * 00085 * The overhead of locking can be avoided by "claiming" a Connection. 00086 * This means that only one thread will ever do operations on that 00087 * Connection; the caller must guarantee this. 00088 * 00089 * If any operation returns a code that indicates that the connection 00090 * is broken (due to an I/O error, normally), it will also have closed 00091 * the connection. Most operations work only on open connections; 00092 * not much can be done with a closed connection except destroy it. 00093 */ 00094 00095 typedef struct Connection Connection; 00096 00097 /* If conn_register was called for this connection, a callback function 00098 * of type conn_callback_t will be called when new input is available, 00099 * or when all data that was previously queued for output is sent. 00100 * The data pointer is the one supplied by the caller of conn_register. 00101 * NOTE: Beware of concurrency issues. The callback function will run 00102 * in the fdset's private thread, not in the caller's thread. 00103 * This also means that if the callback does a lot of work it will slow 00104 * down the polling process. This may be good or bad. */ 00105 typedef void conn_callback_t(Connection *conn, void *data); 00106 00107 /* 00108 * If conn_register was called for this connection, a callback data destroyer 00109 * function will be called if conn_unregister, conn_destroy or conn_register 00110 * (with different data) called for this connection. 00111 * This function is responsible to destroy callback data. 00112 */ 00113 typedef void conn_callback_data_destroyer_t(void *data); 00114 00115 #ifdef HAVE_LIBSSL 00116 /* Open an SSL connection to the given host and port. Same behavior 00117 * as conn_open_tcp() below. 'certkeyfile' specifies a PEM-encoded 00118 * file where OpenSSL looks for a private key and a certificate. 00119 */ 00120 Connection *conn_open_ssl(Octstr *host, int port, Octstr *certkeyfile, Octstr *our_host); 00121 00122 /* Open an SSL connection to the given host and port. Same behavior 00123 * as conn_open_tcp_nb() below. 'certkeyfile' specifies a PEM-encoded 00124 * file where OpenSSL looks for a private key and a certificate. 00125 */ 00126 Connection *conn_open_ssl_nb(Octstr *host, int port, Octstr *certkeyfile, Octstr *our_host); 00127 00128 void server_ssl_init(void); /* used by http.c */ 00129 #endif /* HAVE_LIBSSL */ 00130 00131 /* 00132 * get the SSL config parameters from the provided Config group. 00133 * For a non-SSL system this is a no-op that does nothing. 00134 */ 00135 void conn_config_ssl (CfgGroup *grp); 00136 00137 00138 /* Open a TCP/IP connection to the given host and port. Return the 00139 * new Connection. If the connection can not be made, return NULL 00140 * and log the problem. */ 00141 Connection *conn_open_tcp(Octstr *host, int port, Octstr *our_host); 00142 00143 /* As above, but binds our end to 'our_port'. If 'our_port' is 0, uses 00144 * any port like conn_open_tcp. */ 00145 Connection *conn_open_tcp_with_port(Octstr *host, int port, int our_port, 00146 Octstr *our_host); 00147 00148 /* Open a TCP/IP connection to the given host and port. Return NULL in case of 00149 * error. Overwise return new Connection. */ 00150 Connection *conn_open_tcp_nb(Octstr *host, int port, Octstr *our_host); 00151 00152 /* As above, but binds our end to 'our_port'. If 'our_port' is 0, uses 00153 * any port like conn_open_tcp. */ 00154 Connection *conn_open_tcp_nb_with_port(Octstr *host, int port, int our_port, 00155 Octstr *our_host); 00156 00157 /* Returns 0 if socket is connected, -1 overwise */ 00158 int conn_is_connected(Connection *conn); 00159 00160 /* If socket is in the 'connecting' state, it must be listen by poller. 00161 * After poller returns, connection must be checked for connection 00162 * procedure's result. Return 0 if connection done successfully */ 00163 int conn_get_connect_result(Connection *conn); 00164 00165 /* Create a Connection structure around the given file descriptor. 00166 * The file descriptor must not be used for anything else after this; 00167 * it must always be accessed via the Connection operations. This 00168 * operation cannot fail. Second var indicates if the is a SSL enabled 00169 * connection. */ 00170 Connection *conn_wrap_fd(int fd, int ssl); 00171 00172 /* Close and deallocate a Connection. Log any errors reported by 00173 * the close operation. */ 00174 void conn_destroy(Connection *conn); 00175 00176 /* Assert that the calling thread will be the only one to ever 00177 * use this Connection. From now on no locking will be done 00178 * on this Connection. 00179 * It is a fatal error for two threads to try to claim one Connection, 00180 * or for another thread to try to use a Connection that has been claimed. 00181 */ 00182 void conn_claim(Connection *conn); 00183 00184 /* Return the length of the unsent data queued for sending, in octets. */ 00185 long conn_outbuf_len(Connection *conn); 00186 00187 /* Return the length of the unprocessed data ready for reading, in octets. */ 00188 long conn_inbuf_len(Connection *conn); 00189 00190 /* Return 1 if there was an end-of-file indication from the last read or 00191 * wait operation. */ 00192 int conn_eof(Connection *conn); 00193 00194 /* Return 1 if there was an error indication from the last read or wait 00195 * operation. */ 00196 int conn_error(Connection *conn); 00197 00198 /* Try to write data in chunks of this size or more. Set it to 0 to 00199 * get an unbuffered connection. See the discussion on output buffering 00200 * at the top of this file for more information. */ 00201 void conn_set_output_buffering(Connection *conn, unsigned int size); 00202 00203 /* Register this connection with an FDSet. This will make it unnecessary 00204 * to call conn_wait. Instead, the callback function will be called when 00205 * there is new data available, or when all data queued for output is 00206 * sent (note that small amounts are usually sent immediately without 00207 * queuing, and thus won't trigger the callback). A connection can be 00208 * registered with only one FDSet at a time. Return -1 if it was 00209 * already registered with a different FDSet, otherwise return 0. 00210 * A connection can be re-registered with the same FDSet. This will 00211 * change only the callback information, and is much more efficient 00212 * than calling conn_unregister first. 00213 * NOTE: Using conn_register will always mean that the Connection will be 00214 * used by more than one thread, so don't also call conn_claim. */ 00215 #define conn_register(conn, fdset, callback, data) \ 00216 conn_register_real(conn, fdset, callback, data, NULL) 00217 int conn_register_real(Connection *conn, FDSet *fdset, 00218 conn_callback_t callback, void *data, conn_callback_data_destroyer_t destroyer); 00219 00220 /* 00221 * Remove the current registration and call data destroyer if not NULL. 00222 */ 00223 void conn_unregister(Connection *conn); 00224 00225 /* Block the thread until one of the following is true: 00226 * - The timeout expires 00227 * - New data is available for reading 00228 * - Some data queued for output is sent (if there was any) 00229 * - The thread is woken up via the wakeup interface (in gwthread.h) 00230 * Return 1 if the timeout expired. Return 0 otherwise, if the 00231 * connection is okay. Return -1 if the connection is broken. 00232 * If the timeout is 0 seconds, check for the conditions above without 00233 * actually blocking. If it is negative, block indefinitely. 00234 */ 00235 int conn_wait(Connection *conn, double seconds); 00236 00237 /* Try to send all data currently queued for output. Block until this 00238 * is done, or until the thread is interrupted or woken up. Return 0 00239 * if it worked, 1 if there was an interruption, or -1 if the connection 00240 * is broken. */ 00241 int conn_flush(Connection *conn); 00242 00243 /* Output functions. Each of these takes an open connection and some 00244 * data, formats the data and queues it for sending. It may also 00245 * try to send the data immediately. The current implementation always 00246 * does so. 00247 * Return 0 if the data was sent, 1 if it was queued for sending, 00248 * and -1 if the connection is broken. 00249 */ 00250 int conn_write(Connection *conn, Octstr *data); 00251 int conn_write_data(Connection *conn, unsigned char *data, long length); 00252 /* Write the length of the octstr as a standard network long, then 00253 * write the octstr itself. */ 00254 int conn_write_withlen(Connection *conn, Octstr *data); 00255 00256 /* Input functions. Each of these takes an open connection and 00257 * returns data if it's available, or NULL if it's not. They will 00258 * not block. They will try to read in more data if there's not 00259 * enough in the buffer to fill the request. */ 00260 00261 /* Return whatever data is available. */ 00262 Octstr *conn_read_everything(Connection *conn); 00263 00264 /* Return exactly "length" octets of data, if at least that many 00265 * are available. Otherwise return NULL. 00266 */ 00267 Octstr *conn_read_fixed(Connection *conn, long length); 00268 00269 /* If the input buffer starts with a full line of data (terminated by 00270 * LF or CR LF), then return that line as an Octstr and remove it 00271 * from the input buffer. Otherwise return NULL. 00272 */ 00273 Octstr *conn_read_line(Connection *conn); 00274 00275 /* Read a standard network long giving the length of the following 00276 * data, then read the data itself, and pack it into an Octstr and 00277 * remove it from the input buffer. Otherwise return NULL. 00278 */ 00279 Octstr *conn_read_withlen(Connection *conn); 00280 00281 /* If the input buffer contains a packet delimited by the "startmark" 00282 * and "endmark" characters, then return that packet (including the marks) 00283 * and delete everything up to the end of that packet from the input buffer. 00284 * Otherwise return NULL. 00285 * Everything up to the first startmark is discarded. 00286 */ 00287 Octstr *conn_read_packet(Connection *conn, int startmark, int endmark); 00288 00289 #ifdef HAVE_LIBSSL 00290 00291 #include <openssl/x509.h> 00292 #include <openssl/ssl.h> 00293 00294 /* Returns the SSL peer certificate for the given Connection or NULL 00295 * if none. 00296 */ 00297 X509 *get_peer_certificate(Connection *conn); 00298 00299 /* These are called to initialize and shutdown the OpenSSL mutex locks. 00300 * They should be called before the _init_ssl, _shutdown_ssl functions. 00301 */ 00302 void openssl_init_locks(void); 00303 void openssl_shutdown_locks(void); 00304 00305 /* These must be called if SSL is used. Currently http.c calls 00306 * conn_init_ssl and server_init_ssl from http_init and 00307 * conn_shutdown_ssl and server_shutdown_ssl from http_shutdown. 00308 */ 00309 void conn_init_ssl(void); 00310 void conn_shutdown_ssl(void); 00311 void server_init_ssl(void); 00312 void server_shutdown_ssl(void); 00313 00314 /* Specifies a global PEM-encoded certificate and a private key file 00315 * to be used with SSL client connections (outgoing HTTP requests). 00316 * conn_init_ssl() must be called first. This checks that the private 00317 * key matches with the certificate and will panic if it doesn't. 00318 */ 00319 void use_global_client_certkey_file(Octstr *certkeyfile); 00320 00321 /* Specifies a global PEM-encoded certificate and a private key file 00322 * to be used with SSL server connections (incoming HTTP requests). 00323 * conn_init_ssl() must be called first. This checks that the private 00324 * key matches with the certificate and will panic if it doesn't. 00325 */ 00326 void use_global_server_certkey_file(Octstr *certfile, Octstr *keyfile); 00327 00328 /* Specifies files containing certificates Kannel is willing to trusted when 00329 * actins as https clients 00330 */ 00331 void use_global_trusted_ca_file(Octstr *ssl_trusted_ca_file); 00332 00333 /* Configures all global variables for client and server SSL mode 00334 * from the values specified within the configuration file. 00335 */ 00336 void conn_config_ssl(CfgGroup *grp); 00337 00338 /* Returns the pointer to the SSL structure of the Connection given. 00339 * This should be used for determining if certain connections are 00340 * SSL enabled outside of the scope of conn.c. 00341 */ 00342 SSL *conn_get_ssl(Connection *conn); 00343 00344 00345 X509 *conn_get_peer_certificate(Connection *conn); 00346 #endif /* HAVE_LIBSSL */ 00347 00348 int conn_get_id(Connection *conn);