abyss_socket.c

Go to the documentation of this file.
00001 /******************************************************************************
00002 **
00003 ** socket.c
00004 **
00005 ** This file is part of the ABYSS Web server project.
00006 **
00007 ** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
00008 ** All rights reserved.
00009 **
00010 ** Redistribution and use in source and binary forms, with or without
00011 ** modification, are permitted provided that the following conditions
00012 ** are met:
00013 ** 1. Redistributions of source code must retain the above copyright
00014 **    notice, this list of conditions and the following disclaimer.
00015 ** 2. Redistributions in binary form must reproduce the above copyright
00016 **    notice, this list of conditions and the following disclaimer in the
00017 **    documentation and/or other materials provided with the distribution.
00018 ** 3. The name of the author may not be used to endorse or promote products
00019 **    derived from this software without specific prior written permission.
00020 ** 
00021 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00022 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00025 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031 ** SUCH DAMAGE.
00032 **
00033 *******************************************************************************/
00034 
00035 /*============================================================================
00036   socket.c
00037 ==============================================================================
00038   Implementation of TSocket class: A generic socket over which one can
00039   transport an HTTP stream or manage HTTP connections
00040 ============================================================================*/
00041 
00042 #include <sys/types.h>
00043 #include <assert.h>
00044 #include <netinet/in.h>
00045 #include <arpa/inet.h>
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 
00049 #include "abyss_mallocvar.h"
00050 #include "abyss_xmlrpc_int.h"
00051 #include <xmlrpc-c/abyss.h>
00052 #ifdef WIN32
00053   #include "socket_win.h"
00054 #else
00055   #include "abyss_socket_unix.h"
00056 #endif
00057 #include "abyss_socket.h"
00058 
00059 
00060 #ifdef WIN32
00061   #include "socket_win.h"
00062 #else
00063   #include "abyss_socket_unix.h"
00064 #endif
00065 
00066 #include "abyss_socket.h"
00067 
00068 
00069 static void
00070 socketOsInit(abyss_bool * const succeededP) {
00071 
00072 #ifdef WIN32
00073     SocketWinInit(succeededP);
00074 #else
00075     SocketUnixInit(succeededP);
00076 #endif
00077 }
00078 
00079 
00080 
00081 static void
00082 socketOsTerm(void) {
00083 
00084 #ifdef WIN32
00085     SocketWinTerm();
00086 #else
00087     SocketUnixTerm();
00088 #endif
00089 }
00090     
00091 
00092 
00093 abyss_bool SocketTraceIsActive;
00094 
00095 abyss_bool
00096 SocketInit(void) {
00097     abyss_bool retval;
00098 
00099     socketOsInit(&retval);
00100 
00101     SocketTraceIsActive = (getenv("ABYSS_TRACE_SOCKET") != NULL);
00102     if (SocketTraceIsActive)
00103         fprintf(stderr, "Abyss socket layer will trace socket traffic "
00104                 "due to ABYSS_TRACE_SOCKET environment variable\n");
00105 
00106     return retval;
00107 }
00108 
00109 
00110 
00111 void
00112 SocketTerm(void) {
00113 
00114     socketOsTerm();
00115 }
00116 
00117 
00118 
00119 /* SocketCreate() is not exported to the Abyss user.  It is meant to
00120    be used by an implementation-specific TSocket generator which is
00121    exported to the Abyss user, e.g. SocketCreateUnix() in
00122    socket_unix.c
00123 
00124    The TSocket generator functions are the _only_ user-accessible
00125    functions that are particular to an implementation.
00126 */
00127 
00128 static uint const socketSignature = 0x060609;
00129 
00130 void
00131 SocketCreate(const struct TSocketVtbl * const vtblP,
00132              void *                     const implP,
00133              TSocket **                 const socketPP) {
00134 
00135     TSocket * socketP;
00136 
00137     MALLOCVAR(socketP);
00138 
00139     if (socketP) {
00140         socketP->implP = implP;
00141         socketP->vtbl = *vtblP;
00142         socketP->signature = socketSignature;
00143         *socketPP = socketP;
00144     }
00145 }
00146 
00147 
00148 
00149 void
00150 SocketDestroy(TSocket * const socketP) {
00151 
00152     assert(socketP->signature == socketSignature);
00153 
00154     socketP->vtbl.destroy(socketP);
00155 
00156     socketP->signature = 0;  /* For debuggability */
00157 
00158     free(socketP);
00159 }
00160 
00161 
00162 
00163 void
00164 SocketWrite(TSocket *             const socketP,
00165             const unsigned char * const buffer,
00166             uint32_t              const len,
00167             abyss_bool *          const failedP) {
00168 
00169     (*socketP->vtbl.write)(socketP, buffer, len, failedP );
00170 }
00171 
00172 
00173 
00174 uint32_t
00175 SocketRead(TSocket *       const socketP, 
00176            unsigned char * const buffer, 
00177            uint32_t        const len) {
00178     
00179     return (*socketP->vtbl.read)(socketP, (char*)buffer, len);
00180 }
00181 
00182 
00183 
00184 abyss_bool
00185 SocketConnect(TSocket * const socketP,
00186               TIPAddr * const addrP,
00187               uint16_t  const portNumber) {
00188 
00189     return (*socketP->vtbl.connect)(socketP, addrP, portNumber);
00190 }
00191 
00192 
00193 
00194 abyss_bool
00195 SocketBind(TSocket * const socketP,
00196            TIPAddr * const addrP,
00197            uint16_t  const portNumber) {
00198 
00199     return (*socketP->vtbl.bind)(socketP, addrP, portNumber);
00200 }
00201 
00202 
00203 
00204 abyss_bool
00205 SocketListen(TSocket * const socketP,
00206              uint32_t  const backlog) {
00207 
00208     return (*socketP->vtbl.listen)(socketP, backlog);
00209 }
00210 
00211 
00212 
00213 void
00214 SocketAccept(TSocket *    const listenSocketP,
00215              abyss_bool * const connectedP,
00216              abyss_bool * const failedP,
00217              TSocket **   const acceptedSocketPP,
00218              TIPAddr *    const ipAddrP) {
00219 
00220     (*listenSocketP->vtbl.accept)(listenSocketP,
00221                                   connectedP,
00222                                   failedP,
00223                                   acceptedSocketPP,
00224                                   ipAddrP);
00225 }
00226 
00227 
00228 
00229 uint32_t
00230 SocketWait(TSocket *  const socketP,
00231            abyss_bool const rd,
00232            abyss_bool const wr,
00233            uint32_t   const timems) {
00234 
00235     return (*socketP->vtbl.wait)(socketP, rd, wr, timems);
00236 }
00237 
00238 
00239 
00240 uint32_t
00241 SocketAvailableReadBytes(TSocket * const socketP) {
00242 
00243     return (*socketP->vtbl.availableReadBytes)(socketP);
00244 }
00245 
00246 
00247 
00248 void
00249 SocketGetPeerName(TSocket *    const socketP,
00250                   TIPAddr *    const ipAddrP,
00251                   uint16_t *   const portNumberP,
00252                   abyss_bool * const successP) {
00253 
00254     (*socketP->vtbl.getPeerName)(socketP, ipAddrP, portNumberP, successP);
00255 }
00256 
00257 
00258 
00259 uint32_t
00260 SocketError(TSocket * const socketP) {
00261 
00262     return (*socketP->vtbl.error)(socketP);
00263 }

Generated on Thu May 17 10:00:23 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6