title: Implementing DHCP client
body: <p>On unix using C, my client is listening on port 68 with superuser mode. After sending DHCP discover message, when I try to receive, it blocks in recvfrom means there is no message received  or is it like system has a process (DHCP client) listening on same port 68 which receives the message and thats my process are not able to receive the message. What is the problem?</p>
  
  <p>I have set the socket option SO_REUSEADDR and SO_BROADCAST. I am sending to port 67.</p>
  
  <pre><code>struct dhcpmessage
  {
      uint8_t op;
      uint8_t htype;
      uint8_t hlen;
      uint8_t hops;
      uint32_t xid;
      uint16_t secs;
      uint16_t flags;
      uint32_t ciaddr;
      uint32_t yiaddr;
      uint32_t siaddr;
      uint32_t giaddr;
      char chaddr[16];
      char sname[64];
      char file[128];
      char magic[4];
      char opt[3];
  } __attribute__((__packed__));
  
  #include&lt;stdio.h&gt;
  #include&lt;ctype.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;signal.h&gt;
  #include&lt;sys/wait.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;netinet/in.h&gt;
  #include&lt;arpa/inet.h&gt;
  #include&lt;errno.h&gt;
  #include&lt;sys/file.h&gt;
  #include&lt;sys/msg.h&gt;
  #include&lt;sys/ipc.h&gt;
  #include&lt;time.h&gt;
  #include"defs.h"
  int main() {
  int sockfd,listenfd,connfd;
  const int on=1;
  struct sockaddr_in servaddr,cliaddr,rservaddr;
  if((sockfd=socket(AF_INET,SOCK_DGRAM,0)) &lt; 0)
      die("socket");
  if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&amp;on,sizeof(on)) &lt; 0)
      die("setsockopt");  
  
  if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&amp;on,sizeof(on)) &lt; 0)
      die("setsockopt");
  bzero(&amp;servaddr,sizeof(servaddr));
  bzero(&amp;cliaddr,sizeof(cliaddr));
  cliaddr.sin_port = htons(68);
  cliaddr.sin_family = AF_INET;
  cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if(bind(sockfd,(struct sockaddr*)&amp;cliaddr,sizeof(cliaddr)) &lt; 0)
      die("bind");
  
  servaddr.sin_port = htons(67);
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = inet_addr("255.255.255.255");
  struct dhcpmessage dhcpmsg;
  bzero(&amp;dhcpmsg,sizeof(dhcpmsg));
  dhcpmsg.op = 1;
  dhcpmsg.htype = 1;
  dhcpmsg.hlen = 6;
  dhcpmsg.hops = 0;
  dhcpmsg.xid = htonl(1000);
  dhcpmsg.secs = htons(0);
  dhcpmsg.flags = htons(0x8000);
  dhcpmsg.chaddr[0] = 0x00;
  dhcpmsg.chaddr[1] = 0x1A;
  dhcpmsg.chaddr[2] = 0x80;
  dhcpmsg.chaddr[3] = 0x80;
  dhcpmsg.chaddr[4] = 0x2C;
  dhcpmsg.chaddr[5] = 0xC3;
  dhcpmsg.magic[0]=99;
  dhcpmsg.magic[1]=130;
  dhcpmsg.magic[2]=83;
  dhcpmsg.magic[3]=99;
  dhcpmsg.opt[0]=53;
  dhcpmsg.opt[1]=1;
  dhcpmsg.opt[2]=1;
  if(sendto(sockfd,&amp;dhcpmsg,sizeof(dhcpmsg),0,(struct sockaddr*)&amp;servaddr,sizeof(servaddr)) &lt; 0)
      die("sendto");
  struct dhcpmessage recvdhcpmsg;
  socklen_t rservlen = sizeof(rservaddr);
  if(recvfrom(sockfd,&amp;recvdhcpmsg,sizeof(recvdhcpmsg),0,(struct sockaddr*)&amp;rservaddr,&amp;rservlen) &lt; 0)
      die("recvfrom");
  char *str = (char*)&amp;recvdhcpmsg;
  int i;
  for(i=0;i&lt;sizeof(recvdhcpmsg);i++)
      printf("%d_",str[i]);
  printf("\n");
  return 0;
  }
  </code></pre>