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 argumentsis 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 thenamelenargument is not valid. Something about it just isn't giving valid π«[ENOTSOCK]β The argumentsis 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]β Thenameargument 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
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms