---
name: "mnl_socket_bind"
section: "3"
platform: "linux"
package: "libmnl-doc"
version: "1.0.5-3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mnl_socket_bind"
---

# mnl\_socket\_bind(3)

Netlink socket helpers

## NAME

socket - Netlink socket helpers

## SYNOPSIS

### Functions

int **mnl\_socket\_get\_fd** (const struct mnl\_socket \*nl)\
 unsigned int **mnl\_socket\_get\_portid** (const struct mnl\_socket \*nl)\
 struct mnl\_socket \* **mnl\_socket\_open** (int bus)\
 struct mnl\_socket \* **mnl\_socket\_open2** (int bus, int flags)\
 struct mnl\_socket \* **mnl\_socket\_fdopen** (int fd)\
 int **mnl\_socket\_bind** (struct mnl\_socket \*nl, unsigned int groups, pid\_t pid)\
 ssize\_t **mnl\_socket\_sendto** (const struct mnl\_socket \*nl, const void \*buf, size\_t len)\
 ssize\_t **mnl\_socket\_recvfrom** (const struct mnl\_socket \*nl, void \*buf, size\_t bufsiz)\
 int **mnl\_socket\_close** (struct mnl\_socket \*nl)\
 int **mnl\_socket\_setsockopt** (const struct mnl\_socket \*nl, int type, void \*buf, socklen\_t len)\
 int **mnl\_socket\_getsockopt** (const struct mnl\_socket \*nl, int type, void \*buf, socklen\_t \*len)\

## Detailed Description

## Function Documentation

### int mnl\_socket\_bind (struct mnl\_socket \* nl, unsigned int groups, pid\_t pid)

mnl\_socket\_bind - bind netlink socket

**Parameters**

*nl* netlink socket obtained via **mnl\_socket\_open()** \
 *groups* the group of message you're interested in \
 *pid* the port ID you want to use (use zero for automatic selection)

On error, this function returns -1 and errno is appropriately set. On success, 0 is returned. You can use MNL\_SOCKET\_AUTOPID which is 0 for automatic port ID selection.

Definition at line **193** of file **socket.c**.

### int mnl\_socket\_close (struct mnl\_socket \* nl)

mnl\_socket\_close - close a given netlink socket

**Parameters**

*nl* netlink socket obtained via **mnl\_socket\_open()**

On error, this function returns -1 and errno is appropriately set. On success, it returns 0.

Definition at line **296** of file **socket.c**.

### struct mnl\_socket \* mnl\_socket\_fdopen (int fd)

mnl\_socket\_fdopen - associates a mnl\_socket object with pre-existing socket.

**Parameters**

*fd* pre-existing socket descriptor.

On error, it returns NULL and errno is appropriately set. Otherwise, it returns a valid pointer to the mnl\_socket structure. It also sets the portID if the socket fd is already bound and it is AF\_NETLINK.

Note that **mnl\_socket\_get\_portid()** returns 0 if this function is used with non-netlink socket.

Definition at line **161** of file **socket.c**.

### int mnl\_socket\_get\_fd (const struct mnl\_socket \* nl)

mnl\_socket\_get\_fd - obtain file descriptor from netlink socket

**Parameters**

*nl* netlink socket obtained via **mnl\_socket\_open()**

This function returns the file descriptor of a given netlink socket.

Definition at line **85** of file **socket.c**.

### unsigned int mnl\_socket\_get\_portid (const struct mnl\_socket \* nl)

mnl\_socket\_get\_portid - obtain Netlink PortID from netlink socket

**Parameters**

*nl* netlink socket obtained via **mnl\_socket\_open()**

This function returns the Netlink PortID of a given netlink socket. It's a common mistake to assume that this PortID equals the process ID which is not always true. This is the case if you open more than one socket that is binded to the same Netlink subsystem from the same process.

Definition at line **99** of file **socket.c**.

### int mnl\_socket\_getsockopt (const struct mnl\_socket \* nl, int type, void \* buf, socklen\_t \* len)

mnl\_socket\_getsockopt - get a Netlink socket option

**Parameters**

*nl* netlink socket obtained via **mnl\_socket\_open()** \
 *type* type of Netlink socket options \
 *buf* pointer to the buffer to store the value of this option \
 *len* size of the information written in the buffer

On error, this function returns -1 and errno is appropriately set.

Definition at line **343** of file **socket.c**.

### struct mnl\_socket \* mnl\_socket\_open (int bus)

mnl\_socket\_open - open a netlink socket

**Parameters**

*bus* the netlink socket bus ID (see NETLINK\_\* constants)

On error, it returns NULL and errno is appropriately set. Otherwise, it returns a valid pointer to the mnl\_socket structure.

Definition at line **128** of file **socket.c**.

### struct mnl\_socket \* mnl\_socket\_open2 (int bus, int flags)

mnl\_socket\_open2 - open a netlink socket with appropriate flags

**Parameters**

*bus* the netlink socket bus ID (see NETLINK\_\* constants) \
 *flags* the netlink socket flags (see SOCK\_\* constants in [socket(2)](/socket/2.md))

This is similar to **mnl\_socket\_open()**, but allows one to set flags like SOCK\_CLOEXEC at socket creation time (useful for multi-threaded programs performing exec calls).

On error, it returns NULL and errno is appropriately set. Otherwise, it returns a valid pointer to the mnl\_socket structure.

Definition at line **145** of file **socket.c**.

### ssize\_t mnl\_socket\_recvfrom (const struct mnl\_socket \* nl, void \* buf, size\_t bufsiz)

mnl\_socket\_recvfrom - receive a netlink message

**Parameters**

*nl* netlink socket obtained via **mnl\_socket\_open()** \
 *buf* buffer that you want to use to store the netlink message \
 *bufsiz* size of the buffer passed to store the netlink message

On error, it returns -1 and errno is appropriately set. If errno is set to ENOSPC, it means that the buffer that you have passed to store the netlink message is too small, so you have received a truncated message. To avoid this, you have to allocate a buffer of MNL\_SOCKET\_BUFFER\_SIZE (which is 8KB, see linux/netlink.h for more information). Using this buffer size ensures that your buffer is big enough to store the netlink message without truncating it.

Definition at line **256** of file **socket.c**.

### ssize\_t mnl\_socket\_sendto (const struct mnl\_socket \* nl, const void \* buf, size\_t len)

mnl\_socket\_sendto - send a netlink message of a certain size

**Parameters**

*nl* netlink socket obtained via **mnl\_socket\_open()** \
 *buf* buffer containing the netlink message to be sent \
 *len* number of bytes in the buffer that you want to send

On error, it returns -1 and errno is appropriately set. Otherwise, it returns the number of bytes sent.

Definition at line **232** of file **socket.c**.

### int mnl\_socket\_setsockopt (const struct mnl\_socket \* nl, int type, void \* buf, socklen\_t len)

mnl\_socket\_setsockopt - set Netlink socket option

**Parameters**

*nl* netlink socket obtained via **mnl\_socket\_open()** \
 *type* type of Netlink socket options \
 *buf* the buffer that contains the data about this option \
 *len* the size of the buffer passed

This function allows you to set some Netlink socket option. As of writing this (see linux/netlink.h), the existing options are:

```
 - \#define NETLINK_ADD_MEMBERSHIP  1
 - \#define NETLINK_DROP_MEMBERSHIP 2
 - \#define NETLINK_PKTINFO         3
 - \#define NETLINK_BROADCAST_ERROR 4
 - \#define NETLINK_NO_ENOBUFS      5
```

 In the early days, Netlink only supported 32 groups expressed in a 32-bits mask. However, since 2.6.14, Netlink may have up to 2^32 multicast groups but you have to use setsockopt() with NETLINK\_ADD\_MEMBERSHIP to join a given multicast group. This function internally calls setsockopt() to join a given netlink multicast group. You can still use mnl\_bind() and the 32-bit mask to join a set of Netlink multicast groups.

On error, this function returns -1 and errno is appropriately set.

Definition at line **328** of file **socket.c**.

## Author

Generated automatically by Doxygen for libmnl from the source code.
