getsockname(2)

getsockname2

Get socket name

Usage

#include <sys/types.h>

#include <sys/socket.h>

int

getsockname(int s, struct sockaddr * restrict name, socklen_t * restrict namelen);

Library

Standard C Library (libc, -lc)

Overview

The getsockname() system call returns the current name for the specified socket. The namelen argument should be initialized to indicate the amount of space pointed to by name. On return it contains the actual size of the name returned (in bytes).

Return values

The getsockname() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Errors

The call succeeds unless:

[EBADF]

The argument s is not a valid descriptor.

[ECONNRESET]

The connection has been reset by the peer.

[EINVAL]

The value of the namelen argument is not valid.

[ENOTSOCK]

The argument s is a file, not a socket.

[ENOBUFS]

Insufficient resources were available in the system to perform the operation.

[EFAULT]

The name argument points to memory not in a valid part of the process address space.

Releases

87 releases since FreeBSD 2.0.5-RELEASE; the text changed 45 times.

  1. 2.0.5–2.1.7.1
  2. 2.2.2–3.4
  3. 3.5–3.5.1
  4. 4.0
  5. 4.1
  6. 4.1.1–4.3
  7. 4.4
  8. 4.5–4.11
  9. 5.0
  10. 5.1
  11. 5.2–5.2.1
  12. 5.3–5.5
  13. 6.0–6.3
  14. 6.4
  15. 7.0
  16. 7.1
  17. 7.2
  18. 7.3
  19. 7.4
  20. 8.0
  21. 8.1
  22. 8.2
  23. 8.3
  24. 8.4
  25. 9.0
  26. 9.1
  27. 9.2
  28. 9.3
  29. 10.0
  30. 10.1
  31. 10.2
  32. 10.3
  33. 10.4
  34. 11.0
  35. 11.1
  36. 11.2
  37. 11.3
  38. 11.4
  39. 12.0
  40. 12.1
  41. 12.2
  42. 12.3
  43. 12.4
  44. 13.0–13.2
  45. 13.3–14.5
  46. 15.0–16.0

History

The getsockname() system call appeared in 4.2BSD.

Bugs

Names bound to sockets in the UNIX domain are inaccessible; getsockname() returns a zero length name.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.