GETPEERNAME(1)
π getpeername β bestie edition π
okay so real talk, this page lives in section 1 of the manual, and it's all about the netpipes 4.2 fam π«Ά. Two programs, one brain cell: getpeername and getsockname. Let's get into it β¨
πΈ NAME πΈ
getpeername β get information about this or that end of the socket's connection π¦
netpipes 4.2
β¨ SYNOPSIS β¨
getpeername [ -verbose ] [ -sock ] [ fd ]
getsockname [ -verbose ] [ -peer ] [ fd ]
π DESCRIPTION π
NOT the manual page for the getpeername system call, bestie. That one lives in section 2. You get to it with something like man 2 getpeername or man -s 2 getpeername. Sorry for the confusion, genuinely π.
getpeername does a getpeername(2) system call on one of its file descriptors β the one you name with fd β and then prints out the results. The default fd is 0 (stdin). If you hand it the -sock argument, you can make getpeername act like getsockname. π«
getsockname does a getsockname(2) system call on one of its file descriptors β again, that's fd β and prints the results. Default fd is 0 (stdin). Give it -peer and it'll act like getpeername instead. π
β οΈ There's a severe limitation ofgetpeername, and I need you to hear me on this. If the remote process has closed the connection,getpeernamewill fail with aSocket is not connectederror. And the way this happens with dismaying frequency when the remote process doesn't depend on the local process for input and it's only sending a lil bit of output before it closes the connection?? Tragic. Hopefully the practical uses ofgetpeername(if there are any π) won't run into this problem.
So here's the vibe: you use getpeername to find out the address of the opposite end of a socket. You use getsockname to find out the address of the local end of a socket. They are in fact the same program with different names. And for the rest of this description, we're gonna refer to both of them as getpeername. Same girl, different outfit π
getpeername knows how to display peer info about UNIX and Internet sockets. Try it on any other type of socket and it'll fail with an "unknown address family" error. If you regularly deal with strange sockets and you want getpeername to work with them β send me email, fr fr π§.
- If the socket is a UNIX domain socket,
getpeernameprints the name of the file (which is the port) on a single line. And if-verbosewas specified? Then you get the detailed report: the wordUnixon the first line, the wordPorton the second line, and the name of the file on the third line. π - If the socket is an Internet socket,
getpeernameprints the port number on the first line and the numeric address on the second line. If-verbosewas specified, you get the detailed report: the wordInterneton the first line, the wordPorton the second line, the port numer on the third line, the wordHoston the fourth line. Starting on the fifth line it prints all the numeric internet addresses returned by thegethostbyaddr(3)library routine. And on the rest of the lines it prints all the host names. π€ so thorough
π₯ EASTER EGG π₯
If you specify -verbose twice, the program will print a copyright notice. (iykyk π)
π EXAMPLES π
Okay so lore time: I originally designed getpeername so that a faucet-spawned shell script could find out who was talking to it (and maybe do some access control). I added getsockname for completeness. Now I realize getsockname is useful for multi-homing services. But the thing is β most software you'd want to understand multi-homing (httpd, ftpd) is already capable of doing it, and way more efficiently than a script wrapper. Still, it might come in handy some day. π¦
client$ hose mail.cise.ufl.edu smtp --in ./getpeername
25
128.227.205.210
You connected to mail.cis.ufl.edu on the SMTP port (port 25). For a verbose report:
client$ hose mail.cise.ufl.edu smtp --in ./getpeername -v
Internet
Port
25
Host
128.227.205.210
fireant.cise.ufl.edu
Now let's give an example of a race condition which will cause getpeername to fail:
client$ hose web.cise.ufl.edu 80 -in ./getpeername
./getpeername: getpeername failed on descriptor 0: Socket is not connected
The HTTP daemon tries to read a request, finds that half of the full duplex connection closed (by the special behavior of the -in option on hose(1)) and drops the connection before getpeername can query the file descriptor. We can cause the HTTP daemon to wait for us by leaving both halves of the duplex connection open. π
client$ hose web.cise.ufl.edu 80 -fd0 ./getpeername -v
Internet
Port
80
Host
128.227.205.206
flood.cise.ufl.edu
And, finally, let's extract some useful information from our socket.
client$ hose web.cise.ufl.edu 80 -fd0 sh -c " ./getpeername -v | \
tail +5 | egrep -v '^[0-9.]*$' | head -1"
flood.cise.ufl.edu
slay π
π ERRORS π
Socket operation on non-socketβ Thefdyou specified does not refer to a socket, or refers to a socket that has been closed. This happens when you rungetpeernameby itself (it is unlikely that any of the file descriptors attached to an interactive shell are actually sockets), or if you goof up your faucet/hose command and forgot todup(2)one of your descriptors, or if the remote machine manages to close the connection beforegetpeernamecould run. πBad file numberβ You gave it a bad file number forfd. If you have enough skill to actually generate this error, you probably know what is wrong. π
If you encounter any other errors, clue me in. π«Ά
π SEE ALSO π
netpipes (1), faucet (1), hose (1), sockdown (1), socket (2), shutdown (2),
π BUGS π
These programs are vulnerable to reverse DNS lookup spoofing. You probably want to add nospoof on to your /etc/host.conf. π¨
π NOTES π
Just avoid doing anything funky like passing getpeername strings and it should serve you well.
DOH! 3.0 didn't use the ntohs macro on the port numbers so the output was bogus on machines with non-network-order port numbers (like Linux-i386). 3.1 fixed this. π
π CREDITS π
"Hi Mom! Hi Dad!" π₯Ήπ
β COPYRIGHT β
Copyright (C) 1995-98 Robert Forsman
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
π AUTHOR π
Robert Forsman thoth@purplefrog.com Purple Frog Software http://web.purplefrog.com/~thoth/
okay that's the whole page bestie, thanks for coming to my ted talk π«Ά this version is from the General Commands Manual, dated March 18, 1998, and it's the GETPEERNAME(1) page, periodt π
β¨
π¦ where this page comes from
- debian bookworm: netpipes 4.2-8+b1
- debian trixie: netpipes 4.2-8+b2
- ubuntu jammy: netpipes 4.2-8build1
- ubuntu noble: netpipes 4.2-8build1
- ubuntu questing: netpipes 4.2-8.1
- ubuntu resolute: netpipes 4.2-8.1build1
- ubuntu stonking: netpipes 4.2-8.1build1
- Copyright 1995-98 by Robert Forsman
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms