Retail products


Traffic interception SDK

Control every TCP/IP network connection

  • Route connections via proxy
  • Redirect connections and modify the data
  • Block connections and applications
SSL interception SDK

View SSL in plaintext and modify it

  • View the SSL stream decrypted in plaintext
  • Redirect SSL connection and modify decrypted data
  • Browser shows "SSL lock" without warnings

Documentation


sockinfo.cpp File Reference

#include "lspdef.h"

Include dependency graph for sockinfo.cpp:

Go to the source code of this file.


Functions

SOCK_INFOFindSockInfoFromProviderSocket (PROVIDER *provider, SOCKET socket)
SOCK_INFOFindAndRefSocketContext (SOCKET s, int *lpErrno)
void DerefSocketContext (SOCK_INFO *context, int *lpErrno)
void AcquireSocketLock (SOCK_INFO *SockInfo)
void ReleaseSocketLock (SOCK_INFO *SockInfo)
SOCK_INFOCreateSockInfo (PROVIDER *Provider, SOCKET ProviderSocket, SOCK_INFO *Inherit, BOOL Insert, int *lpErrno)
void FreeSockInfo (SOCK_INFO *info)
void InsertSocketInfo (PROVIDER *provider, SOCK_INFO *sock)
void RemoveSocketInfo (PROVIDER *provider, SOCK_INFO *si)
void CloseAndFreeSocketInfo (PROVIDER *provider, BOOL processDetach)
SOCK_INFOGetCallerSocket (PROVIDER *provider, SOCKET ProviderSock)

Function Documentation

void AcquireSocketLock ( SOCK_INFO SockInfo  ) 

Definition at line 133 of file sockinfo.cpp.

00136 {
00137     EnterCriticalSection( &SockInfo->SockCritSec );
00138 }

void CloseAndFreeSocketInfo ( PROVIDER provider,
BOOL  processDetach 
)

Definition at line 309 of file sockinfo.cpp.

00313 {
00314     LIST_ENTRY   *entry = NULL;
00315     SOCK_INFO    *si = NULL;
00316     struct linger linger;
00317     int           Error, 
00318                   ret;
00319 
00320     ASSERT( provider );
00321 
00322     linger.l_onoff  = 1;
00323     linger.l_linger = 0;
00324 
00325     // Walk the list of sockets
00326     while ( !IsListEmpty( &provider->SocketList ) )
00327     {
00328         entry = RemoveHeadList( &provider->SocketList );
00329 
00330         ASSERT( entry );
00331 
00332         si = CONTAINING_RECORD( entry, SOCK_INFO, Link );
00333 
00334         if ( ( !processDetach ) || 
00335              ( provider->NextProvider.ProtocolChain.ChainLen == BASE_PROTOCOL ) )
00336         {
00337 
00338             ASSERT( provider->NextProcTable.lpWSPSetSockOpt );
00339 
00340             // Set the abortive linger
00341             ret = provider->NextProcTable.lpWSPSetSockOpt(
00342                     si->ProviderSocket,
00343                     SOL_SOCKET,
00344                     SO_LINGER,
00345                     (char *) &linger,
00346                     sizeof(linger),
00347                     &Error
00348                     );
00349             if ( SOCKET_ERROR != ret )
00350             {
00351                 ASSERT( provider->NextProcTable.lpWSPCloseSocket );
00352 
00353                 // Close the lower provider socket
00354                 ret = provider->NextProcTable.lpWSPCloseSocket(
00355                         si->ProviderSocket,
00356                         &Error
00357                         );
00358                 if ( SOCKET_ERROR == ret )
00359                 {
00360                     dbgprint("WSPCloseSocket() on handle %d failed: %d", si->ProviderSocket, Error);
00361                 }
00362 #ifdef DEBUG
00363                 else
00364                 {
00365                     dbgprint("Successfully closed socket %d", si->ProviderSocket);
00366                 }
00367 #endif
00368             }
00369 #ifdef DEBUG
00370             else
00371             {
00372                 dbgprint("WSPSetSockOpt(SO_LINGER) failed: %d", Error);
00373             }
00374 #endif
00375         }
00376 
00377         ASSERT( gMainUpCallTable.lpWPUCloseSocketHandle );
00378 
00379         // Close the layered handle
00380         gMainUpCallTable.lpWPUCloseSocketHandle(
00381                 si->LayeredSocket, 
00382                &Error
00383                 );
00384 
00385         // Free the context structure
00386         FreeSockInfo( si );
00387     }
00388 
00389     return;
00390 }

SOCK_INFO* CreateSockInfo ( PROVIDER Provider,
SOCKET  ProviderSocket,
SOCK_INFO Inherit,
BOOL  Insert,
int *  lpErrno 
)

Definition at line 171 of file sockinfo.cpp.

00178 {
00179     SOCK_INFO   *NewInfo = NULL;
00180 
00181     NewInfo = (SOCK_INFO *) LspAlloc(
00182             sizeof( SOCK_INFO ),
00183             lpErrno
00184             );
00185     if ( NULL == NewInfo )
00186     {
00187         dbgprint("HeapAlloc() failed: %d", GetLastError());
00188        *lpErrno = WSAENOBUFS;
00189         goto cleanup;
00190     }
00191 
00192     //
00193     // Initialize the fields to default values
00194     //
00195     NewInfo->ProviderSocket     = ProviderSocket;
00196     NewInfo->bClosing           = FALSE;
00197     NewInfo->dwOutstandingAsync = 0;
00198     NewInfo->BytesRecv          = 0;
00199     NewInfo->BytesSent          = 0;
00200     NewInfo->Provider           = Provider;
00201     NewInfo->hWnd               = (Inherit ? Inherit->hWnd : 0);
00202     NewInfo->uMsg               = (Inherit ? Inherit->uMsg : 0);
00203 
00204     __try
00205     {
00206         InitializeCriticalSection( &NewInfo->SockCritSec );
00207     }
00208     __except( EXCEPTION_EXECUTE_HANDLER )
00209     {
00210         *lpErrno = WSAENOBUFS;
00211         goto cleanup;
00212     }
00213 
00214     if ( TRUE == Insert )
00215         InsertSocketInfo(Provider, NewInfo);
00216 
00217     return NewInfo;
00218 
00219 cleanup:
00220 
00221     if ( NULL != NewInfo )
00222         LspFree( NewInfo );
00223 
00224     return NULL;
00225 }

void DerefSocketContext ( SOCK_INFO context,
int *  lpErrno 
)

Definition at line 83 of file sockinfo.cpp.

00087 {
00088     LONG    newval;
00089     int     ret = NO_ERROR;
00090 
00091     EnterCriticalSection(&gCriticalSection);
00092 
00093     // Decrement the ref count and see if someone closed this socket (from another thread)
00094     newval = InterlockedDecrement(&context->RefCount);
00095     if ( ( 0 == newval ) && 
00096          ( 0 == context->dwOutstandingAsync ) && 
00097          ( TRUE == context->bClosing ) 
00098        )
00099     {
00100         ASSERT( gMainUpCallTable.lpWPUCloseSocketHandle );
00101 
00102         // Socket has been closed so close the handle and free associated resources
00103         ret = gMainUpCallTable.lpWPUCloseSocketHandle(context->LayeredSocket, lpErrno);
00104         if ( SOCKET_ERROR == ret )
00105         {
00106             dbgprint("DerefSocketContext: WPUCloseSocketHandle() failed: %d", *lpErrno);
00107         }
00108 
00109         context->LayeredSocket = INVALID_SOCKET;
00110 
00111         RemoveSocketInfo(context->Provider, context);
00112 
00113         dbgprint("Closing socket %d Bytes Sent [%lu] Bytes Recv [%lu]", 
00114                 context->LayeredSocket, context->BytesSent, context->BytesRecv);
00115 
00116         FreeSockInfo( context );
00117         context = NULL;
00118     }
00119 
00120     LeaveCriticalSection( &gCriticalSection );
00121 }

SOCK_INFO* FindAndRefSocketContext ( SOCKET  s,
int *  lpErrno 
)

Definition at line 40 of file sockinfo.cpp.

00044 {
00045     SOCK_INFO *SocketContext = NULL;
00046     int        ret;
00047 
00048     EnterCriticalSection(&gCriticalSection);
00049 
00050     ASSERT( gMainUpCallTable.lpWPUQuerySocketHandleContext );
00051 
00052     ret = gMainUpCallTable.lpWPUQuerySocketHandleContext(
00053             s,
00054             (PDWORD_PTR) &SocketContext,
00055             lpErrno
00056             );
00057     if ( SOCKET_ERROR == ret )
00058     {
00059         dbgprint("FindAndRefSocketContext: WPUQuerySocketHandleContext failed: %d", *lpErrno);
00060         *lpErrno = WSAENOTSOCK;
00061     }
00062     else
00063     {
00064         InterlockedIncrement(&SocketContext->RefCount);
00065     }
00066 
00067     LeaveCriticalSection(&gCriticalSection);
00068 
00069     return SocketContext;
00070 }

SOCK_INFO * FindSockInfoFromProviderSocket ( PROVIDER provider,
SOCKET  socket 
)

Definition at line 401 of file sockinfo.cpp.

00405 {
00406     LIST_ENTRY *lptr = NULL;
00407     SOCK_INFO  *si = NULL;
00408 
00409     ASSERT( provider );
00410 
00411     if ( IsListEmpty( &provider->SocketList ) )
00412     {
00413         dbgprint( "FindSockInfoFromProviderSocket: Empty SOCK_INFO list!" );
00414         goto cleanup;
00415     }
00416 
00417     for(lptr = provider->SocketList.Flink ; lptr != &provider->SocketList ; lptr = lptr->Flink )
00418     {
00419         si = CONTAINING_RECORD( lptr, SOCK_INFO, Link );
00420 
00421         if ( socket == si->ProviderSocket )
00422             break;
00423 
00424         si = NULL;
00425     }
00426 
00427 cleanup:
00428 
00429     return si;
00430 }

void FreeSockInfo ( SOCK_INFO info  ) 

Definition at line 234 of file sockinfo.cpp.

00237 {
00238     DeleteCriticalSection( &info->SockCritSec );
00239     LspFree( info );
00240 
00241     return;
00242 }

SOCK_INFO* GetCallerSocket ( PROVIDER provider,
SOCKET  ProviderSock 
)

Definition at line 444 of file sockinfo.cpp.

00448 {
00449     SOCK_INFO *si = NULL;
00450 
00451     EnterCriticalSection( &gCriticalSection );
00452 
00453     if ( NULL != provider )
00454     {
00455         // If we know the provider just search its list of sockets
00456         si = FindSockInfoFromProviderSocket( provider, ProviderSock );
00457     }
00458     else
00459     {
00460         // Don't know the provider so we must search all of them
00461         for(INT i=0; i < gLayerCount ;i++)
00462         {
00463             si = FindSockInfoFromProviderSocket( &gBaseInfo[ i ], ProviderSock );
00464             if ( NULL != si )
00465                 break;
00466         }
00467     }
00468 
00469     LeaveCriticalSection( &gCriticalSection );
00470 
00471     return si;
00472 }

void InsertSocketInfo ( PROVIDER provider,
SOCK_INFO sock 
)

Definition at line 253 of file sockinfo.cpp.

00257 {
00258     if ( ( NULL == provider ) || ( NULL == sock ) )
00259     {
00260         dbgprint("InsertSocketInfo: PROVIDER or SOCK_INFO == NULL!");
00261         goto cleanup;
00262     }
00263 
00264     EnterCriticalSection( &provider->ProviderCritSec );
00265 
00266     InsertTailList( &provider->SocketList, &sock->Link );
00267 
00268     LeaveCriticalSection( &provider->ProviderCritSec );
00269 
00270     SetEvent( gAddContextEvent );
00271 
00272 cleanup:
00273 
00274     return;
00275 }

void ReleaseSocketLock ( SOCK_INFO SockInfo  ) 

Definition at line 147 of file sockinfo.cpp.

00150 {
00151     LeaveCriticalSection( &SockInfo->SockCritSec );
00152 }

void RemoveSocketInfo ( PROVIDER provider,
SOCK_INFO si 
)

Definition at line 286 of file sockinfo.cpp.

00290 {
00291     EnterCriticalSection( &provider->ProviderCritSec );
00292 
00293     RemoveEntryList( &si->Link );
00294 
00295     LeaveCriticalSection( &provider->ProviderCritSec );
00296 
00297     return;
00298 }