Windows Sockets

Now that you are familiar with the various protocols available and their attributes, we'll take a look at using these protocols from Winsock. If you're familiar with Winsock, you know that the API is based on the concept of a socket. A socket is a handle to a transport provider. In Win32, a socket is not the same thing as a file descriptor and therefore is a separate type, SOCKET. Two functions create a socket:

 SOCKET WSASocket ( int af, int type, int protocol, LPWSAPROTOCOL_INFO lpProtocolInfo, GROUP g, DWORD dwFlags ); SOCKET socket ( int af, int type, int protocol ); 

The first parameter, af, is the address family of the protocol. For example, if you want to create either a UDP or a TCP socket, use the constant AF_INET to indicate the Internet Protocol (IP). The second parameter, type, is the socket type of the protocol. A socket type can be one of five values: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, SOCK_RAW, and SOCK_RDM. The third parameter is protocol. This field is used to qualify a specific transport if there are multiple entries for the given address family and socket type. Table 5-4 shows the values used for the address family, socket type, and protocol fields for a given network transport.

Table 5-4. Socket parameters

Protocol Address Family Socket Type Protocol
Internet Protocol (IP) AF_INET TCP SOCK_STREAM IPPROTO_IP
UDP SOCK_DGRAM IPPROTO_UDP
Raw sockets SOCK_RAW IPPROTO_RAW
IPPROTO_ICMP
IPX/SPX AF_NS MSAFD
nwlnkipx [IPX]
SOCK_DGRAM NSPROTO_IPX
AF_IPX MSAFD
nwlnkspx [SPX]
SOCK_SEQPACKET NSPROTO_SPX
MSAFD
nwlnkspx [SPX]
[Pseudo-stream]
SOCK_STREAM NSPROTO_SPX
MSAFD
nwlnkspx [SPXII]
SOCK_SEQPACKET NSPROTO_SPXII
MSAFD
nwlnkspx [SPXII]
[Pseudo-stream]
SOCK_STREAM NSPROTO_SPXII
NetBIOS AF_NETBIOS Sequential
Packets
SOCK_SEQPACKET LANA number
Datagrams SOCK_DGRAM LANA number
AppleTalk AF_APPLETALK MSAFD
AppleTalk [ADSP]
SOCK_RDM ATPROTO_ADSP
MSAFD
AppleTalk [ADSP]
[Pseudo-stream]
SOCK_STREAM ATPROTO_ADSP
MSAFD
AppleTalk [PAP]
SOCK_RDM ATPROTO_PAP
MSAFD
AppleTalk [RTMP]
SOCK_DGRAM DDPPROTO_RTMP
MSAFD
AppleTalk [ZIP]
SOCK_DGRAM DDPPROTO_ZIP
ATM AF_ATM MSAFD ATM AAL5
Native ATM (AAL5)
SOCK_RAW
ATMPROTO_AAL5
SOCK_RAW
ATMPROTO_AAL5
Infrared
Sockets
AF_IRDA MSAFD Irda [IrDA] _SOCK_STREAM SOCK_STREAM IRDA_PROTO

The first three parameters for creating a socket are organized in three tiers. The first and most important parameter is the address family. This specifies which protocol is being used. It also dictates the valid options for the second and third parameters. For example, choosing the ATM address family (AF_ATM) limits you to only raw sockets (SOCK_RAW) for the socket type. Likewise, by selecting an address family and a socket type, you are limited as to the protocol you choose. However, it is possible to pass a 0 for the protocol parameter. The system then chooses a transport provider based on the other two parameters, af and type. When enumerating the catalog entries for protocols, check the dwProviderFlags entry of the WSAPROTOCOL_INFO structure. If this field is set to PFL_MATCHES_PROTOCOL_ZERO, this is the default transport that will be used if the protocol parameter to socket or WSASocket is 0.

If you are using the WSASocket function and have enumerated all protocols using WSAEnumProtocols, you can select a WSAPROTOCOL_INFO structure and pass that to the WSASocket as the lpProtocolInfo parameter. If you then specify the constant FROM_PROTOCOL_INFO in all of the first three parameters (af, type, and protocol), the values from the WSAPROTOCOL_INFO structure you supplied are used instead. This is how you specify an exact protocol entry.

The last two flags of WSASocket are simple. The group parameter is always 0 because no version of Winsock supports socket groups. The dwFlags parameter is used to specify one or more of the following flags:

  • WSA_FLAG_OVERLAPPED
  • WSA_FLAG_MULTIPOINT_C_ROOT
  • WSA_FLAG_MULTIPOINT_C_LEAF
  • WSA_FLAG_MULTIPOINT_D_ROOT
  • WSA_FLAG_MULTIPOINT_D_LEAF

The first flag, WSA_FLAG_OVERLAPPED, is used to specify that this socket is capable of overlapped I/O, which is one of the possible communication models available in Winsock. This topic is covered in detail in Chapter 8. If you create a socket using the socket call, WSA_FLAG_OVERLAPPED is set by default. In general, it is a good idea to always set this flag when using WSASocket. The last four flags deal with multicast sockets.

Raw Sockets

When creating a socket with WSASocket, you can pass a WSAPROTOCOL_INFO structure into the call to define the kind of socket you want to create; however, you can create socket types that don't have an entry in the transport provider catalog. The best example of this is raw sockets under IP. Raw sockets are a form of communication that allows you to encapsulate other protocols within the UDP packet, such as the Internet Control Message Protocol (ICMP). ICMP's purpose is to deliver control, error, and informational messages among Internet hosts. Because ICMP does not provide any data transfer facilities, it is not considered to be at the same level as UDP or TCP, but at the same level as IP itself. Chapter 13 covers raw sockets in further detail.



Network Programming for Microsoft Windows
Linux Server Hacks, Volume Two: Tips & Tools for Connecting, Monitoring, and Troubleshooting
ISBN: 735615799
EAN: 2147483647
Year: 1998
Pages: 159

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net