rem: if (myaddr.sin_port || myaddr.sin_addr.s_addr) { ret = bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr)); if (ret < 0) goto err; }
add: ret = bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr)); if (ret < 0) goto err;
context: static int core_udp_listen(nc_sock_t *ncsock){  int ret, sock, sockopt = 1, timeout = ncsock->timeout;  bool use_ancillary = FALSE;  struct sockaddr_in myaddr;  struct timeval tt;		/* needed by the select() call */  debug_v("core_udp_listen(ncsock=%p)", (void *)ncsock);  sock = netcat_socket_new(PF_INET, SOCK_DGRAM);  if (sock < 0)    return -1;  /* prepare myaddr for the bind() call */  myaddr.sin_family = AF_INET;  myaddr.sin_port = htons(ncsock->local_port.num);  memcpy(&myaddr.sin_addr, &ncsock->local_host.iaddrs[0], sizeof(myaddr.sin_addr));  /* only call bind if it really needed -- most of the cases in this function */  if (myaddr.sin_port || myaddr.sin_addr.s_addr) {    ret = bind(sock, (struct sockaddr *)&myaddr, sizeof(myaddr));    if (ret < 0)      goto err;  }#ifdef USE_PKTINFO  /* set the right flag in order to obtain the ancillary data */  ret = setsockopt(sock, SOL_IP, IP_PKTINFO, &sockopt, sizeof(sockopt));  if (ret >= 0)    use_ancillary = TRUE;#else# warning "Couldn't setup ancillary data helpers"#endif  /* since this protocol is connectionless, we need a special handling here.     We want to simulate a two-ends connection but in order to do this we need     a remote address and a local address (in case we bound to INADDR_ANY).     Wait here until a packet is received, and use its source and destination     addresses as default endpoints.  If we have the zero-I/O option set, we     just eat the packet and will never return (if a timeout is not set). */  tt.tv_sec = timeout;  tt.tv_usec = 0;  while (TRUE) {    fd_set ins;    FD_ZERO(&ins);    FD_SET(sock, &ins);    select(sock + 1, &ins, NULL, NULL, (timeout > 0 ? &tt : NULL));    if (FD_ISSET(sock, &ins)) {      int recv_ret, write_ret;      struct msghdr my_hdr;      bool local_fetch = FALSE;      unsigned char buf[1024];      struct iovec my_hdr_vec;      struct sockaddr_in rem_addr;      struct sockaddr_in local_addr;#ifdef USE_PKTINFO      unsigned char anc_buf[512];#endif      /* I've looked for this code for a lot of hours, and finally found the         RFC 2292 which provides a socket API for fetching the destination         interface of the incoming packet. */      memset(&my_hdr, 0, sizeof(my_hdr));      memset(&rem_addr, 0, sizeof(rem_addr));      memset(&local_addr, 0, sizeof(local_addr));      my_hdr.msg_name = &rem_addr;      my_hdr.msg_namelen = sizeof(rem_addr);      /* initialize the vector struct and then the vectory member of the header */      my_hdr_vec.iov_base = buf;      my_hdr_vec.iov_len = sizeof(buf);      my_hdr.msg_iov = &my_hdr_vec;      my_hdr.msg_iovlen = 1;#ifdef USE_PKTINFO      /* now the most important part: the ancillary data, used to recovering the dst */      my_hdr.msg_control = anc_buf;      my_hdr.msg_controllen = sizeof(anc_buf);#endif      /* now check the remote address.  If we are simulating a routing then         use the MSG_PEEK flag, which leaves the received packet untouched */      recv_ret = recvmsg(sock, &my_hdr, (opt_zero ? 0 : MSG_PEEK));      debug_v("received packet from %s:%d%s", netcat_inet_ntop(&rem_addr.sin_addr),		ntohs(rem_addr.sin_port), (opt_zero ? "" : ", using as default dest"));#ifdef USE_PKTINFO      /* let's hope that there is some ancillary data! */      if (my_hdr.msg_controllen > 0) {	struct cmsghdr *get_cmsg;	/* We don't know which is the order of the ancillary messages and we	   dont know how many are there.  So I simply parse all of them until	   we find the right one, checking the index type. */	for (get_cmsg = CMSG_FIRSTHDR(&my_hdr); get_cmsg;		get_cmsg = CMSG_NXTHDR(&my_hdr, get_cmsg)) {	  debug_v("Analizing ancillary header (id=%d)", get_cmsg->cmsg_type);	  if (get_cmsg->cmsg_type == IP_PKTINFO) {	    struct in_pktinfo *get_pktinfo;	    /* can we get this field double? RFC isn't clear on this */	    assert(local_fetch == FALSE);	    get_pktinfo = (struct in_pktinfo *) CMSG_DATA(get_cmsg);	    memcpy(&local_addr.sin_addr, &get_pktinfo->ipi_spec_dst, sizeof(local_addr.sin_addr));	    local_addr.sin_port = myaddr.sin_port;	    local_addr.sin_family = myaddr.sin_family;	    local_fetch = TRUE;	  }	}      }#endif      if (local_fetch) {	char tmpbuf[127];	strncpy(tmpbuf, netcat_inet_ntop(&rem_addr.sin_addr), sizeof(tmpbuf));	ncprint(NCPRINT_VERB1, _("Received packet from %s:%d -> %s:%d (local)"),		tmpbuf, ntohs(rem_addr.sin_port), netcat_inet_ntop(&local_addr.sin_addr),		ntohs(local_addr.sin_port));      }      else	ncprint(NCPRINT_VERB1, _("Received packet from %s:%d"),		netcat_inet_ntop(&rem_addr.sin_addr), ntohs(rem_addr.sin_port));      if (opt_zero) {	/* FIXME: why don't allow -z with -L? but only for udp! (?) */	write_ret = write(STDOUT_FILENO, buf, recv_ret);	bytes_recv += write_ret;	debug_dv("write_u(stdout) = %d", write_ret);	if (write_ret < 0) {	  perror("write_u(stdout)");	  exit(EXIT_FAILURE);	}	/* FIXME: unhandled exception */	assert(write_ret == recv_ret);	/* if the hexdump option is set, hexdump the received data */	if (opt_hexdump) {#ifndef USE_OLD_HEXDUMP	  fprintf(output_fd, "Received %d bytes from %s:%d\n", recv_ret,		netcat_inet_ntop(&rem_addr.sin_addr), ntohs(rem_addr.sin_port));#endif	  netcat_fhexdump(output_fd, '<', buf, write_ret);	}      }      else {	nc_sock_t dup_socket;	/* don't duplicate the socket if we don't have ancillary data available */	if (!use_ancillary)	  return sock;	memset(&dup_socket, 0, sizeof(dup_socket));	dup_socket.domain = ncsock->domain;	dup_socket.proto = ncsock->proto;	memcpy(&dup_socket.local_host.iaddrs[0], &local_addr.sin_addr, sizeof(local_addr));	memcpy(&dup_socket.host.iaddrs[0], &rem_addr.sin_addr, sizeof(local_addr));	dup_socket.local_port.num = ntohs(local_addr.sin_port);	dup_socket.port.num = ntohs(rem_addr.sin_port);	/* copy the received data in the socket's queue */	ncsock->recvq.len = recv_ret;	ncsock->recvq.head = ncsock->recvq.pos = malloc(recv_ret);	memcpy(ncsock->recvq.head, my_hdr_vec.iov_base, recv_ret);	/* FIXME: this ONLY saves the first 1024 bytes! and the others? */	close(sock);	/* this is all we want from this function */	debug_dv("calling the udp_connect() function...");	return core_udp_connect(&dup_socket);      }    }    else			/* select() timed out! */      break;  }				/* end of packet receiving loop */  /* no packets until timeout, set errno and proceed to general error handling */  errno = ETIMEDOUT; err:  close(sock);  return -1;}				/* end of core_udp_listen() */