开发者

DHCP client doesnt receive response on port 68

I have used wireshark to see the DHCP packet structure. Now I have create开发者_如何学God a DHCPDISCOVER request and stored it in 'message'. I then broadcast it on the network.

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if (sockfd < 0) {
  perror("socket");
  exit(1);
}

setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST, &on,sizeof(on));
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR, &on,sizeof(on));

bzero(&cliaddr, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliaddr.sin_port = htons(68);

if (bind(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) < 0) {        
  perror("bind");
  exit(1);
}  

bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("255.255.255.255");
addr.sin_port = htons(67);

cnt = sendto(sockfd, message, sizeof(message), 0,(struct sockaddr *) &addr, sizeof(addr));    
if (cnt < 0) {
  perror("sendto");
  exit(1);
}

addrlen = sizeof(servaddr);

cnt = recvfrom(sockfd, reply, sizeof(reply), 0,(struct sockaddr *) &servaddr, &addrlen);
if (cnt < 0) {
  perror("recvfrom");
  exit(1);
} 

printf("\nReply Received\n");

I run this program and analyze the packets sent and received using wireshark. I see that a DHCPDISCOVER packet is sent on port 67 and a DHCPOffer packet is received on port 68 in the wireshark window. My client sends the packet fine but does not receive this packet and it blocks on recvfrom call. What is going wrong?


You need to put your receive out before you send the packet request, or else the response probably comes back before you are ready to receive it.

Also, is the response broadcast? If not, and you don't currently have an IP address assigned to your machine, then you're going to have some trouble receiving it because your host will filter received packets by IP address won't know that the response is destined for it (even though the link layer address matches), so it'll not deliver it.

But my guess is it's the first problem. You'll have to either use threads or do a non-blocking receive, or else your receive will block and thus you'll never getting around to sending the request.


Well i disagree with the above answer.. recvfrom should be able receive both the packets. Here problem lies because ,port no is within the range of 1000. typically , these packets are filtered by linux kernel(iptables) and not sent to. user application. https://bugzilla.redhat.com/show_bug.cgi?id=983672

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜