libnftnl  1.0.8
obj/counter.c
1 /*
2  * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <inttypes.h>
15 
16 #include <linux/netfilter/nf_tables.h>
17 
18 #include <libmnl/libmnl.h>
19 #include <libnftnl/object.h>
20 
21 #include "internal.h"
22 #include "obj.h"
23 
24 static int
25 nftnl_obj_counter_set(struct nftnl_obj *e, uint16_t type,
26  const void *data, uint32_t data_len)
27 {
28  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
29 
30  switch(type) {
31  case NFTNL_OBJ_CTR_BYTES:
32  ctr->bytes = *((uint64_t *)data);
33  break;
34  case NFTNL_OBJ_CTR_PKTS:
35  ctr->pkts = *((uint64_t *)data);
36  break;
37  default:
38  return -1;
39  }
40  return 0;
41 }
42 
43 static const void *
44 nftnl_obj_counter_get(const struct nftnl_obj *e, uint16_t type,
45  uint32_t *data_len)
46 {
47  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
48 
49  switch(type) {
50  case NFTNL_OBJ_CTR_BYTES:
51  *data_len = sizeof(ctr->bytes);
52  return &ctr->bytes;
53  case NFTNL_OBJ_CTR_PKTS:
54  *data_len = sizeof(ctr->pkts);
55  return &ctr->pkts;
56  }
57  return NULL;
58 }
59 
60 static int nftnl_obj_counter_cb(const struct nlattr *attr, void *data)
61 {
62  const struct nlattr **tb = data;
63  int type = mnl_attr_get_type(attr);
64 
65  if (mnl_attr_type_valid(attr, NFTA_COUNTER_MAX) < 0)
66  return MNL_CB_OK;
67 
68  switch(type) {
69  case NFTA_COUNTER_BYTES:
70  case NFTA_COUNTER_PACKETS:
71  if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
72  abi_breakage();
73  break;
74  }
75 
76  tb[type] = attr;
77  return MNL_CB_OK;
78 }
79 
80 static void
81 nftnl_obj_counter_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
82 {
83  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
84 
85  if (e->flags & (1 << NFTNL_OBJ_CTR_BYTES))
86  mnl_attr_put_u64(nlh, NFTA_COUNTER_BYTES, htobe64(ctr->bytes));
87  if (e->flags & (1 << NFTNL_OBJ_CTR_PKTS))
88  mnl_attr_put_u64(nlh, NFTA_COUNTER_PACKETS, htobe64(ctr->pkts));
89 }
90 
91 static int
92 nftnl_obj_counter_parse(struct nftnl_obj *e, struct nlattr *attr)
93 {
94  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
95  struct nlattr *tb[NFTA_COUNTER_MAX+1] = {};
96 
97  if (mnl_attr_parse_nested(attr, nftnl_obj_counter_cb, tb) < 0)
98  return -1;
99 
100  if (tb[NFTA_COUNTER_BYTES]) {
101  ctr->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_BYTES]));
102  e->flags |= (1 << NFTNL_OBJ_CTR_BYTES);
103  }
104  if (tb[NFTA_COUNTER_PACKETS]) {
105  ctr->pkts = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_PACKETS]));
106  e->flags |= (1 << NFTNL_OBJ_CTR_PKTS);
107  }
108 
109  return 0;
110 }
111 
112 static int
113 nftnl_obj_counter_json_parse(struct nftnl_obj *e, json_t *root,
114  struct nftnl_parse_err *err)
115 {
116 #ifdef JSON_PARSING
117  uint64_t uval64;
118 
119  if (nftnl_jansson_parse_val(root, "pkts", NFTNL_TYPE_U64, &uval64,
120  err) == 0)
121  nftnl_obj_set_u64(e, NFTNL_OBJ_CTR_PKTS, uval64);
122 
123  if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &uval64,
124  err) == 0)
125  nftnl_obj_set_u64(e, NFTNL_OBJ_CTR_BYTES, uval64);
126 
127  return 0;
128 #else
129  errno = EOPNOTSUPP;
130  return -1;
131 #endif
132 }
133 
134 static int nftnl_obj_counter_export(char *buf, size_t size,
135  const struct nftnl_obj *e, int type)
136 {
137  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
138  NFTNL_BUF_INIT(b, buf, size);
139 
140  if (e->flags & (1 << NFTNL_OBJ_CTR_PKTS))
141  nftnl_buf_u64(&b, type, ctr->pkts, PKTS);
142  if (e->flags & (1 << NFTNL_OBJ_CTR_BYTES))
143  nftnl_buf_u64(&b, type, ctr->bytes, BYTES);
144 
145  return nftnl_buf_done(&b);
146 }
147 
148 static int nftnl_obj_counter_snprintf_default(char *buf, size_t len,
149  const struct nftnl_obj *e)
150 {
151  struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
152 
153  return snprintf(buf, len, "pkts %"PRIu64" bytes %"PRIu64" ",
154  ctr->pkts, ctr->bytes);
155 }
156 
157 static int nftnl_obj_counter_snprintf(char *buf, size_t len, uint32_t type,
158  uint32_t flags,
159  const struct nftnl_obj *e)
160 {
161  if (len)
162  buf[0] = '\0';
163 
164  switch (type) {
165  case NFTNL_OUTPUT_DEFAULT:
166  return nftnl_obj_counter_snprintf_default(buf, len, e);
167  case NFTNL_OUTPUT_XML:
168  case NFTNL_OUTPUT_JSON:
169  return nftnl_obj_counter_export(buf, len, e, type);
170  default:
171  break;
172  }
173  return -1;
174 }
175 
176 struct obj_ops obj_ops_counter = {
177  .name = "counter",
178  .type = NFT_OBJECT_COUNTER,
179  .alloc_len = sizeof(struct nftnl_obj_counter),
180  .max_attr = NFTA_COUNTER_MAX,
181  .set = nftnl_obj_counter_set,
182  .get = nftnl_obj_counter_get,
183  .parse = nftnl_obj_counter_parse,
184  .build = nftnl_obj_counter_build,
185  .snprintf = nftnl_obj_counter_snprintf,
186  .json_parse = nftnl_obj_counter_json_parse,
187 };