GETPEERNAME(2)

✨ GETPEERNAME(2) ✨

okay bestie, welcome to the System Calls Manual section 2, where we talk about getpeername β€” the little syscall that tells you who's on the other end of your socket 🫢


πŸ’– NAME πŸ’–

getpeername β€” get name of connected peer

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

πŸŽ€ SYNOPSIS πŸŽ€

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

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

πŸ¦‹ DESCRIPTION πŸ¦‹

So the getpeername() system call returns the name of the peer that's connected to socket s. Like, who is on the other side of this line? It's giving caller ID but for sockets, no cap πŸ’…

The namelen argument should be initialized to indicate the amount of space pointed to by name. Then on return it contains the actual size of the name returned (in bytes). Heads up though: the name is truncated if the buffer you provided is too small. So don't come crying if it's clipped, bestie β€” size your buffer correctly πŸ™ˆ

πŸ‘‘ RETURN VALUES πŸ‘‘

The getpeername() function returns the value 0 if successful. Otherwise the value -1 is returned and the global variable errno is set to indicate the error. Iconic behavior honestly β€” 0 means slay, -1 means check errno ✨

πŸ’€ ERRORS πŸ’€

The call succeeds unless (aka here's the drama):

  • [EBADF] β€” The argument s is not a valid descriptor. Not a real one, not invited 🚫
  • [ECONNRESET] β€” The connection has been reset by the peer. They hung up on you, literally 😭
  • [EINVAL] β€” The value of the namelen argument is not valid. Something about it just isn't giving valid 🫠
  • [ENOTSOCK] β€” The argument s is a file, not a socket. Wrong vibe entirely, that's not even a socket bestie πŸ“„
  • [ENOTCONN] β€” The socket is not connected. You can't ask who your peer is if there IS no peer, babe πŸ’”
  • [ENOBUFS] β€” Insufficient resources were available in the system to perform the operation. The system is lowkey out of bandwidth for your nonsense 🌧️
  • [EFAULT] β€” The name argument points to memory not in a valid part of the process address space. That pointer is somewhere it should not be, and we do not go there πŸ•³οΈ

🌈 SEE ALSO 🌈

accept(2), bind(2), getsockname(2), socket(2)

🌸 HISTORY 🌸

The getpeername() system call appeared in 4.2BSD. Ancient. Vintage. Still serving looks after all these decades πŸ’œ


and that's the whole page, bestie πŸŽ€ this one came from Debian, dated June 4, 1993 β€” yeah, it's older than us, and it still ate fr fr ⭐ πŸ¦‹ πŸ“

πŸ“¦ where this page comes from