//關(guān)機函數(shù)************************************************?
void ShutDown(void)//2000 or NT?
{?
OSVERSIONINFO osv;?
osv.dwOSVersionInfoSize=sizeof OSVERSIONINFO;?
GetVersionEx(&osv);?
if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT)//VER_PLATFORM_WIN32_WINDOWS 98 Me用這個宏?
{?
HANDLE hProcess,hToken;?
TOKEN_PRIVILEGES Privileges;?
LUID luid;?
hProcess=GetCurrentProcess();?
OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);?
Privileges.PrivilegeCount=1;?
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);?
Privileges.Privileges[0].Luid=luid;?
Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;?
AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);?
}?
ExitWindowsEx(EWX_POWEROFF,0);?
}?
//***************************************?
//創(chuàng)建一個非模態(tài)對話框,如?
//1聲明父對話框?
#include "Tx.h"?
//2在頭文件中改為CTx* pParent = NULL,并聲明CTx *tx;?
public:?
CInputmr(CTx* pParent = NULL); // standard constructor?
CTx *tx;?
//3在CPP文件中改為CTx* pParent /*=NULL*/,并聲明ID:CDialog(CInputmr::IDD, pParent)和tx(pParent)?
CInputmr::CInputmr(CTx* pParent /*=NULL*/)?
: CDialog(CInputmr::IDD, pParent),tx(pParent)?
{?
if(Create(CInputmr::IDD,pParent))//創(chuàng)建?
ShowWindow(SW_SHOW);//顯示窗口?
//就可以使用 tx-> 了?
}?
常用網(wǎng)絡(luò)數(shù)據(jù)包報頭結(jié)構(gòu)->以太網(wǎng)、ARP、IP、TCP、UDP、ICMP、DNS、UDP偽報頭***************************************?
#pragma pack(1)?
typedef struct ethdr //以太網(wǎng)包頭14字節(jié)?
{?
unsigned char destination_mac[6];//目標(biāo)MAC 6字節(jié)?
unsigned char source_mac[6];//源MAC 6字節(jié)?
unsigned short type;//后面的協(xié)議類型2字節(jié) 為0806表示后面跟的包為ARP包?
}ET_HEADER,*PETHDR;?
typedef struct arphdr //arp協(xié)議28字節(jié)?
{?
unsigned short hard_tpye;//硬件類型2字節(jié) 通常為01 (以太網(wǎng)地址)?
unsigned short protocol;//協(xié)議類型2字節(jié) 通常為80 (IP地址)?
unsigned char hard_length;//硬件地址長度1字節(jié) 通常為6?
unsigned char protocol_length;//協(xié)議地址長度1字節(jié) 通常為4 (IP協(xié)議)?
unsigned short operation_type;//操作類型 1為ARP請求,2為ARP應(yīng)答,3為RARP請求,4為RARP應(yīng)答?
unsigned char source_mac[6];//源物理地址?
struct in_addr source_ip;//源IP地址?
unsigned char destination_mac[6];//目的物理地址?
struct in_addr destination_ip;//目的IP地址?
}ARP_HEADER,*PARPHDR;?
// IP Header -- RFC 791 IP數(shù)據(jù)報頭?
typedef struct tagIPHDR?
{?
u_char VIHL; // Version and IHL 版本4bit = 4 和 首部長度4bit = 5?
u_char TOS; // Type Of Service 服務(wù)類型 1字節(jié)?
short TotLen; // Total Length 總長度2字節(jié),包括數(shù)據(jù)和報頭?
short ID; // Identification 標(biāo)識符2字節(jié)?
short FlagOff; // Flags and Fragment Offset 標(biāo)志 3bit 和分段偏移量 13bit?
u_char TTL; // Time To Live 生存期1字節(jié),為經(jīng)過路由器的總次數(shù)?
u_char Protocol; // Protocol 協(xié)議類型1字節(jié)?
u_short Checksum; // Checksum 首部(只是IP首部!!不然就錯了,過不了網(wǎng)關(guān)!!)校驗和2字節(jié)?
struct in_addr source_ip; // Internet Address - Source 源IP地址?
struct in_addr destination_ip; // Internet Address - Destination 目的IP地址?
}IP_HEADER, *PIP_HEADER;?
//TCP Header TCP數(shù)據(jù)報頭?
typedef struct tcp_hdr?
{?
unsigned short source_port; //源端口?
unsigned short destination_port; //目的端口?
unsigned long index; //32位序號?
unsigned long makesuer_index; //32位確認(rèn)序號?
unsigned short header_length_and_flags; //首部長度和標(biāo)志位?
unsigned short window_size; //窗口大小?
unsigned short checksum; //檢查和?
unsigned short exigency_pointer; //緊急指針?
}TCP_HEADER;?
//UDP Header --UDP數(shù)據(jù)報頭?
typedef struct udp_hdr?
{?
unsigned short source_port; //源端口?
unsigned short destination_port; //目的端口?
unsigned short length; //數(shù)據(jù)長度?
unsigned short checksum; //帶數(shù)據(jù)!檢查和?
} UDP_HEADER;?
// ICMP Header - RFC 792 ICMP數(shù)據(jù)報頭?
typedef struct tagICMPHDR?
{?
u_char Type; // Type 類型?
u_char Code; // Code 代碼?
u_short Checksum; // Checksum 校驗和?
u_short ID; // Identification 標(biāo)識符?
u_short Seq; // Sequence 序列號?
char Data; // Data 數(shù)據(jù)(依情況而定)?
}ICMP_HEADER, *PICMP_HEADER;?
// ICMP Echo Request ICMP 請求數(shù)據(jù)報?
typedef struct tagECHOREQUEST?
{?
ICMPHDR icmpHdr;?
DWORD dwTime;?
char cData[32];?
}IMCP_REQUEST, *PIMCP_REQUEST;?
// ICMP Echo Reply ICMP 回響數(shù)據(jù)報?
typedef struct tagECHOREPLY?
{?
IPHDR ipHdr;?
ECHOREQUEST echoRequest;?
char cFiller[256];?
}ICMP_REPLY, *ICMP_REPLY;?
typedef struct dns //DNS數(shù)據(jù)報:?
{?
unsigned short id;//標(biāo)識,通過它客戶端可以將DNS的請求與應(yīng)答相匹配;?
unsigned short flags;//標(biāo)志:[QR | opcode | AA| TC| RD| RA | zero | rcode ]?
unsigned short quests;//問題數(shù)目;?
unsigned short answers;//資源記錄數(shù)目;?
unsigned short author;//授權(quán)資源記錄數(shù)目;?
unsigned short addition;//額外資源記錄數(shù)目;?
}DNS,*PDNS;?
//在16位的標(biāo)志中:QR位判斷是查詢/響應(yīng)報文,opcode區(qū)別查詢類型,AA判斷是否為授權(quán)回答,TC判斷?
//是否可截斷,RD判斷是否期望遞歸查詢,RA判斷是否為可用遞歸,zero必須為0,rcode為返回碼字段。?
typedef struct psd //偽報頭,用于計算UDP校驗和?
{?
unsigned int source_ip; //源IP?
unsigned int destination_ip; //目的IP?
char mbz; // 0?
char protocol; //協(xié)議 UDP = 17?
unsigned short udp_length; //UDP 長度?
}PSD,*PPSD;?
//DNS查詢數(shù)據(jù)報:?
typedef struct query?
{?
unsigned short type; //查詢類型,大約有20個不同的類型?
unsigned short classes; //查詢類,通常是A類既查詢IP地址。?
}QUERY,*PQUERY;?
//DNS響應(yīng)數(shù)據(jù)報:?
typedef struct response?
{?
unsigned short name; //查詢的域名?
unsigned short type; //查詢類型?
unsigned short classes; //類型碼?
unsigned int ttl; //生存時間?
unsigned short length; //資源數(shù)據(jù)長度?
unsigned int addr; //資源數(shù)據(jù)?
}RESPONSE,*PRESPONSE;?
#pragma pack()?
網(wǎng)絡(luò)編程部分源碼->校驗和、隱藏IP方法、取得本地MAC********************************?
//注意!!!!!!?
//IP的檢查和只是IP頭部的20個字節(jié),不然過不了網(wǎng)關(guān)!!!!?
//而TCP和UDP和檢查和是數(shù)據(jù)和頭部都包括!!?
//在計算檢查和之前IP或UDP或TCP報頭中 所有的成員 必須被初始化,包括 checksum!!!?
//為了保證系統(tǒng)和機器的******兼容性******,在定義網(wǎng)絡(luò)結(jié)構(gòu)體或共用體時要加上?
#pragma pack(1)?
#pragma pack()?
//如:?
#pragma pack(1)?
typedef struct response?
{?
unsigned short name;?
unsigned short type;?
unsigned short classes;?
unsigned int ttl;?
unsigned short length;?
unsigned int addr;?
}RESPONSE,*PRESPONSE;?
#pragma pack()?
//sizeof(RESPONSE)==16,如果不加#pragma pack那么sizeof(RESPONSE)==20?
//局域網(wǎng)內(nèi)相同網(wǎng)段下的IP地址,數(shù)據(jù)包直接發(fā)給該主機的MAC地址,不同網(wǎng)段下的,數(shù)據(jù)包發(fā)給網(wǎng)關(guān),?
//如:202.198.153.17->202.198.153.64 以太包目的地址為202.198.153.64 的MAC地址?
//如:202.198.153.17->202.198.159.139 以太包目的地址為202.198.153.254(網(wǎng)關(guān)) 的MAC地址?
//隱藏IP的方法************************************************************************?
WSADATA wsd;?
SOCKET s;?
BOOL bOpt;?
struct sockaddr_in remote;?
USHORT *buf=NULL;?
ECHOREQUEST icmpHdr;?
int ret;?
unsigned short iTotalSize,?
iIPVersion,?
iIPSize,?
cksum = 0;?
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)//打開WSA網(wǎng)絡(luò)函數(shù)?
{?
MessageBox("WSAStartup() failed: ");?
return ;?
}?
s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0,0);//建立SOCKET,注意第二個一定為SOCK_RAW?
if (s == INVALID_SOCKET)?
{?
MessageBox("WSASocket() failed: ");?
return ;?
}?
bOpt = TRUE;?
//設(shè)置SOCKET屬性?
ret = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt));?
if (ret == SOCKET_ERROR)?
{?
MessageBox("setsockopt(IP_HDRINCL) failed: %d/n");?
return ;?
}?
//修改IP包的值?
iTotalSize = sizeof(icmpHdr) ;?
iIPVersion = 4;?
iIPSize = sizeof(IP_HDR) / sizeof(unsigned long);?
icmpHdr.ipHdr.ip_verlen = (iIPVersion << 4) | iIPSize;?
icmpHdr.ipHdr.ip_tos = 0;?
icmpHdr.ipHdr.ip_totallength = htons(iTotalSize);?
icmpHdr.ipHdr.ip_id = 0;?
icmpHdr.ipHdr.ip_offset = 0;?
icmpHdr.ipHdr.ip_ttl = 128;?
icmpHdr.ipHdr.ip_protocol = 1; //ICMP?
buf=(USHORT *)&icmpHdr;?
icmpHdr.ipHdr.ip_checksum = checksum(buf,sizeof(IP_HDR));?
icmpHdr.ipHdr.ip_srcaddr = inet_addr(m_source_ip);?
icmpHdr.ipHdr.ip_destaddr = inet_addr(m_destinition);?
icmpHdr.icmpHdr.Type=8;?
icmpHdr.icmpHdr.Code=0;?
icmpHdr.icmpHdr.ID=1;?
icmpHdr.icmpHdr.Seq=1;?
buf=(USHORT *)&icmpHdr;?
icmpHdr.icmpHdr.Checksum=checksum(buf+sizeof(IP_HDR),sizeof(icmpHdr)-sizeof(IP_HDR));?
icmpHdr.dwTime=GetTickCount();?
remote.sin_family = AF_INET;?
remote.sin_port = 0;?
remote.sin_addr.s_addr = inet_addr(m_destinition);?
struct timeval Timeout;?
fd_set readfds;?
readfds.fd_count = 1;?
readfds.fd_array[0] = s;?
Timeout.tv_sec = 1;?
Timeout.tv_usec = 0;?
int nRet;?
int nAddrLen = sizeof(struct sockaddr_in);?
ECHOREPLY echoReply;?
CString cs;?
for(int i=0;i<m_ping_times;i++)?
{ //發(fā)送IP包?
ret = sendto(s, (char *)&icmpHdr, iTotalSize, 0, (SOCKADDR *)&remote, sizeof(remote));?
if (ret == SOCKET_ERROR)?
MessageBox("sendto() failed:");?
/*?
select(1, &readfds, NULL, NULL, &Timeout);?
//接收請求回應(yīng)?
nRet = recvfrom(s,?
(LPSTR)&echoReply,?
sizeof(ECHOREPLY),?
0,?
(LPSOCKADDR)&remote,?
&nAddrLen);?
//檢查返回值?
if (nRet == SOCKET_ERROR)?
MessageBox("recvfrom()");?
cs.Format("%d",echoReply.ipHdr.ip_ttl);?
MessageBox(cs);?
//返回發(fā)送的時間?
*/?
Sleep(m_separete);?
}?
closesocket(s) ;?
WSACleanup() ;//關(guān)閉WSA?
}?
//校驗和程序*****************************************************************************?
USHORT CFffDlg::checksum(USHORT *buffer, int size)?
{?
unsigned long cksum=0;?
while (size > 1)?
{?
cksum += *buffer++;?
size -= sizeof(USHORT);?
}?
if (size)?
{?
cksum += *(UCHAR*)buffer;?
}?
cksum = (cksum >> 16) + (cksum & 0xffff);?
cksum += (cksum >>16);?
return (USHORT)(~cksum);?
}?
//取得本地機器IP地址*********************************************************************************************?
WORD wv=MAKEWORD(1,1);?
WSADATA ws;?
WSAStartup(wv,&ws);?
char name[256];?
gethostname(name,256);?
HOSTENT *hos=gethostbyname(name);?
CString cs;?
for(int i=0;hos!=NULL&&hos->h_addr_list[i]!=NULL;i++)?
{?
cs=inet_ntoa(*(struct in_addr*)hos->h_addr_list[i]);?
MessageBox(cs);?
}?
WSACleanup();?
//取得本地MAC地址*********************************************************************************************?
#include