Code

strbuf: Added a string buffer class for dynamically growing strings.
[libjunos.git] / src / junos.h
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 /* string buffer */
57 typedef struct junos_strbuf junos_strbuf_t;
59 /* netrc */
61 typedef struct {
62         char *machine;
63         char *login;
64         char *password;
65         /* we don't care for 'account' or 'macdef' */
66 } junos_netrc_entry_t;
68 typedef struct junos_netrc junos_netrc_t;
70 /* access types */
72 typedef struct junos_ssh_access junos_ssh_access_t;
74 /* error information */
76 enum {
77         JUNOS_OK = 0,
78         JUNOS_SYS_ERROR,
79         JUNOS_GAI_ERROR,
80         JUNOS_XML_ERROR,
81         JUNOS_ACCESS_ERROR
82 };
84 typedef struct {
85         int  type;
86         int  error;
87         char errmsg[1024];
88 } junos_error_t;
90 #define JUNOS_NO_ERROR { JUNOS_OK, 0, "" }
92 /*
93  * JUNOS object
94  */
96 int
97 junos_init(void);
99 junos_t *
100 junos_new(char *hostname, char *username, char *password);
102 char *
103 junos_get_hostname(junos_t *junos);
104 char *
105 junos_get_username(junos_t *junos);
106 char *
107 junos_get_password(junos_t *junos);
109 int
110 junos_connect(junos_t *junos);
112 xmlDocPtr
113 junos_simple_method(junos_t *junos, const char *name);
115 int
116 junos_disconnect(junos_t *junos);
118 void
119 junos_free(junos_t *junos);
121 /*
122  * string buffer
123  */
125 junos_strbuf_t *
126 junos_strbuf_new(size_t size);
128 void
129 junos_strbuf_free(junos_strbuf_t *strbuf);
131 ssize_t
132 junos_strbuf_sprintf(junos_strbuf_t *strbuf, const char *fmt, ...);
134 ssize_t
135 junos_strbuf_vsprintf(junos_strbuf_t *strbuf, const char *fmt, va_list ap);
137 char *
138 junos_strbuf_string(junos_strbuf_t *strbuf);
140 size_t
141 junos_strbuf_len(junos_strbuf_t *strbuf);
143 /*
144  * netrc
145  */
147 junos_netrc_t *
148 junos_netrc_read(char *filename);
150 void
151 junos_netrc_free(junos_netrc_t *netrc);
153 const junos_netrc_entry_t *
154 junos_netrc_lookup(junos_netrc_t *netrc, char *hostname);
156 /*
157  * error handling
158  */
160 const char *
161 junos_get_errstr(junos_t *junos);
163 int
164 junos_set_error(junos_t *junos, int type, int error,
165                 char *msg_prefix, ...);
166 int
167 junos_set_verror(junos_t *junos, int type, int error,
168                 char *msg_prefix, va_list ap);
170 int
171 junos_set_ssh_error(junos_error_t *err, junos_ssh_access_t *ssh,
172                 char *msg_prefix, ...);
173 int
174 junos_set_ssh_verror(junos_error_t *err, junos_ssh_access_t *ssh,
175                 char *msg_prefix, va_list ap);
177 void
178 junos_clear_error(junos_t *junos);
180 /*
181  * SSH
182  */
184 junos_ssh_access_t *
185 junos_ssh_new(junos_t *junos);
187 int
188 junos_ssh_connect(junos_ssh_access_t *ssh);
190 ssize_t
191 junos_ssh_recv(junos_ssh_access_t *ssh, char *buf, size_t buf_len);
193 ssize_t
194 junos_ssh_send(junos_ssh_access_t *ssh, char *buf, size_t buf_len);
196 int
197 junos_ssh_disconnect(junos_ssh_access_t *ssh);
199 int
200 junos_ssh_free(junos_ssh_access_t *ssh);
202 #ifdef __cplusplus
203 } /* extern "C" */
204 #endif
206 #endif /* ! JUNOS_H */
208 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */