2 * libmikrotik - src/ros.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
37 #include "routeros_api.h"
40 # define __attribute__(x) /**/
43 static const char *opt_username = "admin";
45 static int result_handler (ros_connection_t *c, const ros_reply_t *r, /* {{{ */
53 printf ("Status: %s\n", ros_reply_status (r));
55 for (i = 0; /* true */; i++)
60 key = ros_reply_param_key_by_index (r, i);
61 val = ros_reply_param_val_by_index (r, i);
63 if ((key == NULL) || (val == NULL))
66 fprintf (stderr, "val is NULL but key is %s!\n", key);
68 fprintf (stderr, "key is NULL but val is %s!\n", val);
72 printf (" Param %u: %s = %s\n", i, key, val);
77 return (result_handler (c, ros_reply_next (r), user_data));
78 } /* }}} int result_handler */
80 static void regtable_dump (const ros_registration_table_t *r) /* {{{ */
85 printf ("=== %s / %s ===\n", r->interface, r->radio_name);
86 printf ("Mode: %12s\n",
87 r->ap ? (r->wds ? "AP with WDS" : "Access point") : "Station");
88 printf ("Rate: %7g Mbps / %7g Mbps\n", r->rx_rate, r->tx_rate);
89 printf ("Packets: %12"PRIu64" / %12"PRIu64"\n",
90 r->rx_packets, r->tx_packets);
91 printf ("Bytes: %12"PRIu64" / %12"PRIu64"\n",
92 r->rx_bytes, r->tx_bytes);
93 printf ("Frames %12"PRIu64" / %12"PRIu64"\n",
94 r->rx_frames, r->tx_frames);
95 printf ("Frame Bytes: %12"PRIu64" / %12"PRIu64"\n",
96 r->rx_frame_bytes, r->tx_frame_bytes);
97 printf ("HW Frames: %12"PRIu64" / %12"PRIu64"\n",
98 r->rx_hw_frames, r->tx_hw_frames);
99 printf ("HW Frame Bytes: %12"PRIu64" / %12"PRIu64"\n",
100 r->rx_hw_frame_bytes, r->tx_hw_frame_bytes);
101 printf ("Quality: %10g %% / %10g %%\n",
102 r->rx_ccq, r->tx_ccq);
103 printf ("Signal str.: %8g dBm / %8g dBm\n",
104 r->rx_signal_strength, r->tx_signal_strength);
105 printf ("Signal / noise: %8g dBm\n", r->signal_to_noise);
106 printf ("==========\n");
108 regtable_dump (r->next);
109 } /* }}} void regtable_dump */
111 static int regtable_handler (__attribute__((unused)) ros_connection_t *c, /* {{{ */
112 const ros_registration_table_t *r,
113 __attribute__((unused)) void *user_data)
117 } /* }}} int regtable_handler */
119 static void interface_dump (const ros_interface_t *i) /* {{{ */
124 printf ("=== %s ===\n"
127 "Bytes: %12"PRIu64" / %12"PRIu64"\n"
128 "Packets: %12"PRIu64" / %12"PRIu64"\n"
129 "Errors: %12"PRIu64" / %12"PRIu64"\n"
130 "Drops: %12"PRIu64" / %12"PRIu64"\n"
137 i->name, i->type, i->comment,
138 i->rx_bytes, i->tx_bytes,
139 i->rx_packets, i->tx_packets,
140 i->rx_errors, i->tx_errors,
141 i->rx_drops, i->tx_drops,
143 i->running ? "true" : "false",
144 i->dynamic ? "true" : "false",
145 i->enabled ? "true" : "false");
147 interface_dump (i->next);
148 } /* }}} void interface_dump */
150 static int interface_handler (__attribute__((unused)) ros_connection_t *c, /* {{{ */
151 const ros_interface_t *i,
152 __attribute__((unused)) void *user_data)
156 } /* }}} int interface_handler */
158 static char *read_password (void) /* {{{ */
161 struct termios old_flags;
162 struct termios new_flags;
168 tty = fopen ("/dev/tty", "w+");
171 fprintf (stderr, "Unable to open /dev/tty: %s\n",
176 fprintf (tty, "Password for user %s: ", opt_username);
179 memset (&old_flags, 0, sizeof (old_flags));
180 tcgetattr (fileno (tty), &old_flags);
181 new_flags = old_flags;
183 new_flags.c_lflag &= ~ECHO;
185 new_flags.c_lflag |= ECHONL;
187 status = tcsetattr (fileno (tty), TCSANOW, &new_flags);
190 fprintf (stderr, "tcsetattr failed: %s\n", strerror (errno));
195 fgets (buffer, sizeof (buffer), tty);
196 buffer[sizeof (buffer) - 1] = 0;
197 buffer_len = strlen (buffer);
199 status = tcsetattr (fileno (tty), TCSANOW, &old_flags);
201 fprintf (stderr, "tcsetattr failed: %s\n", strerror (errno));
206 while ((buffer_len > 0) && ((buffer[buffer_len-1] == '\n') || (buffer[buffer_len-1] == '\r')))
209 buffer[buffer_len] = 0;
214 passwd = malloc (strlen (buffer) + 1);
217 memcpy (passwd, buffer, strlen (buffer) + 1);
218 memset (buffer, 0, sizeof (buffer));
221 } /* }}} char *read_password */
223 static void exit_usage (void) /* {{{ */
225 printf ("Usage: ros [options] <host> <command> [args]\n");
227 } /* }}} void exit_usage */
229 int main (int argc, char **argv) /* {{{ */
238 while ((option = getopt (argc, argv, "u:h?")) != -1)
243 opt_username = optarg;
254 if ((argc - optind) < 2)
258 command = argv[optind+1];
260 passwd = read_password ();
264 c = ros_connect (argv[optind], ROUTEROS_API_PORT,
265 opt_username, passwd);
266 memset (passwd, 0, strlen (passwd));
269 fprintf (stderr, "ros_connect failed: %s\n", strerror (errno));
273 if (command[0] == '/')
275 ros_query (c, command,
276 (size_t) (argc - (optind + 2)), (const char * const *) (argv + optind + 2),
277 result_handler, /* user data = */ NULL);
279 else if (strcmp ("interface", command) == 0)
281 ros_interface (c, interface_handler, /* user data = */ NULL);
283 else if (strcmp ("registration-table", command) == 0)
285 ros_registration_table (c, regtable_handler, /* user data = */ NULL);
289 fprintf (stderr, "Unknown built-in command %s. "
290 "Are you missing a leading slash?\n", command);
298 /* vim: set ts=2 sw=2 noet fdm=marker : */