a122330022b14feeec748c16cf200aa17c7a5377
1 /*
2 * libJUNOS - src/junos.h
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * Base object used to manage the connection to a JUNOS device.
30 */
32 #ifndef JUNOS_H
33 #define JUNOS_H 1
35 #include <stdio.h>
37 #include <libxml/tree.h>
39 #if defined(DEBUG) && (! defined(NDEBUG))
40 # define dprintf(...) fprintf(stderr, "LIBJUNOS DEBUG: "__VA_ARGS__)
41 #else
42 # define dprintf(...) /* noop */
43 #endif
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
49 /*
50 * data types
51 */
53 typedef struct junos junos_t;
55 /* method invocation */
57 enum {
58 JUNOS_NO_ARGS = 0,
59 JUNOS_ARG_TOGGLE,
60 JUNOS_ARG_TOGGLE_NO,
61 JUNOS_ARG_STRING,
62 JUNOS_ARG_INTEGER,
63 JUNOS_ARG_DOUBLE,
64 JUNOS_ARG_DOM,
65 JUNOS_ATTR_STRING,
66 JUNOS_ATTR_INTEGER,
67 JUNOS_ATTR_DOUBLE,
68 };
70 /* string buffer */
72 typedef struct junos_strbuf junos_strbuf_t;
74 /* netrc */
76 typedef struct {
77 char *machine;
78 char *login;
79 char *password;
80 /* we don't care for 'account' or 'macdef' */
81 } junos_netrc_entry_t;
83 typedef struct junos_netrc junos_netrc_t;
85 /* access types */
87 typedef struct junos_ssh_access junos_ssh_access_t;
89 /* error information */
91 enum {
92 JUNOS_OK = 0,
93 JUNOS_SYS_ERROR,
94 JUNOS_GAI_ERROR,
95 JUNOS_XML_ERROR,
96 JUNOS_ACCESS_ERROR
97 };
99 typedef struct {
100 int type;
101 int error;
102 char errmsg[1024];
103 } junos_error_t;
105 #define JUNOS_NO_ERROR { JUNOS_OK, 0, "" }
107 /*
108 * JUNOS object
109 */
111 int
112 junos_init(void);
114 junos_t *
115 junos_new(char *hostname, char *username, char *password);
117 char *
118 junos_get_hostname(junos_t *junos);
119 char *
120 junos_get_username(junos_t *junos);
121 char *
122 junos_get_password(junos_t *junos);
124 int
125 junos_connect(junos_t *junos);
127 xmlDocPtr
128 junos_invoke_method(junos_t *junos, const char *name, ...);
130 #define junos_simple_method(junos, name) \
131 junos_invoke_method((junos), (name), JUNOS_NO_ARGS)
133 int
134 junos_disconnect(junos_t *junos);
136 void
137 junos_free(junos_t *junos);
139 /*
140 * string buffer
141 */
143 junos_strbuf_t *
144 junos_strbuf_new(size_t size);
146 void
147 junos_strbuf_free(junos_strbuf_t *strbuf);
149 ssize_t
150 junos_strbuf_sprintf(junos_strbuf_t *strbuf, const char *fmt, ...);
152 ssize_t
153 junos_strbuf_vsprintf(junos_strbuf_t *strbuf, const char *fmt, va_list ap);
155 char *
156 junos_strbuf_string(junos_strbuf_t *strbuf);
158 size_t
159 junos_strbuf_len(junos_strbuf_t *strbuf);
161 /*
162 * netrc
163 */
165 junos_netrc_t *
166 junos_netrc_read(char *filename);
168 void
169 junos_netrc_free(junos_netrc_t *netrc);
171 const junos_netrc_entry_t *
172 junos_netrc_lookup(junos_netrc_t *netrc, char *hostname);
174 /*
175 * error handling
176 */
178 const char *
179 junos_get_errstr(junos_t *junos);
181 int
182 junos_set_error(junos_t *junos, int type, int error,
183 char *msg_prefix, ...);
184 int
185 junos_set_verror(junos_t *junos, int type, int error,
186 char *msg_prefix, va_list ap);
188 int
189 junos_set_ssh_error(junos_error_t *err, junos_ssh_access_t *ssh,
190 char *msg_prefix, ...);
191 int
192 junos_set_ssh_verror(junos_error_t *err, junos_ssh_access_t *ssh,
193 char *msg_prefix, va_list ap);
195 void
196 junos_clear_error(junos_t *junos);
198 /*
199 * SSH
200 */
202 junos_ssh_access_t *
203 junos_ssh_new(junos_t *junos);
205 int
206 junos_ssh_connect(junos_ssh_access_t *ssh);
208 ssize_t
209 junos_ssh_recv(junos_ssh_access_t *ssh, char *buf, size_t buf_len);
211 ssize_t
212 junos_ssh_send(junos_ssh_access_t *ssh, char *buf, size_t buf_len);
214 int
215 junos_ssh_disconnect(junos_ssh_access_t *ssh);
217 int
218 junos_ssh_free(junos_ssh_access_t *ssh);
220 #ifdef __cplusplus
221 } /* extern "C" */
222 #endif
224 #endif /* ! JUNOS_H */
226 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */