title: Socket - Permission denied in C
body: <p>I'm trying to get an MX record from an DNS server in C.
  My problem is, that everytime I call sendto or recvfrom I get a Permission denied error.
  (IDE - Xcode 4, Mac OS X Lion)
  Haven't really done anything in C up to this point but I need this for an assignment.
  "Inspiration" from <a href="http://www.binarytides.com/blog/dns-query-code-in-c-with-linux-sockets/" rel="nofollow">http://www.binarytides.com/blog/dns-query-code-in-c-with-linux-sockets/</a>
  My code so far:</p>
  
  <pre><code>struct QUESTION
  {
  unsigned short qtype;
  unsigned short qclass;
  };
  
  typedef struct
  {
  unsigned char *name;
  struct QUESTION *ques;
  } QUERY;
  
  typedef struct
  {
  unsigned short type;
  unsigned short _class;
  unsigned int ttl;
  unsigned short data_len;
  } R_DATA;
  
  
  
  int main(int argc, char *argv[]) 
  { 
  
  
  
  int sockfd, i;
  unsigned char buf[65536],*qname;
  struct QUESTION *qinfo = NULL;
  
  struct sockaddr_in dest;
  
  struct dnsheader *dns = NULL;
  
  sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  
  
  dest.sin_family=AF_INET;
  dest.sin_port=htons(53);
  dest.sin_addr.s_addr= inet_addr("ns1.sil.at");
  
  
  srand((unsigned)time(NULL)); 
  int16_t rand_number = (rand()%1000+1);
  printf("generated random number : %i \n", rand_number);
  
  
  dns = (struct dnsheader *)&amp;buf;
  dns-&gt;flags = STANDARD_QUERY_FLAGS;
  dns-&gt;question_count = 1;
  dns-&gt;transaction_id = rand_number;
  dns-&gt;answer_rr_count = 0;
  dns-&gt;additional_rr_count = 0;
  dns-&gt;nameserver_rr_count = 0;
  
  
  qname =(unsigned char*)&amp;buf[sizeof(struct dnsheader)];
  
  
  qinfo =(struct QUESTION*)&amp;buf[sizeof(struct dnsheader) + (strlen((const char*)qname) + 1)]; //fill it
  
  qinfo-&gt;qtype = htons( 15 ); // MX = 15
  qinfo-&gt;qclass = htons(1); 
  
  printf("\nSending Packet...");
  if( sendto(sockfd,(char*)buf,sizeof(struct dnsheader) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION),0,(struct sockaddr*)&amp;dest,sizeof(dest)) &lt; 0){
      perror("\nsendto failed");
  }
  printf("\nDone\n");
  
  
  i = sizeof dest;
  printf("\nReceiving answer...");
  if(recvfrom (sockfd,(char*)buf , 65536 , 0 , (struct sockaddr*)&amp;dest , (socklen_t*)&amp;i ) &lt; 0){
      perror("recvfrom failed");
  }
  printf("Done");
  
  dns = (struct dnsheader*) buf;
  
  print_dns_answers(buf, sizeof((char*)buf));
  
  
  
  
  close(sockfd); 
  
  
  return 0; 
  } 
  </code></pre>