libnl 1.1.4

Flags Translations

char * rtnl_addr_flags2str (int flags, char *buf, size_t size)
 
int rtnl_addr_str2flags (const char *name)
 

Allocation/Freeing

struct rtnl_addr * rtnl_addr_alloc (void)
 
void rtnl_addr_put (struct rtnl_addr *addr)
 

Cache Management

struct nl_cache * rtnl_addr_alloc_cache (struct nl_handle *handle)
 

Addition

struct nl_msg * rtnl_addr_build_add_request (struct rtnl_addr *addr, int flags)
 Build netlink request message to request addition of new address.
 
int rtnl_addr_add (struct nl_handle *handle, struct rtnl_addr *addr, int flags)
 Request addition of new address.
 

Deletion

struct nl_msg * rtnl_addr_build_delete_request (struct rtnl_addr *addr, int flags)
 Build a netlink request message to request deletion of an address.
 
int rtnl_addr_delete (struct nl_handle *handle, struct rtnl_addr *addr, int flags)
 Request deletion of an address.
 

Attributes

void rtnl_addr_set_label (struct rtnl_addr *addr, const char *label)
 
char * rtnl_addr_get_label (struct rtnl_addr *addr)
 
void rtnl_addr_set_ifindex (struct rtnl_addr *addr, int ifindex)
 
int rtnl_addr_get_ifindex (struct rtnl_addr *addr)
 
void rtnl_addr_set_family (struct rtnl_addr *addr, int family)
 
int rtnl_addr_get_family (struct rtnl_addr *addr)
 
void rtnl_addr_set_prefixlen (struct rtnl_addr *addr, int prefix)
 
int rtnl_addr_get_prefixlen (struct rtnl_addr *addr)
 
void rtnl_addr_set_scope (struct rtnl_addr *addr, int scope)
 
int rtnl_addr_get_scope (struct rtnl_addr *addr)
 
void rtnl_addr_set_flags (struct rtnl_addr *addr, unsigned int flags)
 
void rtnl_addr_unset_flags (struct rtnl_addr *addr, unsigned int flags)
 
unsigned int rtnl_addr_get_flags (struct rtnl_addr *addr)
 
int rtnl_addr_set_local (struct rtnl_addr *addr, struct nl_addr *local)
 
struct nl_addr * rtnl_addr_get_local (struct rtnl_addr *addr)
 
int rtnl_addr_set_peer (struct rtnl_addr *addr, struct nl_addr *peer)
 
struct nl_addr * rtnl_addr_get_peer (struct rtnl_addr *addr)
 
int rtnl_addr_set_broadcast (struct rtnl_addr *addr, struct nl_addr *bcast)
 
struct nl_addr * rtnl_addr_get_broadcast (struct rtnl_addr *addr)
 
int rtnl_addr_set_anycast (struct rtnl_addr *addr, struct nl_addr *anycast)
 
struct nl_addr * rtnl_addr_get_anycast (struct rtnl_addr *addr)
 
int rtnl_addr_set_multicast (struct rtnl_addr *addr, struct nl_addr *multicast)
 
struct nl_addr * rtnl_addr_get_multicast (struct rtnl_addr *addr)
 

Detailed Description

Note
The maximum size of an address label is IFNAMSIZ.
The address may not contain a prefix length if the peer address has been specified already.
1) Address Addition
// Allocate an empty address object to be filled out with the attributes
// of the new address.
struct rtnl_addr *addr = rtnl_addr_alloc();
// Fill out the mandatory attributes of the new address. Setting the
// local address will automatically set the address family and the
// prefix length to the correct values.
rtnl_addr_set_ifindex(addr, ifindex);
rtnl_addr_set_local(addr, local_addr);
// The label of the address can be specified, currently only supported
// by IPv4 and DECnet.
rtnl_addr_set_label(addr, "mylabel");
// The peer address can be specified if necessary, in either case a peer
// address will be sent to the kernel in order to fullfil the interface
// requirements. If none is set, it will equal the local address.
// Note: Real peer addresses are only supported by IPv4 for now.
rtnl_addr_set_peer(addr, peer_addr);
// In case you want to have the address have a scope other than global
// it may be overwritten using rtnl_addr_set_scope(). The scope currently
// cannot be set for IPv6 addresses.
rtnl_addr_set_scope(addr, rtnl_str2scope("site"));
// Broadcast and anycast address may be specified using the relevant
// functions, the address family will be verified if one of the other
// addresses has been set already. Currently only works for IPv4.
rtnl_addr_set_broadcast(addr, broadcast_addr);
rtnl_addr_set_anycast(addr, anycast_addr);
// Build the netlink message and send it to the kernel, the operation will
// block until the operation has been completed. Alternatively the required
// netlink message can be built using rtnl_addr_build_add_request() to be
// sent out using nl_send_auto_complete().
rtnl_addr_add(handle, addr, 0);
// Free the memory
rtnl_addr_put(addr);
int rtnl_addr_add(struct nl_handle *handle, struct rtnl_addr *addr, int flags)
Request addition of new address.
Definition: addr.c:765
2) Address Deletion
// Allocate an empty address object to be filled out with the attributes
// matching the address to be deleted. Alternatively a fully equipped
// address object out of a cache can be used instead.
struct rtnl_addr *addr = rtnl_addr_alloc();
// The only mandatory parameter besides the address family is the interface
// index the address is on, i.e. leaving out all other parameters will
// result in all addresses of the specified address family interface tuple
// to be deleted.
rtnl_addr_set_ifindex(addr, ifindex);
// Specyfing the address family manually is only required if neither the
// local nor peer address have been specified.
rtnl_addr_set_family(addr, AF_INET);
// Specyfing the local address is optional but the best choice to delete
// specific addresses.
rtnl_addr_set_local(addr, local_addr);
// The label of the address can be specified, currently only supported
// by IPv4 and DECnet.
rtnl_addr_set_label(addr, "mylabel");
// The peer address can be specified if necessary, in either case a peer
// address will be sent to the kernel in order to fullfil the interface
// requirements. If none is set, it will equal the local address.
// Note: Real peer addresses are only supported by IPv4 for now.
rtnl_addr_set_peer(addr, peer_addr);
// Build the netlink message and send it to the kernel, the operation will
// block until the operation has been completed. Alternatively the required
// netlink message can be built using rtnl_addr_build_delete_request()
// to be sent out using nl_send_auto_complete().
rtnl_addr_delete(handle, addr, 0);
// Free the memory
rtnl_addr_put(addr);
int rtnl_addr_delete(struct nl_handle *handle, struct rtnl_addr *addr, int flags)
Request deletion of an address.
Definition: addr.c:838

Function Documentation

◆ rtnl_addr_alloc()

struct rtnl_addr * rtnl_addr_alloc ( void  )

Definition at line 625 of file addr.c.

626{
627 return (struct rtnl_addr *) nl_object_alloc(&addr_obj_ops);
628}
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition: object.c:42

◆ rtnl_addr_put()

void rtnl_addr_put ( struct rtnl_addr *  addr)

Definition at line 630 of file addr.c.

631{
632 nl_object_put((struct nl_object *) addr);
633}
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:178

◆ rtnl_addr_alloc_cache()

struct nl_cache * rtnl_addr_alloc_cache ( struct nl_handle *  handle)

Definition at line 642 of file addr.c.

643{
644 struct nl_cache *cache;
645
646 cache = nl_cache_alloc(&rtnl_addr_ops);
647 if (!cache)
648 return NULL;
649
650 if (handle && nl_cache_refill(handle, cache) < 0) {
651 nl_cache_free(cache);
652 return NULL;
653 }
654
655 return cache;
656}
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:277
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate an empty cache.
Definition: cache.c:170
int nl_cache_refill(struct nl_handle *handle, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
Definition: cache.c:680

◆ rtnl_addr_build_add_request()

struct nl_msg * rtnl_addr_build_add_request ( struct rtnl_addr *  addr,
int  flags 
)
Parameters
addrAddress object representing the new address.
flagsAdditional netlink message flags.

Builds a new netlink message requesting the addition of a new address. The netlink message header isn't fully equipped with all relevant fields and must thus be sent out via nl_send_auto_complete() or supplemented as needed.

Minimal required attributes:

  • interface index (rtnl_addr_set_ifindex())
  • local address (rtnl_addr_set_local())

The scope will default to universe except for loopback addresses in which case a host scope is used if not specified otherwise.

Note
Free the memory after usage using nlmsg_free().
Returns
Newly allocated netlink message or NULL if an error occured.

Definition at line 737 of file addr.c.

738{
739 int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY |
740 ADDR_ATTR_PREFIXLEN | ADDR_ATTR_LOCAL;
741
742 if ((addr->ce_mask & required) != required) {
743 nl_error(EINVAL, "Missing mandatory attributes, required are: "
744 "ifindex, family, prefixlen, local address.");
745 return NULL;
746 }
747
748 return build_addr_msg(addr, RTM_NEWADDR, NLM_F_CREATE | flags);
749}
#define NLM_F_CREATE
Create config object if it doesn't already exist.

References NLM_F_CREATE.

Referenced by rtnl_addr_add().

◆ rtnl_addr_add()

int rtnl_addr_add ( struct nl_handle *  handle,
struct rtnl_addr *  addr,
int  flags 
)
Parameters
handleNetlink handle.
addrAddress object representing the new address.
flagsAdditional netlink message flags.

Builds a netlink message by calling rtnl_addr_build_add_request(), sends the request to the kernel and waits for the next ACK to be received and thus blocks until the request has been fullfilled.

See also
rtnl_addr_build_add_request()
Returns
0 on sucess or a negative error if an error occured.

Definition at line 765 of file addr.c.

766{
767 struct nl_msg *msg;
768 int err;
769
770 msg = rtnl_addr_build_add_request(addr, flags);
771 if (!msg)
772 return nl_get_errno();
773
774 err = nl_send_auto_complete(handle, msg);
775 nlmsg_free(msg);
776 if (err < 0)
777 return err;
778
779 return nl_wait_for_ack(handle);
780}
void nlmsg_free(struct nl_msg *n)
Free a netlink message.
Definition: msg.c:656
int nl_send_auto_complete(struct nl_handle *handle, struct nl_msg *msg)
Send netlink message and check & extend header values as needed.
Definition: nl.c:373
int nl_wait_for_ack(struct nl_handle *handle)
Wait for ACK.
Definition: nl.c:801
struct nl_msg * rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags)
Build netlink request message to request addition of new address.
Definition: addr.c:737

References nl_send_auto_complete(), nl_wait_for_ack(), nlmsg_free(), and rtnl_addr_build_add_request().

◆ rtnl_addr_build_delete_request()

struct nl_msg * rtnl_addr_build_delete_request ( struct rtnl_addr *  addr,
int  flags 
)
Parameters
addrAddress object to be deleteted.
flagsAdditional netlink message flags.

Builds a new netlink message requesting a deletion of an address. The netlink message header isn't fully equipped with all relevant fields and must thus be sent out via nl_send_auto_complete() or supplemented as needed.

Minimal required attributes:

  • interface index (rtnl_addr_set_ifindex())
  • address family (rtnl_addr_set_family())

Optional attributes:

  • local address (rtnl_addr_set_local())
  • label (rtnl_addr_set_label(), IPv4/DECnet only)
  • peer address (rtnl_addr_set_peer(), IPv4 only)
Note
Free the memory after usage using nlmsg_free().
Returns
Newly allocated netlink message or NULL if an error occured.

Definition at line 811 of file addr.c.

812{
813 int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY;
814
815 if ((addr->ce_mask & required) != required) {
816 nl_error(EINVAL, "Missing mandatory attributes, required are: "
817 "ifindex, family");
818 return NULL;
819 }
820
821 return build_addr_msg(addr, RTM_DELADDR, flags);
822}

Referenced by rtnl_addr_delete().

◆ rtnl_addr_delete()

int rtnl_addr_delete ( struct nl_handle *  handle,
struct rtnl_addr *  addr,
int  flags 
)
Parameters
handleNetlink handle.
addrAddress object to be deleted.
flagsAdditional netlink message flags.

Builds a netlink message by calling rtnl_addr_build_delete_request(), sends the request to the kernel and waits for the next ACK to be received and thus blocks until the request has been fullfilled.

See also
rtnl_addr_build_delete_request();
Returns
0 on sucess or a negative error if an error occured.

Definition at line 838 of file addr.c.

840{
841 struct nl_msg *msg;
842 int err;
843
844 msg = rtnl_addr_build_delete_request(addr, flags);
845 if (!msg)
846 return nl_get_errno();
847
848 err = nl_send_auto_complete(handle, msg);
849 nlmsg_free(msg);
850 if (err < 0)
851 return err;
852
853 return nl_wait_for_ack(handle);
854}
struct nl_msg * rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags)
Build a netlink request message to request deletion of an address.
Definition: addr.c:811

References nl_send_auto_complete(), nl_wait_for_ack(), nlmsg_free(), and rtnl_addr_build_delete_request().

◆ rtnl_addr_set_label()

void rtnl_addr_set_label ( struct rtnl_addr *  addr,
const char *  label 
)

Definition at line 863 of file addr.c.

864{
865 strncpy(addr->a_label, label, sizeof(addr->a_label) - 1);
866 addr->ce_mask |= ADDR_ATTR_LABEL;
867}

◆ rtnl_addr_get_label()

char * rtnl_addr_get_label ( struct rtnl_addr *  addr)

Definition at line 869 of file addr.c.

870{
871 if (addr->ce_mask & ADDR_ATTR_LABEL)
872 return addr->a_label;
873 else
874 return NULL;
875}

◆ rtnl_addr_set_ifindex()

void rtnl_addr_set_ifindex ( struct rtnl_addr *  addr,
int  ifindex 
)

Definition at line 877 of file addr.c.

878{
879 addr->a_ifindex = ifindex;
880 addr->ce_mask |= ADDR_ATTR_IFINDEX;
881}

◆ rtnl_addr_get_ifindex()

int rtnl_addr_get_ifindex ( struct rtnl_addr *  addr)

Definition at line 883 of file addr.c.

884{
885 if (addr->ce_mask & ADDR_ATTR_IFINDEX)
886 return addr->a_ifindex;
887 else
888 return RTNL_LINK_NOT_FOUND;
889}

◆ rtnl_addr_set_family()

void rtnl_addr_set_family ( struct rtnl_addr *  addr,
int  family 
)

Definition at line 891 of file addr.c.

892{
893 addr->a_family = family;
894 addr->ce_mask |= ADDR_ATTR_FAMILY;
895}

◆ rtnl_addr_get_family()

int rtnl_addr_get_family ( struct rtnl_addr *  addr)

Definition at line 897 of file addr.c.

898{
899 if (addr->ce_mask & ADDR_ATTR_FAMILY)
900 return addr->a_family;
901 else
902 return AF_UNSPEC;
903}

◆ rtnl_addr_set_prefixlen()

void rtnl_addr_set_prefixlen ( struct rtnl_addr *  addr,
int  prefix 
)

Definition at line 905 of file addr.c.

906{
907 addr->a_prefixlen = prefix;
908 addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
909}

◆ rtnl_addr_get_prefixlen()

int rtnl_addr_get_prefixlen ( struct rtnl_addr *  addr)

Definition at line 911 of file addr.c.

912{
913 if (addr->ce_mask & ADDR_ATTR_PREFIXLEN)
914 return addr->a_prefixlen;
915 else
916 return -1;
917}

◆ rtnl_addr_set_scope()

void rtnl_addr_set_scope ( struct rtnl_addr *  addr,
int  scope 
)

Definition at line 919 of file addr.c.

920{
921 addr->a_scope = scope;
922 addr->ce_mask |= ADDR_ATTR_SCOPE;
923}

◆ rtnl_addr_get_scope()

int rtnl_addr_get_scope ( struct rtnl_addr *  addr)

Definition at line 925 of file addr.c.

926{
927 if (addr->ce_mask & ADDR_ATTR_SCOPE)
928 return addr->a_scope;
929 else
930 return -1;
931}

◆ rtnl_addr_set_flags()

void rtnl_addr_set_flags ( struct rtnl_addr *  addr,
unsigned int  flags 
)

Definition at line 933 of file addr.c.

934{
935 addr->a_flag_mask |= flags;
936 addr->a_flags |= flags;
937 addr->ce_mask |= ADDR_ATTR_FLAGS;
938}

◆ rtnl_addr_unset_flags()

void rtnl_addr_unset_flags ( struct rtnl_addr *  addr,
unsigned int  flags 
)

Definition at line 940 of file addr.c.

941{
942 addr->a_flag_mask |= flags;
943 addr->a_flags &= ~flags;
944 addr->ce_mask |= ADDR_ATTR_FLAGS;
945}

◆ rtnl_addr_get_flags()

unsigned int rtnl_addr_get_flags ( struct rtnl_addr *  addr)

Definition at line 947 of file addr.c.

948{
949 return addr->a_flags;
950}

◆ rtnl_addr_set_local()

int rtnl_addr_set_local ( struct rtnl_addr *  addr,
struct nl_addr *  local 
)

Definition at line 970 of file addr.c.

971{
972 int err;
973
974 err = __assign_addr(addr, &addr->a_local, local, ADDR_ATTR_LOCAL);
975 if (err < 0)
976 return err;
977
978 if (!(addr->ce_mask & ADDR_ATTR_PEER)) {
979 addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_local);
980 addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
981 }
982
983 return 0;
984}
unsigned int nl_addr_get_prefixlen(struct nl_addr *addr)
Get prefix length of abstract address object.
Definition: addr.c:779

◆ rtnl_addr_get_local()

struct nl_addr * rtnl_addr_get_local ( struct rtnl_addr *  addr)

Definition at line 986 of file addr.c.

987{
988 if (addr->ce_mask & ADDR_ATTR_LOCAL)
989 return addr->a_local;
990 else
991 return NULL;
992}

◆ rtnl_addr_set_peer()

int rtnl_addr_set_peer ( struct rtnl_addr *  addr,
struct nl_addr *  peer 
)

Definition at line 994 of file addr.c.

995{
996 return __assign_addr(addr, &addr->a_peer, peer, ADDR_ATTR_PEER);
997
998 addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_peer);
999 addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
1000
1001 return 0;
1002}

◆ rtnl_addr_get_peer()

struct nl_addr * rtnl_addr_get_peer ( struct rtnl_addr *  addr)

Definition at line 1004 of file addr.c.

1005{
1006 if (addr->ce_mask & ADDR_ATTR_PEER)
1007 return addr->a_peer;
1008 else
1009 return NULL;
1010}

◆ rtnl_addr_set_broadcast()

int rtnl_addr_set_broadcast ( struct rtnl_addr *  addr,
struct nl_addr *  bcast 
)

Definition at line 1012 of file addr.c.

1013{
1014 return __assign_addr(addr, &addr->a_bcast, bcast, ADDR_ATTR_BROADCAST);
1015}

◆ rtnl_addr_get_broadcast()

struct nl_addr * rtnl_addr_get_broadcast ( struct rtnl_addr *  addr)

Definition at line 1017 of file addr.c.

1018{
1019 if (addr->ce_mask & ADDR_ATTR_BROADCAST)
1020 return addr->a_bcast;
1021 else
1022 return NULL;
1023}

◆ rtnl_addr_set_anycast()

int rtnl_addr_set_anycast ( struct rtnl_addr *  addr,
struct nl_addr *  anycast 
)

Definition at line 1025 of file addr.c.

1026{
1027 return __assign_addr(addr, &addr->a_anycast, anycast,
1028 ADDR_ATTR_ANYCAST);
1029}

◆ rtnl_addr_get_anycast()

struct nl_addr * rtnl_addr_get_anycast ( struct rtnl_addr *  addr)

Definition at line 1031 of file addr.c.

1032{
1033 if (addr->ce_mask & ADDR_ATTR_ANYCAST)
1034 return addr->a_anycast;
1035 else
1036 return NULL;
1037}

◆ rtnl_addr_set_multicast()

int rtnl_addr_set_multicast ( struct rtnl_addr *  addr,
struct nl_addr *  multicast 
)

Definition at line 1039 of file addr.c.

1040{
1041 return __assign_addr(addr, &addr->a_multicast, multicast,
1042 ADDR_ATTR_MULTICAST);
1043}

◆ rtnl_addr_get_multicast()

struct nl_addr * rtnl_addr_get_multicast ( struct rtnl_addr *  addr)

Definition at line 1045 of file addr.c.

1046{
1047 if (addr->ce_mask & ADDR_ATTR_MULTICAST)
1048 return addr->a_multicast;
1049 else
1050 return NULL;
1051}

◆ rtnl_addr_flags2str()

char * rtnl_addr_flags2str ( int  flags,
char *  buf,
size_t  size 
)

Definition at line 1067 of file addr.c.

1068{
1069 return __flags2str(flags, buf, size, addr_flags,
1070 ARRAY_SIZE(addr_flags));
1071}

◆ rtnl_addr_str2flags()

int rtnl_addr_str2flags ( const char *  name)

Definition at line 1073 of file addr.c.

1074{
1075 return __str2flags(name, addr_flags, ARRAY_SIZE(addr_flags));
1076}