attr: add NLA_S* definitions
[project/libnl-tiny.git] / include / netlink / attr.h
1 /*
2 * netlink/attr.h Netlink Attributes
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
7 * of the License.
8 *
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10 */
11
12 #ifndef NETLINK_ATTR_H_
13 #define NETLINK_ATTR_H_
14
15 #include <netlink/netlink.h>
16 #include <netlink/object.h>
17 #include <netlink/addr.h>
18 #include <netlink/data.h>
19 #include <netlink/msg.h>
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 struct nl_msg;
26
27 /**
28 * @name Basic Attribute Data Types
29 * @{
30 */
31
32 /**
33 * @ingroup attr
34 * Basic attribute data types
35 *
36 * See \ref attr_datatypes for more details.
37 */
38 enum {
39 NLA_UNSPEC, /**< Unspecified type, binary data chunk */
40 NLA_U8, /**< 8 bit integer */
41 NLA_U16, /**< 16 bit integer */
42 NLA_U32, /**< 32 bit integer */
43 NLA_U64, /**< 64 bit integer */
44 NLA_STRING, /**< NUL terminated character string */
45 NLA_FLAG, /**< Flag */
46 NLA_MSECS, /**< Micro seconds (64bit) */
47 NLA_NESTED, /**< Nested attributes */
48 NLA_NESTED_ARRAY,
49 NLA_NUL_STRING,
50 NLA_BINARY,
51 NLA_S8,
52 NLA_S16,
53 NLA_S32,
54 NLA_S64,
55 __NLA_TYPE_MAX,
56 };
57
58 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
59
60 /** @} */
61
62 /**
63 * @ingroup attr
64 * Attribute validation policy.
65 *
66 * See \ref attr_datatypes for more details.
67 */
68 struct nla_policy {
69 /** Type of attribute or NLA_UNSPEC */
70 uint16_t type;
71
72 /** Minimal length of payload required */
73 uint16_t minlen;
74
75 /** Maximal length of payload allowed */
76 uint16_t maxlen;
77 };
78
79 /* Attribute parsing */
80 extern int nla_ok(const struct nlattr *, int);
81 extern struct nlattr * nla_next(const struct nlattr *, int *);
82 extern int nla_parse(struct nlattr **, int, struct nlattr *,
83 int, struct nla_policy *);
84 extern int nla_validate(struct nlattr *, int, int,
85 struct nla_policy *);
86 extern struct nlattr * nla_find(struct nlattr *, int, int);
87
88 /* Unspecific attribute */
89 extern struct nlattr * nla_reserve(struct nl_msg *, int, int);
90 extern int nla_put(struct nl_msg *, int, int, const void *);
91
92 /**
93 * nlmsg_find_attr - find a specific attribute in a netlink message
94 * @arg nlh netlink message header
95 * @arg hdrlen length of familiy specific header
96 * @arg attrtype type of attribute to look for
97 *
98 * Returns the first attribute which matches the specified type.
99 */
100 static inline struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype)
101 {
102 return nla_find(nlmsg_attrdata(nlh, hdrlen),
103 nlmsg_attrlen(nlh, hdrlen), attrtype);
104 }
105
106
107 /**
108 * Return size of attribute whithout padding.
109 * @arg payload Payload length of attribute.
110 *
111 * @code
112 * <-------- nla_attr_size(payload) --------->
113 * +------------------+- - -+- - - - - - - - - +- - -+
114 * | Attribute Header | Pad | Payload | Pad |
115 * +------------------+- - -+- - - - - - - - - +- - -+
116 * @endcode
117 *
118 * @return Size of attribute in bytes without padding.
119 */
120 static inline int nla_attr_size(int payload)
121 {
122 return NLA_HDRLEN + payload;
123 }
124
125 /**
126 * Return size of attribute including padding.
127 * @arg payload Payload length of attribute.
128 *
129 * @code
130 * <----------- nla_total_size(payload) ----------->
131 * +------------------+- - -+- - - - - - - - - +- - -+
132 * | Attribute Header | Pad | Payload | Pad |
133 * +------------------+- - -+- - - - - - - - - +- - -+
134 * @endcode
135 *
136 * @return Size of attribute in bytes.
137 */
138 static inline int nla_total_size(int payload)
139 {
140 return NLA_ALIGN(nla_attr_size(payload));
141 }
142
143 /**
144 * Return length of padding at the tail of the attribute.
145 * @arg payload Payload length of attribute.
146 *
147 * @code
148 * +------------------+- - -+- - - - - - - - - +- - -+
149 * | Attribute Header | Pad | Payload | Pad |
150 * +------------------+- - -+- - - - - - - - - +- - -+
151 * <--->
152 * @endcode
153 *
154 * @return Length of padding in bytes.
155 */
156 static inline int nla_padlen(int payload)
157 {
158 return nla_total_size(payload) - nla_attr_size(payload);
159 }
160
161 /**
162 * Return type of the attribute.
163 * @arg nla Attribute.
164 *
165 * @return Type of attribute.
166 */
167 static inline int nla_type(const struct nlattr *nla)
168 {
169 return nla->nla_type & NLA_TYPE_MASK;
170 }
171
172 /**
173 * Return pointer to the payload section.
174 * @arg nla Attribute.
175 *
176 * @return Pointer to start of payload section.
177 */
178 static inline void *nla_data(const struct nlattr *nla)
179 {
180 return (char *) nla + NLA_HDRLEN;
181 }
182
183 /**
184 * Return length of the payload .
185 * @arg nla Attribute
186 *
187 * @return Length of payload in bytes.
188 */
189 static inline int nla_len(const struct nlattr *nla)
190 {
191 return nla->nla_len - NLA_HDRLEN;
192 }
193
194 /**
195 * Copy attribute payload to another memory area.
196 * @arg dest Pointer to destination memory area.
197 * @arg src Attribute
198 * @arg count Number of bytes to copy at most.
199 *
200 * Note: The number of bytes copied is limited by the length of
201 * the attribute payload.
202 *
203 * @return The number of bytes copied to dest.
204 */
205 static inline int nla_memcpy(void *dest, struct nlattr *src, int count)
206 {
207 int minlen;
208
209 if (!src)
210 return 0;
211
212 minlen = min_t(int, count, nla_len(src));
213 memcpy(dest, nla_data(src), minlen);
214
215 return minlen;
216 }
217
218
219 /**
220 * Add abstract data as unspecific attribute to netlink message.
221 * @arg msg Netlink message.
222 * @arg attrtype Attribute type.
223 * @arg data Abstract data object.
224 *
225 * Equivalent to nla_put() except that the length of the payload is
226 * derived from the abstract data object.
227 *
228 * @see nla_put
229 * @return 0 on success or a negative error code.
230 */
231 static inline int nla_put_data(struct nl_msg *msg, int attrtype, struct nl_data *data)
232 {
233 return nla_put(msg, attrtype, nl_data_get_size(data),
234 nl_data_get(data));
235 }
236
237 /**
238 * Add abstract address as unspecific attribute to netlink message.
239 * @arg msg Netlink message.
240 * @arg attrtype Attribute type.
241 * @arg addr Abstract address object.
242 *
243 * @see nla_put
244 * @return 0 on success or a negative error code.
245 */
246 static inline int nla_put_addr(struct nl_msg *msg, int attrtype, struct nl_addr *addr)
247 {
248 return nla_put(msg, attrtype, nl_addr_get_len(addr),
249 nl_addr_get_binary_addr(addr));
250 }
251
252 /** @} */
253
254 /**
255 * @name Integer Attributes
256 */
257
258 /**
259 * Add 8 bit signed integer attribute to netlink message.
260 * @arg msg Netlink message.
261 * @arg attrtype Attribute type.
262 * @arg value Numeric value to store as payload.
263 *
264 * @see nla_put
265 * @return 0 on success or a negative error code.
266 */
267 static inline int nla_put_s8(struct nl_msg *msg, int attrtype, int8_t value)
268 {
269 return nla_put(msg, attrtype, sizeof(int8_t), &value);
270 }
271
272 /**
273 * Return value of 8 bit signed integer attribute.
274 * @arg nla 8 bit integer attribute
275 *
276 * @return Payload as 8 bit integer.
277 */
278 static inline int8_t nla_get_s8(struct nlattr *nla)
279 {
280 return *(int8_t *) nla_data(nla);
281 }
282
283 /**
284 * Add 8 bit integer attribute to netlink message.
285 * @arg msg Netlink message.
286 * @arg attrtype Attribute type.
287 * @arg value Numeric value to store as payload.
288 *
289 * @see nla_put
290 * @return 0 on success or a negative error code.
291 */
292 static inline int nla_put_u8(struct nl_msg *msg, int attrtype, uint8_t value)
293 {
294 return nla_put(msg, attrtype, sizeof(uint8_t), &value);
295 }
296
297 /**
298 * Return value of 8 bit integer attribute.
299 * @arg nla 8 bit integer attribute
300 *
301 * @return Payload as 8 bit integer.
302 */
303 static inline uint8_t nla_get_u8(struct nlattr *nla)
304 {
305 return *(uint8_t *) nla_data(nla);
306 }
307
308 /**
309 * Add 16 bit signed integer attribute to netlink message.
310 * @arg msg Netlink message.
311 * @arg attrtype Attribute type.
312 * @arg value Numeric value to store as payload.
313 *
314 * @see nla_put
315 * @return 0 on success or a negative error code.
316 */
317 static inline int nla_put_s16(struct nl_msg *msg, int attrtype, int16_t value)
318 {
319 return nla_put(msg, attrtype, sizeof(int16_t), &value);
320 }
321
322 /**
323 * Return payload of 16 bit signed integer attribute.
324 * @arg nla 16 bit integer attribute
325 *
326 * @return Payload as 16 bit integer.
327 */
328 static inline int16_t nla_get_s16(struct nlattr *nla)
329 {
330 return *(int16_t *) nla_data(nla);
331 }
332
333 /**
334 * Add 16 bit integer attribute to netlink message.
335 * @arg msg Netlink message.
336 * @arg attrtype Attribute type.
337 * @arg value Numeric value to store as payload.
338 *
339 * @see nla_put
340 * @return 0 on success or a negative error code.
341 */
342 static inline int nla_put_u16(struct nl_msg *msg, int attrtype, uint16_t value)
343 {
344 return nla_put(msg, attrtype, sizeof(uint16_t), &value);
345 }
346
347 /**
348 * Return payload of 16 bit integer attribute.
349 * @arg nla 16 bit integer attribute
350 *
351 * @return Payload as 16 bit integer.
352 */
353 static inline uint16_t nla_get_u16(struct nlattr *nla)
354 {
355 return *(uint16_t *) nla_data(nla);
356 }
357
358 /**
359 * Add 32 bit signed integer attribute to netlink message.
360 * @arg msg Netlink message.
361 * @arg attrtype Attribute type.
362 * @arg value Numeric value to store as payload.
363 *
364 * @see nla_put
365 * @return 0 on success or a negative error code.
366 */
367 static inline int nla_put_s32(struct nl_msg *msg, int attrtype, int32_t value)
368 {
369 return nla_put(msg, attrtype, sizeof(int32_t), &value);
370 }
371
372 /**
373 * Return payload of 32 bit signed integer attribute.
374 * @arg nla 32 bit integer attribute.
375 *
376 * @return Payload as 32 bit integer.
377 */
378 static inline int32_t nla_get_s32(struct nlattr *nla)
379 {
380 return *(int32_t *) nla_data(nla);
381 }
382
383 /**
384 * Add 32 bit integer attribute to netlink message.
385 * @arg msg Netlink message.
386 * @arg attrtype Attribute type.
387 * @arg value Numeric value to store as payload.
388 *
389 * @see nla_put
390 * @return 0 on success or a negative error code.
391 */
392 static inline int nla_put_u32(struct nl_msg *msg, int attrtype, uint32_t value)
393 {
394 return nla_put(msg, attrtype, sizeof(uint32_t), &value);
395 }
396
397 /**
398 * Return payload of 32 bit integer attribute.
399 * @arg nla 32 bit integer attribute.
400 *
401 * @return Payload as 32 bit integer.
402 */
403 static inline uint32_t nla_get_u32(struct nlattr *nla)
404 {
405 return *(uint32_t *) nla_data(nla);
406 }
407
408 /**
409 * Add 64 bit signed integer attribute to netlink message.
410 * @arg msg Netlink message.
411 * @arg attrtype Attribute type.
412 * @arg value Numeric value to store as payload.
413 *
414 * @see nla_put
415 * @return 0 on success or a negative error code.
416 */
417 static inline int nla_put_s64(struct nl_msg *msg, int attrtype, int64_t value)
418 {
419 return nla_put(msg, attrtype, sizeof(int64_t), &value);
420 }
421
422 /**
423 * Return payload of s64 attribute
424 * @arg nla s64 netlink attribute
425 *
426 * @return Payload as 64 bit integer.
427 */
428 static inline int64_t nla_get_s64(struct nlattr *nla)
429 {
430 int64_t tmp;
431
432 nla_memcpy(&tmp, nla, sizeof(tmp));
433
434 return tmp;
435 }
436
437 /**
438 * Add 64 bit integer attribute to netlink message.
439 * @arg msg Netlink message.
440 * @arg attrtype Attribute type.
441 * @arg value Numeric value to store as payload.
442 *
443 * @see nla_put
444 * @return 0 on success or a negative error code.
445 */
446 static inline int nla_put_u64(struct nl_msg *msg, int attrtype, uint64_t value)
447 {
448 return nla_put(msg, attrtype, sizeof(uint64_t), &value);
449 }
450
451 /**
452 * Return payload of u64 attribute
453 * @arg nla u64 netlink attribute
454 *
455 * @return Payload as 64 bit integer.
456 */
457 static inline uint64_t nla_get_u64(struct nlattr *nla)
458 {
459 uint64_t tmp;
460
461 nla_memcpy(&tmp, nla, sizeof(tmp));
462
463 return tmp;
464 }
465
466 /**
467 * Add string attribute to netlink message.
468 * @arg msg Netlink message.
469 * @arg attrtype Attribute type.
470 * @arg str NUL terminated string.
471 *
472 * @see nla_put
473 * @return 0 on success or a negative error code.
474 */
475 static inline int nla_put_string(struct nl_msg *msg, int attrtype, const char *str)
476 {
477 return nla_put(msg, attrtype, strlen(str) + 1, str);
478 }
479
480 /**
481 * Return payload of string attribute.
482 * @arg nla String attribute.
483 *
484 * @return Pointer to attribute payload.
485 */
486 static inline char *nla_get_string(struct nlattr *nla)
487 {
488 return (char *) nla_data(nla);
489 }
490
491 static inline char *nla_strdup(struct nlattr *nla)
492 {
493 return strdup(nla_get_string(nla));
494 }
495
496 /** @} */
497
498 /**
499 * @name Flag Attribute
500 */
501
502 /**
503 * Add flag netlink attribute to netlink message.
504 * @arg msg Netlink message.
505 * @arg attrtype Attribute type.
506 *
507 * @see nla_put
508 * @return 0 on success or a negative error code.
509 */
510 static inline int nla_put_flag(struct nl_msg *msg, int attrtype)
511 {
512 return nla_put(msg, attrtype, 0, NULL);
513 }
514
515 /**
516 * Return true if flag attribute is set.
517 * @arg nla Flag netlink attribute.
518 *
519 * @return True if flag is set, otherwise false.
520 */
521 static inline int nla_get_flag(struct nlattr *nla)
522 {
523 return !!nla;
524 }
525
526 /** @} */
527
528 /**
529 * @name Microseconds Attribute
530 */
531
532 /**
533 * Add a msecs netlink attribute to a netlink message
534 * @arg n netlink message
535 * @arg attrtype attribute type
536 * @arg msecs number of msecs
537 */
538 static inline int nla_put_msecs(struct nl_msg *n, int attrtype, unsigned long msecs)
539 {
540 return nla_put_u64(n, attrtype, msecs);
541 }
542
543 /**
544 * Return payload of msecs attribute
545 * @arg nla msecs netlink attribute
546 *
547 * @return the number of milliseconds.
548 */
549 static inline unsigned long nla_get_msecs(struct nlattr *nla)
550 {
551 return nla_get_u64(nla);
552 }
553
554 /**
555 * Add nested attributes to netlink message.
556 * @arg msg Netlink message.
557 * @arg attrtype Attribute type.
558 * @arg nested Message containing attributes to be nested.
559 *
560 * Takes the attributes found in the \a nested message and appends them
561 * to the message \a msg nested in a container of the type \a attrtype.
562 * The \a nested message may not have a family specific header.
563 *
564 * @see nla_put
565 * @return 0 on success or a negative error code.
566 */
567 static inline int nla_put_nested(struct nl_msg *msg, int attrtype, struct nl_msg *nested)
568 {
569 return nla_put(msg, attrtype, nlmsg_len(nested->nm_nlh),
570 nlmsg_data(nested->nm_nlh));
571 }
572
573 /**
574 * Start a new level of nested attributes.
575 * @arg msg Netlink message.
576 * @arg attrtype Attribute type of container.
577 *
578 * @return Pointer to container attribute.
579 */
580 static inline struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
581 {
582 struct nlattr *start = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
583
584 if (nla_put(msg, attrtype | NLA_F_NESTED, 0, NULL) < 0)
585 return NULL;
586
587 return start;
588 }
589
590 /**
591 * Finalize nesting of attributes.
592 * @arg msg Netlink message.
593 * @arg start Container attribute as returned from nla_nest_start().
594 *
595 * Corrects the container attribute header to include the appeneded attributes.
596 *
597 * @return 0
598 */
599 static inline int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
600 {
601 start->nla_len = (unsigned char *) nlmsg_tail(msg->nm_nlh) -
602 (unsigned char *) start;
603 return 0;
604 }
605
606 /**
607 * Create attribute index based on nested attribute
608 * @arg tb Index array to be filled (maxtype+1 elements).
609 * @arg maxtype Maximum attribute type expected and accepted.
610 * @arg nla Nested Attribute.
611 * @arg policy Attribute validation policy.
612 *
613 * Feeds the stream of attributes nested into the specified attribute
614 * to nla_parse().
615 *
616 * @see nla_parse
617 * @return 0 on success or a negative error code.
618 */
619 static inline int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
620 struct nla_policy *policy)
621 {
622 return nla_parse(tb, maxtype, (struct nlattr *)nla_data(nla), nla_len(nla), policy);
623 }
624
625 /**
626 * Compare attribute payload with memory area.
627 * @arg nla Attribute.
628 * @arg data Memory area to compare to.
629 * @arg size Number of bytes to compare.
630 *
631 * @see memcmp(3)
632 * @return An integer less than, equal to, or greater than zero.
633 */
634 static inline int nla_memcmp(const struct nlattr *nla, const void *data, size_t size)
635 {
636 int d = nla_len(nla) - size;
637
638 if (d == 0)
639 d = memcmp(nla_data(nla), data, size);
640
641 return d;
642 }
643
644 /**
645 * Compare string attribute payload with string
646 * @arg nla Attribute of type NLA_STRING.
647 * @arg str NUL terminated string.
648 *
649 * @see strcmp(3)
650 * @return An integer less than, equal to, or greater than zero.
651 */
652 static inline int nla_strcmp(const struct nlattr *nla, const char *str)
653 {
654 int len = strlen(str) + 1;
655 int d = nla_len(nla) - len;
656
657 if (d == 0)
658 d = memcmp(nla_data(nla), str, len);
659
660 return d;
661 }
662
663 /**
664 * Copy string attribute payload to a buffer.
665 * @arg dst Pointer to destination buffer.
666 * @arg nla Attribute of type NLA_STRING.
667 * @arg dstsize Size of destination buffer in bytes.
668 *
669 * Copies at most dstsize - 1 bytes to the destination buffer.
670 * The result is always a valid NUL terminated string. Unlike
671 * strlcpy the destination buffer is always padded out.
672 *
673 * @return The length of string attribute without the terminating NUL.
674 */
675 static inline size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
676 {
677 size_t srclen = (size_t)nla_len(nla);
678 char *src = (char*)nla_data(nla);
679
680 if (srclen > 0 && src[srclen - 1] == '\0')
681 srclen--;
682
683 if (dstsize > 0) {
684 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
685
686 memset(dst, 0, dstsize);
687 memcpy(dst, src, len);
688 }
689
690 return srclen;
691 }
692
693
694 /**
695 * @name Attribute Construction (Exception Based)
696 * @{
697 */
698
699 /**
700 * @ingroup attr
701 * Add unspecific attribute to netlink message.
702 * @arg msg Netlink message.
703 * @arg attrtype Attribute type.
704 * @arg attrlen Length of attribute payload.
705 * @arg data Head of attribute payload.
706 */
707 #define NLA_PUT(msg, attrtype, attrlen, data) \
708 do { \
709 if (nla_put(msg, attrtype, attrlen, data) < 0) \
710 goto nla_put_failure; \
711 } while(0)
712
713 /**
714 * @ingroup attr
715 * Add atomic type attribute to netlink message.
716 * @arg msg Netlink message.
717 * @arg type Atomic type.
718 * @arg attrtype Attribute type.
719 * @arg value Head of attribute payload.
720 */
721 #define NLA_PUT_TYPE(msg, type, attrtype, value) \
722 do { \
723 type __tmp = value; \
724 NLA_PUT(msg, attrtype, sizeof(type), &__tmp); \
725 } while(0)
726
727 /**
728 * Add 8 bit signed integer attribute to netlink message.
729 * @arg msg Netlink message.
730 * @arg attrtype Attribute type.
731 * @arg value Numeric value.
732 */
733 #define NLA_PUT_S8(msg, attrtype, value) \
734 NLA_PUT_TYPE(msg, int8_t, attrtype, value)
735
736 /**
737 * Add 8 bit signed integer attribute to netlink message.
738 * @arg msg Netlink message.
739 * @arg attrtype Attribute type.
740 * @arg value Numeric value.
741 */
742 #define NLA_PUT_S8(msg, attrtype, value) \
743 NLA_PUT_TYPE(msg, int8_t, attrtype, value)
744
745 /**
746 * Add 8 bit integer attribute to netlink message.
747 * @arg msg Netlink message.
748 * @arg attrtype Attribute type.
749 * @arg value Numeric value.
750 */
751 #define NLA_PUT_U8(msg, attrtype, value) \
752 NLA_PUT_TYPE(msg, uint8_t, attrtype, value)
753
754 /**
755 * Add 16 bit signed integer attribute to netlink message.
756 * @arg msg Netlink message.
757 * @arg attrtype Attribute type.
758 * @arg value Numeric value.
759 */
760 #define NLA_PUT_S16(msg, attrtype, value) \
761 NLA_PUT_TYPE(msg, int16_t, attrtype, value)
762
763 /**
764 * Add 16 bit integer attribute to netlink message.
765 * @arg msg Netlink message.
766 * @arg attrtype Attribute type.
767 * @arg value Numeric value.
768 */
769 #define NLA_PUT_U16(msg, attrtype, value) \
770 NLA_PUT_TYPE(msg, uint16_t, attrtype, value)
771
772 /**
773 * Add 32 bit signed integer attribute to netlink message.
774 * @arg msg Netlink message.
775 * @arg attrtype Attribute type.
776 * @arg value Numeric value.
777 */
778 #define NLA_PUT_S32(msg, attrtype, value) \
779 NLA_PUT_TYPE(msg, int32_t, attrtype, value)
780
781 /**
782 * Add 32 bit integer attribute to netlink message.
783 * @arg msg Netlink message.
784 * @arg attrtype Attribute type.
785 * @arg value Numeric value.
786 */
787 #define NLA_PUT_U32(msg, attrtype, value) \
788 NLA_PUT_TYPE(msg, uint32_t, attrtype, value)
789
790 /**
791 * Add 64 bit signed integer attribute to netlink message.
792 * @arg msg Netlink message.
793 * @arg attrtype Attribute type.
794 * @arg value Numeric value.
795 */
796 #define NLA_PUT_S64(msg, attrtype, value) \
797 NLA_PUT_TYPE(msg, int64_t, attrtype, value)
798
799 /**
800 * Add 64 bit integer attribute to netlink message.
801 * @arg msg Netlink message.
802 * @arg attrtype Attribute type.
803 * @arg value Numeric value.
804 */
805 #define NLA_PUT_U64(msg, attrtype, value) \
806 NLA_PUT_TYPE(msg, uint64_t, attrtype, value)
807
808 /**
809 * Add string attribute to netlink message.
810 * @arg msg Netlink message.
811 * @arg attrtype Attribute type.
812 * @arg value NUL terminated character string.
813 */
814 #define NLA_PUT_STRING(msg, attrtype, value) \
815 NLA_PUT(msg, attrtype, strlen(value) + 1, value)
816
817 /**
818 * Add flag attribute to netlink message.
819 * @arg msg Netlink message.
820 * @arg attrtype Attribute type.
821 */
822 #define NLA_PUT_FLAG(msg, attrtype) \
823 NLA_PUT(msg, attrtype, 0, NULL)
824
825 /**
826 * Add msecs attribute to netlink message.
827 * @arg msg Netlink message.
828 * @arg attrtype Attribute type.
829 * @arg msecs Numeric value in micro seconds.
830 */
831 #define NLA_PUT_MSECS(msg, attrtype, msecs) \
832 NLA_PUT_U64(msg, attrtype, msecs)
833
834 /**
835 * Add address attribute to netlink message.
836 * @arg msg Netlink message.
837 * @arg attrtype Attribute type.
838 * @arg addr Abstract address object.
839 */
840 #define NLA_PUT_ADDR(msg, attrtype, addr) \
841 NLA_PUT(msg, attrtype, nl_addr_get_len(addr), \
842 nl_addr_get_binary_addr(addr))
843
844 /** @} */
845
846 /**
847 * @name Iterators
848 * @{
849 */
850
851 /**
852 * @ingroup attr
853 * Iterate over a stream of attributes
854 * @arg pos loop counter, set to current attribute
855 * @arg head head of attribute stream
856 * @arg len length of attribute stream
857 * @arg rem initialized to len, holds bytes currently remaining in stream
858 */
859 #define nla_for_each_attr(pos, head, len, rem) \
860 for (pos = head, rem = len; \
861 nla_ok(pos, rem); \
862 pos = nla_next(pos, &(rem)))
863
864 /**
865 * @ingroup attr
866 * Iterate over a stream of nested attributes
867 * @arg pos loop counter, set to current attribute
868 * @arg nla attribute containing the nested attributes
869 * @arg rem initialized to len, holds bytes currently remaining in stream
870 */
871 #define nla_for_each_nested(pos, nla, rem) \
872 for (pos = (struct nlattr *)nla_data(nla), rem = nla_len(nla); \
873 nla_ok(pos, rem); \
874 pos = nla_next(pos, &(rem)))
875
876 /** @} */
877
878 #ifdef __cplusplus
879 }
880 #endif
881
882 #endif