Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:15:27+00:00 2026-06-14T12:15:27+00:00

I am creating proxy dns and I recieved query from dig, send it to

  • 0

I am creating proxy dns and I recieved query from dig, send it to server but can´t get answer from dns server. I know my code doesn´t look good but I am working on this for hours and still can´t get what is wrong. My program just stuck before second recvfrom. Can someone help me? (btw my OS for this is FreeBSD and I am testing this with 8.8.8.8 dns server)

This is my code:

int main(int argc, char *argv[] )
{

  char buf[BUFSIZE];
  char serverbuf[BUFSIZE];
  int UDPSocket, clientSocket;
  int lenght;
  int addr_len;
  struct addrinfo *clientInfo;
  struct addrinfo hintsClient;
  struct sockaddr_in serverSocket, clientAddr, serverAddr;
  int serverAddrLen = sizeof(serverAddr);
  fd_set set, tcpset;
  struct timeval tv; // struktura pro timeout
  bool end = false;
  int m;

  TprogParam param = processParams(argc, argv);
  if(param.error)
  {
    printError(EPARAMS);
    return ERROR;
  }
  if(param.help)
  {
    printHelp();
    return EXIT_SUCCESS;
  }

  memset(&hintsClient, 0, sizeof(struct addrinfo));
  hintsClient.ai_family = AF_INET;
  hintsClient.ai_socktype = SOCK_DGRAM;
  hintsClient.ai_flags = 0;
  hintsClient.ai_protocol = IPPROTO_UDP;

  int status;


  if(param.ipadress[0] == '\0')
  {
    if((status = getaddrinfo(INADDR_ANY, NULL, &hintsClient, &clientInfo)) != 0)
    {
      printf("Spatna adresa pro naslouchani");
      printError(1);
      return ERROR;
    }
  }
  else
  {
    if((status = getaddrinfo(param.ipadress, "13066", &hintsClient, &clientInfo)) != 0)
    {
      printf("Spatna adresa pro naslouchani");
      printError(1);
      return ERROR;
    }
  }


  memset(&serverAddr, 0, sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr(param.dnsserver);;
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serverAddr.sin_port = 53;

  // vytvorime socket
  if((UDPSocket = socket(clientInfo->ai_family, clientInfo->ai_socktype, clientInfo->ai_protocol)) == -1)
  {
    printf("Nelze vytvorit udp socket");
    printError(1);
    return ERROR;
  }

  if((clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  {
    printf("Nelze vytvorit tcp socket");
    printError(1);
    return ERROR;
  }


  // Nastavime socket do neblokovaciho rezimu


  if(bind(UDPSocket, clientInfo->ai_addr, clientInfo->ai_addrlen) == -1)
  {
    if (errno != EINPROGRESS)
    {
      printf("Nelze navazat spojeni pro naslouchani: %d", errno);
      return ERROR;
    }
  }
  printf("Poslal jsem zadost o spojeni. Zatimco se spojeni navazuje mohu delat jine veci.\n");

  FD_ZERO(&set);
  FD_SET(UDPSocket, &set);
  tv.tv_sec = 15;
  tv.tv_usec = 0;

  addr_len = sizeof(struct sockaddr);
  while(1)
  {
      printf("Prijimam pozadavek k vyrizeni.\n");
      if ((lenght = recvfrom(UDPSocket, buf, BUFSIZE - 1, 0, (struct sockaddr *)&clientAddr, &addr_len)) == -1)//recv(UDPSocket, buf, BUFSIZE - 1, 0)) == -1)
      {
        printf("Nejaka chyba pri prijimani dat");
        return ERROR;
      }
      buf[lenght] = '\0';
      printf("Prijata nasledujici data od klienta na predklad dns: %s o delce %d \n", buf, lenght);

      if((m = sendto(clientSocket, buf, lenght, 0, (struct sockaddr *)&serverAddr, serverAddrLen)) == -1)
      {
        printf("Problem s odeslanim dat: %d %s", errno, strerror(errno));
        return ERROR;
      }

      if((m = recvfrom(clientSocket, buf, BUFSIZE - 1, 0, (struct sockaddr *)&serverAddr,&serverAddrLen)) == -1)
      {
        printf("nejaka chyba");
        return ERROR;
      }

    if((lenght = sendto(UDPSocket, buf, lenght, 0,(struct sockaddr *)&clientAddr, lenght)) == -1)
    {
      printf("Problem s odeslanim dat: errno %d", errno);
      return ERROR;
    }
  }
}

Edit: New code after some fixes:

#include "dns_stat.h"

TprogParam processParams(int argc, char *argv[]) {

TprogParam parInfo =
{
.error = false,
.help = false,
.ipadress[0] = '\0',
.port = 0,
.dnsserver[0] = '\0',
.type = false,
.source = false,
.destination = false,
};

if(argc == 1)
{
  parInfo.help = true;
}
else
{
  int c;
  const char *short_options = ":l:p:s:";

  const struct option long_options[] = {
    { "type", 0, NULL, 't' },
    { "source", 0, NULL, 'z' },
    { "destination", 0, NULL, 'd' },
    { NULL, 0, NULL, 0 }
  };
  while((c = getopt_long_only(argc, argv, short_options, long_options, NULL)) != -1) {
    switch (c) {
      case 'l':
        strcpy(parInfo.ipadress,optarg);
        break;
      case 'p':
        parInfo.port = atoi(optarg);
        break;
      case 's':
        strcpy(parInfo.dnsserver,optarg);//optarg);
        //parInfo.dnsserver = strdup(optarg);
        break;
      case 't':
        parInfo.type = true;
        break;
      case 'z':
        parInfo.source = true;
        break;
      case 'd':
        parInfo.destination = true;
        break;
      case '?':
        parInfo.error = true;
        break;
      default:
        printf("default");
        parInfo.error = true;
        abort();
    }
  }
}

return parInfo;
}

void printHelp()
{
  printf("Napoveda\n");
}
void printError(int ecode)
{
  if(ecode<EOK||ecode>EUNKNOWN){
    ecode = EUNKNOWN;
  }
  fprintf(stderr, "%s", ECODEMSG[ecode]);
}
int main(int argc, char *argv[] )
{

  char buf[BUFSIZE];
  char serverbuf[BUFSIZE];
  int UDPSocket, clientSocket;
  int lenght;
  int addr_len;
  struct addrinfo *clientInfo;
  struct addrinfo hintsClient;
  struct sockaddr_in serverSocket, clientAddr, serverAddr;
  int serverAddrLen = sizeof(serverAddr);
  fd_set set, tcpset;
  struct timeval tv; // struktura pro timeout
  bool end = false;
  bool end2 = false;
  int m;

  TprogParam param = processParams(argc, argv);
  if(param.error)
  {
    printError(EPARAMS);
    return ERROR;
  }
  if(param.help)
  {
    printHelp();
    return EXIT_SUCCESS;
  }

  //printf("type: %d, source: %d, destination: %d\n", param.type, param.source, param.destination);
  memset(&hintsClient, 0, sizeof(struct addrinfo));
  hintsClient.ai_family = AF_INET;
  hintsClient.ai_socktype = SOCK_DGRAM;
  hintsClient.ai_flags = 0;
  hintsClient.ai_protocol = IPPROTO_UDP;

  int status;


  if(param.ipadress[0] == '\0')
  {
    if((status = getaddrinfo(INADDR_ANY, NULL, &hintsClient, &clientInfo)) != 0)
    {
      printf("Spatna adresa pro naslouchani");
      printError(1);
      return ERROR;
    }
  }
  else
  {
    if((status = getaddrinfo(param.ipadress, "13066", &hintsClient, &clientInfo)) != 0)
    {
      printf("Spatna adresa pro naslouchani");
      printError(1);
      return ERROR;
    }
  }


  memset(&serverAddr, 0, sizeof(serverAddr));
;
  serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr(param.dnsserver);;
    serverAddr.sin_port = htons(53);

  // vytvorime socket
  if((UDPSocket = socket(clientInfo->ai_family, clientInfo->ai_socktype, clientInfo->ai_protocol)) == -1)
  {
    printf("Nelze vytvorit udp socket");
    printError(1);
    return ERROR;
  }

  if((clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  {
    printf("Nelze vytvorit tcp socket");
    printError(1);
    return ERROR;
  }


  // Nastavime socket do neblokovaciho rezimu


  if(bind(UDPSocket, clientInfo->ai_addr, clientInfo->ai_addrlen) == -1)
  {
    if (errno != EINPROGRESS)
    {
      printf("Nelze navazat spojeni pro naslouchani: %d", errno);
      return ERROR;
    }
  }
  printf("Poslal jsem zadost o spojeni. Zatimco se spojeni navazuje mohu delat jine veci.\n");

  FD_ZERO(&set);
  FD_SET(UDPSocket, &set);
  tv.tv_sec = 15;
  tv.tv_usec = 0;

  addr_len = sizeof(struct sockaddr);
  while(1)
  {
      printf("Prijimam pozadavek k vyrizeni.\n");
      if ((lenght = recvfrom(UDPSocket, buf, BUFSIZE - 1, 0, (struct sockaddr *)&clientAddr, &addr_len)) == -1)//recv(UDPSocket, buf, BUFSIZE - 1, 0)) == -1)
      {
        printf("Nejaka chyba pri prijimani dat");
        return ERROR;
      }
      buf[lenght] = '\0';
      printf("Prijata nasledujici data od klienta na predklad dns: %s o delce %d \n", buf, lenght);

      if((m = sendto(clientSocket, buf, lenght, 0, (struct sockaddr *)&serverAddr, serverAddrLen)) == -1)
      {
        printf("Problem s odeslanim dat: %d %s", errno, strerror(errno));
        return ERROR;
      }
      printf("tady");
      fflush(stdout);
      if((m = recvfrom(clientSocket, buf, BUFSIZE - 1, 0, (struct sockaddr *)&serverAddr,&serverAddrLen)) == -1)
      {
        printf("nejaka chyba");
        return ERROR;
      }

      printf("Prijata nasledujici data od dns serveru: %s o delce %d \n", buf, lenght);

    if((lenght = sendto(UDPSocket, buf, lenght, 0,(struct sockaddr *)&clientAddr, addr_len)) == -1)
    {
      printf("Problem s odeslanim dat: errno %d %s", errno, strerror(errno));
      return ERROR;
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:15:29+00:00Added an answer on June 14, 2026 at 12:15 pm

    At least two issues here:

    serverAddr.sin_addr.s_addr = inet_addr(param.dnsserver);; /* line 1 */
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* line 2 */
    serverAddr.sin_port = 53; /* line 3 */
    
    • Line 2 overwrites server address you just translated on line 1. Just remove line 2.
    • Line 3 needs to change to htons(53) to convert port number to network byte order.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating simple proxy server but I faced a strange situation, I've following code
I am creating a servlet(proxy server) which handles multiple request from the client.I do
I'm creating a data store that will load the data from server. I'm wondering
I'm creating spring.net proxy in code by using ProxyFactory object with ProxyTargetType to true
I am creating a simple Proxy server for my website. Why I am not
currently I'm creating proxy classes from interfaces with spring 3 xml config like this:
I am creating a proxy with Netty framework but I am noticing that the
I'm creating proxies with javassist ProxyFactory . When creating a single proxy all works
Creating a server-side socket will fail if I'm trying to use the same port
Creating a simple RPG game, first time using XNA. Trying to get my character

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.