rawsocket侦听数据包,发送伪包的问题
用一个rawsocket侦听数据包,另外一个rawsocket发送伪造数据包
现象:侦听正常,发送伪包时提示错误10022
问题:1、一个侦听一个构造伪包发送伪包,这样做可以么?
2、如果不可以应该如何做?如果可以为什么错误10022?是不是跟构造的数据包内容有关,还是因为第二个发送的rawsocket设置的不对?
//初始化两个rawsocket int iErrorCode; WSADATA wsaData; iErrorCode = WSAStartup(MAKEWORD(2,1), &wsaData); m_SockRaw = socket(AF_INET , SOCK_RAW , IPPROTO_IP); if(m_SockRaw == INVALID_SOCKET) return m_SockRaw; SOCKADDR_IN sa; sa.sin_family = AF_INET; sa.sin_port = htons(6000); //get the local IP char FAR name[MAX_HOSTNAME_LAN]; iErrorCode = gethostname(name, MAX_HOSTNAME_LAN); if(iErrorCode == SOCKET_ERROR) return SOCKET_ERROR; struct hostent FAR Hostent; struct hostent FAR * pHostent = &Hostent; pHostent = gethostbyname(name);// for(int i=0; pHostent->h_addr_list[i] != NULL; i++)// { memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[_interface], pHostent->h_length); iErrorCode = bind(m_SockRaw, (PSOCKADDR)&sa, sizeof(sa)); if(iErrorCode == SOCKET_ERROR) return SOCKET_ERROR;// } DWORD dwBufferLen[10] ; DWORD dwBufferInLen = 1 ; DWORD dwBytesReturned = 0 ; iErrorCode=WSAIoctl(m_SockRaw, SIO_RCVALL,&dwBufferInLen, sizeof(dwBufferInLen), &dwBufferLen, sizeof(dwBufferLen),&dwBytesReturned , NULL , NULL ); if(iErrorCode == SOCKET_ERROR) return SOCKET_ERROR; if((m_SendSock = WSASocket(AF_INET,SOCK_RAW,IPPROTO_RAW,NULL,0,WSA_FLAG_OVERLAPPED))==INVALID_SOCKET) { return 0; } //设置自己填充数据包 BOOL flag=1; if(setsockopt(m_SendSock, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag)) == SOCKET_ERROR) { return 0; } //设置超时时间 int timeout = 1000; if(setsockopt(m_SendSock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout,sizeof(timeout))==SOCKET_ERROR) { return 0; } while (1) { memset(RecvBuf, 0, sizeof(RecvBuf)); iErrorCode = recvfrom(frmMain->m_SockRaw, RecvBuf, sizeof(RecvBuf), 0, &sAddr, &fromLen); frmMain->ParseBuf(RecvBuf,iErrorCode); int iSendError = sendto(frmMain->m_SendSock,SendBuf,40,0,(struct sockaddr*)&sAddr,sizeof(sAddr)); if(iSendError<=0) { iSendError = GetLastError(); AnsiString strError; strError.sprintf("发送伪包错误:%d",iSendError); frmMain->mmoRec->Lines->Add(strError); } } }