Rawsocket UDP (WinXPSp2) - sendto() bekommt 10004
-
Ich versuche gerade IP/UDP Pakete über einen raw-socket zu senden, benutze dabei Windows XP SP3. Jedoch wenn ich versuche das Paket zu versenden, bekomme ich 10004 als errorcode (der Funktions-Aufruf wurde von außen gestoppt). Hier ist mein Code:
IP/TCP/UDP header:
/*Headr.h Apollo*/ #include <WinSock2.h> #include <WS2tcpip.h> #pragma once #ifndef _IP4_TCP_H_ //known example portnumbers #define _HTTPPORT_ 80 #define _FTPPORT_ 21 #define _NBIOSPORT_ 139 #define _HTTPSPORT_ 443 enum __tcp_data_overlay_ { IP_HEADER, IP_DATA = 24, IP_TCP_HEADER = 24, IP_TCP_DATA = 44 }; #define _UINT1(x) unsigned int x :1; #define _UINT2(x) unsigned int x :2; #define _UINT4(x) unsigned int x :4; #define _UINT6(x) unsigned int x :6; #define _UINT8(x) unsigned int x :8; #define _UINT14(x) unsigned int x :14; #define _UINT16(x) unsigned int x :16; #define _UINT32(x) unsigned int x :32; typedef struct __udp_hdr_ { _UINT16( __ui16_udp_src_ ) //source portnumber _UINT16( __ui16_udp_dest_) //destinational portnumber _UINT16( __ui16_udp_len_ ) //data length _UINT16( __ui16_udp_chk_ ) //checksum __udp_hdr_( __in unsigned __int16 _16_srcport, __in unsigned __int16 _16_destport, __in unsigned __int16 _16_length, __in unsigned __int16 _16_checksum ) : __ui16_udp_src_ (_16_srcport), __ui16_udp_dest_(_16_destport), __ui16_udp_len_ (_16_length ), __ui16_udp_chk_ (_16_checksum) {} }UDP_HDR; typedef struct __tcp_hdr_ { _UINT16( __ui16_tcp_src_ ) //source portnumber _UINT16( __ui16_tcp_dest_) //destinational portnumber _UINT32( __ui32_tcp_seq_ ) //sequenze number _UINT32( __ui32_tcp_ack_ ) //acknowledge number _UINT4( __ui4_tcp_off_ ) //offset _UINT6( __ui6_tcp_res_ ) //reserved _UINT1( __ui1_tcp_urg_ ) //flag urgent _UINT1( __ui1_tcp_ack_ ) //flag acknowledge _UINT1( __ui1_tcp_psh_ ) //flag puffer _UINT1( __ui1_tcp_rst_ ) //flag reset (connection stop) _UINT1( __ui1_tcp_syn_ ) //flag syncronization (connection initialization) _UINT1( __ui1_tcp_fin_ ) //flag finish (connection start) _UINT16( __ui16_tcp_win_ ) //window _UINT16( __ui16_tcp_chk_ ) //checksum _UINT16( __ui16_tcp_urp_ ) //urgent pointer __tcp_hdr_( __in unsigned __int16 _16_srcport_ = _HTTPPORT_, __in unsigned __int16 _16_destport_ = _HTTPPORT_, __in unsigned __int32 _32_seq_ = 0x00000000, __in unsigned __int32 _32_ack_ = 0x00000000, __in unsigned char _8_4off = 0x00, __in unsigned char _8_6res = 0x00, __in bool _8_1urg = 0x00, __in bool _8_1ack = 0x00, __in bool _8_1psh = 0x00, __in bool _8_1pst = 0x00, __in bool _8_1syn = 0x00, __in bool _8_1fin = 0x00, __in unsigned __int16 _16_win = 0x0000, __in unsigned __int16 _16_chk = 0x0000, __in unsigned __int16 _16_urp = 0x0000 ) : __ui16_tcp_src_(_16_srcport_), __ui16_tcp_dest_(_16_destport_), __ui32_tcp_seq_(_32_seq_), __ui32_tcp_ack_(_32_ack_), __ui4_tcp_off_(_8_4off), __ui6_tcp_res_(_8_6res), __ui1_tcp_urg_(_8_1urg), __ui1_tcp_ack_(_8_1ack), __ui1_tcp_psh_(_8_1psh), __ui1_tcp_rst_(_8_1pst), __ui1_tcp_syn_(_8_1syn), __ui1_tcp_fin_(_8_1fin), __ui16_tcp_win_(_16_win), __ui16_tcp_chk_(_16_chk), __ui16_tcp_urp_(_16_urp) {} }TCP_HDR; typedef struct __ip_4_hdr_ { _UINT4( __ui4_ip4_ver_ ) //ip version _UINT4( __ui4_ip4_hlen_ ) //header length _UINT8( __ui8_ip4_tos_ ) //type of service _UINT16( __ui16_ip4_tlen_) //total length _UINT16( __ui16_ip4_id_ ) //identity descriptor _UINT2( __ui2_ip4_flag_ ) //flag _UINT14( __ui14_ip4_off_ ) //offset _UINT8( __ui8_ip4_ttl_ ) //time to live _UINT8( __ui8_ip4_prot_ ) //protocol _UINT16( __ui16_ip4_chk_ ) //checksum _UINT32( __ui32_ip4_src_ ) //source address _UINT32( __ui32_ip4_dest_) //destinational address __ip_4_hdr_( __in unsigned __int8 _8_4ver = 0x04, __in unsigned __int8 _8_4hl = 0x00, __in unsigned __int8 _8_tos = 0x00, __in unsigned __int16 _16_tlen = 0x0000, __in unsigned __int16 _16_id = 0x0000, __in unsigned __int8 _8_2flag = 0x00, __in unsigned __int16 _16_14off= 0x0000, __in unsigned __int8 _8_ttl = 0x00, __in unsigned __int8 _8_prot = 0x00, __in unsigned __int16 _16_chk = 0x0000, __in unsigned __int32 _32_src = 0x00000000, __in unsigned __int32 _32_dest = 0x00000000 ) : __ui4_ip4_ver_(_8_4ver), __ui4_ip4_hlen_(_8_4hl), __ui8_ip4_tos_(_8_tos), __ui16_ip4_tlen_(_16_tlen), __ui16_ip4_id_(_16_id), __ui2_ip4_flag_(_8_2flag), __ui14_ip4_off_(_16_14off), __ui8_ip4_ttl_(_8_ttl), __ui8_ip4_prot_(_8_prot), __ui16_ip4_chk_(_16_chk), __ui32_ip4_src_(_32_src), __ui32_ip4_dest_(_32_dest) {} }IP4_HDR; #define _IP4_TCP_H_ #endif
Testcode:
/*Udp.cpp Apollo*/ #include "Headr.h" #include <stdio.h> #include <new> #pragma comment(lib,"ws2_32.lib") unsigned char* __stdcall GenerateUDPPackageA( __in const unsigned char* _DataToSend, __in const unsigned short _DataLength, __in const unsigned long _ulSrcAddress, __in const unsigned long _ulDestAddress, __in const unsigned short _usSrcPort, __in const unsigned short _usDestPort ) { unsigned char* pmem = new unsigned char[ sizeof(IP4_HDR) + sizeof(UDP_HDR) + _DataLength + 1 ], * pindex = pmem; if( !pmem ){ return ERROR; } else { if( new (pindex) IP4_HDR( 4, sizeof(IP4_HDR) / sizeof(unsigned long), 0, htons(sizeof(IP4_HDR) + sizeof( UDP_HDR ) + _DataLength), 0, 1, 0, 0xA0, 17, 0xFFFF, _ulSrcAddress, _ulDestAddress ) == NULL ) return ERROR; else pindex += sizeof(IP4_HDR); if( new (pindex) UDP_HDR( _usSrcPort, _usDestPort, sizeof( UDP_HDR ) + _DataLength, 0 ) == NULL ) return ERROR; else pindex += sizeof(TCP_HDR); memcpy( pindex, _DataToSend, _DataLength ); } return pmem; } unsigned __int64 __stdcall GenerateUDPSock() { unsigned __int64 ui64sock = false; if(!(ui64sock = socket(AF_INET,SOCK_RAW,IPPROTO_RAW))){ printf("GenerateUDPSock : socket %d\n",GetLastError()); return ERROR; } else { if( setsockopt( ui64sock,IPPROTO_IP,IP_HDRINCL,"\x00000001",sizeof(int)) == SOCKET_ERROR ){ printf("GenerateUDPSock : setsockopt %d\n",GetLastError()); return ERROR; } } return ui64sock; } /*************************************** * Mal testen....geht nicht */ WSADATA wsa = {0}; unsigned char data[]={"hello UDP!\n"}; void main() { if(WSAStartup(MAKEWORD(2,2),&wsa)) printf("WSAStartup %d\n",GetLastError); SOCKET fd = GenerateUDPSock(); if(!fd) printf("No socket!\n"); unsigned char *ppack = GenerateUDPPackageA( data, strlen((const char*)data), *(unsigned long*) gethostbyname("localhost")->h_addr_list[0], *(unsigned long*) gethostbyname("google.com")->h_addr_list[0], 20, 21 ); if(!ppack) printf("No package!\n"); SOCKADDR_IN sa = {0}; sa.sin_addr.s_addr = *(unsigned long*) gethostbyname("google.com")->h_addr_list[0]; sa.sin_family = AF_INET; sa.sin_port = htons(21); if( sendto( fd, (PCHAR) ppack, sizeof(IP4_HDR)+sizeof(UDP_HDR)+strlen((PCHAR)data), 0, (SOCKADDR*) &sa, sizeof(SOCKADDR_IN) ) == SOCKET_ERROR ) printf("No send %d!\n",GetLastError()); getchar(); } /* :-( */
Weiß mir jemand zu helfen?
-
Oh hab mein Problem schon gefunden:
setsockopt(...,"\x00\x00\x00\x01",sizeof(int));