2 * librouteros - src/interface.c
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
42 #include "routeros_api.h"
43 #include "ros_parse.h"
48 struct rt_internal_data_s
50 ros_interface_handler_t handler;
53 typedef struct rt_internal_data_s rt_internal_data_t;
58 static ros_interface_t *rt_reply_to_interface (const ros_reply_t *r) /* {{{ */
65 if (strcmp ("re", ros_reply_status (r)) != 0)
66 return (rt_reply_to_interface (ros_reply_next (r)));
68 ret = malloc (sizeof (*ret));
71 memset (ret, 0, sizeof (*ret));
73 ret->name = ros_reply_param_val_by_key (r, "name");
74 ret->type = ros_reply_param_val_by_key (r, "type");
75 ret->comment = ros_reply_param_val_by_key (r, "comment");
77 sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "packets"),
78 &ret->rx_packets, &ret->tx_packets);
79 sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "bytes"),
80 &ret->rx_bytes, &ret->tx_bytes);
81 sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "errors"),
82 &ret->rx_errors, &ret->tx_errors);
83 sstrto_rx_tx_counters (ros_reply_param_val_by_key (r, "drops"),
84 &ret->rx_drops, &ret->tx_drops);
86 ret->mtu = sstrtoui (ros_reply_param_val_by_key (r, "mtu"));
87 ret->l2mtu = sstrtoui (ros_reply_param_val_by_key (r, "l2mtu"));
89 ret->dynamic = sstrtob (ros_reply_param_val_by_key (r, "dynamic"));
90 ret->running = sstrtob (ros_reply_param_val_by_key (r, "running"));
91 ret->enabled = !sstrtob (ros_reply_param_val_by_key (r, "disabled"));
93 ret->next = rt_reply_to_interface (ros_reply_next (r));
96 } /* }}} ros_interface_t *rt_reply_to_interface */
98 static void if_interface_free (const ros_interface_t *r) /* {{{ */
100 const ros_interface_t *next;
108 } /* }}} void if_interface_free */
110 static int if_internal_handler (ros_connection_t *c, /* {{{ */
111 const ros_reply_t *r, void *user_data)
113 ros_interface_t *if_data;
114 rt_internal_data_t *internal_data;
117 if_data = rt_reply_to_interface (r);
121 internal_data = user_data;
123 status = internal_data->handler (c, if_data, internal_data->user_data);
125 if_interface_free (if_data);
128 } /* }}} int if_internal_handler */
133 int ros_interface (ros_connection_t *c, /* {{{ */
134 ros_interface_handler_t handler, void *user_data)
136 rt_internal_data_t data;
138 if ((c == NULL) || (handler == NULL))
141 data.handler = handler;
142 data.user_data = user_data;
144 return (ros_query (c, "/interface/print",
145 /* args_num = */ 0, /* args = */ NULL,
146 if_internal_handler, &data));
147 } /* }}} int ros_interface */
149 /* vim: set ts=2 sw=2 noet fdm=marker : */