// FreeSenderDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FreeSender.h"
#include "FreeSenderDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
CString m_from;//
CString m_to;
CString m_msg;
byte f1,f2,f3,f4,t1,t2,t3,t4;
//****************************************************************
#pragma comment( lib, "ws2_32.lib" ) /*这句不能少。*/
// Packet format found thanks to a bit a sniffing
static unsigned char packet_header[] =
"\x04\x00\x28\x00"
"\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\xf8\x91\x7b\x5a\x00\xff\xd0\x11\xa9\xb2\x00\xc0"
"\x4f\xb6\xe6\xfc"
"\xff\xff\xff\xff" // @40 : unique id over 16 bytes ?
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\xff\xff\xff\xff"
"\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\xff\xff\xff\xff"
"\xff\xff\xff\xff" // @74 : fields length
"\x00\x00";
unsigned char field_header[] =
"\xff\xff\xff\xff" // @0 : field length
"\x00\x00\x00\x00"
"\xff\xff\xff\xff"; // @8 : field length
//打包发送函数,单ip可调用之。
int sd(unsigned long ip,CString from="a",CString machine="b",CString msg="c")
{
int i, packet_size, fields_size, s;
char packet[8192];
struct sockaddr_in addr;
WSADATA wsaData;
WSAStartup(0x0202, &wsaData);
ZeroMemory(&addr, sizeof(addr));
addr.sin_addr.s_addr = ip;
addr.sin_port = htons(135);
addr.sin_family = AF_INET;
ZeroMemory(packet, sizeof(packet));
packet_size = 0;
memcpy(&packet[packet_size], packet_header, 80);
packet_size += 80;
i = from.GetLength()+1;
*(unsigned int *)(&field_header[0]) = i;
*(unsigned int *)(&field_header[8]) = i;
memcpy(&packet[packet_size], field_header, 12);
packet_size += 12;
strcpy(&packet[packet_size], from);
packet_size += (((i - 1) >> 2) + 1) << 2; // padded to a multiple of 4
i = machine.GetLength() + 1;
*(unsigned int *)(&field_header[0]) = i;
*(unsigned int *)(&field_header[8]) = i;
memcpy(&packet[packet_size], field_header, 12);
packet_size +=12;
strcpy(&packet[packet_size], machine);
packet_size += (((i - 1) >> 2) + 1) << 2; // padded to a multiple of 4
i = msg.GetLength() + 1;
*(unsigned int *)(&field_header[0]) = i;
*(unsigned int *)(&field_header[8]) = i;
memcpy(&packet[packet_size], field_header, 12);
packet_size += 12;
strcpy(&packet[packet_size], msg);
packet_size += i;
fields_size = packet_size - 80;
*(unsigned int *)(&packet[40]) = time(NULL);
*(unsigned int *)(&packet[74]) = fields_size;
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
return 0;
if (sendto(s, packet, packet_size, 0, (struct sockaddr *)&addr, sizeof(addr)) == -1)
return 0;
return 1;
}
//消息群发,发送线程
UINT Send(LPVOID mpPara)
{
CString str;
long int ip;
int i, packet_size, fields_size, s;
char packet[8192];
struct sockaddr_in addr;
WSADATA wsaData;
ZeroMemory(packet, sizeof(packet));
packet_size = 0;
memcpy(&packet[packet_size], packet_header, 80);
packet_size += 80;
i = m_from.GetLength()+1;
*(unsigned int *)(&field_header[0]) = i;
*(unsigned int *)(&field_header[8]) = i;
memcpy(&packet[packet_size], field_header, 12);
packet_size += 12;
strcpy(&packet[packet_size], m_from);
packet_size += (((i - 1) >> 2) + 1) << 2; // padded to a multiple of 4
i = m_to.GetLength() + 1;
*(unsigned int *)(&field_header[0]) = i;
*(unsigned int *)(&field_header[8]) = i;
memcpy(&packet[packet_size], field_header, 12);
packet_size +=12;
strcpy(&packet[packet_size], m_to);
packet_size += (((i - 1) >> 2) + 1) << 2; // padded to a multiple of 4
i = m_msg.GetLength() + 1;
*(unsigned int *)(&field_header[0]) = i;
*(unsigned int *)(&field_header[8]) = i;
memcpy(&packet[packet_size], field_header, 12);
packet_size += 12;
strcpy(&packet[packet_size], m_msg);
packet_size += i;
fields_size = packet_size - 80;
*(unsigned int *)(&packet[40]) = time(NULL);
*(unsigned int *)(&packet[74]) = fields_size;
for(int l=f1;l<=t1;l++)
for(int j=f2;j<=t2;j++)
for(int m=f3;m<=t3;m++)
for(int n=f4;n<=t4;n++)
{
str.Format("%d.%d.%d.%d",l,j,m,n);
ip=inet_addr(str);
WSAStartup(0x0202, &wsaData);
ZeroMemory(&addr, sizeof(addr));
addr.sin_addr.s_addr = ip;
addr.sin_port = htons(135);
addr.sin_family = AF_INET;
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
AfxMessageBox("create socket error!");
continue;
}
if(sendto(s, packet, packet_size, 0, (struct sockaddr *)&addr, sizeof(addr)) == -1)
{
AfxMessageBox("send failed!");
continue;
}
}
return 1;
}
//****************************************************************
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFreeSenderDlg dialog
CFreeSenderDlg::CFreeSenderDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFreeSenderDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFreeSenderDlg)
m_from = _T("USTC");
m_to = _T("USTC");
m_msg = _T("");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFreeSenderDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFreeSenderDlg)
DDX_Control(pDX, IDC_IPADDRESS2, m_ip2);
DDX_Control(pDX, IDC_IPADDRESS1, m_ip1);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_EDIT1, m_from);
DDX_Text(pDX, IDC_EDIT2, m_to);
DDX_Text(pDX, IDC_EDIT3, m_msg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFreeSenderDlg, CDialog)
//{{AFX_MSG_MAP(CFreeSenderDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
ON_BN_CLICKED(IDC_ADD, OnAdd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFreeSenderDlg message handlers
BOOL CFreeSenderDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE); // Set big