summaryrefslogtreecommitdiffstats
path: root/examples/nfacct-add.c
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2011-12-28 20:16:46 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2011-12-29 15:40:36 +0100
commit3388d7df304f26617c4487418c05734ae4fce5b8 (patch)
treec4fe8bb3cd31a8d85a908485e0b79e1817fefdad /examples/nfacct-add.c
parenta69ac90ec039509ef6ec2684bda156ed6d41fc83 (diff)
src: major API redesign
This patch reworks the initial API. Now it provides functions to: - allocate/release accounting objects. - set/unset/get attributes of accounting objects. - build one netlink message from one accounting object. - parse one netlink message to one accounting object. - print one accounting object into a buffer. Binary layout of nfacct objects are opaque. This is good for extensibility without breaking backward compatibility. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'examples/nfacct-add.c')
-rw-r--r--examples/nfacct-add.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/examples/nfacct-add.c b/examples/nfacct-add.c
index 64f94d1..40e0e31 100644
--- a/examples/nfacct-add.c
+++ b/examples/nfacct-add.c
@@ -10,18 +10,28 @@ int main(int argc, char *argv[])
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
uint32_t portid, seq;
- struct nfacct nfacct = {};
+ struct nfacct *nfacct;
int ret;
if (argc != 2) {
fprintf(stderr, "Usage: %s [name]\n", argv[0]);
exit(EXIT_FAILURE);
}
- strncpy(nfacct.name, argv[1], NFACCT_NAME_MAX);
- nfacct.name[NFACCT_NAME_MAX-1] = '\0';
- nlh = nfacct_add(buf, &nfacct);
- seq = nlh->nlmsg_seq = time(NULL);
+ nfacct = nfacct_alloc();
+ if (nfacct == NULL) {
+ perror("OOM");
+ exit(EXIT_FAILURE);
+ }
+
+ nfacct_attr_set(nfacct, NFACCT_ATTR_NAME, argv[1]);
+
+ seq = time(NULL);
+ nlh = nfacct_nlmsg_build_hdr(buf, NFNL_MSG_ACCT_NEW,
+ NLM_F_CREATE | NLM_F_ACK, seq);
+ nfacct_nlmsg_build_payload(nlh, nfacct);
+
+ nfacct_free(nfacct);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
@@ -42,7 +52,7 @@ int main(int argc, char *argv[])
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
while (ret > 0) {
- ret = mnl_cb_run(buf, ret, seq, portid, nfacct_list_cb, NULL);
+ ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
if (ret <= 0)
break;
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));