BIND(3P)

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

okay so first things first: this page is part of the POSIX Programmer's Manual. And listen β€” the Linux implementation of this interface may differ (so if you're on Linux, go check the corresponding Linux manual page for the details of Linux behavior), or honestly the interface may not be implemented on Linux at all. Just so you know going in. πŸ™ˆ


✨ NAME ✨

bind β€” bind a name to a socket

like, give your lil socket a name. a whole identity. main character energy. πŸ‘‘


🌸 SYNOPSIS 🌸

#include <sys/socket.h>

int bind(int socket, const struct sockaddr *address,
    socklen_t address_len);

πŸ¦‹ DESCRIPTION πŸ¦‹

So the bind() function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned. Key detail bestie: sockets created with the socket() function are initially unnamed β€” they're identified only by their address family. They're literally just vibing with no name until you do this. πŸ’…

The bind() function takes the following arguments:

  • socket β€” Specifies the file descriptor of the socket to be bound.
  • address β€” Points to a sockaddr structure containing the address to be bound to the socket. The length and format of the address depend on the address family of the socket.
  • address_len β€” Specifies the length of the sockaddr structure pointed to by the address argument.

The socket specified by socket may require the process to have appropriate privileges to use the bind() function. so like, sometimes you gotta have the right permissions. know your power. 🫢

If the address family of the socket is AF_UNIX and the pathname in address names a symbolic link, bind() shall fail and set errno to [EADDRINUSE]. no symlink shenanigans, we don't do that here. 🚫

If the socket address cannot be assigned immediately and O_NONBLOCK is set for the file descriptor for the socket, bind() shall fail and set errno to [EINPROGRESS], but the assignment request shall not be aborted, and the assignment shall be completed asynchronously. So it's not cancelled, it's just doing it later, on its own time. Subsequent calls to bind() for the same socket, before the assignment is completed, shall fail and set errno to [EALREADY]. girl, be patient, it's already happening. 😭

When the assignment has been performed asynchronously, pselect(), select(), and poll() shall indicate that the file descriptor for the socket is ready for reading and writing.


🌈 RETURN VALUE 🌈

Upon successful completion, bind() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error. zero means slay, negative one means something went wrong. ⭐


πŸ’€ ERRORS πŸ’€

The bind() function shall fail if:

  • EADDRINUSE β€” The specified address is already in use.
  • EADDRNOTAVAIL β€” The specified address is not available from the local machine.
  • EAFNOSUPPORT β€” The specified address is not a valid address for the address family of the specified socket.
  • EALREADY β€” An assignment request is already in progress for the specified socket.
  • EBADF β€” The socket argument is not a valid file descriptor.
  • EINPROGRESS β€” O_NONBLOCK is set for the file descriptor for the socket and the assignment cannot be immediately performed; the assignment shall be performed asynchronously.
  • EINVAL β€” The socket is already bound to an address, and the protocol does not support binding to a new address; or the socket has been shut down.
  • ENOBUFS β€” Insufficient resources were available to complete the call.
  • ENOTSOCK β€” The socket argument does not refer to a socket.
  • EOPNOTSUPP β€” The socket type of the specified socket does not support binding to an address.

If the address family of the socket is AF_UNIX, then bind() shall fail if:

  • EACCES β€” A component of the path prefix denies search permission, or the requested name requires writing in a directory with a mode that denies write permission.
  • EDESTADDRREQ or EISDIR β€” The address argument is a null pointer.
  • EIO β€” An I/O error occurred.
  • ELOOP β€” A loop exists in symbolic links encountered during resolution of the pathname in address.
  • ENAMETOOLONG β€” The length of a component of a pathname is longer than {NAME_MAX}.
  • ENOENT β€” A component of the path prefix of the pathname in address does not name an existing file or the pathname is an empty string.
  • ENOENT or ENOTDIR β€” The pathname in address contains at least one non-<slash> character and ends with one or more trailing <slash> characters. If the pathname without the trailing <slash> characters would name an existing file, an [ENOENT] error shall not occur.
  • ENOTDIR β€” A component of the path prefix of the pathname in address names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in address contains at least one non-<slash> character and ends with one or more trailing <slash> characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.
  • EROFS β€” The name would reside on a read-only file system.

The bind() function may fail if:

  • EACCES β€” The specified address is protected and the current user does not have permission to bind to it.
  • EINVAL β€” The address_len argument is not a valid length for the address family.
  • EISCONN β€” The socket is already connected.
  • ELOOP β€” More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in address.
  • ENAMETOOLONG β€” The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.
heads up bestie: the following sections are informative. that means they're the vibe-check sections, not the rules-rules sections. πŸ’–

πŸŽ€ EXAMPLES πŸŽ€

The following code segment shows how to create a socket and bind it to a name in the AF_UNIX domain. copy it, love it, run it:

#define MY_SOCK_PATH "/somepath"

int sfd;
struct sockaddr_un my_addr;

sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1)
    /* Handle error */;

memset(&my_addr, '\0', sizeof(struct sockaddr_un));
                     /* Clear structure */
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) -1);

if (bind(sfd, (struct sockaddr *) &my_addr,
    sizeof(struct sockaddr_un)) == -1)
    /* Handle error */;

🧚 APPLICATION USAGE 🧚

An application program can retrieve the assigned socket name with the getsockname() function. so if you ever forget what you named it (relatable), just ask. πŸ’œ


πŸ“ RATIONALE πŸ“

None.

(no thoughts, head empty, iconic.) ✨


⭐ FUTURE DIRECTIONS ⭐

None.


πŸ”₯ SEE ALSO πŸ”₯

  • connect()
  • getsockname()
  • listen()
  • socket()

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 β€” brought to you by IEEE/The Open Group, dated 2017, landing in the BIND(3P) slot of the POSIX Programmer's Manual. go name your sockets, you absolute icon πŸ’…βœ¨

πŸ“¦ where this page comes from