[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] How to get the client IP as IPv4 in dual stack mode?

From: silvioprog
Subject: Re: [libmicrohttpd] How to get the client IP as IPv4 in dual stack mode?
Date: Thu, 28 Nov 2019 22:07:01 -0300

Hi Christian.

It solved the problem. Thank you so much! :-)

This is the updated version of the function, if someone need it:

int get_ip(const void *socket, char *buf, size_t size) {
  const struct sockaddr *sa;
  const void *addr6;
  size_t len;
  if (!socket || !buf || (ssize_t) size < 0)
    return EINVAL;
  sa = socket;
  if (sa->sa_family == AF_INET6) {
    addr6 = &(((struct sockaddr_in6 *) sa)->sin6_addr);
    if (!inet_ntop(AF_INET6, addr6, buf, size))
      return errno;
    len = strlen("::ffff:");
    if (IN6_IS_ADDR_V4MAPPED(addr6) && (size > len))
      memcpy(buf, buf + len, strlen(buf + len) + 1);
    return 0;
  if (!inet_ntop(AF_INET, &(((struct sockaddr_in *) sa)->sin_addr), buf, size))
    return errno;
  return 0;

Thank you!

On Thu, Nov 28, 2019 at 5:02 PM Christian Grothoff <address@hidden> wrote:
Hi silvioprog,

Have you considered simply using


on the v6 address, and if it matches, extracting the mapped v4 address?

Happy hacking!


Silvio Clécio

reply via email to

[Prev in Thread] Current Thread [Next in Thread]