Code

Renamed 'command' to 'method'.
[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 typedef struct {
56         char *machine;
57         char *login;
58         char *password;
59         /* we don't care for 'account' or 'macdef' */
60 } junos_netrc_entry_t;
62 typedef struct junos_netrc junos_netrc_t;
64 /* access types */
66 typedef struct junos_ssh_access junos_ssh_access_t;
68 /* error information */
70 enum {
71         JUNOS_OK = 0,
72         JUNOS_SYS_ERROR,
73         JUNOS_GAI_ERROR,
74         JUNOS_XML_ERROR,
75         JUNOS_ACCESS_ERROR
76 };
78 typedef struct {
79         int  type;
80         int  error;
81         char errmsg[1024];
82 } junos_error_t;
84 #define JUNOS_NO_ERROR { JUNOS_OK, 0, "" }
86 /*
87  * JUNOS object
88  */
90 int
91 junos_init(void);
93 junos_t *
94 junos_new(char *hostname, char *username, char *password);
96 char *
97 junos_get_hostname(junos_t *junos);
98 char *
99 junos_get_username(junos_t *junos);
100 char *
101 junos_get_password(junos_t *junos);
103 int
104 junos_connect(junos_t *junos);
106 xmlDocPtr
107 junos_simple_method(junos_t *junos, const char *name);
109 int
110 junos_disconnect(junos_t *junos);
112 void
113 junos_free(junos_t *junos);
115 /*
116  * netrc
117  */
119 junos_netrc_t *
120 junos_netrc_read(char *filename);
122 void
123 junos_netrc_free(junos_netrc_t *netrc);
125 const junos_netrc_entry_t *
126 junos_netrc_lookup(junos_netrc_t *netrc, char *hostname);
128 /*
129  * error handling
130  */
132 const char *
133 junos_get_errstr(junos_t *junos);
135 int
136 junos_set_error(junos_t *junos, int type, int error,
137                 char *msg_prefix, ...);
138 int
139 junos_set_verror(junos_t *junos, int type, int error,
140                 char *msg_prefix, va_list ap);
142 int
143 junos_set_ssh_error(junos_error_t *err, junos_ssh_access_t *ssh,
144                 char *msg_prefix, ...);
145 int
146 junos_set_ssh_verror(junos_error_t *err, junos_ssh_access_t *ssh,
147                 char *msg_prefix, va_list ap);
149 void
150 junos_clear_error(junos_t *junos);
152 /*
153  * SSH
154  */
156 junos_ssh_access_t *
157 junos_ssh_new(junos_t *junos);
159 int
160 junos_ssh_connect(junos_ssh_access_t *ssh);
162 ssize_t
163 junos_ssh_recv(junos_ssh_access_t *ssh, char *buf, size_t buf_len);
165 ssize_t
166 junos_ssh_send(junos_ssh_access_t *ssh, char *buf, size_t buf_len);
168 int
169 junos_ssh_disconnect(junos_ssh_access_t *ssh);
171 int
172 junos_ssh_free(junos_ssh_access_t *ssh);
174 #ifdef __cplusplus
175 } /* extern "C" */
176 #endif
178 #endif /* ! JUNOS_H */
180 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */