|
| 1 | +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF |
| 2 | +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 3 | +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A |
| 4 | +// PARTICULAR PURPOSE. |
| 5 | +// |
| 6 | +// Copyright (C) 2004 Microsoft Corporation. All Rights Reserved. |
| 7 | +// |
| 8 | +// Module Name: sockinfo.cpp |
| 9 | +// |
| 10 | +// Description: |
| 11 | +// |
| 12 | +// This sample illustrates an IFS based LSP which implements basic proxying |
| 13 | +// capabilities. |
| 14 | +// |
| 15 | +// This file contains routines associated with the SOCKET_CONTEXT structure. This |
| 16 | +// structure maintains the mapping between the upper layer's socket and the |
| 17 | +// corresponding lower layer's socket. The routines in this file are for allocating, |
| 18 | +// linked list management, etc. |
| 19 | +// |
| 20 | + |
| 21 | +#include "lspdef.h" |
| 22 | + |
| 23 | +// |
| 24 | +// Function: FindAndRefSocketContext |
| 25 | +// |
| 26 | +// Description: |
| 27 | +// This routine grabs the LSP critical seciton to lookup the socket context |
| 28 | +// and increase its ref count. Any operation on the socket context holds |
| 29 | +// the critical section so that it cannot be freed while its state changes. |
| 30 | +// |
| 31 | +SOCKET_CONTEXT * |
| 32 | +FindSocketContext( |
| 33 | + SOCKET s, |
| 34 | + BOOL Remove |
| 35 | + ) |
| 36 | +{ |
| 37 | + SOCKET_CONTEXT *SocketContext = NULL, |
| 38 | + *info = NULL; |
| 39 | + LIST_ENTRY *lptr = NULL; |
| 40 | + int i; |
| 41 | + |
| 42 | + EnterCriticalSection( &gCriticalSection ); |
| 43 | + |
| 44 | + for(i=0; i < gLayerCount ;i++) |
| 45 | + { |
| 46 | + EnterCriticalSection( &gLayerInfo[ i ].ProviderCritSec ); |
| 47 | + |
| 48 | + for(lptr = gLayerInfo[ i ].SocketList.Flink ; |
| 49 | + lptr != &gLayerInfo[ i ].SocketList ; |
| 50 | + lptr = lptr->Flink ) |
| 51 | + { |
| 52 | + info = CONTAINING_RECORD( lptr, SOCKET_CONTEXT, Link ); |
| 53 | + |
| 54 | + if ( s == info->Socket ) |
| 55 | + { |
| 56 | + SocketContext = info; |
| 57 | + |
| 58 | + if ( TRUE == Remove ) |
| 59 | + { |
| 60 | + RemoveEntryList( &info->Link ); |
| 61 | + } |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + LeaveCriticalSection( &gLayerInfo[ i ].ProviderCritSec ); |
| 67 | + |
| 68 | + if ( NULL != SocketContext ) |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + LeaveCriticalSection( &gCriticalSection ); |
| 73 | + |
| 74 | + return SocketContext; |
| 75 | +} |
| 76 | + |
| 77 | +// |
| 78 | +// Function: CreateSockInfo |
| 79 | +// |
| 80 | +// Description: |
| 81 | +// Allocates a new socket info context structure and initializes the fields |
| 82 | +// except for the LayeredSocket field. The context must be allocated first, |
| 83 | +// then the layered socket is created (with the SOCKET_CONTEXT structure as the |
| 84 | +// context information), and then the LayeredSocket field is set. If |
| 85 | +// the Inherit context is provided, information is copied to the new socket |
| 86 | +// context structure (such as with WSPAccept). If the Insert flag is TRUE |
| 87 | +// then the context is automatically inserted into the list of sockets |
| 88 | +// for the given provider. If not then the caller must insert the context |
| 89 | +// (WSPAccept does this to ensure all fields of the context are valid |
| 90 | +// including LayeredSocket before insertion so that the async thread |
| 91 | +// handler will work properly). |
| 92 | +// |
| 93 | +SOCKET_CONTEXT * |
| 94 | +CreateSocketContext( |
| 95 | + PROVIDER *Provider, |
| 96 | + SOCKET Socket, |
| 97 | + int *lpErrno |
| 98 | + ) |
| 99 | +{ |
| 100 | + SOCKET_CONTEXT *newContext = NULL; |
| 101 | + |
| 102 | + newContext = (SOCKET_CONTEXT *) LspAlloc( |
| 103 | + sizeof( SOCKET_CONTEXT ), |
| 104 | + lpErrno |
| 105 | + ); |
| 106 | + if ( NULL == newContext ) |
| 107 | + { |
| 108 | + dbgprint("CreateSocketContext: LspAlloc failed: %d", *lpErrno ); |
| 109 | + goto cleanup; |
| 110 | + } |
| 111 | + |
| 112 | + newContext->Socket = Socket; |
| 113 | + newContext->Provider = Provider; |
| 114 | + newContext->Proxied = FALSE; |
| 115 | + |
| 116 | + EnterCriticalSection( &Provider->ProviderCritSec ); |
| 117 | + |
| 118 | + InsertHeadList( &Provider->SocketList, &newContext->Link ); |
| 119 | + |
| 120 | + LeaveCriticalSection( &Provider->ProviderCritSec ); |
| 121 | + |
| 122 | + return newContext; |
| 123 | + |
| 124 | +cleanup: |
| 125 | + |
| 126 | + return NULL; |
| 127 | +} |
| 128 | + |
| 129 | +// |
| 130 | +// Function: FreeSockInfo |
| 131 | +// |
| 132 | +// Description: |
| 133 | +// This routine frees the socket context structure. |
| 134 | +// |
| 135 | +void |
| 136 | +FreeSocketContext( |
| 137 | + PROVIDER *Provider, |
| 138 | + SOCKET_CONTEXT *Context |
| 139 | + ) |
| 140 | +{ |
| 141 | + EnterCriticalSection( &Provider->ProviderCritSec ); |
| 142 | + |
| 143 | + RemoveEntryList( &Context->Link ); |
| 144 | + LspFree( Context ); |
| 145 | + |
| 146 | + LeaveCriticalSection( &Provider->ProviderCritSec ); |
| 147 | + |
| 148 | + return; |
| 149 | +} |
| 150 | + |
| 151 | +// |
| 152 | +// Function: CloseAndFreeSocketInfo |
| 153 | +// |
| 154 | +// Description: |
| 155 | +// Closes all sockets belonging to the specified provider and frees |
| 156 | +// the context information. If the lower provider socket is still |
| 157 | +// valid, set an abortive linger, and close the socket. |
| 158 | +// |
| 159 | +void |
| 160 | +FreeSocketContextList( |
| 161 | + PROVIDER *provider |
| 162 | + ) |
| 163 | +{ |
| 164 | + LIST_ENTRY *lptr = NULL; |
| 165 | + SOCKET_CONTEXT *context = NULL; |
| 166 | + |
| 167 | + ASSERT( provider ); |
| 168 | + |
| 169 | + // Walk the list of sockets |
| 170 | + while ( !IsListEmpty( &provider->SocketList ) ) |
| 171 | + { |
| 172 | + lptr = RemoveHeadList( &provider->SocketList ); |
| 173 | + |
| 174 | + ASSERT( lptr ); |
| 175 | + |
| 176 | + context = CONTAINING_RECORD( lptr, SOCKET_CONTEXT, Link ); |
| 177 | + |
| 178 | + // Context is already removed so just free it |
| 179 | + LspFree( context ); |
| 180 | + } |
| 181 | + |
| 182 | + return; |
| 183 | +} |
0 commit comments