Home > Back-end >  The Socket Socket
The Socket Socket

Time:09-20

# define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include


# pragma comment (lib, "Ws2_32. Lib")

# define SOURCE_PORT 7234
# define MAX_RECEIVEBYTE 255
# define MAX_ADDR_LEN 32
# define SIO_RCVALL (IOC_IN | IOC_VENDOR | 1)//define network adapter for mixed mode

Typedef struct ip_hdr//define IP header
{
Unsigned char h_verlen;//4 first length, four IP version
Unsigned char tos.//eight service type TOS
Unsigned short tatal_len; The total length//16
Unsigned short ident.//16 labeled
Unsigned short frag_and_flags;//the offset and three marks a
Unsigned char TTL.//8 survival time TTL
Unsigned char proto.//8-bit protocol (TCP, UDP, or other)
Unsigned short checksum.//16 IP first inspection and
Unsigned int sourceIP;//32-bit source IP address
Unsigned int destIP;//32 destination IP address
} IPHEADER;



Typedef struct udp_hdr//define the UDP header
{
Unsigned short sport;//16 source port
Unsigned short dport;//16 purpose port
Unsigned short len.//UDP length
Unsigned short cksum;//check and
} UDPHEADER;

Int main (int arg c, char * * argv)
{
//try {


The SOCKET sock;
WSADATA WSD.
Char recvBuf [65535]={0};
DWORD dwBytesRet;

Int pCount=0;
Unsigned int optval=1;
Unsigned char * dataudp=NULL;

Int lenudp lenip;
WSAStartup (MAKEWORD (2, 1), & amp; WSD);

If ((the sock=socket (AF_INET SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)//create a raw socket
{
exit(0);
}

The char FAR name [MAXBYTE];
Gethostname (name, MAXBYTE);
The struct hostent FAR * pHostent;

PHostent=(struct hostent *) malloc (sizeof (struct hostent));
PHostent=gethostbyname (name);
SOCKADDR_IN sa;
Sa. Sin_family=AF_INET;
Sa. Sin_port=htons (1);//raw socket without the concept of port, so this value to set the
Memcpy (& amp; Sa. Sin_addr pHostent - & gt; H_addr_list [0], pHostent - & gt; H_length);//set the machine address

Bind (sock, (SOCKADDR *) & amp; Sa, sizeof (sa));//bind
If (WSAGetLastError ()==10013)
{
exit(0);
}

//set the nic for mixed mode, also called pan-listens mode, can listen to after all of the packages,
WSAIoctl (sock, SIO_RCVALL, & amp; Optval, sizeof (optval), NULL, 0, & amp; DwBytesRet, NULL, NULL);

UDPHEADER * pUdpheader;//UDP header structure pointer
IPHEADER * pIpheader;//IP head structure pointer

Char szSourceIP [MAX_ADDR_LEN], szDestIP [MAX_ADDR_LEN];//source IP and destination IP
SOCKADDR_IN saSource saDest; Structure//the source address, destination address structure

//set the various head pointer
PIpheader=(IPHEADER * recvBuf);
PUdpheader=(UDPHEADER *) (recvBuf + sizeof (IPHEADER));
//int iIphLen=sizeof (unsigned long) * (pIpheader - & gt; H_verlen & amp; 0 x0f);
While (1)
{

Memset (recvBuf, 0, sizeof (recvBuf));//to empty buffer
Recv (sock, recvBuf, sizeof (recvBuf), 0).//receive package

//get the source and destination addresses
SaSource. Sin_addr. S_addr=pIpheader - & gt; SourceIP;
Strncpy (szSourceIP, inet_ntoa (saSource sin_addr), MAX_ADDR_LEN);
SaDest. Sin_addr. S_addr=pIpheader - & gt; DestIP;
Strncpy (szDestIP, inet_ntoa (saDest sin_addr), MAX_ADDR_LEN);

//in calculating the length of the package (only after the judgment whether the package have a meaning, first calculated)

Lenudp=ntohs (pIpheader - & gt; Tatal_len) - (sizeof (IPHEADER) + sizeof (UDPHEADER));


//judge whether the UDP packet
If (pIpheader - & gt; Proto==IPPROTO_UDP & amp; & Lenudp!=0)
{
PCount++;//count plus one

Dataudp=(unsigned char *) recvBuf + sizeof (IPHEADER) + sizeof (UDPHEADER);
system("cls");

Printf (" \ n# # # # # # # # # # # # # # # # # packets [I] %=% d bytes of data # # # # # # # # # # # # # ", pCount, lenudp);
Printf (" \ n * * * * * * * * * * IP head * * * * * * * * * * * ");
Printf (" \ n source IP: % s ", szSourceIP);
Printf (" \ n destination IP: % s ", szDestIP);
Printf (" \ n * * * * * * * * * * the UDP protocol head * * * * * * * * * * * ");
Printf (" \ n source port: % I ", ntohs (pUdpheader - & gt; Sport));
Printf (" \ n destination port: % I ", ntohs (pUdpheader - & gt; Dport));

If (ntohs (pUdpheader - & gt; Dport)==5169) {

Printf (" \ n data: \ n ");
for (int i=0; i <189; I++)//print out the first 100 bytes of hexadecimal data
{
Printf (" % x, "dataudp [I]);//
}
}

}

}//while

}



Consult:

Code can intercept the machine from the normal UDP data

Problems when running in the process, to disable the network card, IP address into 127.0.0.1 card when I was in the while loop

Excuse me how wrong the try is invalid
  • Related