代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
监听发往本机和从本机发出的数据包
作者:
纳兰清风
/ 发布于
2013/3/18
/
720
#ifndef _TCP_HEAD_ #define _TCP_HEAD_ typedef struct _tcphdr { unsigned short tcp_sport; unsigned short tcp_dport; unsigned int tcp_seq; unsigned int tcp_ack; unsigned char tcp_lenres; unsigned char tcp_flag; unsigned short tcp_win; unsigned short tcp_sum; unsigned short tcp_urp; } TCP_HEADER,*pTCP_HEADER; #endif #ifndef _UDP_HEAD_ #define _UDP_HEAD_ typedef struct _udphdr { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_len; unsigned short uh_sum; }UDP_HEADER,*pUDP_HEADER; #endif #ifndef _IP_HEAD_ #define _IP_HEAD_ typedef struct _iphdr { unsigned char h_verlen; unsigned char tos; unsigned short total_len; unsigned short ident; unsigned short frag_and_flags; unsigned char ttl; unsigned char proto; unsigned short checksum; unsigned int sourceIP; unsigned int destIP; }IP_HEADER,*pIP_HEADER; #endif #pragma once #include <SDKDDKVer.h> #include <stdio.h> #include <tchar.h> #include "head.h" #include <winsock2.h> #include <iostream> #include <memory.h> #include <time.h> #include <windows.h> #include <Ws2tcpip.h> #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1) // socket.cpp // #include "stdafx.h" #include <string> #pragma comment (lib,"Ws2_32") void Decodepkt(char* buff,int len); std::string CheckProto(int pro); void DecodeUDP(char* buff); void DecodeTCP(char* buff); using namespace std; int main(int argc, char* argv[]) { WSADATA wsaData; struct sockaddr_in SourceAddr; char recvbuff[65535]={'\0'}; SOCKET ListenSocket; bool flag=true; int err=WSAStartup(MAKEWORD(2,2),&wsaData); if(err!=0){ cout<<"failed in WSAStartup()"<<endl; goto error; } ListenSocket=socket(AF_INET, SOCK_RAW,IPPROTO_IP); if(ListenSocket==INVALID_SOCKET) { cout<<"failed in socket()"<<WSAGetLastError()<<endl; goto error; } if(setsockopt(ListenSocket,IPPROTO_IP,IP_HDRINCL,(char*)&flag,sizeof(int))==SOCKET_ERROR) { cout<<"failed in setsockopt(IPP)"<<WSAGetLastError()<<endl; goto error; } struct hostent* phost; char hostname[100]={'\0'}; gethostname(hostname,100); phost=gethostbyname(hostname); int Sourcelen=sizeof(SourceAddr); SourceAddr.sin_family=AF_INET; SourceAddr.sin_port=htons(10000); memcpy(&SourceAddr.sin_addr.s_addr,phost->h_addr_list[0],phost->h_length); if(bind(ListenSocket,(struct sockaddr*)&SourceAddr,Sourcelen)!=0) { cout<<"failed in bind()"<<endl; goto error; } DWORD dwBuffIn=1; DWORD dwBuffOut[10]; DWORD dwBytesReturned =0; if(WSAIoctl(ListenSocket,SIO_RCVALL,&dwBuffIn,sizeof(dwBuffIn),dwBuffOut,sizeof(dwBuffOut),&dwBytesReturned,NULL,NULL)<0){ cout<<"failed in WSAIoctl()"<<endl; goto error; } int bytes_recv=0; while(true) { memset(recvbuff,0x00,sizeof(recvbuff)); bytes_recv=recv(ListenSocket,recvbuff,sizeof(recvbuff),0); if(bytes_recv==0) { continue; cout<<"empty"<<endl; } Decodepkt(recvbuff,sizeof(recvbuff)); } error: WSACleanup(); system("pause"); return 0; } void Decodepkt(char* buff,int len) { IP_HEADER* ip; ip=(pIP_HEADER)buff; string pro=CheckProto(ip->proto); struct in_addr source,dest; source.s_addr=(u_long)ip->sourceIP; dest.s_addr=(u_long)ip->destIP; cout<<"Network:"<<endl; cout<<"Src IP: "<<inet_ntoa(source)<<endl; cout<<"Dest IP: "<<inet_ntoa(dest)<<endl; cout<<"protocal:"<<pro<<endl; int iplen=(ip->h_verlen&0x0f)<<2; if(pro=="TCP") { DecodeTCP(buff+iplen); } else if(pro=="UDP") { DecodeUDP(buff+iplen); } } std::string CheckProto(int pro) { std::string result=""; switch(pro) { case 1: result="ICMP"; break; case 2: result="IGMP"; break; case 3: result="GGP"; break; case 4: result="IP"; break; case 6: result="TCP"; break; case 17: result="UDP"; break; default: result="Invalid protocol"; break; } return result; } void DecodeUDP(char* buff) { pUDP_HEADER udp=(pUDP_HEADER)buff; cout<<"Src Port:"<<ntohs(udp->uh_sport)<<endl; cout<<"Dest Port:"<<ntohs(udp->uh_dport)<<endl; char* data=buff+sizeof(UDP_HEADER); cout<<"Data:"<<data<<endl; } void DecodeTCP(char* buff) { pTCP_HEADER tcp=(pTCP_HEADER)buff; cout<<"Src Port:"<<ntohs(tcp->tcp_sport)<<endl; cout<<"Dest Port:"<<ntohs(tcp->tcp_dport)<<endl; char* data=buff+sizeof(TCP_HEADER); cout<<"Data:"<<data<<endl; }
试试其它关键字
数据包
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
纳兰清风
贡献的其它代码
(
2
)
.
监听发往本机和从本机发出的数据包
.
万年历实现类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3