0
votes

I'm trying to send some random datagram to host www.google.fr to both ipv4 and ipv6 interfaces, this work fine for ipv4 but not for ipv6. I wanted to do it with a single not-connected socket via UDP protocol.

I did something using getaddrinfo, and sendto function but when I call sendto on ipv6 interfaces, it fails and print "network is unreachable".

int main() {
  int s = socket(AF_INET6, SOCK_DGRAM, 0);
  char test[4];

  struct addrinfo hints = {0};
  hints.ai_family   = AF_INET6;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_protocol = 0;
  hints.ai_flags    = AI_V4MAPPED|AI_ALL;
  struct addrinfo* res = {0};
  struct addrinfo* list;

  int exit_status = getaddrinfo("www.google.fr","8080", &hints, &res);

  if (exit_status != 0){
    perror("getaddrinfo:");
    return EXIT_FAILURE;
  }

  for (list = res; list != NULL; list = list->ai_next) {
    if (list->ai_family == AF_INET6) {
      printf("AF_INET6\n");
      int rc = sendto(s, test, 4, MSG_MORE, list->ai_addr, sizeof(*list->ai_addr));
      if (rc < 0) {
         perror("PROBLEME ... ");
      }

    }

  }

  return EXIT_SUCCESS; 
}

I Expect no perror printing but I get network is unreacheable. I would like to know why this dont work someone any idea ?

2

2 Answers

1
votes

One obvious problem is that you are not passing enough bytes for the address length in the sendto call. The the ai_addr field is a struct sockaddr *, but a struct sockaddr is merely a header structure, and the real address structures will allocate larger structures.

Use list->ai_addrlen instead.

0
votes

It was because, I do not have ipv6 connection on 4g mode, we can do

ip -6 route | grep default

to check if we have ipv6 connection