Call now: (800) 766-1884  


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA

 SQL Server Scripts
 Scripts Menu





SQL Server xxx

SQL Server Tips by Gama and Naughter Consulting



The raw sockets support in the Microsoft stacks for Windows 2000, XP and 2003 is limited: raw ICMP and IGMP packets are possible but raw TCP and UDP packets are only possible with the IP_HDRINCL option. This option states that the entire IP header had to be built and the TCP, UDP or any other headers built within that IP header. (code based on Ken Christensen's rawsend.c)

Notice the two headers:

#include <winsock2.h>
#include <ws2tcpip.h>

The first one is the Winsock 2 header and the second is for IP_HDRINCL.

The function Hex2Dec simply converts hexadecimal data into its corresponding numeric value. The inputs are both nibbles (4-bits) in hexadecimal and the output is an integer value. Next there is some validation, to make sure the input is hexadecimal. The hexadecimal data is turned into binary data with the Hex2Dec function. This binary data is the entire IP data for the packet. There are a few steps to follow for sending the packet.

The first step is to initialize the Winsock interface:

int iResult = WSAStartup(wVersionRequested, &wsaData);

The second step is to create the raw socket:

dest_s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_IP, 0 , 0, 0);

The third step is to define the socket options:

dest_addr.sin_family = AF_INET;
ErrorCode = setsockopt(dest_s, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(int));

The fourth step is to send the data:

ErrorCode = sendto(dest_s, (char *)mess_buf, mess_len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));

The fifth step is to close the socket:

ErrorCode = closesocket(dest_s);

The sixth and last step is to clean up:


The input of this XP is an hexadecimal string that will contain the packetís data. The data can be created manually or captured with a packet sniffer and exported in hexadecimal format. Usually the best way to do it is to print to a file and specify the data appended as hexadecimal. The notorious Ethereal Packet sniffer utility and similar tools have this option.

Note: in captured data it is necessary to remove the first fourteen bytes because they are the Ethernet II header and Winsock 2 can only create packets one level above, like IP packets. Usually an IP packet will start with the hexadecimal values 45 00, the 4 is the IP version number, the 5 is the IP header length in blocks of 32 bits (for example a value of 4 means 128 bits). The 00 is the services field and has usually a zero value.

The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter





Burleson Consulting Remote DB Administration







Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.

Burleson Consulting
SQL Server database support


Copyright © 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter