title: Checking return address in recvfrom() (C UDP Sockets)
body: <p>This pertains to attempting to validate the return address in recvfrom() (the fifth argument to the function) in this UDP echo client:</p>
  
  <p>While I can send data to the server and receive return communications correctly, I'm having trouble validating the return IP address when comparing the fromAddr.sin_addr and echoServAddr.sin_addr. </p>
  
  <p>The goal here is to compare the address in the structure that was used in sendto() and the address in the structure returned from recvfrom() to validate the echo reply from the server indeed came from where the initial transmission from the client was sent (A rudimentary POC that there wasn't a Man In The Middle).</p>
  
  <p><em>What should I be looking at in order to appropriately validate the return address as it is returned from recvfrom() matches the address transmitted to as referenced in the sendto() call?</em></p>
  
  <pre><code>#include &lt;stdio.h&gt;      /* for printf() and fprintf() */
  #include &lt;sys/socket.h&gt; /* for socket(), connect(), sendto(), and recvfrom() */
  #include &lt;arpa/inet.h&gt;  /* for sockaddr_in and inet_addr() */
  #include &lt;stdlib.h&gt;     /* for atoi() and exit() */
  #include &lt;string.h&gt;     /* for memset() */
  #include &lt;unistd.h&gt;     /* for close() */
  
  #define ECHOMAX 255     /* Longest string to echo */
  
  void DieWithError(char *errorMessage);  /* External error handling function */
  
  int main(int argc, char *argv[])
  {
      int sock;                        /* Socket descriptor */
      struct sockaddr_in echoServAddr; /* Echo server address */
      struct sockaddr_in fromAddr;     /* Source address of echo */
      unsigned short echoServPort;     /* Echo server port */
      unsigned int fromSize;           /* In-out of address size for recvfrom() */
      char *servIP;                    /* IP address of server */
      char *echoString;                /* String to send to echo server */
      char echoBuffer[ECHOMAX+1];      /* Buffer for receiving echoed string */
      int echoStringLen;               /* Length of string to echo */
      int respStringLen;               /* Length of received response */
  
      // manage the command line arguments and errors
      if ((argc &lt; 3) || (argc &gt; 4)) {
          fprintf(stderr, "Usage: %s &lt;Server IP&gt; &lt;Echo Word&gt; [&lt;Echo Port&gt;]\n",
              argv[0]);
          exit(1);
          }
      // load servIP
      servIP = argv[1];
      // load echoString
      echoString = argv[2];
      // check echoString and error if too long
      echoStringLen = strlen(echoString);
      if (!(echoStringLen &lt;= 255)) {
          DieWithError("BUFFER EXCEEDED ERROR");
      }
      // load port
      if (argc == 4)
          echoServPort = atoi(argv[3]);
      else
          echoServPort = 7;
      // create the socket
      if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) &lt; 0) {
          DieWithError("SOCKET CREATION ERROR");
      }
          /* Construct the server address structure */
          memset(&amp;echoServAddr, 0, sizeof(echoServAddr));    /* Zero out structure */
          echoServAddr.sin_family = AF_INET;                 /* Internet addr family */
          echoServAddr.sin_addr.s_addr = inet_addr(servIP);  /* Server IP address */
          echoServAddr.sin_port   = htons(echoServPort);     /* Server port */
  
      // send the string
      inet_pton(AF_INET, servIP, &amp;echoServAddr.sin_addr);
      if ((sendto(sock, echoString, strlen(echoString), 0, (struct sockaddr *)
          &amp;echoServAddr, sizeof(echoServAddr))) &lt; 0) {
          DieWithError("SEND ERROR");
      }
      // recieve a response
      respStringLen = recvfrom(sock, echoBuffer, sizeof(echoBuffer), 0,
          (struct sockaddr *) &amp;fromAddr, &amp;fromSize);
      if (respStringLen &lt; 0) {
          DieWithError("RECV ERROR");
      }
      // print the message sent
      printf("SENT FROM CLIENT: '%s'\n", echoString);
      // print the message recieved
      echoBuffer[respStringLen] = '\0';
      printf("RECEIVED FROM SERVER: '%s'\n", echoBuffer);
      // check if from the correct server
      if ((struct sockaddr *) &amp;echoServAddr.sin_addr != (struct sockaddr *) &amp;fromAddr.sin_addr) {
          DieWithError("INVALID RETURN ADDRESS");
      }
      // close the socket
      close(sock);
      // exit the program
      exit(0);
  
  }
  </code></pre>