c++ - Why raw socket sendto() function fails with 10022? -
i developing protocol fuzzer icmp, tcp, udp , on under vs2010. have sent out icmp , udp packets raw socket interface. encountered problem when sending tcp.
my environment: windows 7 ultimate x64, administrator account.
here way:
1) construct transport header , data myself.
2) use raw socket ipv4 header , below. rawconnect() function used create such raw socket. strip ip address, , strprotocol ip protocol number need specified (like icmp 1, igmp 2, tcp 6, udp 17). because didn't construct ip header myself (m_bselfconstructipheader = false), ip protocol number necessary. didn't know if used right or not?
3) use sendto() function send buffer, returns -1, , wsagetlasterror() returns 10022 means "an invalid argument supplied."
below code, thx!
#include "stdafx.h" #include "afrawsocket.h" #define ip_hdrincl 2 afrawsocket::afrawsocket() { m_socket = null; m_bselfconstructipheader = false; } cstring afrawsocket::itos(int i) { cstring strtemp; strtemp.format(_t("%d"), i); return strtemp; } int afrawsocket::stoi(cstring s) { return _wtoi(s); } // // cstringaתcstringw // cstringw afrawsocket::cstra2cstrw(const cstringa &cstrsrca) { int len = multibytetowidechar(cp_acp, 0, lpcstr(cstrsrca), -1, null, 0); wchar_t *wstr = new wchar_t[len]; memset(wstr, 0, len*sizeof(wchar_t)); multibytetowidechar(cp_acp, 0, lpcstr(cstrsrca), -1, wstr, len); cstringw cstrdestw = wstr; delete[] wstr; return cstrdestw; } // // cstringwתcstringa // cstringa afrawsocket::cstrw2cstra(const cstringw &cstrsrcw) { int len = widechartomultibyte(cp_acp, 0, lpcwstr(cstrsrcw), -1, null, 0, null, null); char *str = new char[len]; memset(str, 0, len); widechartomultibyte(cp_acp, 0, lpcwstr(cstrsrcw), -1, str, len, null, null); cstringa cstrdesta = str; delete[] str; return cstrdesta; } void afrawsocket::setselfconstructipheader(bool bselfconstructipheader) { m_bselfconstructipheader = bselfconstructipheader; } bool afrawsocket::rawconnect(cstring strip, cstring strprotocol) { int optval; cstringa straip = cstrw2cstra(strip); int iprotocol; if (strprotocol == _t("n/a")) { iprotocol = 0; } else { iprotocol = stoi(strprotocol); } sockaddr_in mysock; zeromemory((char*)&mysock,sizeof(mysock));//初始化 mysock.sin_family=af_inet; mysock.sin_addr.s_addr=inet_addr(straip); //create raw tcp packet //printf("\ncreating raw tcp socket..."); //eth_p_all //af_packet // if (iprotocol == ipproto_tcp) // { // iprotocol = ipproto_udp; // } if((m_socket = socket(af_inet, sock_raw, iprotocol/*ipproto_raw*/))==socket_error) { //printf("creation of raw socket failed."); mymessagebox_error(_t("rawconnect")); return false; } //printf("raw tcp socket created successfully."); //////////////////////////////////////////////// if (m_bselfconstructipheader) { //put socket in raw mode. //printf("\nsetting socket in raw mode..."); if(setsockopt(m_socket, ipproto_ip, ip_hdrincl, (char *)&optval, sizeof(optval))==socket_error) { //printf("failed set socket in raw mode."); mymessagebox_error(_t("rawconnect")); return false; } //printf("successful."); //////////////////////////////////////////////// } // //target hostname // printf("\nenter hostname : "); // gets(host); // printf("\nresolving hostname..."); // if((server=gethostbyname(host))==0) // { // printf("unable resolve."); // return 0; // } zeromemory(&m_dest, sizeof(m_dest)); m_dest.sin_family = af_inet; //m_dest.sin_port = htons(0); //your destination port m_dest.sin_addr.s_addr=inet_addr(straip); //m_dest.sin_addr.s_addr=inet_addr("8.8.8.8"); //m_dest.sin_addr.s_addr=inet_addr("202.112.128.50"); //memcpy(&m_dest.sin_addr.s_addr,&mysock.sin_addr.s_addr,4); //printf("resolved."); ///////////////////////////////////////////////// // if (bind(m_socket, (sockaddr *) &m_dest, sizeof(m_dest)) < 0) // { // int ierror = wsagetlasterror(); // mymessagebox_error(_t("rawconnect")); // return false; // } return true; } void afrawsocket::rawsend(lpbyte lpbuffer, dword dwsize) { int iret = sendto(m_socket , (const char *) lpbuffer, dwsize, 0, (sockaddr *)&m_dest, sizeof(m_dest)); //if (iret == socket_error) if (iret <= 0) { //printf("error sending packet : %d",wsagetlasterror()); int ierror =wsagetlasterror(); mymessagebox_error(_t("rawsend")); } } void afrawsocket::rawdisconnect() { if (m_socket) { closesocket(m_socket); m_socket = null; } }
Comments
Post a Comment