3.0 Winsock
3.0 Winsock
3.0 Winsock
5
à Out-of-band data ready for reading WSAGetLastError() returns error codes which
à Socket readiness for accepting incoming avoid conflict with standard Microsoft C error
connection codes. Certain error codes returned by certain
à Completion of non-blocking connect() Windows Sockets routines fall into the standard
à Connection closure range of error codes as defined by Microsoft C. If
you are NOT using an application development
environment which defines error codes consistent
Asynchronous Support Routines
with Microsoft C, you are advised to use the
The asynchronous "database" functions allow Windows Sockets error codes prefixed by "WSA"
applications to request information in an to ensure accurate error code detection.
asynchronous manner. Some network
implementations and/or configurations perform Note that this specification defines a
network based operations to resolve such recommended set of error codes, and lists the
requests. The WSAAsyncGetXByY() functions possible errors which may be returned as a result
allow application developers to request services of each function. It may be the case in some
which would otherwise block the operation of the implementations that other Windows Sockets
whole Windows environment if the standard error codes will be returned in addition to those
Berkeley function were used. The listed, and applications should be prepared to
WSACancelAsyncRequest() function allows an handle errors other than those enumerated under
application to cancel any outstanding each API description. However a Windows
asynchronous request. Sockets implementation must not return any
value which is not enumerated in the table of
legal Windows Sockets errors given in Error
Hooking Blocking Methods Codes
As noted in Blocking/Non blocking & Data
Volatility, Windows Sockets implements
Accessing a Windows Sockets DLL from an
blocking operations in such a way that Windows Intermediate DLL
message processing can continue, which may
result in the application which issued the call A Windows Sockets DLL may be accessed both
receiving a Windows message. In certain directly from an application and through an
situations an application may want to influence or "intermediate" DLL. An example of such an
change the way in which this pseudo-blocking intermediate DLL would be a virtual network
process is implemented. The API layer that supports generalized network
WSASetBlockingHook() provides the ability to functionality for applications and uses Windows
substitute a named routine which the Windows Sockets. Such a DLL could be used by several
Sockets implementation is to use when applications simultaneously, and the DLL must
relinquishing the processor during a "blocking" take special precautions with respect to the
operation. WSAStartup() and WSACleanup() calls to
ensure that these routines are called in the context
of each task that will make Windows Sockets
Error Handling calls. This is because the Windows Sockets DLL
For compatibility with thread-based will need a call to WSAStartup() for each task in
environments, details of API errors are obtained order to set up task-specific data structures, and a
through the WSAGetLastError() API. Although call to WSACleanup() to free any resources
the accepted "Berkeley-Style" mechanism for allocated for the task.
obtaining socket-based network errors is via There are (at least) two ways to accomplish this.
"errno", this mechanism cannot guarantee the The simplest method is for the intermediate DLL
integrity of an error ID in a multi-threaded to have calls similiar to WSAStartup() and
environment. WSAGetLastError() allows you to WSACleanup() that applications call as
retrieve an error code on a per thread basis. appropriate. The DLL would then call
6
WSAStartup() or WSACleanup() from within The current version of Winsock API is Version 2.2.2,
these routines. Another mechanism is for the which has several important features:
intermediate DLL to build a table of task handles, l Multiple Protocol support
which are obtained from the GetCurrentTask() l Transport Protocol Independence: Choose
Windows API, and at each entry point into the protocol by the services they provide
l Multiple Namespaces: Select the protocol you
intermediate DLL check whether WSAStartup() want to resolve hostnames, or locate services
has been called for the current task, then call l Scatter and Gather: Receive and send, to and from
WSAStartup() if necessary. multiple buffers
l Overlapped I/O and Event Objects: Utilize Win32
If a DLL makes a blocking call and does not
paradigms for enhanced throughput
install its own blocking hook, then the DLL l Quality of Service: Negotiate and keep track of
author must be aware that control may be bandwidth per socket
returned to the application either by an l Multipoint and Multicast: Protocol independent
application-installed blocking hook or by the APIs and protocol specific APIs
default blocking hook. Thus, it is possible that the l Conditional Acceptance: Ability to reject or defer
application will cancel the DLL's blocking a connect request before it occurs
l Connect and Disconnect data: For transport
operation via WSACancelBlockingCall(). If this protocols that support it (NOTE: TCP/IP does not)
occurs, the DLL's blocking operation will fail l Socket Sharing: Two or more processes can share
with the error code WSAEINTR, and the DLL a socket handle
must return control to the calling task as quickly l Vendor IDs and a mechanism for vendor
as possible, as the used has likely pressed a extensions: Vendor specific APIs can be added
l Layered Service Providers: The ability to add
cancel or close button and the task has requested
services to existing transport providers
control of the CPU. It is recommended that DLLs
which make blocking calls install their own
blocking hooks with WSASetBlockingHook() to
prevent unforeseen interactions between the
application and the DLL.
Note that this is not necessary for DLLs in
Windows NT because of its different process and
DLL structure. Under Windows NT, the
intermediate DLL could simply call
WSAStartup() in its DLL initialization routine,
which is called whenever a new process which
uses the DLL starts.
7
Winsock
Description
DLLs
Winsock.dll 16-bit Winsock 1.1
Wsock32.dll 32-bit Winsock 1.1
Ws2_32.dll Main Winsock 2.0
WSK applications discover and attach to the WSK
Microsoft extensions to Winsock. subsystem by using a set of WSK registration functions.
Mswsock.dll Mswsock.dll is an API that supplies Applications can use these functions to dynamically
services that are not part of Winsock. detect when the WSK subsystem is available and to
Platform-specific utilities. Ws2help.dll exchange dispatch tables that constitute the provider and
Ws2help.dll supplies operating system–specific code client side implementations of the WSK NPI.
that is not part of Winsock.
What is a DLL?
Wshtcpip.dll Helper for TCP
In a nut shell, a dynamic link library (DLL) is a collection
Wshnetbs.dll Helper for NetBT of small programs, which can be called upon when
Wshirda.dll Helper for IrDA needed by the executable program (EXE) that is running.
The DLL lets the executable communicate with a specific
Wshatm.dll Helper for ATM device such as a printer or may contain source code to do
Wshisn.dll Helper for Netware particular functions.
Wshisotp.dll Helper for OSI transports An example would be if the program (exe) needs to get
the free space of your hard drive. It can call the DLL file
Sfmwshat.dll Helper for Macintosh that contains the function with parameters and a call
Nwprovau.dll Name resolution provider for IPX function. The DLL will then tell the executable the free
space. This allows the executable to be smaller in size and
Rnr20.dll Main name resolution not have to write the function that has already exists.
Winrnr.dll LDAP name resolution This allows any program the information about the free
Msafd.dll Winsock interface to kernel space, without having to write all the source code and it
saves space on your hard drive as well. When a DLL is
Winsock kernel interface to TDI transport used in this fashion are also known as shared files.
Afd.sys
protocols
Advantage of DLL
The advantage of DLL files is that, because they do not
The DLL (Dynamic Link Library) files installed on a get loaded into random access memory (RAM) together
Windows XP system to provide the Winsock API with the main program, space is saved in RAM. When
(Application Programming Interface) to all Winsock and if a DLL file is called, then it is loaded. For example,
applications: you are editing a Microsoft Word document, the printer
l WS2_32.DLL - Providing Winsock 2 32-bit API DLL file does not need to be loaded into RAM. If you
and running on top of a collection of Winsock 2 decide to print the document, then the printer DLL file is
SPIs (Service Provider Interfaces). loaded and a call is made to print.
l WSOCK32.DLL - Providing Winsock 1.1 32-bit Uses fewer resources
API and running on top of the Winsock 2 API. When multiple programs use the same library of
l WINSOCK.DLL - Providing Winsock 1.1 16-bit functions, a DLL can reduce the duplication of code that
API and running on top of the Winsock 2 API. is loaded on the disk and in physical memory.
l mswsock.dll is the DLL (Dynamic Link Library)
file that implements the Winsock 2 SPI (Service Promotes modular architecture
Provider Interface) as the Basic Server Provider in A DLL helps promote developing modular programs.
the Winsock 2 SPI architecture as described in the This helps you develop large programs that require
previous section. multiple language versions or a program that requires
modular architecture.
Eases deployment and installation
Winsock Kernel Architecture When a function within a DLL needs an update or a fix,
the deployment and installation of the DLL does not
The architecture of Winsock Kernel (WSK) is shown in require the program to be relinked with the DLL.
the following diagram. Additionally, if multiple programs use the same DLL, the
8
multiple programs will all benefit from the update or the
fix.
Windows Socket Library Overview
All in all a DLL is an executable file that cannot run on
Socket Functions
its own, it can only run from inside an executable file.
To do load a DLLl file, an executable needs to declare the
The Windows Sockets specification includes the
DLL function. A DLL may have many different functions following Berkeley-style socket routines:
in it. Then when needed the call is made with the required Øaccept() An incoming connection is
parameters. acknowledged and associated with an
The following list describes some of the files that are immediately created socket. The original socket
implemented as DLLs in Windows operating systems: is returned to the listening state.
l ActiveX Controls (.ocx) files Øbind() Assign a local name to an unnamed
An example of an ActiveX control is a calendar socket.
control that lets you select a date from a calendar. Øclosesocket() Remove a socket descriptor from
l Control Panel (.cpl) files the per-process object reference table. Only
An example of a .cpl file is an item that is located blocks if SO_LINGER is set.
in Control Panel. Each item is a specialized DLL. Øconnect() Initiate a connection on the specified
l Device driver (.drv) files
An example of a device driver is a printer driver socket.
that controls the printing to a printer. Øgetpeername() Retrieve the name of the peer
connected to the specified socket descriptor.
DLL dependencies Øgetsockname() Retrieve the current name for
When a program or a DLL uses a DLL function in the specified socket
another DLL, a dependency is created. Therefore, the Øgetsockopt() Retrieve options associated with
program is no longer self-contained, and the program may the specified socket descriptor.
experience problems if the dependency is broken. For Øhtonl() Convert a 32-bit quantity from host byte
example, the program may not run if one of the following order to network byte order.
actions occurs: Øhtons() Convert a 16-bit quantity from host
byte order to network byte order.
l A dependent DLL is upgraded to a new version.
Øinet_addr() Converts a character string
l A dependent DLL is fixed. representing a number in the Internet standard
l A dependent DLL is overwritten with an earlier ``.'' notation to an Internet address value.
version. Øinet_ntoa() Converts an Internet address value
to an ASCII string in ``.'' notation i.e. ``a.b.c.d''.
l A dependent DLL is removed from the computer. Øioctlsocket() Provide control for descriptors.
Ølisten() Listen for incoming connections on a
specified socket.
Internal use of Messages by Windows Øntohl() Convert a 32-bit quantity from network
Sockets Implementations byte order to host byte order.
In order to implement Windows Sockets purely Øntohs() Convert a 16-bit quantity from network
as a DLL, it may be necessary for the DLL to byte order to host byte order.
post messages internally for communication and Ørecv()* Receive data from a connected socket.
timing. This is perfectly legal; however, a Ørecvfrom()* Receive data from either a
Windows Sockets DLL must not post messages connected or unconnected socket.
to a window handle opened by a client Øselect()* Perform synchronous I/O
application except for those messages requested multiplexing.
by the application. A Windows Sockets DLL that Øsend()* Send data to a connected socket.
needs to use messages for its own purposes must Øsendto()* Send data to either a connected or
open a hidden window and post any necessary unconnected socket.
messages to the handle for that window. Øsetsockopt() Store options associated with the
specified socket descriptor.
9
Øshutdown() Shut down part of a full-duplex use of this extended API set is not mandatory for
connection. socket-based programming (with the exception of
Øsocket() Create an endpoint for communication WSAStartup() and WSACleanup()), it is
and return a socket descriptor. recommended for conformance with the
à * The routine can block if acting on a Microsoft Windows programming paradigm.
blocking socket. ØAsynchronous select() Mechanism
Database Functions ØAsynchronous Support Routines
The Windows Sockets specification defines the ØHooking Blocking Methods
following "database" routines. As noted earlier, a ØError Handling
Windows Sockets supplier may choose to ØAccessing a Windows Sockets DLL from an
implement these in a manner which does not Intermediate DLL
depend on local database files. The pointer ØInternal Use of Messages by Windows Sockets
returned by certain database routines such as Implementations
gethostbyname() points to a structure which is ØPrivate API Interfaces
allocated by the Windows Sockets library. The ØWSAAsyncGetHostByAddr() A set of
data which is pointed to is volatile and is good functions which provide asynchronous
only until the next Windows Sockets API call ØWSAAsyncGetHostByName() versions of the
from that thread. Additionally, the application standard Berkeley
must never attempt to modify this structure or to ØWSAAsyncGetProtoByName() getXbyY()
free any of its components. Only one copy of this functions. For example, the
structure is allocated for a thread, and so the ØWSAAsyncGetProtoByNumber()
application should copy any information which it WSAAsyncGetHostByName() function
needs before issuing any other Windows Sockets provides an asynchronous message based
API calls. ØWSAAsyncGetServByName() implementation
Øgethostbyaddr()* Retrieve the name(s) and of the standard Berkeley
address corresponding to a network address. ØWSAAsyncGetServByPort() gethostbyname()
Øgethostname() Retrieve the name of the local function.
host. ØWSAAsyncSelect() Perform asynchronous
Øgethostbyname()* Retrieve the name(s) and version of select()
address corresponding to a host name. ØWSACancelAsyncRequest() Cancel an
Øgetprotobyname()* Retrieve the protocol name outstanding instance of a
and number corresponding to a protocol name. WSAAsyncGetXByY() function.
Øgetprotobynumber()* Retrieve the protocol ØWSACancelBlockingCall() Cancel an
name and number corresponding to a protocol outstanding "blocking" API call
number. ØWSACleanup() Sign off from the underlying
Øgetservbyname()* Retrieve the service name Windows Sockets DLL.
and port corresponding to a service name. ØWSAGetLastError() Obtain details of last
Øgetservbyport()* Retrieve the service name and Windows Sockets API error
port corresponding to a port. ØWSAIsBlocking() Determine if the underlying
à * The routine can block under some Windows Sockets DLL is already blocking an
circumstances. existing call for this thread
ØWSASetBlockingHook() "Hook" the blocking
Microsoft Windows-specific Extension
Functions
method used by the underlying Windows
Sockets implementation
The Windows Sockets specification provides a ØWSASetLastError() Set the error to be returned
number of extensions to the standard set of by a subsequent WSAGetLastError()
Berkeley Sockets routines. Principally, these ØWSAStartup() Initialize the underlying
extended APIs allow message-based, Windows Sockets DLL.
asynchronous access to network events. While ØWSAUnhookBlockingHook() Restore the
10
original blocking function
11