GETSOCKNAME(2)

✨ GETSOCKNAME(2) ✨

okay bestie, we're in the System Calls Manual section, and today's main character is getsockname β€” yes, that's the whole name, no notes πŸ’…


🌸 NAME 🌸

getsockname β€” get socket name πŸŽ€


πŸ’œ LIBRARY πŸ’œ

Standard C Library (libc, -lc) β€” she lives in libc, as she should πŸ‘‘


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <sys/types.h>
#include <sys/socket.h>

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

✨ DESCRIPTION ✨

so basically, the getsockname() system call returns the current name for the socket you point at. that's it, that's the vibe πŸ’–

the namelen argument? you gotta initialize it first, bestie β€” it tells the system how much space name has to work with. and on return, it's been updated to hold the actual size of the name returned, in bytes. glow-up behavior fr fr ✨


πŸ”₯ RETURN VALUES πŸ”₯

getsockname() returns the value 0 if it's successful 🫢 otherwise it returns -1 and sets the global variable errno to indicate the error. no cap, that's just how she communicates 😭


πŸ’€ ERRORS πŸ’€

the call succeeds unless β€” and girl, there are receipts:

  • [EBADF] β€” the argument s is not a valid descriptor. not it. 🚫
  • [ECONNRESET] β€” the connection has been reset by the peer. they hung up, literally πŸ’”
  • [EINVAL] β€” the value of the namelen argument is not valid. πŸ₯΄
  • [ENOTSOCK] β€” the argument s is a file, not a socket. wrong main character energy πŸ’€
  • [ENOBUFS] β€” insufficient resources were available in the system to perform the operation. the system said "i'm tired" 😭
  • [EFAULT] β€” the name argument points to memory not in a valid part of the process address space. lost in the void πŸŒ€

🌈 SEE ALSO 🌈

bind(2), getpeername(2), socket(2) β€” the whole friend group, go read them too 🫢


πŸŽ€ HISTORY πŸŽ€

the getsockname() system call appeared in 4.2BSD. vintage icon behavior, she's been serving since day one πŸ‘‘


πŸ™ˆ BUGS πŸ™ˆ

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

so yeah, if you were expecting a name back, you're getting nothing. zero. the void. it's giving ghosted 😭


and that's the whole page, bestie β€” written on the Debian vibe, dated June 4, 1993, and filed under GETSOCKNAME(2) πŸ’…βœ¨ mwah, go call getsockname() responsibly πŸ«ΆπŸ’–

πŸ“¦ where this page comes from