怎样使用花生壳域名解析出IP.doc_第1页
怎样使用花生壳域名解析出IP.doc_第2页
怎样使用花生壳域名解析出IP.doc_第3页
怎样使用花生壳域名解析出IP.doc_第4页
怎样使用花生壳域名解析出IP.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

怎样使用花生壳域名解析出IP?-即通常所说的上线2010年10月05日 星期二 下午 01:09本例仅为学习.一个写远控的,连上线都不会.一问三不知,还能算是个网络编程的程序员?上线:就是敲门,1台电脑给另外一台电脑敲门,有人在就进去,没人在就回去.这是最基本的常识.方法:用的是原始套接字icmp获取的,其实就是一个ping程序(包括IP与网站名的那种ping,仅IP的不可以)主程序:ping.h与ping.cpp封装好的.直接在你的dlg.cpp程序中调用.其实算不上什么本事,都是抄来的.因为不是搞专业的远控,仅是学习,所以我也没花时间仔细去学这.cpp顺便介绍下花生壳:从/下载花生壳软件,登录后21就是本机现在的外网IP.就是你账号对应的网站,无论你用的是哪一台机器.(那么端口呢?端口指定的一样的).花生壳其它的就不用管了.你只要做一步即可:打开花生壳软件,登录你的账号即可.难吗?不难,很简单的1步-源代码 包括ping.h与ping.cpp,以及外部调用的函数*dlg.cpp-ping.h-/ Ping.h: interface for the CPing class./#if !defined(AFX_PING_H_DD196130_22F6_48DA_BAFC_B799A0034437_INCLUDED_)#define AFX_PING_H_DD196130_22F6_48DA_BAFC_B799A0034437_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#include #include #include #include #define IP_RECORD_ROUTE 0x7#define DEF_PACKET_SIZE 32 #define MAX_PACKET 1024 / Max ICMP packet size#define MAX_IP_HDR_SIZE 60 / Max IP header size w/options#define ICMP_ECHO 8#define ICMP_ECHOREPLY 0#define ICMP_MIN 8 / Minimum 8-byte ICMP packet (header)class CMyPingDlg;typedef struct _iphdr unsigned int h_len:4; / Length of the header unsigned int version:4; / Version of IP unsigned char tos; / Type of service unsigned short total_len; / Total length of the packet unsigned short ident; / Unique identifier unsigned short frag_and_flags; / Flags unsigned char ttl; / Time to live unsigned char proto; / Protocol (TCP, UDP etc) unsigned short checksum; / IP checksum unsigned int sourceIP; unsigned int destIP; IpHeader;typedef struct _icmphdr BYTE i_type; BYTE i_code; / Type sub code USHORT i_cksum; USHORT i_id; USHORT i_seq; / This is not the standard header, but we reserve space for time ULONG timestamp; IcmpHeader;/ IP option header - use with socket option IP_OPTIONS/typedef struct _ipoptionhdr unsigned char code; / Option type unsigned char len; / Length of option hdr unsigned char ptr; / Offset into options unsigned long addr9; / List of IP addrs IpOptionHeader;class CPing public:void SetConfigure(char * host,BOOL recordrout=FALSE,int size=DEF_PACKET_SIZE);void DecodeIPOptions(char *buf, int bytes);void Cleanup();void Ping(int timeout =1000);SOCKET m_hSocket;IpOptionHeader m_ipopt;SOCKADDR_IN m_addrDest;SOCKADDR_IN m_addrFrom;char *icmp_data; char *recvbuf;USHORT seq_no ;char *lpdest;int datasize;BOOL m_bRecordRout;CMyPingDlg * m_dlg;CPing(CMyPingDlg *dlg);virtual CPing();private:void DecodeICMPHeader(char *buf, int bytes, SOCKADDR_IN* from);USHORT checksum(USHORT *buffer, int size);void FillICMPData(char *icmp_data, int datasize);#endif / !defined(AFX_PING_H_DD196130_22F6_48DA_BAFC_B799A0034437_INCLUDED_)-ping.cpp-/ Ping.cpp: implementation of the CPing class./#include stdafx.h#include MyPing.h#include Ping.h#include MyPingDlg.h#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE=_FILE_;#define new DEBUG_NEW#endifCString DNS_IP=;/ Construction/Destruction/CPing:CPing(CMyPingDlg * dlg)m_dlg=dlg;icmp_data = NULL;seq_no = 0;recvbuf = NULL;m_bRecordRout = FALSE;lpdest = NULL;datasize = DEF_PACKET_SIZE;WSADATA wsaData;if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) AfxMessageBox(Sorry, you cannot load socket dll!); return ;m_hSocket = INVALID_SOCKET;CPing:CPing()if(lpdest) delete lpdest;void CPing:Ping(int timeout) m_hSocket = WSASocket (AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, WSA_FLAG_OVERLAPPED); if (m_hSocket = INVALID_SOCKET) AfxMessageBox(socket 创建失败!); return ; if (m_bRecordRout) / Setup the IP option header to go out on every ICMP packet / ZeroMemory(&m_ipopt, sizeof(m_ipopt); m_ipopt.code = IP_RECORD_ROUTE; / Record route option m_ipopt.ptr = 4; / Point to the first addr offset m_ipopt.len = 39; / Length of option header int ret = setsockopt(m_hSocket, IPPROTO_IP, IP_OPTIONS, (char *)&m_ipopt, sizeof(m_ipopt); if (ret = SOCKET_ERROR) AfxMessageBox(设置socket协议选项错误!); / Set the send/recv timeout values / int bread = setsockopt(m_hSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout); if(bread = SOCKET_ERROR) AfxMessageBox(设置socket接收超时选项错误!); return ; timeout = 1000; bread = setsockopt(m_hSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout); if (bread = SOCKET_ERROR) AfxMessageBox(设置socket发送超时选项错误!); return ; memset(&m_addrDest, 0, sizeof(m_addrDest); / / Resolve the endpoints name if necessary / m_addrDest.sin_family = AF_INET; if (m_addrDest.sin_addr.s_addr = inet_addr(lpdest) = INADDR_NONE) struct hostent *hp = NULL; if (hp = gethostbyname(lpdest) != NULL) memcpy(&(m_addrDest.sin_addr), hp-h_addr, hp-h_length); m_addrDest.sin_family = hp-h_addrtype;/ printf(m_addrDest.sin_addr = %sn, inet_ntoa(m_addrDest.sin_addr); else / printf(gethostbyname() failed: %dn, / WSAGetLastError(); AfxMessageBox(输入的主机不存在!); return ; / / Create the ICMP packet / datasize += sizeof(IcmpHeader); icmp_data =(char*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PACKET); recvbuf =(char*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PACKET); if (!icmp_data) AfxMessageBox(堆分配错误!); return ; memset(icmp_data,0,MAX_PACKET); FillICMPData(icmp_data,datasize); / / Start sending/receiving ICMP packets /static int nCount = 0;int nCount=0; while(1) int bwrote; if (nCount+ = 4) break; (IcmpHeader*)icmp_data)-i_cksum = 0; (IcmpHeader*)icmp_data)-timestamp = GetTickCount(); (IcmpHeader*)icmp_data)-i_seq = seq_no+; (IcmpHeader*)icmp_data)-i_cksum = checksum(USHORT*)icmp_data, datasize); bwrote = sendto(m_hSocket, icmp_data, datasize, 0, (struct sockaddr*)&m_addrDest, sizeof(m_addrDest); if (bwrote = SOCKET_ERROR) if (WSAGetLastError() = WSAETIMEDOUT) m_dlg-m_result+=Timed out ! rn; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); continue; AfxMessageBox(发送数据函数调用错误!); return ; if (bwrote m_result+=temp; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); int fromlen = sizeof(m_addrFrom); bread = recvfrom(m_hSocket, recvbuf, MAX_PACKET, 0, (struct sockaddr*)&m_addrFrom, &fromlen); if (bread = SOCKET_ERROR) if (WSAGetLastError() = WSAETIMEDOUT) m_dlg-m_result+=Timed out !rn; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); continue; AfxMessageBox(接收数据函数调用错误!); return ; DecodeICMPHeader(recvbuf, bread, &m_addrFrom); if(DNS_IP!=)/不等于空时表示ping通有IP了 AfxMessageBox(解析出域名IP:+DNS_IP);void CPing:Cleanup()if (m_hSocket != INVALID_SOCKET) closesocket(m_hSocket); HeapFree(GetProcessHeap(), 0, recvbuf); HeapFree(GetProcessHeap(), 0, icmp_data);/ WSACleanup(); return ;void CPing:FillICMPData(char *icmp_data, int datasize) IcmpHeader *icmp_hdr = NULL; char *datapart = NULL; icmp_hdr = (IcmpHeader*)icmp_data; icmp_hdr-i_type = ICMP_ECHO; / Request an ICMP echo icmp_hdr-i_code = 0; icmp_hdr-i_id = (USHORT)GetCurrentProcessId(); icmp_hdr-i_cksum = 0; icmp_hdr-i_seq = 0; datapart = icmp_data + sizeof(IcmpHeader);void CPing:DecodeIPOptions(char *buf, int bytes) IpOptionHeader *ipopt = NULL; IN_ADDR inaddr; int i; HOSTENT *host = NULL; ipopt = (IpOptionHeader *)(buf + 20);m_dlg-m_result+=Ping 结果: rn; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); for(i = 0; i ptr / 4) - 1; i+) inaddr.S_un.S_addr = ipopt-addri; if (i != 0) / m_dlg-m_result+= ; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); host = gethostbyaddr(char *)&inaddr.S_un.S_addr, sizeof(inaddr.S_un.S_addr), AF_INET); CString temp; if (host) temp.Format(%-15s) %s rn, inet_ntoa(inaddr), host-h_name); m_dlg-m_result+=temp; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); else temp.Format(%-15s)rn, inet_ntoa(inaddr); m_dlg-m_result+=temp; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); return;USHORT CPing: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);void CPing:SetConfigure(char * host,BOOL recordrout,int size)if(lpdest) delete lpdest; lpdest=NULL; m_bRecordRout = recordrout; datasize = size; lpdest = new char strlen(host)+1;strcpy(lpdest,host);void CPing:DecodeICMPHeader(char *buf, int bytes, SOCKADDR_IN *from) IpHeader *iphdr = NULL; IcmpHeader *icmphdr = NULL; unsigned short iphdrlen; DWORD tick; static int icmpcount = 0; iphdr = (IpHeader *)buf;/ Number of 32-bit words * 4 = bytes iphdrlen = iphdr-h_len * 4; tick = GetTickCount(); if (iphdrlen = MAX_IP_HDR_SIZE) & (!icmpcount) DecodeIPOptions(buf, bytes);CString temp; if (bytes sin_addr); m_dlg-m_result+=temp; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); icmphdr = (IcmpHeader*)(buf + iphdrlen); if (icmphdr-i_type != ICMP_ECHOREPLY) temp.Format(nonecho type %d recvd rn, icmphdr-i_type); m_dlg-m_result+=temp; m_dlg-SetDlgItemText(IDC_EDIT2,m_dlg-m_result); return; / Make sure this is an ICMP reply to something we sent! / if (icmphdr-i_id != (USHORT)GetCurrentProcessId() temp.Format(someone elses pa

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论