GETSOCKNAME(3POSIX)

πŸ’… GETSOCKNAME β€” get the socket name πŸ’…

okay bestie, real talk: this page lives in the POSIX Programmer's Manual (GETSOCKNAME(3POSIX)), and it's the whole "who am I, actually?" moment for your socket. Let's get into it 🫢


✨ PROLOG ✨

so like, heads up before we even start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” so if you're on Linux, go consult the corresponding Linux manual page for the real Linux tea β˜•. And also? The interface may not be implemented on Linux at all. it's giving "spec vs. reality", iykyk πŸ’€


🌸 NAME 🌸

getsockname β€” get the socket name πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <sys/socket.h>

int getsockname(int socket, struct sockaddr *restrict address,
    socklen_t *restrict address_len);

πŸ’– DESCRIPTION πŸ’–

okay so here's the vibe. The getsockname() function shall retrieve the locally-bound name of the specified socket, store this address in the sockaddr structure pointed to by the address argument, and store the length of this address in the object pointed to by the address_len argument. main character energy, honestly πŸ‘‘

now, the address_len argument points to a socklen_t object which:

  • on input β€” specifies the length of the supplied sockaddr structure
  • on output β€” specifies the length of the stored address

and if the actual length of the address is greater than the length of the supplied sockaddr structure? the stored address shall be truncated. not me shrinking your address to fit the fit πŸ™ˆ

⚠️ important caveat bestie: if the socket has not been bound to a local name, the value stored in the object pointed to by address is unspecified. so like, don't come for her, she never said she'd have an answer πŸ’€

⭐ RETURN VALUE ⭐

upon successful completion:

  • 0 shall be returned
  • the address argument shall point to the address of the socket
  • the address_len argument shall point to the length of the address

otherwise, -1 shall be returned and errno set to indicate the error. slay when it works, screaming when it doesn't 😭


πŸ”₯ ERRORS πŸ”₯

the getsockname() function shall fail if:

  • EBADF β€” the socket argument is not a valid file descriptor. bestie that's not even a socket, that's a vibe check you failed πŸ’€
  • ENOTSOCK β€” the socket argument does not refer to a socket. it's giving "you brought the wrong thing to the party" πŸ™ˆ
  • EOPNOTSUPP β€” the operation is not supported for this socket's protocol. respectfully, not every protocol does this 🚫

the getsockname() function may fail if:

  • EINVAL β€” the socket has been shut down. she's done, she's logged off, don't @ her πŸ’…
  • ENOBUFS β€” insufficient resources were available in the system to complete the function. the system said "no thoughts, out of memory" 😭
πŸ“Œ the following sections are informative. (i.e. they're the bonus content, not the rules, bestie)

🌈 EXAMPLES 🌈

None. literally nothing. empty. a void. ghosted ✨


πŸŽ€ APPLICATION USAGE πŸŽ€

None. again, nothing here. the page said "no notes" 🫢


πŸ’œ RATIONALE πŸ’œ

None. no reasoning given, she just IS. iconic behavior, honestly πŸ‘‘


🧚 FUTURE DIRECTIONS 🧚

None. the future is unwritten and also this section is empty πŸ¦‹


πŸ’« SEE ALSO πŸ’«

  • accept()
  • bind()
  • getpeername()
  • socket()

and also: the Base Definitions volume of POSIX.1‐2017, <sys_socket.h> πŸ“š


okay so legal moment, but make it cute. portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.

in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. so like, the original is the one holding the mic 🎀

the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . (not me snitching on the converters, but also... yes me πŸ’€)


and that's the whole page, bestie!! brought to you by IEEE/The Open Group, 2017, on the GETSOCKNAME(3POSIX) manual page. go get your socket's name and live your truth πŸ’…βœ¨πŸ¦‹