Function: format-network-address

format-network-address is a function defined in process.c.

Signature

(format-network-address ADDRESS &optional OMIT-PORT)

Documentation

Convert network ADDRESS from internal format to a string.

A 4 or 5 element vector represents an IPv4 address (with port number). An 8 or 9 element vector represents an IPv6 address (with port number). If optional second argument OMIT-PORT is non-nil, don't include a port number in the string, even when present in ADDRESS. Return nil if format of ADDRESS is invalid.

View in manual

Probably introduced at or before Emacs version 22.1.

Source Code

// Defined in /usr/src/emacs/src/process.c
{
  if (NILP (address))
    return Qnil;

  if (STRINGP (address))  /* AF_LOCAL */
    return address;

  if (VECTORP (address))  /* AF_INET or AF_INET6 */
    {
      register struct Lisp_Vector *p = XVECTOR (address);
      ptrdiff_t size = p->header.size;
      Lisp_Object args[10];
      int nargs, i;
      char const *format;

      if (size == 4 || (size == 5 && !NILP (omit_port)))
	{
	  format = "%d.%d.%d.%d";
	  nargs = 4;
	}
      else if (size == 5)
	{
	  format = "%d.%d.%d.%d:%d";
	  nargs = 5;
	}
      else if (size == 8 || (size == 9 && !NILP (omit_port)))
	{
	  format = "%x:%x:%x:%x:%x:%x:%x:%x";
	  nargs = 8;
	}
      else if (size == 9)
	{
	  format = "[%x:%x:%x:%x:%x:%x:%x:%x]:%d";
	  nargs = 9;
	}
      else
	return Qnil;

      AUTO_STRING (format_obj, format);
      args[0] = format_obj;

      for (i = 0; i < nargs; i++)
	{
	  if (! RANGED_FIXNUMP (0, p->contents[i], 65535))
	    return Qnil;

	  if (nargs <= 5         /* IPv4 */
	      && i < 4           /* host, not port */
	      && XFIXNUM (p->contents[i]) > 255)
	    return Qnil;

	  args[i + 1] = p->contents[i];
	}

      return Fformat (nargs + 1, args);
    }

  if (CONSP (address))
    {
      AUTO_STRING (format, "<Family %d>");
      return CALLN (Fformat, format, Fcar (address));
    }

  return Qnil;
}