GETPEERNAME(3POSIX)

πŸ’– GETPEERNAME(3POSIX) β€” the POSIX Programmer's Manual, bestie edition πŸŽ€

okay so first things first, bestie, this page lives in the POSIX Programmer's Manual (yes the whole manual, we're fancy here πŸ’…). But real talk: the Linux implementation of this interface may differ β€” so if you're on Linux, go consult the corresponding Linux manual page for the deets on Linux behavior. Or, plot twist, the interface might not even be implemented on Linux at all. πŸ™ˆ iykyk.


✨ NAME ✨

getpeername β€” get the name of the peer socket. that's it, that's the whole job, and she does it flawlessly πŸ‘‘


🌸 SYNOPSIS 🌸

#include <sys/socket.h>

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

πŸ¦‹ DESCRIPTION πŸ¦‹

So the way that getpeername() works is: it retrieves the peer address of the socket you specified, stores that address in the sockaddr structure pointed to by the address argument, and stores the length of that address in the object pointed to by the address_len argument. she's organized. she's thorough. we love her 🫢

The address_len argument points to a socklen_t object, and this girly has two moods:

  • on input, it specifies the length of the supplied sockaddr structure 🌱
  • on output, it 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. yeah. she's cutting it off. no cap. πŸ’€

one lil edge case though: if the protocol permits connections by unbound clients, and the peer is not bound, then the value stored in the object pointed to by address is unspecified. lowkey mysterious of her but okay πŸ’œ


πŸ”₯ RETURN VALUE πŸ”₯

  • if it ate and left no crumbs: 0 shall be returned βœ…
  • otherwise: -1 shall be returned, and errno gets set to indicate the error 😭

πŸ‘‘ ERRORS πŸ‘‘

The getpeername() function shall fail if:

  • EBADF β€” the socket argument is not a valid file descriptor. like girl. that's not a real fd. πŸ’€
  • EINVAL β€” the socket has been shut down. she's gone, bestie, she's not coming back πŸ₯€
  • ENOTCONN β€” the socket is not connected or otherwise has not had the peer pre-specified. no connection, no peer, no vibe 🌧️
  • ENOTSOCK β€” the socket argument does not refer to a socket. it's giving "that's not a socket" 😭
  • EOPNOTSUPP β€” the operation is not supported for the socket protocol. not every protocol is gonna serve you this, and that's on them πŸ’…

The getpeername() function may fail if:

  • ENOBUFS β€” insufficient resources were available in the system to complete the call. the system is tired. the system needs a nap. 😴
✨ heads up bestie: the following sections are informative. not normative. informative. she's just chatting. πŸ’¬

🌈 EXAMPLES 🌈

None. nothing. empty. she said "figure it out" πŸ™ˆ


πŸ’… APPLICATION USAGE πŸ’…

None. literally none. iconic of her honestly. no notes. πŸŽ€


🧚 RATIONALE 🧚

None. she simply did not feel like explaining herself. main character energy fr fr πŸ‘‘


⭐ FUTURE DIRECTIONS ⭐

None. the future is undefined and so is this section. 🌌


πŸ“ SEE ALSO πŸ“

  • accept()
  • bind()
  • getsockname()
  • socket()

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


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. 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 .


and that's the whole page, bestie!! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

signed off by IEEE/The Open Group, 2017 β€” yeah, the whole entire year, that's the date, we're keeping it πŸ—“οΈβœ¨ stay connected, stay valid, and always check your errno before you blame yourself, ily!! πŸ‘‘πŸ’œ