libnftnl  1.0.6
buffer.c
1 /*
2  * (C) 2012-2014 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 <stdarg.h>
13 #include <inttypes.h>
14 #include <string.h>
15 #include <buffer.h>
16 #include <libnftnl/common.h>
17 #include "internal.h"
18 
19 int nftnl_buf_update(struct nftnl_buf *b, int ret)
20 {
21  if (ret < 0) {
22  b->fail = true;
23  } else {
24  b->off += ret;
25  if (ret > b->len)
26  ret = b->len;
27  b->size += ret;
28  b->len -= ret;
29  }
30 
31  return ret;
32 }
33 
34 int nftnl_buf_done(struct nftnl_buf *b)
35 {
36  if (b->fail)
37  return -1;
38 
39  /* Remove trailing comma in json */
40  if (b->size > 0 && b->buf[b->size - 1] == ',') {
41  b->off--;
42  b->size--;
43  b->len++;
44  }
45 
46  return b->off;
47 }
48 
49 static int nftnl_buf_put(struct nftnl_buf *b, const char *fmt, ...)
50 {
51  int ret;
52  va_list ap;
53 
54  va_start(ap, fmt);
55  ret = vsnprintf(b->buf + b->off, b->len, fmt, ap);
56  ret = nftnl_buf_update(b, ret);
57  va_end(ap);
58 
59  return ret;
60 }
61 
62 int nftnl_buf_open(struct nftnl_buf *b, int type, const char *tag)
63 {
64  switch (type) {
65  case NFTNL_OUTPUT_XML:
66  return nftnl_buf_put(b, "<%s>", tag);
67  case NFTNL_OUTPUT_JSON:
68  return nftnl_buf_put(b, "{\"%s\":{", tag);
69  default:
70  return 0;
71  }
72 }
73 
74 int nftnl_buf_close(struct nftnl_buf *b, int type, const char *tag)
75 {
76  switch (type) {
77  case NFTNL_OUTPUT_XML:
78  return nftnl_buf_put(b, "</%s>", tag);
79  case NFTNL_OUTPUT_JSON:
80  /* Remove trailing comma in json */
81  if (b->size > 0 && b->buf[b->size - 1] == ',') {
82  b->off--;
83  b->size--;
84  b->len++;
85  }
86 
87  return nftnl_buf_put(b, "}}");
88  default:
89  return 0;
90  }
91 }
92 
93 int nftnl_buf_open_array(struct nftnl_buf *b, int type, const char *tag)
94 {
95  switch (type) {
96  case NFTNL_OUTPUT_JSON:
97  return nftnl_buf_put(b, "{\"%s\":[", tag);
98  case NFTNL_OUTPUT_XML:
99  return nftnl_buf_put(b, "<%s>", tag);
100  default:
101  return 0;
102  }
103 }
104 
105 int nftnl_buf_close_array(struct nftnl_buf *b, int type, const char *tag)
106 {
107  switch (type) {
108  case NFTNL_OUTPUT_JSON:
109  return nftnl_buf_put(b, "]}");
110  case NFTNL_OUTPUT_XML:
111  return nftnl_buf_put(b, "</%s>", tag);
112  default:
113  return 0;
114  }
115 }
116 
117 int nftnl_buf_u32(struct nftnl_buf *b, int type, uint32_t value, const char *tag)
118 {
119  switch (type) {
120  case NFTNL_OUTPUT_XML:
121  return nftnl_buf_put(b, "<%s>%u</%s>", tag, value, tag);
122  case NFTNL_OUTPUT_JSON:
123  return nftnl_buf_put(b, "\"%s\":%u,", tag, value);
124  default:
125  return 0;
126  }
127 }
128 
129 int nftnl_buf_s32(struct nftnl_buf *b, int type, uint32_t value, const char *tag)
130 {
131  switch (type) {
132  case NFTNL_OUTPUT_XML:
133  return nftnl_buf_put(b, "<%s>%d</%s>", tag, value, tag);
134  case NFTNL_OUTPUT_JSON:
135  return nftnl_buf_put(b, "\"%s\":%d,", tag, value);
136  default:
137  return 0;
138  }
139 }
140 
141 int nftnl_buf_u64(struct nftnl_buf *b, int type, uint64_t value, const char *tag)
142 {
143  switch (type) {
144  case NFTNL_OUTPUT_XML:
145  return nftnl_buf_put(b, "<%s>%"PRIu64"</%s>", tag, value, tag);
146  case NFTNL_OUTPUT_JSON:
147  return nftnl_buf_put(b, "\"%s\":%"PRIu64",", tag, value);
148  default:
149  return 0;
150  }
151 }
152 
153 int nftnl_buf_str(struct nftnl_buf *b, int type, const char *str, const char *tag)
154 {
155  switch (type) {
156  case NFTNL_OUTPUT_XML:
157  return nftnl_buf_put(b, "<%s>%s</%s>", tag, str, tag);
158  case NFTNL_OUTPUT_JSON:
159  return nftnl_buf_put(b, "\"%s\":\"%s\",", tag, str);
160  default:
161  return 0;
162  }
163 }
164 
165 int nftnl_buf_reg(struct nftnl_buf *b, int type, union nftnl_data_reg *reg,
166  int reg_type, const char *tag)
167 {
168  int ret;
169 
170  switch (type) {
171  case NFTNL_OUTPUT_XML:
172  ret = nftnl_buf_put(b, "<%s>", tag);
173  ret = nftnl_data_reg_snprintf(b->buf + b->off, b->len, reg,
174  NFTNL_OUTPUT_XML, 0, reg_type);
175  nftnl_buf_update(b, ret);
176  return nftnl_buf_put(b, "</%s>", tag);
177  case NFTNL_OUTPUT_JSON:
178  nftnl_buf_put(b, "\"%s\":{", tag);
179  ret = nftnl_data_reg_snprintf(b->buf + b->off, b->len, reg,
180  NFTNL_OUTPUT_JSON, 0, reg_type);
181  nftnl_buf_update(b, ret);
182  return nftnl_buf_put(b, "},");
183  }
184  return 0;
185 }