title: ICMP "destination unreachable" packet on udp connected socket
body: <p>I've created connected UDP socket with this function</p>
  
  <pre><code>/* Creates connected udp socket */
  int
  udp_connect( const char *host, const char *serv)
  {
      int             sockfd, n;
      struct addrinfo hints, *res, *ressave;
  
      bzero(&amp;hints, sizeof(struct addrinfo));
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_DGRAM;
  
      if ( ( n = getaddrinfo( host, serv, &amp;hints, &amp;res)) != 0)
          err_quit( "udp_connect error for %s, %s: %s",
                   host, serv, gai_strerror(n));
      ressave = res;
      do {
          sockfd = socket( res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol);
          if ( sockfd &lt; 0)
              continue;   /* ignore this one */
                  /* The call to connect with a UDP socket does not send anything
                   * to the peer. If something is wrong ( the peer is unreachable
                   * or there is no server at the specified port), the caller 
                   * does not discover that until it sends 
                   * a datagram to the peer */
          if ( connect( sockfd, res-&gt;ai_addr, res-&gt;ai_addrlen) == 0)
              break;      /* success */
          Close( sockfd); /* ignore this one */
      } while ( ( res = res-&gt;ai_next) != NULL);
  
      if ( res == NULL)   /* errno set from final connect() */
          err_sys( "udp_connect error for %s, %s", host, serv);
  
      freeaddrinfo( ressave);
      return( sockfd);
  }
  </code></pre>
  
  <p>I would like to do a test how it behaves when peer is actually unreachable. Since call to <code>connect()</code> cannot result in this information we need to actually send something. I will describe what do I do and what do I get in the following snippet:</p>
  
  <pre><code>printf( "sending to %s\n", Sock_ntop_host( sa, salen));
  // prints: sending to 127.0.0.1
  
  Sendto( sockfd, "", 1, 0, sa, salen);   /* send 1-byte datagram */
  // prints: nbytes:1
  // it is sent, I check via tcpdump or with Wireshark that datagram
  // has been sent and ICMP "destination unreachable" comes back from host
  
  printf( "sent, errno:%d,%s\n", errno, strerror(errno));
  // prints: sent, errno:0,Success
  
  n = Recvfrom( sockfd, recvline, MAXLINE, 0, NULL, NULL);
  // never gets here
  printf( "received n=%d\n", n);
  </code></pre>
  
  <p>The <code>Sendto</code> function is a wrapper over <code>sendto</code> that just prints error and exits:</p>
  
  <pre><code>void
  Sendto(int fd, const void *ptr, size_t nbytes, int flags,
         const struct sockaddr *sa, socklen_t salen)
  {
      if ( sendto(fd, ptr, nbytes, flags, sa, salen) == -1)
          exit(-1);//err_sys("sendto error");
      printf( "nbytes:%d\n",nbytes); // prints: nbytes:1
  }
  
  ssize_t
  Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
           struct sockaddr *sa, socklen_t *salenptr)
  {
      ssize_t     n;
  
      if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) &lt; 0)
          err_sys("recvfrom error");
      return(n);
  }
  </code></pre>
  
  <p>So the call to <code>Recvfrom</code> blocks forever while <code>Sendto</code> returnes errno with code <code>Success</code>. <strong>How then should I code this to get notification about ICMP response</strong>? is this possible without timeout on socket?</p>