doc/librouteros.pod: Describe the high level "interface" interface.
[routeros-api.git] / doc / librouteros.pod
1 =head1 NAME
2
3 librouteros - Library for accessing MikroTik's RouterOS via its API
4
5 =head1 DESCRIPTION
6
7 B<librouteros> is a library to communicate with I<RouterOS>, the operating
8 system of I<MikroTik's> I<RouterBoards>. It uses the "API port" provided by
9 those systems to connect and talk to the devices. librouteros is a low-level
10 library in that it abstracts the network protocol used but has next to no
11 knowledge about the commands and responses available. Should such an high-level
12 interface prove useful, it will be added as the need arises.
13
14 =head1 GENERAL USAGE
15
16 The interface of librouteros is designed to be lightweight and simple to use.
17 The first thing when using the library is to create a connection object. Once a
18 connection has been established and logging in succeeded, queries can be sent
19 to the remote device. The library will encode and send the request, read back a
20 reply, decode it and pass it to a callback function. The callback function has
21 access to the parsed reply and can easily access all parts and parameters. It
22 does not need to worry about memory management because everything provided to
23 it will be freed by the library before returning to where the query was called
24 from. When the application is done talking to the device and disconnects, the
25 connection object will be freed.
26
27 =head1 INTERFACES
28
29 =head2 Connection
30
31 The connection to a I<RouterOS> device is represented by a pointer to a
32 B<ros_connection_t> type. This is an opaque data type which cannot be
33 dereferenced or manipulated directly.
34
35 Connection management is done with the following functions:
36
37 =over 4
38
39 =item ros_connection_t *B<ros_connect> (const char *I<node>, const char *I<service>, const char *I<username>, const char *I<password>)
40
41 Connects to the remote device using I<node> as the address or hostname. If
42 I<service> is C<NULL>, the standard port B<8728> will be used. When a
43 connection has been established, the library will try to authenticate using
44 I<username> and I<password>.
45
46 On failure, C<NULL> is returned and B<errno> is set appropriately.
47
48 =item int B<ros_disconnect> (ros_connection_t *I<c>)
49
50 Disconnects from the device and frees all memory associated with the
51 connection. After this call, I<c> may not be used anymore.
52
53 Returns zero upon success and an error code otherwise.
54
55 =back
56
57 =head2 General / low level queries
58
59 This interface abstracts the network protocol only and leaves actually
60 formulating queries and interpreting replies to the calling code. This is meant
61 for queries and features for which no higher level interface exists.
62
63 The query is sent to the daemon using the B<ros_query> function. The reply is
64 decoded and passed to a callback function with the following prototype, also
65 called B<ros_reply_handler_t>:
66
67   int callback (ros_connection_t *c, const ros_reply_t *r, void *user_data);
68
69 The reply is broken into parts (or "sentences") and passed to the callback
70 function as a linked list of type B<ros_reply_t>. The callback uses a couple of
71 helper functions to iterate over this list and fetch parameters as necessary.
72
73 The first and second arguments are objects representing the connection and the
74 reply respectively. I<user_data> is a pointer as passed to B<ros_query> (see
75 below). The arguments I<c> and I<r> may not be modified or freed. They will be
76 freed after the function returns, so pointers to this data are invalid after
77 this.
78
79 The value returned by the callback function will be returned by B<ros_query> to
80 the calling code. To distinguish from error codes returned by B<ros_query> upon
81 errors, use negative values in the callback function.
82
83 General queries / replies are handled using the following functions:
84
85 =over 4
86
87 =item int B<ros_query> (ros_connection_t *I<c>, const char *I<command>, size_t I<args_num>, const char * const *I<args>, ros_reply_handler_t I<handler>, void *I<user_data>)
88
89 Sends the command I<command> with the I<args_num> arguments I<args> via the
90 connection I<c> and reads back the reply. The reply is parsed and passed to the
91 callback function I<handler>. I<user_data> is a pointer passed through to the
92 callback function. Please note that the callback function is called only once,
93 even if the reply consists of multiple parts. Use the B<ros_reply_next>
94 function (see below) to access the other parts.
95
96 Returns the value returned by the callback function upon success and an error
97 code otherwise.
98
99 =item const ros_reply_t *B<ros_reply_next> (const ros_reply_t *I<r>)
100
101 Each reply can consist of several parts or "sentences". If there is more than
102 one sentence returned by the device, this function will return the next part.
103 When the end of the list is reached, C<NULL> is returned.
104
105 =item int B<ros_reply_num> (const ros_reply_t *I<r>)
106
107 Returns the number of replies in the list pointed to by and including I<r>.
108
109 =item const char *B<ros_reply_status> (const ros_reply_t *I<r>)
110
111 Returns the status message of this part or reply. This is usually "re" for data
112 parts, "done" for the last part in a reply and "trap" for errors.
113 The returned pointer must not be freed.
114
115 =item const char *B<ros_reply_param_key_by_index> (const ros_reply_t *I<r>, unsigned int I<index>)
116
117 Returns the parameter key at index I<index> (starting with zero) of reply I<r>.
118 If I<index> is out of bounds, returns C<NULL>.
119 The returned pointer must not be freed.
120
121 =item const char *B<ros_reply_param_val_by_index> (const ros_reply_t *I<r>, unsigned int I<index>)
122
123 Returns the parameter value at index I<index> (starting with zero) of reply
124 I<r>. If I<index> is out of bounds, returns C<NULL>.
125 The returned pointer must not be freed.
126
127 =item const char *B<ros_reply_param_val_by_key> (const ros_reply_t *I<r>, const char *I<key>)
128
129 Returns the parameter value corresponding to key I<key> (or C<NULL> if there is
130 no such key) of reply I<r>.
131 The returned pointer must not be freed.
132
133 =back
134
135 =head2 High level interface functions for "interface"
136
137 This function and the associated struct provide basic information about the
138 device's interfaces in an easy and intuitive to use form. It is equivalent to
139 issuing the C</interface/print> command. As with the generic interface above, a
140 query function is called with a pointer to a callback function. That callback
141 function is then called with a list of B<ros_interface_t> structures.
142
143 The query function has the following prototype:
144
145  int ros_interface (ros_connection_t *c,
146      ros_interface_handler_t handler, void *user_data);
147
148 I<c> is a pointer to a connection object as returned by B<ros_connect>.
149
150 I<handler> is a function pointer to a callback function handling the result.
151 The callback function must have the following prototype, called
152 B<ros_interface_handler_t> for convenience:
153
154  int callback (ros_connection_t *c,
155      const ros_interface_t *i, void *user_data);
156
157 The argument I<i> is a pointer to the first element of the list of interfaces
158 received from the device. This struct is defined in E<lt>routeros_api.hE<gt>
159 and contains information such as the name of the device, received and
160 transmitted bytes and whether or not the interface is currently running. No
161 element of the list nor any of their members may be modified and will be freed
162 when the callback returns.
163
164 =head2 High level interface functions for "registration-table"
165
166 B<TODO>: Describe the registration-table interface.
167
168 =head1 ERROR HANDLING
169
170 Some of the functions above return an "error code". This error code can be
171 transferred to a string describing the error using L<strerror(3)> or
172 L<strerror_r(3)>. Since the error codes are all positive integers, it is
173 recommended to use negative return values in the callback functions to indicate
174 custom errors if appropriate.
175
176 =head1 THREAD SAFETY
177
178 librouteros uses only thread-safe functions and does not store any global data
179 itself. It is therefore fully thread and reentrant safe as long as you don't
180 call any functions with the same connection object.
181
182 =head1 LICENSE
183
184 librouteros is licensed under the GPLv2. No other version of the license is
185 applicable.
186
187 =head1 AUTHOR
188
189 librouteros is written by Florian octo Forster E<lt>octo at verplant.orgE<gt>.
190 It's homepage can be found at L<http://verplant.org/librouteros/>.
191
192 (c) 2009 by Florian octo Forster.