|
Hi,
I am trying to broadcast a DHCP Message with unbound adapter (after ipconfig /release) on windows XP SP2.
When I try to send the message, I get the error 10065 (WSAEHOSTUNREACH).
I am trying to send the message using Microsoft Visual C++. I have tried using a RAW socket.
The Message is succesfully sent when an IP address is bound, but I receive 10065 when it is unbound (0.0.0.0).
I have also tried using a Java program, but I get the equivalent error: NoRouteToHostException.
I know it is possible to send such a message, since the Microsoft DHCP Client does it at "ipconfig /renew" after an "ipconfig /release".
Could anyone help me?
|
|
|
|
|
I'm confused. How can there be a "route to host" if your sending socket isn't bound?
Great job, team. Head back to base for debriefing and cocktails.
|
|
|
|
|
Thanks for your answer.
I don't know how there can be a route to host!
The destination of the message is not a specific host, it is broadcasted to 255.255.255.255.
But I know that this is possible. I you unbind your adapter with "ipconfig /release", then if you run "ipconfig /renew", a DHCP Message is broadcasted from 0.0.0.0 to 255.255.255.255.
It is possible but I do not know how to configure my socket to do this.
|
|
|
|
|
You still need to bind the socket.
Maybe create the broadcast socket something like (note I've omitted error handling):
sockaddr_in bindsocketaddr;
memset(&bindsocketaddr, 0, sizeof(bindsocketaddr));
bindsocketaddr.sin_family = AF_INET;
bindsocketaddr.sin_port = htons(portnumber);
bindsocketaddr.sin_addr.s_addr = INADDR_ANY;
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
BOOL fBroadcast = 1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&fBroadcast, sizeof(BOOL));
bind(sock, (sockaddr *)&bindsocketaddr, sizeof(bindsocketaddr));
Then using the broadcast socket, use sendto() to broadcast a DHCP message - the destination
address could be set up something like:
#define DHCP_SERVER_PORT 67
sockaddr_in destsocketaddr;
memset(&destsocketaddr, 0, sizeof(destsocketaddr));
destsocketaddr.sin_family = AF_INET;
destsocketaddr.sin_port = htons(DHCP_SERVER_PORT);
destsocketaddr.sin_addr.s_addr = INADDR_BROADCAST;
Hopefully that helps a bit
Mark
Great job, team. Head back to base for debriefing and cocktails.
|
|
|
|
|
Hi Mark,
thanks for the advice.
The code I was using was very close from this one.
I have tried with your code and I still get the 10065 "no route to host".
|
|
|
|
|
Have you checked for errors on all socket operations?
Is the port number you are broadcasting to valid?
Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
I just tested the following code exactly as shown and it worked for me.
Anything you are doing differently?
sockaddr_in bindsocketaddr;
memset(&bindsocketaddr, 0, sizeof(bindsocketaddr));
bindsocketaddr.sin_family = AF_INET;
bindsocketaddr.sin_port = htons(2700);
bindsocketaddr.sin_addr.s_addr = INADDR_ANY;
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
BOOL fBroadcast = 1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&fBroadcast, sizeof(BOOL));
bind(sock, (sockaddr *)&bindsocketaddr, sizeof(bindsocketaddr));
#define DHCP_SERVER_PORT 67
sockaddr_in destsocketaddr;
memset(&destsocketaddr, 0, sizeof(destsocketaddr));
destsocketaddr.sin_family = AF_INET;
destsocketaddr.sin_port = htons(DHCP_SERVER_PORT);
destsocketaddr.sin_addr.s_addr = INADDR_BROADCAST;
sendto(sock, "12345", 5, 0, (const struct sockaddr*)&destsocketaddr, sizeof(destsocketaddr));
closesocket(sock);
-- modified at 13:23 Wednesday 21st February, 2007
*EDIT* added comment to code for sendto()
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
One other thought - you have to use sendto() unless you call connect() on your socket.
Calling send() will fail because there's no destination address specified (not even the broadcast
address).
Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
Hello Mark,
I'm confused... I must be missing something!
Here is my code (same as yours with error handling). I compile using visual C++ 6.0. I have tested it on Windows XP Service Pack 1 and 2.
If I execute ipconfig /release, and then the following code, I get the "Error sending Packet : 10065" displayed.
Can you please e-mail me your code?
Thank you very much for your help.
#include "stdio.h"
#include "winsock2.h"
int main()
{
//Initialise Winsock
WSADATA wsock;
printf("\nInitialising Winsock...\n");
if (WSAStartup(MAKEWORD(2,2),&wsock) != 0){
fprintf(stderr,"WSAStartup() failed");
exit(EXIT_FAILURE);
}
printf("Initialised successfully.\n");
sockaddr_in bindsocketaddr;
memset(&bindsocketaddr, 0, sizeof(bindsocketaddr));
bindsocketaddr.sin_family = AF_INET;
bindsocketaddr.sin_port = htons(2700);
bindsocketaddr.sin_addr.s_addr = INADDR_ANY;
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sock==SOCKET_ERROR){
printf("Creation of socket failed.");
return 0;
}
BOOL fBroadcast = 1;
int iResult = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&fBroadcast, sizeof(BOOL));
if (iResult == SOCKET_ERROR){
printf("Error setsockopt : %d\n",WSAGetLastError());
}
iResult =bind(sock, (sockaddr *)&bindsocketaddr, sizeof(bindsocketaddr));
if (iResult == SOCKET_ERROR ){
printf("Unable to bind.");
return 0;
}
#define DHCP_SERVER_PORT 67
sockaddr_in destsocketaddr;
memset(&destsocketaddr, 0, sizeof(destsocketaddr));
destsocketaddr.sin_family = AF_INET;
destsocketaddr.sin_port = htons(DHCP_SERVER_PORT);
destsocketaddr.sin_addr.s_addr = INADDR_BROADCAST;
// broadcast 5 meaningless bytes to any listening DHCP server
iResult = sendto(sock, "12345", 5, 0, (const struct sockaddr*)&destsocketaddr, sizeof(destsocketaddr));
if (iResult == SOCKET_ERROR){
printf("Error sending Packet : %d\n",WSAGetLastError());
}
closesocket(sock);
return 0;
}
|
|
|
|
|
locquet wrote: If I execute ipconfig /release, and then the following code, I get the "Error sending Packet : 10065" displayed.
Ohhh - yeah that won't work. If you release the ip for the adapter then, well you'll get the
error you are seeing.
I'm going to take a look at that code again - I'll be back.
Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
It was me that was confused If I had read your original post carefully I would have noticed
the "after ipconfig /release" part. Sorry.
What did you try with raw sockets? Can you post sample code?
Also, are you required to do the DHCP communication yourself? If not, then maybe the IPHelper
APIs can help. Check out IPReleaseAddress()/IPRenewAddress().
Mark
"Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot?
Of course you don't, no one does. It never happens. It's a dumb question... skip it."
|
|
|
|
|
Unfortunately, I have to write the DHCP communication myself. I have to set configurable DHCP Options, and to choose how to treat DHCP ACK and OFFER Messages. According to my understanding of the IPHelper api, it is not possible.
Here is an extract of my code using a raw socket, but I do not think that the solution consists in using raw sockets.
Tell me if you want me to email you the whole project.
char host[100],buf[1000],*data=NULL,source_ip[20];
SOCKET s;<br />
int k=1;<br />
<br />
struct pseudo_entete pseudo_udp;<br />
<br />
IPV4_HDR *v4hdr=NULL;<br />
UDP_HDR *udphdr=NULL;<br />
<br />
DhcpMessage * pdmDiscover = new DhcpMessage();
<br />
int payload=DHCP_MESSAGE_SIZE , optval;<br />
SOCKADDR_IN dest;<br />
hostent *server;<br />
<br />
WSADATA wsock;<br />
printf("\nInitialising Winsock...");<br />
if (WSAStartup(MAKEWORD(2,2),&wsock) != 0)<br />
{<br />
fprintf(stderr,"WSAStartup() failed");<br />
exit(EXIT_FAILURE); <br />
} <br />
printf("Initialised successfully.");<br />
<br />
printf("\nCreating Raw Socket...");<br />
if((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))==SOCKET_ERROR)<br />
{<br />
printf("Creation of raw socket failed.");<br />
return 0;<br />
}<br />
printf("Raw TCP Socket Created successfully.");<br />
<br />
<br />
BOOL OptValue = 1 ;<br />
if (setsockopt( s, SOL_SOCKET, SO_BROADCAST, (char *)&OptValue, sizeof(BOOL))==SOCKET_ERROR) {<br />
printf("failed to set socket in broadcast mode.");<br />
return 0;<br />
}<br />
printf("Set socket in broadcast mode successful.");<br />
<br />
host[0] = 0;<br />
strcpy(host, "255.255.255.255");<br />
printf("\nResolving Hostname...");<br />
if((server=gethostbyname(host))==0)<br />
{<br />
printf("Unable to resolve.");<br />
return 0;<br />
}<br />
dest.sin_family = AF_INET;<br />
dest.sin_port = htons(67);
memcpy(&dest.sin_addr.s_addr,server->h_addr,server->h_length);<br />
printf("Resolved.");<br />
<br />
source_ip[0] = 0;<br />
strcpy(source_ip, "0.0.0.0");<br />
<br />
v4hdr = (IPV4_HDR *)buf;
v4hdr->ip_version=4;<br />
v4hdr->ip_header_len=5;<br />
v4hdr->ip_tos = 0;<br />
v4hdr->ip_total_length = htons ( sizeof(IPV4_HDR) + sizeof(UDP_HDR) + payload );<br />
v4hdr->ip_id = htons(2);<br />
v4hdr->ip_frag_offset = 0;<br />
v4hdr->ip_frag_offset1 = 0;<br />
v4hdr->ip_reserved_zero = 0;<br />
v4hdr->ip_dont_fragment = 0;
v4hdr->ip_more_fragment = 0;<br />
v4hdr->ip_ttl = 8;<br />
v4hdr->ip_protocol = IPPROTO_UDP;<br />
v4hdr->ip_srcaddr = inet_addr(source_ip);<br />
v4hdr->ip_destaddr = inet_addr(inet_ntoa(dest.sin_addr));<br />
v4hdr->ip_checksum = 0;<br />
<br />
udphdr = (UDP_HDR *)&buf[sizeof(IPV4_HDR)];<br />
<br />
udphdr->sport = htons(68);<br />
udphdr->dport = htons(67);<br />
udphdr->Length = htons(8 + payload);<br />
udphdr->Checksum = 0;<br />
<br />
data = &buf[sizeof(IPV4_HDR) + sizeof(UDP_HDR)];<br />
memcpy(data, pdmDiscover->m_rgwbMessage [0].buf, payload);<br />
<br />
v4hdr->ip_checksum = checksum((unsigned short *)&v4hdr, sizeof(ip_hdr));<br />
<br />
<br />
pseudo_udp.ip_source=inet_addr(source_ip);<br />
pseudo_udp.ip_destination=inet_addr(inet_ntoa(dest.sin_addr));<br />
pseudo_udp.mbz=0;<br />
pseudo_udp.type=IPPROTO_UDP;<br />
pseudo_udp.length=htons((unsigned short)(sizeof(udp_hdr)+(unsigned short)payload));<br />
<br />
char tampon[65535];<br />
<br />
memcpy(tampon,&pseudo_udp,sizeof(pseudo_udp));<br />
memcpy(tampon+sizeof(pseudo_udp),&udphdr,sizeof(udphdr));<br />
memcpy(tampon+sizeof(pseudo_udp)+sizeof(udphdr),data,payload);<br />
int checksum1=checksum((unsigned short*)tampon,sizeof(pseudo_udp)+sizeof(udphdr)+payload);<br />
udphdr->Checksum = 0x3b92;<br />
<br />
printf("\nSending packet...\n");<br />
<br />
sockaddr_in saddr ;<br />
saddr.sin_family = AF_INET ;<br />
saddr.sin_port = htons(68) ; <br />
saddr.sin_addr.S_un.S_addr = INADDR_ANY ;<br />
<br />
int iRet = bind(s, (LPSOCKADDR) & saddr, sizeof saddr) ;<br />
if (iRet == SOCKET_ERROR ){<br />
printf("Unable to bind.");<br />
return 0;<br />
}<br />
<br />
<br />
iRet = sendto(s , buf , sizeof(IPV4_HDR)+sizeof(UDP_HDR) + payload, 0,<br />
(SOCKADDR *)&dest, sizeof(dest)))==SOCKET_ERROR);<br />
if (iRet == SOCKET_ERROR ){<br />
<br />
printf("Error sending Packet : %d\n",WSAGetLastError());<br />
break;<br />
}<br />
<br />
|
|
|
|
|
Hi, we finally managed to solve this problem using RawEther.
Thanks a lot for your help.
|
|
|
|
|
|
what do you call "physical address" ? the base address of the file on the Hard disk ? or the UNC name ?
|
|
|
|
|
i need to send the file to NIC card thro DMA
for sending a data thro NIC it converts virtual address to physical address
so its better to dircetly supply ther physical address
help in this
-- modified at 5:34 Thursday 15th February, 2007
|
|
|
|
|
Give your account a name, then maybe you will get an answer.
|
|
|
|
|
Why you delete your post!?
|
|
|
|
|
sorry !!! no one responded to mee!!
i cant express my thing to U
wht shall i do?
|
|
|
|
|
arun kumar spk wrote: wht shall i do?
1) speak well (not in SMS)
2) be patient. waiting for 1/2 of an hour is not enough for a forum. you'r not on a chat room ; think of it.
3) be less selfish and more intelligent. how can one answer you if you delete the qsuestion ?
4) consider rethinking your question if you still got no answers after a long time. It may not be well formulated
|
|
|
|
|
ive re posted the question!!!
mailny i ve never give the name in my main message so i have given this again
SORRY
|
|
|
|
|
Are you sure all members see your post,No you must wating
|
|
|
|
|
sorry the last time i forgot to give my name!!!
soo its hard for u to recognizetht i only posted
so i deleted it
and i have reposted it again
soo pls respond to it!!!
thnks
|
|
|
|
|
|
thank you
but im doing it for windows sooo i think this malloc wont work in windows
|
|
|
|
|