Logo Search packages:      
Sourcecode: libnl2 version File versions  Download package

Generic Netlink


Detailed Description

Message Format
  <------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
 +----------------------------+- - -+- - - - - - - - - - -+- - -+
 |           Header           | Pad |       Payload       | Pad |
 |      struct nlmsghdr       |     |                     |     |
 +----------------------------+- - -+- - - - - - - - - - -+- - -+
  <-------- GENL_HDRLEN -------> <--- hdrlen -->
                                 <------- genlmsg_len(ghdr) ------>
 +------------------------+- - -+---------------+- - -+------------+
 | Generic Netlink Header | Pad | Family Header | Pad | Attributes |
 |    struct genlmsghdr   |     |               |     |            |
 +------------------------+- - -+---------------+- - -+------------+
 genlmsg_data(ghdr)--------------^                     ^
 genlmsg_attrdata(ghdr, hdrlen)-------------------------
Example
 #include <netlink/netlink.h>
 #include <netlink/genl/genl.h>
 #include <netlink/genl/ctrl.h>

 struct nl_sock *sock;
 struct nl_msg *msg;
 int family;

 // Allocate a new netlink socket
 sock = nl_socket_alloc();

 // Connect to generic netlink socket on kernel side
 genl_connect(sock);

 // Ask kernel to resolve family name to family id
 family = genl_ctrl_resolve(sock, "generic_netlink_family_name");

 // Construct a generic netlink by allocating a new message, fill in
 // the header and append a simple integer attribute.
 msg = nlmsg_alloc();
 genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_ECHO,
             CMD_FOO_GET, FOO_VERSION);
 nla_put_u32(msg, ATTR_FOO, 123);

 // Send message over netlink socket
 nl_send_auto_complete(sock, msg);

 // Free message
 nlmsg_free(msg);

 // Prepare socket to receive the answer by specifying the callback
 // function to be called for valid messages.
 nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, parse_cb, NULL);

 // Wait for the answer and receive it
 nl_recvmsgs_default(sock);

 static int parse_cb(struct nl_msg *msg, void *arg)
 {
     struct nlmsghdr *nlh = nlmsg_hdr(msg);
     struct nlattr *attrs[ATTR_MAX+1];

     // Validate message and parse attributes
     genlmsg_parse(nlh, 0, attrs, ATTR_MAX, policy);

     if (attrs[ATTR_FOO]) {
         uint32_t value = nla_get_u32(attrs[ATTR_FOO]);
         ...
     }

     return 0;
 }


Modules

 Generic Netlink Family
 Management

Socket Creating

int genl_connect (struct nl_sock *sk)

Sending

int genl_send_simple (struct nl_sock *sk, int family, int cmd, int version, int flags)

Message Parsing

struct nlattr * genlmsg_attrdata (const struct genlmsghdr *gnlh, int hdrlen)
int genlmsg_attrlen (const struct genlmsghdr *gnlh, int hdrlen)
void * genlmsg_data (const struct genlmsghdr *gnlh)
int genlmsg_len (const struct genlmsghdr *gnlh)
int genlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
int genlmsg_valid_hdr (struct nlmsghdr *nlh, int hdrlen)
int genlmsg_validate (struct nlmsghdr *nlh, int hdrlen, int maxtype, struct nla_policy *policy)

Message Building

void * genlmsg_put (struct nl_msg *msg, uint32_t pid, uint32_t seq, int family, int hdrlen, int flags, uint8_t cmd, uint8_t version)


Generated by  Doxygen 1.6.0   Back to index