getsockname(2)

โœจ getsockname(2) โœจ

okay so bestie, this is the getsockname page from the System Calls Manual โ€” section 2, system calls, iykyk ๐Ÿ’…. It's the one that tells you what name your socket is currently wearing. Let's get into it ๐Ÿซถ


๐ŸŽ€ NAME ๐ŸŽ€

getsockname โ€” get socket name โœจ


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) โ€” she lives in libc, no cap ๐Ÿ’–


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <sys/socket.h>

int getsockname(int sockfd, struct sockaddr *restrict addr,
                socklen_t *restrict addrlen);

there she is ๐Ÿ‘‘ note those restrict qualifiers, they're doing real work, we respect them ๐Ÿ™


๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so getsockname() returns the current address that the socket sockfd is bound to, and it drops it into the buffer pointed at by addr. it's giving "who am I, really" โœจ

The addrlen argument? You gotta initialize it yourself to say how much space (in bytes) you've got at the address pointed to by addr. Then, on return, it holds the actual size of the socket address โ€” so it may come back different than you sent it. main character energy, honestly ๐Ÿ’…

So the returned address gets truncated if the buffer you handed over is too small. And in that case, addrlen will return a value greater than what you supplied to the call โ€” that's how you know you got cut off ๐Ÿ˜ญ plan your buffer accordingly babes.


๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

  • On success: zero is returned. slay โœจ
  • On error: -1 is returned, and errno gets set to tell you which error it was. not the vibe ๐Ÿ’€

๐Ÿ’… ERRORS ๐Ÿ’…

  • EBADF โ€” the sockfd argument is not a valid file descriptor ๐Ÿ™ˆ
  • EFAULT โ€” the addr argument points to memory that isn't in a valid part of the process address space. off the map, literally ๐Ÿ—บ๏ธ๐Ÿ’€
  • EINVAL โ€” addrlen is invalid, like for example it's negative. girl. negative bytes?? ๐Ÿ˜ญ
  • ENOBUFS โ€” insufficient resources were available in the system to perform the operation ๐Ÿฅฒ the system was simply not serving today
  • ENOTSOCK โ€” the file descriptor sockfd does not refer to a socket. it's giving impostor ๐Ÿซ 

๐ŸŒˆ STANDARDS ๐ŸŒˆ

POSIX.1-2024. periodt ๐Ÿ’œ


๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

POSIX.1-2001, SVr4, 4.2BSD. she's been around, she's iconic, she's seen things ๐Ÿ‘‘


๐Ÿ“ SEE ALSO ๐Ÿ“

bind(2), socket(2), getifaddrs(3), ip(7), socket(7), unix(7)


and that's the whole page bestie, go check your socket's name, she deserves to be seen ๐ŸŒธ know your bind, own your bind ๐Ÿ’–

this has been Linux man-pages 6.20, dated 2025-10-29 โ€” mwah, sending you the biggest hug, now go read bind(2) next, mwah ๐Ÿ’‹โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms