getprotoent(3)

๐Ÿ’– getprotoent(3) โ€” the protocol tea, bestie ๐Ÿ’–

okay so this is a page from the Library Functions Manual (section 3, the libc girlies section ๐ŸŽ€), and it's all about looking up protocols โ€” like, the IPPROTO_TCP kind of protocols, not the social kind (though honestly both are giving drama). Let's get into it โœจ


๐ŸŒธ NAME ๐ŸŒธ

getprotoent, getprotobyname, getprotobynumber, setprotoent, endprotoent โ€” get protocol entry ๐Ÿฆ‹


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). yes bestie, you just link -lc and you're slaying ๐Ÿ’…


โœจ SYNOPSIS โœจ

#include <netdb.h>

struct protoent *getprotoent(void);

struct protoent *getprotobyname(const char *name);
struct protoent *getprotobynumber(int proto);

void setprotoent(int stayopen);
void endprotoent(void);

๐Ÿ‘‘ DESCRIPTION ๐Ÿ‘‘

okay so picture this: there's a whole protocols database (catch it over at protocols(5) ๐Ÿซถ) and these functions are your little backstage pass to it.

  • getprotoent() โ€” reads the next entry from the protocols database and hands you back a protoent structure stuffed with all the broken-out fields from that entry. If a connection to the database doesn't exist yet, it opens one. no biggie ๐Ÿ’…
  • getprotobyname() โ€” give it a protocol name in name and it returns a protoent structure for the entry from the database that matches that name. connection opened if necessary, obviously โœจ
  • getprotobynumber() โ€” same energy but for a protocol number. it returns a protoent structure for the entry matching that protocol number, which the docs call number (and the synopsis calls proto โ€” the docs said what they said ๐Ÿ™ˆ). also opens a connection if necessary.
  • setprotoent() โ€” opens a connection to the database and rewinds you back to the first entry. if stayopen is nonzero, that connection to the database will not be closed between calls to one of the getproto*() functions. it's giving commitment ๐Ÿ’–
  • endprotoent() โ€” closes the connection to the database. bye bestie, the vibe check is over ๐Ÿฆ‹

๐ŸŽ€ the protoent structure ๐ŸŽ€

it's defined in <netdb.h> like this, no cap:

struct protoent {
    char  *p_name;       /* official protocol name */
    char **p_aliases;    /* alias list */
    int    p_proto;      /* protocol number */
}

and the members are:

  • p_name โ€” the official name of the protocol. the main character ๐Ÿ‘‘
  • p_aliases โ€” a NULL-terminated list of alternative names for the protocol. the entourage โœจ
  • p_proto โ€” the protocol number. the digits, the receipts ๐Ÿ”ข

๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

getprotoent(), getprotobyname(), and getprotobynumber() all return a pointer to a statically allocated protoent structure. and if something goes wrong, or if you've hit the end of the file? you get a null pointer. that's it, that's the whole mood ๐Ÿ˜ญ

careful bestie โ€” statically allocated means it's the library's one shared little structure, not a fresh one you own. it's not yours, it's communal property ๐Ÿ’€

๐Ÿ“ FILES ๐Ÿ“

  • /etc/protocols โ€” protocol database file. this is the source of truth, the group chat everyone's reading from ๐Ÿ“

๐ŸŒˆ ATTRIBUTES ๐ŸŒˆ

for an explanation of the terms used in this section, go see attributes(7). the goods:

  • getprotoent() โ€” Thread safety โ€” MT-Unsafe race:protoent race:protoentbuf locale
  • getprotobyname() โ€” Thread safety โ€” MT-Unsafe race:protobyname locale
  • getprotobynumber() โ€” Thread safety โ€” MT-Unsafe race:protobynumber locale
  • setprotoent(), endprotoent() โ€” Thread safety โ€” MT-Unsafe race:protoent locale

and here's the lore: in that list up there, protoent inside race:protoent means that if any of the functions setprotoent(), getprotoent(), or endprotoent() are used in parallel in different threads of a program, then data races could occur. ๐Ÿ’€ yes bestie, actual races. threads fighting over the shared static struct. be careful fr fr.


โญ STANDARDS โญ

POSIX.1-2008. certified, iconic, official ๐Ÿซถ


๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

POSIX.1-2001, 4.3BSD. it's been around since forever, a true legend ๐Ÿ‘‘


๐Ÿ’œ SEE ALSO ๐Ÿ’œ


and that's the whole page, bestie!! this little gem is from Linux man-pages 6.20, dated 2026-02-08 ๐Ÿ’– go forth and getprotobyname("tcp") responsibly, ily, mwah ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms