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"
47 struct rt_internal_data_s
49 ros_interface_handler handler;
52 typedef struct rt_internal_data_s rt_internal_data_t;
57 static unsigned int sstrtoui (const char *str) /* {{{ */
67 ret = (unsigned int) strtoul (str, &endptr, /* base = */ 10);
68 if ((endptr == str) || (errno != 0))
72 } /* }}} unsigned int sstrtoui */
74 static _Bool sstrtob (const char *str) /* {{{ */
79 if (strcasecmp ("true", str) == 0)
82 } /* }}} _Bool sstrtob */
84 static int string_to_rx_tx_counters (const char *str, /* {{{ */
85 uint64_t *rx, uint64_t *tx)
90 if ((rx == NULL) || (tx == NULL))
102 *rx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10);
103 if ((endptr == str) || (errno != 0))
106 assert (endptr != NULL);
113 *tx = (uint64_t) strtoull (ptr, &endptr, /* base = */ 10);
114 if ((endptr == str) || (errno != 0))
118 } /* }}} int string_to_rx_tx_counters */
120 static ros_interface_t *rt_reply_to_interface (const ros_reply_t *r) /* {{{ */
122 ros_interface_t *ret;
127 if (strcmp ("re", ros_reply_status (r)) != 0)
128 return (rt_reply_to_interface (ros_reply_next (r)));
130 ret = malloc (sizeof (*ret));
133 memset (ret, 0, sizeof (*ret));
135 ret->name = ros_reply_param_val_by_key (r, "name");
136 ret->type = ros_reply_param_val_by_key (r, "type");
137 ret->comment = ros_reply_param_val_by_key (r, "comment");
139 string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "packets"),
140 &ret->rx_packets, &ret->tx_packets);
141 string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "bytes"),
142 &ret->rx_bytes, &ret->tx_bytes);
143 string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "errors"),
144 &ret->rx_errors, &ret->tx_errors);
145 string_to_rx_tx_counters (ros_reply_param_val_by_key (r, "drops"),
146 &ret->rx_drops, &ret->tx_drops);
148 ret->mtu = sstrtoui (ros_reply_param_val_by_key (r, "mtu"));
149 ret->l2mtu = sstrtoui (ros_reply_param_val_by_key (r, "l2mtu"));
151 ret->dynamic = sstrtob (ros_reply_param_val_by_key (r, "dynamic"));
152 ret->running = sstrtob (ros_reply_param_val_by_key (r, "running"));
153 ret->enabled = !sstrtob (ros_reply_param_val_by_key (r, "disabled"));
155 ret->next = rt_reply_to_interface (ros_reply_next (r));
158 } /* }}} ros_interface_t *rt_reply_to_interface */
160 static void if_interface_free (ros_interface_t *r) /* {{{ */
162 ros_interface_t *next;
170 } /* }}} void if_interface_free */
172 static int if_internal_handler (ros_connection_t *c, /* {{{ */
173 const ros_reply_t *r, void *user_data)
175 ros_interface_t *if_data;
176 rt_internal_data_t *internal_data;
179 if_data = rt_reply_to_interface (r);
183 internal_data = user_data;
185 status = internal_data->handler (c, if_data, internal_data->user_data);
187 if_interface_free (if_data);
190 } /* }}} int if_internal_handler */
195 int ros_interface (ros_connection_t *c, /* {{{ */
196 ros_interface_handler handler, void *user_data)
198 rt_internal_data_t data;
200 if ((c == NULL) || (handler == NULL))
203 data.handler = handler;
204 data.user_data = user_data;
206 return (ros_query (c, "/interface/print",
207 /* args_num = */ 0, /* args = */ NULL,
208 if_internal_handler, &data));
209 } /* }}} int ros_interface */
211 /* vim: set ts=2 sw=2 noet fdm=marker : */