libssh  0.6.5
priv.h
1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2003-2009 by Aris Adamantiadis
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * priv.h file
23  * This include file contains everything you shouldn't deal with in
24  * user programs. Consider that anything in this file might change
25  * without notice; libssh.h file will keep backward compatibility
26  * on binary & source
27  */
28 
29 #ifndef _LIBSSH_PRIV_H
30 #define _LIBSSH_PRIV_H
31 
32 #include "config.h"
33 
34 #if !defined(HAVE_STRTOULL)
35 # if defined(HAVE___STRTOULL)
36 # define strtoull __strtoull
37 # elif defined(HAVE__STRTOUI64)
38 # define strtoull _strtoui64
39 # elif defined(__hpux) && defined(__LP64__)
40 # define strtoull strtoul
41 # else
42 # error "no strtoull function found"
43 # endif
44 #endif /* !defined(HAVE_STRTOULL) */
45 
46 #ifdef _WIN32
47 
48 /* Imitate define of inttypes.h */
49 # ifndef PRIdS
50 # define PRIdS "Id"
51 # endif
52 
53 # ifndef PRIu64
54 # if __WORDSIZE == 64
55 # define PRIu64 "lu"
56 # else
57 # define PRIu64 "llu"
58 # endif /* __WORDSIZE */
59 # endif /* PRIu64 */
60 
61 # ifdef _MSC_VER
62 # include <stdio.h>
63 
64 /* On Microsoft compilers define inline to __inline on all others use inline */
65 # undef inline
66 # define inline __inline
67 
68 # define strcasecmp _stricmp
69 # define strncasecmp _strnicmp
70 # if ! defined(HAVE_ISBLANK)
71 # define isblank(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n' || (ch) == '\r')
72 # endif
73 
74 # define usleep(X) Sleep(((X)+1000)/1000)
75 
76 # undef strtok_r
77 # define strtok_r strtok_s
78 
79 # if defined(HAVE__SNPRINTF_S)
80 # undef snprintf
81 # define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
82 # else /* HAVE__SNPRINTF_S */
83 # if defined(HAVE__SNPRINTF)
84 # undef snprintf
85 # define snprintf _snprintf
86 # else /* HAVE__SNPRINTF */
87 # if !defined(HAVE_SNPRINTF)
88 # error "no snprintf compatible function found"
89 # endif /* HAVE_SNPRINTF */
90 # endif /* HAVE__SNPRINTF */
91 # endif /* HAVE__SNPRINTF_S */
92 
93 # if defined(HAVE__VSNPRINTF_S)
94 # undef vsnprintf
95 # define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
96 # else /* HAVE__VSNPRINTF_S */
97 # if defined(HAVE__VSNPRINTF)
98 # undef vsnprintf
99 # define vsnprintf _vsnprintf
100 # else
101 # if !defined(HAVE_VSNPRINTF)
102 # error "No vsnprintf compatible function found"
103 # endif /* HAVE_VSNPRINTF */
104 # endif /* HAVE__VSNPRINTF */
105 # endif /* HAVE__VSNPRINTF_S */
106 
107 # endif /* _MSC_VER */
108 
109 struct timeval;
110 int gettimeofday(struct timeval *__p, void *__t);
111 
112 #else /* _WIN32 */
113 
114 #include <unistd.h>
115 #define PRIdS "zd"
116 
117 #endif /* _WIN32 */
118 
119 #include "libssh/libssh.h"
120 #include "libssh/callbacks.h"
121 
122 /* some constants */
123 #ifndef MAX_PACKAT_LEN
124 #define MAX_PACKET_LEN 262144
125 #endif
126 #ifndef ERROR_BUFFERLEN
127 #define ERROR_BUFFERLEN 1024
128 #endif
129 #ifndef CLIENTBANNER1
130 #define CLIENTBANNER1 "SSH-1.5-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
131 #endif
132 #ifndef CLIENTBANNER2
133 #define CLIENTBANNER2 "SSH-2.0-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
134 #endif
135 #ifndef KBDINT_MAX_PROMPT
136 #define KBDINT_MAX_PROMPT 256 /* more than openssh's :) */
137 #endif
138 #ifndef MAX_BUF_SIZE
139 #define MAX_BUF_SIZE 4096
140 #endif
141 
142 #ifndef HAVE_COMPILER__FUNC__
143 # ifdef HAVE_COMPILER__FUNCTION__
144 # define __func__ __FUNCTION__
145 # else
146 # error "Your system must provide a __func__ macro"
147 # endif
148 #endif
149 
150 #if defined(HAVE_GCC_THREAD_LOCAL_STORAGE)
151 # define LIBSSH_THREAD __thread
152 #elif defined(HAVE_MSC_THREAD_LOCAL_STORAGE)
153 # define LIBSSH_THREAD __declspec(thread)
154 #else
155 # define LIBSSH_THREAD
156 #endif
157 
158 /*
159  * This makes sure that the compiler doesn't optimize out the code
160  *
161  * Use it in a macro where the provided variable is 'x'.
162  */
163 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
164 # define LIBSSH_MEM_PROTECTION __asm__ volatile("" : : "r"(&(x)) : "memory")
165 #else
166 # define LIBSSH_MEM_PROTECTION
167 #endif
168 
169 #ifdef HAVE_SYS_TIME_H
170 #include <sys/time.h>
171 #endif
172 
173 /* forward declarations */
174 struct ssh_common_struct;
175 struct ssh_kex_struct;
176 
177 int ssh_get_key_params(ssh_session session, ssh_key *privkey);
178 
179 /* LOGGING */
180 void ssh_log_function(int verbosity,
181  const char *function,
182  const char *buffer);
183 #define SSH_LOG(priority, ...) \
184  _ssh_log(priority, __func__, __VA_ARGS__)
185 
186 /* LEGACY */
187 void ssh_log_common(struct ssh_common_struct *common,
188  int verbosity,
189  const char *function,
190  const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
191 
192 
193 /* ERROR HANDLING */
194 
195 /* error handling structure */
196 struct error_struct {
197  int error_code;
198  char error_buffer[ERROR_BUFFERLEN];
199 };
200 
201 #define ssh_set_error(error, code, ...) \
202  _ssh_set_error(error, code, __func__, __VA_ARGS__)
203 void _ssh_set_error(void *error,
204  int code,
205  const char *function,
206  const char *descr, ...) PRINTF_ATTRIBUTE(4, 5);
207 
208 #define ssh_set_error_oom(error) \
209  _ssh_set_error_oom(error, __func__)
210 void _ssh_set_error_oom(void *error, const char *function);
211 
212 #define ssh_set_error_invalid(error) \
213  _ssh_set_error_invalid(error, __func__)
214 void _ssh_set_error_invalid(void *error, const char *function);
215 
216 
217 /* server.c */
218 #ifdef WITH_SERVER
219 int ssh_auth_reply_default(ssh_session session,int partial);
220 int ssh_auth_reply_success(ssh_session session, int partial);
221 #endif
222 /* client.c */
223 
224 int ssh_send_banner(ssh_session session, int is_server);
225 
226 /* connect.c */
227 socket_t ssh_connect_host(ssh_session session, const char *host,const char
228  *bind_addr, int port, long timeout, long usec);
229 socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
230  const char *bind_addr, int port);
231 
232 /* in base64.c */
233 ssh_buffer base64_to_bin(const char *source);
234 unsigned char *bin_to_base64(const unsigned char *source, int len);
235 
236 /* gzip.c */
237 int compress_buffer(ssh_session session,ssh_buffer buf);
238 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
239 
240 /* match.c */
241 int match_hostname(const char *host, const char *pattern, unsigned int len);
242 
243 
244 
245 
247 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
248 
250 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
251 
253 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
254 
256 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
257 
258 /*
259  * See http://llvm.org/bugs/show_bug.cgi?id=15495
260  */
261 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
262 
263 # define BURN_STRING(x) do { \
264  if ((x) != NULL) \
265  memset((x), '\0', strlen((x))); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
266  } while(0)
267 
269 # define BURN_BUFFER(x, size) do { \
270  if ((x) != NULL) \
271  memset((x), '\0', (size)); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
272  } while(0)
273 #else /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
274 
275 # define BURN_STRING(x) do { \
276  if ((x) != NULL) memset((x), '\0', strlen((x))); \
277  } while(0)
278 
280 # define BURN_BUFFER(x, size) do { \
281  if ((x) != NULL) \
282  memset((x), '\0', (size)); \
283  } while(0)
284 #endif /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
285 
298 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
299 
303 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
304 
305 #endif /* _LIBSSH_PRIV_H */
306 /* vim: set ts=4 sw=4 et cindent: */