/build/buildd/libnl-1.0~pre6/lib/msg.c

00001 /*
00002  * lib/msg.c            Netlink Messages Interface
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 /**
00013  * @ingroup nl
00014  * @defgroup msg Messages
00015  * Netlink Message Construction/Parsing Interface
00016  * 
00017  * The following information is partly extracted from RFC3549
00018  * (ftp://ftp.rfc-editor.org/in-notes/rfc3549.txt)
00019  *
00020  * @par Message Format
00021  * Netlink messages consist of a byte stream with one or multiple
00022  * Netlink headers and an associated payload.  If the payload is too big
00023  * to fit into a single message it, can be split over multiple Netlink
00024  * messages, collectively called a multipart message.  For multipart
00025  * messages, the first and all following headers have the \c NLM_F_MULTI
00026  * Netlink header flag set, except for the last header which has the
00027  * Netlink header type \c NLMSG_DONE.
00028  *
00029  * @par
00030  * The Netlink message header (\link nlmsghdr struct nlmsghdr\endlink) is shown below.
00031  * @code   
00032  * 0                   1                   2                   3
00033  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
00034  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00035  * |                          Length                             |
00036  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00037  * |            Type              |           Flags              |
00038  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00039  * |                      Sequence Number                        |
00040  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00041  * |                      Process ID (PID)                       |
00042  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00043  * @endcode
00044  *
00045  * @par
00046  * The netlink message header and payload must be aligned properly:
00047  * @code
00048  *  <------- NLMSG_ALIGN(hlen) ------> <---- NLMSG_ALIGN(len) --->
00049  * +----------------------------+- - -+- - - - - - - - - - -+- - -+
00050  * |           Header           | Pad |       Payload       | Pad |
00051  * |      struct nlmsghdr       |     |                     |     |
00052  * +----------------------------+- - -+- - - - - - - - - - -+- - -+
00053  * @endcode
00054  * @par
00055  * Message Format:
00056  * @code
00057  *    <--- nlmsg_total_size(payload)  --->
00058  *    <-- nlmsg_msg_size(payload) ->
00059  *   +----------+- - -+-------------+- - -+-------- - -
00060  *   | nlmsghdr | Pad |   Payload   | Pad | nlmsghdr
00061  *   +----------+- - -+-------------+- - -+-------- - -
00062  *   nlmsg_data(nlh)---^                   ^
00063  *   nlmsg_next(nlh)-----------------------+
00064  * @endcode
00065  * @par
00066  * The payload may consist of arbitary data but may have strict
00067  * alignment and formatting rules depening on the specific netlink
00068  * families.
00069  * @par
00070  * @code
00071  *    <---------------------- nlmsg_len(nlh) --------------------->
00072  *    <------ hdrlen ------>       <- nlmsg_attrlen(nlh, hdrlen) ->
00073  *   +----------------------+- - -+--------------------------------+
00074  *   |     Family Header    | Pad |           Attributes           |
00075  *   +----------------------+- - -+--------------------------------+
00076  *   nlmsg_attrdata(nlh, hdrlen)---^
00077  * @endcode
00078  * @par The ACK Netlink Message
00079  * This message is actually used to denote both an ACK and a NACK.
00080  * Typically, the direction is from FEC to CPC (in response to an ACK
00081  * request message).  However, the CPC should be able to send ACKs back
00082  * to FEC when requested.
00083  * @code
00084  *  0                   1                   2                   3
00085  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
00086  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00087  * |                       Netlink message header                  |
00088  * |                       type = NLMSG_ERROR                      |
00089  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00090  * |                          Error code                           |
00091  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00092  * |                       OLD Netlink message header              |
00093  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
00094  * @endcode
00095  *
00096  * @par 1) Creating a new netlink message
00097  * @code
00098  * // The most common way to start creating a message is by providing an
00099  * // defined netlink header to nlmsg_build():
00100  * struct nlmsghdr hdr = {
00101  *      .nlmsg_type = MY_TYPE,
00102  *      .nlmsg_flags = MY_FLAGS,
00103  * };
00104  * struct nl_msg *msg = nlmsg_build(&hdr);
00105  *
00106  * // For simple usages where only the message type and flags is of
00107  * // interenst a shortcut can be taken:
00108  * struct nl_msg *msg = nlmsg_build_simple(MY_TYPE, MY_FLAGS);
00109  *
00110  * // When using a headerless message for creating nested attributes
00111  * // the header is not required and nlmsg_build_no_hdr() may be used:
00112  * struct nl_msg *msg = nlmsg_build_no_hdr();
00113  *
00114  * // The header can later be retrieved with nlmsg_hdr() and changed again:
00115  * nlmsg_hdr(msg)->nlmsg_flags |= YET_ANOTHER_FLAG;
00116  * @endcode
00117  *
00118  * @par 2) Appending data to the message
00119  * @code
00120  * // Payload may be added to the message via nlmsg_append(). The fourth
00121  * // parameter specifies whether to pad up to NLMSG_ALIGN to make sure
00122  * // that a possible further data block is properly aligned.
00123  * nlmsg_append(msg, &mydata, sizeof(mydata), 0);
00124  * @endcode
00125  *
00126  * @par 3) Cleaning up message construction
00127  * @code
00128  * // After successful use of the message, the memory must be freed
00129  * // using nlmsg_free()
00130  * nlmsg_free(msg);
00131  * @endcode
00132  * 
00133  * @par Example 2 (Parsing messages):
00134  * @code
00135  * int n;
00136  * unsigned char *buf;
00137  * struct nlmsghdr *hdr;
00138  *
00139  * n = nl_recv(handle, NULL, &buf);
00140  * 
00141  * hdr = (struct nlmsghdr *) buf;
00142  * while (nlmsg_ok(hdr, n)) {
00143  *      // Process message here...
00144  *      hdr = nlmsg_next(hdr, &n);
00145  * }
00146  * @endcode
00147  * @{
00148  */
00149 
00150 #include <netlink-local.h>
00151 #include <netlink/netlink.h>
00152 #include <netlink/utils.h>
00153 #include <netlink/cache.h>
00154 #include <netlink/attr.h>
00155 #include <linux/socket.h>
00156 
00157 /**
00158  * @name Size Calculations
00159  * @{
00160  */
00161 
00162 /**
00163  * length of netlink message not including padding
00164  * @arg payload         length of message payload
00165  */
00166 int nlmsg_msg_size(int payload)
00167 {
00168         return NLMSG_HDRLEN + payload;
00169 }
00170 
00171 /**
00172  * length of netlink message including padding
00173  * @arg payload         length of message payload
00174  */
00175 int nlmsg_total_size(int payload)
00176 {
00177         return NLMSG_ALIGN(nlmsg_msg_size(payload));
00178 }
00179 
00180 /**
00181  * length of padding at the message's tail
00182  * @arg payload         length of message payload
00183  */
00184 int nlmsg_padlen(int payload)
00185 {
00186         return nlmsg_total_size(payload) - nlmsg_msg_size(payload);
00187 }
00188 
00189 /** @} */
00190 
00191 /**
00192  * @name Payload Access
00193  * @{
00194  */
00195 
00196 /**
00197  * head of message payload
00198  * @arg nlh             netlink messsage header
00199  */
00200 void *nlmsg_data(const struct nlmsghdr *nlh)
00201 {
00202         return (unsigned char *) nlh + NLMSG_HDRLEN;
00203 }
00204 
00205 void *nlmsg_tail(const struct nlmsghdr *nlh)
00206 {
00207         return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len);
00208 }
00209 
00210 /**
00211  * length of message payload
00212  * @arg nlh             netlink message header
00213  */
00214 int nlmsg_len(const struct nlmsghdr *nlh)
00215 {
00216         return nlh->nlmsg_len - NLMSG_HDRLEN;
00217 }
00218 
00219 /** @} */
00220 
00221 /**
00222  * @name Attribute Access
00223  * @{
00224  */
00225 
00226 /**
00227  * head of attributes data
00228  * @arg nlh             netlink message header
00229  * @arg hdrlen          length of family specific header
00230  */
00231 struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen)
00232 {
00233         unsigned char *data = nlmsg_data(nlh);
00234         return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen));
00235 }
00236 
00237 /**
00238  * length of attributes data
00239  * @arg nlh             netlink message header
00240  * @arg hdrlen          length of family specific header
00241  */
00242 int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen)
00243 {
00244         return nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen);
00245 }
00246 
00247 /** @} */
00248 
00249 /**
00250  * @name Message Parsing
00251  * @{
00252  */
00253 
00254 /**
00255  * check if the netlink message fits into the remaining bytes
00256  * @arg nlh             netlink message header
00257  * @arg remaining       number of bytes remaining in message stream
00258  */
00259 int nlmsg_ok(const struct nlmsghdr *nlh, int remaining)
00260 {
00261         return (remaining >= sizeof(struct nlmsghdr) &&
00262                 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
00263                 nlh->nlmsg_len <= remaining);
00264 }
00265 
00266 /**
00267  * next netlink message in message stream
00268  * @arg nlh             netlink message header
00269  * @arg remaining       number of bytes remaining in message stream
00270  *
00271  * @returns the next netlink message in the message stream and
00272  * decrements remaining by the size of the current message.
00273  */
00274 struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining)
00275 {
00276         int totlen = NLMSG_ALIGN(nlh->nlmsg_len);
00277 
00278         *remaining -= totlen;
00279 
00280         return (struct nlmsghdr *) ((unsigned char *) nlh + totlen);
00281 }
00282 
00283 /**
00284  * parse attributes of a netlink message
00285  * @arg nlh             netlink message header
00286  * @arg hdrlen          length of family specific header
00287  * @arg tb              destination array with maxtype+1 elements
00288  * @arg maxtype         maximum attribute type to be expected
00289  * @arg policy          validation policy
00290  *
00291  * See nla_parse()
00292  */
00293 int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
00294                 int maxtype, struct nla_policy *policy)
00295 {
00296         if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
00297                 return nl_errno(EINVAL);
00298 
00299         return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen),
00300                          nlmsg_attrlen(nlh, hdrlen), policy);
00301 }
00302 
00303 /**
00304  * nlmsg_find_attr - find a specific attribute in a netlink message
00305  * @arg nlh             netlink message header
00306  * @arg hdrlen          length of familiy specific header
00307  * @arg attrtype        type of attribute to look for
00308  *
00309  * Returns the first attribute which matches the specified type.
00310  */
00311 struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
00312 {
00313         return nla_find(nlmsg_attrdata(nlh, hdrlen),
00314                         nlmsg_attrlen(nlh, hdrlen), attrtype);
00315 }
00316 
00317 /**
00318  * nlmsg_validate - validate a netlink message including attributes
00319  * @arg nlh             netlinket message header
00320  * @arg hdrlen          length of familiy specific header
00321  * @arg maxtype         maximum attribute type to be expected
00322  * @arg policy          validation policy
00323  */
00324 int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype,
00325                    struct nla_policy *policy)
00326 {
00327         if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
00328                 return nl_errno(EINVAL);
00329 
00330         return nla_validate(nlmsg_attrdata(nlh, hdrlen),
00331                             nlmsg_attrlen(nlh, hdrlen), maxtype, policy);
00332 }
00333 
00334 /** @} */
00335 
00336 /**
00337  * @name Message Building/Access
00338  * @{
00339  */
00340 
00341 struct nl_msg *nlmsg_new(void)
00342 {
00343         struct nl_msg *nm;
00344 
00345         nm = calloc(1, sizeof(*nm));
00346         if (!nm)
00347                 goto errout;
00348 
00349         nm->nm_nlh = calloc(1, nlmsg_msg_size(0));
00350         if (!nm->nm_nlh)
00351                 goto errout;
00352 
00353         nm->nm_nlh->nlmsg_len = nlmsg_msg_size(0);
00354         return nm;
00355 errout:
00356         free(nm);
00357         nl_errno(ENOMEM);
00358         return NULL;
00359 }
00360 
00361 
00362 /**
00363  * Build a new netlink message
00364  * @arg hdr             Netlink message header template
00365  *
00366  * Builds a new netlink message with a tailroom for the netlink
00367  * message header. If \a hdr is not NULL it will be used as a
00368  * template for the netlink message header, otherwise the header
00369  * is left blank.
00370  * 
00371  * @return Newly allocated netlink message or NULL
00372  */ 
00373 struct nl_msg *nlmsg_build(struct nlmsghdr *hdr)
00374 {
00375         struct nl_msg *nm;
00376 
00377         nm = nlmsg_new();
00378         if (nm && hdr) {
00379                 int size = nm->nm_nlh->nlmsg_len;
00380                 memcpy(nm->nm_nlh, hdr, sizeof(*hdr));
00381                 nm->nm_nlh->nlmsg_len = size;
00382         }
00383 
00384         return nm;
00385 }
00386 
00387 struct nl_msg *nlmsg_build_simple(int nlmsgtype, int flags)
00388 {
00389         struct nlmsghdr nlh = {
00390                 .nlmsg_type = nlmsgtype,
00391                 .nlmsg_flags = flags,
00392         };
00393 
00394         return nlmsg_build(&nlh);
00395 }
00396 
00397 struct nl_msg *nlmsg_build_no_hdr(void)
00398 {
00399         return nlmsg_build(NULL);
00400 }
00401 
00402 struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr)
00403 {
00404         struct nl_msg *nm;
00405 
00406         nm = calloc(1, sizeof(struct nl_msg));
00407         if (!nm)
00408                 goto errout;
00409 
00410         nm->nm_nlh = calloc(1, NLMSG_ALIGN(hdr->nlmsg_len));
00411         if (!nm->nm_nlh)
00412                 goto errout;
00413 
00414         memcpy(nm->nm_nlh, hdr, NLMSG_ALIGN(hdr->nlmsg_len));
00415 
00416         return nm;
00417 errout:
00418         free(nm);
00419         nl_errno(ENOMEM);
00420         return NULL;
00421 }
00422 
00423 /**
00424  * Append raw data to a netlink message
00425  * @arg n               netlink message
00426  * @arg data            data to add
00427  * @arg len             length of data
00428  * @arg pad             add padding at the end?
00429  *
00430  * Extends the netlink message as needed and appends the data of given
00431  * length to the message. The length of the message is not aligned to
00432  * anything. The caller is responsible to provide a length and
00433  * evtentually padded data to fullfil any alignment requirements.
00434  *
00435  * @return 0 on success or a negative error code
00436  * @attention Appending of improperly aligned raw data may result in
00437  *            a corrupt message. It is left to you to add the right
00438  *            amount of data to have the message aligned to NLMSG_ALIGNTO
00439  *            in the end.
00440  */
00441 int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
00442 {
00443         void *tmp;
00444 
00445         if (pad)
00446                 len = NLMSG_ALIGN(len);
00447 
00448         tmp = realloc(n->nm_nlh, n->nm_nlh->nlmsg_len + len);
00449         if (!tmp)
00450                 return nl_errno(ENOMEM);
00451 
00452         n->nm_nlh = tmp;
00453         memcpy((void *) n->nm_nlh + n->nm_nlh->nlmsg_len, data, len);
00454         n->nm_nlh->nlmsg_len += len;
00455 
00456         return 0;
00457 }
00458 
00459 
00460 /**
00461  * nlmsg_put - Add a netlink message header
00462  * @arg n               netlink message
00463  * @arg pid             netlink process id
00464  * @arg seq             sequence number of message
00465  * @arg type            message type
00466  * @arg payload         length of message payload
00467  * @arg flags           message flags
00468  */
00469 struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq,
00470                            int type, int payload, int flags)
00471 {
00472         struct nlmsghdr *nlh;
00473 
00474         if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN)
00475                 BUG();
00476 
00477         nlh = (struct nlmsghdr *) n->nm_nlh;
00478         nlh->nlmsg_type = type;
00479         nlh->nlmsg_len = nlmsg_msg_size(payload);
00480         nlh->nlmsg_flags = flags;
00481         nlh->nlmsg_pid = pid;
00482         nlh->nlmsg_seq = seq;
00483 
00484         memset((unsigned char *) nlmsg_data(nlh) + payload, 0,
00485                nlmsg_padlen(payload));
00486 
00487         return nlh;
00488 }
00489 
00490 /**
00491  * Return actual netlink message
00492  * @arg n               netlink message
00493  * 
00494  * Returns the actual netlink message casted to the type of the netlink
00495  * message header.
00496  * 
00497  * @return A pointer to the netlink message.
00498  */
00499 struct nlmsghdr *nlmsg_hdr(struct nl_msg *n)
00500 {
00501         return n->nm_nlh;
00502 }
00503 
00504 /**
00505  * Free a netlink message
00506  * @arg n               netlink message
00507  *
00508  * Destroys a netlink message and frees up all used memory.
00509  *
00510  * @pre The message must be unused.
00511  */
00512 void nlmsg_free(struct nl_msg *n)
00513 {
00514         if (!n)
00515                 return;
00516 
00517         free(n->nm_nlh);
00518         free(n);
00519 }
00520 
00521 /** @} */
00522 
00523 /**
00524  * @name Attribute Modification
00525  * @{
00526  */
00527 
00528 void nlmsg_set_proto(struct nl_msg *msg, int protocol)
00529 {
00530         msg->nm_protocol = protocol;
00531 }
00532 
00533 int nlmsg_get_proto(struct nl_msg *msg)
00534 {
00535         return msg->nm_protocol;
00536 }
00537 
00538 void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr)
00539 {
00540         memcpy(&msg->nm_src, addr, sizeof(*addr));
00541 }
00542 
00543 struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg)
00544 {
00545         return &msg->nm_src;
00546 }
00547 
00548 void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr)
00549 {
00550         memcpy(&msg->nm_dst, addr, sizeof(*addr));
00551 }
00552 
00553 struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg)
00554 {
00555         return &msg->nm_dst;
00556 }
00557 
00558 void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds)
00559 {
00560         memcpy(&msg->nm_creds, creds, sizeof(*creds));
00561         msg->nm_flags |= NL_MSG_CRED_PRESENT;
00562 }
00563 
00564 struct ucred *nlmsg_get_creds(struct nl_msg *msg)
00565 {
00566         if (msg->nm_flags & NL_MSG_CRED_PRESENT)
00567                 return &msg->nm_creds;
00568         return NULL;
00569 }
00570 
00571 /** @} */
00572 
00573 /**
00574  * @name Netlink Message Type Translations
00575  * @{
00576  */
00577 
00578 static struct trans_tbl nl_msgtypes[] = {
00579         __ADD(NLMSG_NOOP,NOOP)
00580         __ADD(NLMSG_ERROR,ERROR)
00581         __ADD(NLMSG_DONE,DONE)
00582         __ADD(NLMSG_OVERRUN,OVERRUN)
00583 };
00584 
00585 /**
00586  * Convert netlink message type number to character string.
00587  * @arg type            Netlink message type.
00588  * @arg buf             Destination buffer.
00589  * @arg size            Size of destination buffer.
00590  *
00591  * Converts a netlink message type number to a character string and stores
00592  * it in the provided buffer.
00593  *
00594  * @return The destination buffer or the type encoded in hexidecimal form
00595  *         if no match was found.
00596  */
00597 char *nl_nlmsgtype2str(int type, char *buf, size_t size)
00598 {
00599         return __type2str(type, buf, size, nl_msgtypes,
00600                           ARRAY_SIZE(nl_msgtypes));
00601 }
00602 
00603 /**
00604  * Convert character string to netlink message type.
00605  * @arg name            Name of netlink message type.
00606  *
00607  * Converts the provided character string specifying a netlink message type
00608  * into the corresponding numeric value
00609  *
00610  * @return Numeric netlink message type or a negative value
00611  *         if no match was found.
00612  */
00613 int nl_str2nlmsgtype(const char *name)
00614 {
00615         return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes));
00616 }
00617 
00618 /** @} */
00619 
00620 /**
00621  * @name Netlink Message Flags Translations
00622  * @{
00623  */
00624 
00625 /**
00626  * Translate netlink message flags into a character string (Reentrant).
00627  * @arg flags           netlink message flags
00628  * @arg buf             destination buffer
00629  * @arg len             buffer length
00630  *
00631  * Translates netlink message flags into a character string and stores
00632  * it in the provided buffer.
00633  *
00634  * @return The destination buffer
00635  */
00636 char *nl_nlmsg_flags2str(int flags, char *buf, size_t len)
00637 {
00638         memset(buf, 0, len);
00639 
00640 #define PRINT_FLAG(f) \
00641         if (flags & NLM_F_##f) { \
00642                 flags &= ~NLM_F_##f; \
00643                 strncat(buf, #f, len - strlen(buf) - 1); \
00644                 if (flags) \
00645                         strncat(buf, ",", len - strlen(buf) - 1); \
00646         }
00647         
00648         PRINT_FLAG(REQUEST);
00649         PRINT_FLAG(MULTI);
00650         PRINT_FLAG(ACK);
00651         PRINT_FLAG(ECHO);
00652         PRINT_FLAG(ROOT);
00653         PRINT_FLAG(MATCH);
00654         PRINT_FLAG(ATOMIC);
00655         PRINT_FLAG(REPLACE);
00656         PRINT_FLAG(EXCL);
00657         PRINT_FLAG(CREATE);
00658         PRINT_FLAG(APPEND);
00659 
00660         if (flags) {
00661                 char s[32];
00662                 snprintf(s, sizeof(s), "0x%x", flags);
00663                 strncat(buf, s, len - strlen(buf) - 1);
00664         }
00665 #undef PRINT_FLAG
00666 
00667         return buf;
00668 }
00669 
00670 /** @} */
00671 
00672 /**
00673  * @name Direct Parsing
00674  * @{
00675  */
00676 
00677 /** @cond SKIP */
00678 struct dp_xdata {
00679         void (*cb)(struct nl_object *, void *);
00680         void *arg;
00681 };
00682 /** @endcond */
00683 
00684 static int parse_cb(struct nl_object *obj, struct nl_parser_param *p)
00685 {
00686         struct dp_xdata *x = p->pp_arg;
00687 
00688         x->cb(obj, x->arg);
00689         nl_object_put(obj);
00690         return 0;
00691 }
00692 
00693 int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *),
00694                  void *arg)
00695 {
00696         struct nl_cache_ops *ops;
00697         struct nl_parser_param p = {
00698                 .pp_cb = parse_cb
00699         };
00700         struct dp_xdata x = {
00701                 .cb = cb,
00702                 .arg = arg,
00703         };
00704 
00705         ops = nl_cache_mngt_associate(nlmsg_get_proto(msg),
00706                                       nlmsg_hdr(msg)->nlmsg_type);
00707         if (ops == NULL)
00708                 return nl_error(ENOENT, "Unknown message type %d",
00709                                 nlmsg_hdr(msg)->nlmsg_type);
00710         p.pp_arg = &x;
00711 
00712         return nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p);
00713 }
00714 
00715 /** @} */
00716 
00717 /** @} */

Generated on Fri Apr 27 14:14:07 2007 for libnl by  doxygen 1.5.1