b7934dfa625eccb6b32b820a75f11af1b6c0022a
[routeros-api.git] / src / ros.c
1 /**
2  * libmikrotik - src/ros.c
3  * Copyright (C) 2009  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <termios.h>
35 #include <getopt.h>
36
37 #include "routeros_api.h"
38
39 static const char *opt_username = "admin";
40
41 static int result_handler (ros_connection_t *c, const ros_reply_t *r, /* {{{ */
42                 void *user_data)
43 {
44         unsigned int i;
45
46         if (r == NULL)
47                 return (0);
48
49         printf ("Status: %s\n", ros_reply_status (r));
50
51         for (i = 0; /* true */; i++)
52         {
53                 const char *key;
54                 const char *val;
55
56                 key = ros_reply_param_key_by_index (r, i);
57                 val = ros_reply_param_val_by_index (r, i);
58
59                 if ((key == NULL) || (val == NULL))
60                 {
61                         if (key != NULL)
62                                 fprintf (stderr, "val is NULL but key is %s!\n", key);
63                         if (val != NULL)
64                                 fprintf (stderr, "key is NULL but val is %s!\n", val);
65                         break;
66                 }
67
68                 printf ("  Param %u: %s = %s\n", i, key, val);
69         }
70
71         printf ("===\n");
72
73         return (result_handler (c, ros_reply_next (r), user_data));
74 } /* }}} int result_handler */
75
76 static void regtable_dump (const ros_registration_table_t *r) /* {{{ */
77 {
78         if (r == NULL)
79                 return;
80
81         printf ("=== %s / %s ===\n", r->interface, r->radio_name);
82         printf ("Mode:           %12s\n",
83                         r->ap ? (r->wds ? "AP with WDS" : "Access point") : "Station");
84         printf ("Rate:           %7g Mbps / %7g Mbps\n", r->rx_rate, r->tx_rate);
85         printf ("Packets:        %12"PRIu64" / %12"PRIu64"\n",
86                         r->rx_packets, r->tx_packets);
87         printf ("Bytes:          %12"PRIu64" / %12"PRIu64"\n",
88                         r->rx_bytes, r->tx_bytes);
89         printf ("Frames          %12"PRIu64" / %12"PRIu64"\n",
90                         r->rx_frames, r->tx_frames);
91         printf ("Frame Bytes:    %12"PRIu64" / %12"PRIu64"\n",
92                         r->rx_frame_bytes, r->tx_frame_bytes);
93         printf ("HW Frames:      %12"PRIu64" / %12"PRIu64"\n",
94                         r->rx_hw_frames, r->tx_hw_frames);
95         printf ("HW Frame Bytes: %12"PRIu64" / %12"PRIu64"\n",
96                         r->rx_hw_frame_bytes, r->tx_hw_frame_bytes);
97         printf ("Quality:        %10g %% / %10g %%\n",
98                         r->rx_ccq, r->tx_ccq);
99         printf ("Signal str.:    %8g dBm / %8g dBm\n",
100                 r->rx_signal_strength, r->tx_signal_strength);
101         printf ("Signal / noise: %8g dBm\n", r->signal_to_noise);
102         printf ("==========\n");
103
104         regtable_dump (r->next);
105 } /* }}} void regtable_dump */
106
107 static int regtable_handler (ros_connection_t *c, /* {{{ */
108                 const ros_registration_table_t *r, void *user_data)
109 {
110         regtable_dump (r);
111         return (0);
112 } /* }}} int regtable_handler */
113
114 static void interface_dump (const ros_interface_t *i) /* {{{ */
115 {
116         if (i == NULL)
117                 return;
118
119         printf ("=== %s ===\n"
120                         "Type:    %12s\n"
121                         "Comment: %12s\n"
122                         "Bytes:   %12"PRIu64" / %12"PRIu64"\n"
123                         "Packets: %12"PRIu64" / %12"PRIu64"\n"
124                         "Errors:  %12"PRIu64" / %12"PRIu64"\n"
125                         "Drops:   %12"PRIu64" / %12"PRIu64"\n"
126                         "MTU:     %12u\n"
127                         "L2 MTU:  %12u\n"
128                         "Running: %12s\n"
129                         "Dynamic: %12s\n"
130                         "Enabled: %12s\n"
131                         "==========\n",
132                         i->name, i->type, i->comment,
133                         i->rx_bytes, i->tx_bytes,
134                         i->rx_packets, i->tx_packets,
135                         i->rx_errors, i->tx_errors,
136                         i->rx_drops, i->tx_drops,
137                         i->mtu, i->l2mtu,
138                         i->running ? "true" : "false",
139                         i->dynamic ? "true" : "false",
140                         i->enabled ? "true" : "false");
141
142         interface_dump (i->next);
143 } /* }}} void interface_dump */
144
145 static int interface_handler (ros_connection_t *c, /* {{{ */
146                 const ros_interface_t *i, void *user_data)
147 {
148         interface_dump (i);
149         return (0);
150 } /* }}} int interface_handler */
151
152 static char *read_password (void) /* {{{ */
153 {
154         FILE *tty;
155         struct termios old_flags;
156         struct termios new_flags;
157         int status;
158         char buffer[1024];
159         size_t buffer_len;
160         char *passwd;
161
162         tty = fopen ("/dev/tty", "w+");
163         if (tty == NULL)
164         {
165                 fprintf (stderr, "Unable to open /dev/tty: %s\n",
166                                 strerror (errno));
167                 return (NULL);
168         }
169
170         fprintf (tty, "Password for user %s: ", opt_username);
171         fflush (tty);
172
173         memset (&old_flags, 0, sizeof (old_flags));
174         tcgetattr (fileno (tty), &old_flags);
175         new_flags = old_flags;
176         /* clear ECHO */
177         new_flags.c_lflag &= ~ECHO;
178         /* set ECHONL */
179         new_flags.c_lflag |= ECHONL;
180
181         status = tcsetattr (fileno (tty), TCSANOW, &new_flags);
182         if (status != 0)
183         {
184                 fprintf (stderr, "tcsetattr failed: %s\n", strerror (errno));
185                 fclose (tty);
186                 return (NULL);
187         }
188
189         fgets (buffer, sizeof (buffer), tty);
190         buffer[sizeof (buffer) - 1] = 0;
191         buffer_len = strlen (buffer);
192
193         status = tcsetattr (fileno (tty), TCSANOW, &old_flags);
194         if (status != 0)
195                 fprintf (stderr, "tcsetattr failed: %s\n", strerror (errno));
196
197         fclose (tty);
198         tty = NULL;
199
200         while ((buffer_len > 0) && ((buffer[buffer_len-1] == '\n') || (buffer[buffer_len-1] == '\r')))
201         {
202                 buffer_len--;
203                 buffer[buffer_len] = 0;
204         }
205         if (buffer_len == 0)
206                 return (NULL);
207
208         passwd = malloc (strlen (buffer) + 1);
209         if (passwd == NULL)
210                 return (NULL);
211         memcpy (passwd, buffer, strlen (buffer) + 1);
212         memset (buffer, 0, sizeof (buffer));
213
214         return (passwd);
215 } /* }}} char *read_password */
216
217 static void exit_usage (void) /* {{{ */
218 {
219         printf ("Usage: ros [options] <host> <command> [args]\n");
220         exit (EXIT_SUCCESS);
221 } /* }}} void exit_usage */
222
223 int main (int argc, char **argv) /* {{{ */
224 {
225         ros_connection_t *c;
226         char *passwd;
227         const char *host;
228         const char *command;
229
230         int option;
231
232         while ((option = getopt (argc, argv, "u:h?")) != -1)
233         {
234                 switch (option)
235                 {
236                         case 'u':
237                                 opt_username = optarg;
238                                 break;
239
240                         case 'h':
241                         case '?':
242                         default:
243                                 exit_usage ();
244                                 break;
245                 }
246         }
247
248         if ((argc - optind) < 2)
249                 exit_usage ();
250
251         host = argv[optind];
252         command = argv[optind+1];
253
254         passwd = read_password ();
255         if (passwd == NULL)
256                 exit (EXIT_FAILURE);
257
258         c = ros_connect (argv[optind], ROUTEROS_API_PORT,
259                         opt_username, passwd);
260         memset (passwd, 0, strlen (passwd));
261         if (c == NULL)
262         {
263                 fprintf (stderr, "ros_connect failed: %s\n", strerror (errno));
264                 exit (EXIT_FAILURE);
265         }
266
267         if (command[0] == '/')
268         {
269                 ros_query (c, command,
270                                 (size_t) (argc - (optind + 2)), (const char * const *) (argv + optind + 2),
271                                 result_handler, /* user data = */ NULL);
272         }
273         else if (strcmp ("interface", command) == 0)
274         {
275                 ros_interface (c, interface_handler, /* user data = */ NULL);
276         }
277         else if (strcmp ("registration-table", command) == 0)
278         {
279                 ros_registration_table (c, regtable_handler, /* user data = */ NULL);
280         }
281         else
282         {
283                 fprintf (stderr, "Unknown built-in command %s. "
284                                 "Are you missing a leading slash?\n", command);
285         }
286
287         ros_disconnect (c);
288
289         return (0);
290 } /* }}} int main */
291
292 /* vim: set ts=2 sw=2 noet fdm=marker : */