libssh  0.7.2
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 # include <stdarg.h> /* va_copy define check */
64 
65 /* On Microsoft compilers define inline to __inline on all others use inline */
66 # undef inline
67 # define inline __inline
68 
69 # ifndef va_copy
70 # define va_copy(dest, src) (dest = src)
71 # endif
72 
73 # define strcasecmp _stricmp
74 # define strncasecmp _strnicmp
75 # if ! defined(HAVE_ISBLANK)
76 # define isblank(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n' || (ch) == '\r')
77 # endif
78 
79 # define usleep(X) Sleep(((X)+1000)/1000)
80 
81 # undef strtok_r
82 # define strtok_r strtok_s
83 
84 # if defined(HAVE__SNPRINTF_S)
85 # undef snprintf
86 # define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
87 # else /* HAVE__SNPRINTF_S */
88 # if defined(HAVE__SNPRINTF)
89 # undef snprintf
90 # define snprintf _snprintf
91 # else /* HAVE__SNPRINTF */
92 # if !defined(HAVE_SNPRINTF)
93 # error "no snprintf compatible function found"
94 # endif /* HAVE_SNPRINTF */
95 # endif /* HAVE__SNPRINTF */
96 # endif /* HAVE__SNPRINTF_S */
97 
98 # if defined(HAVE__VSNPRINTF_S)
99 # undef vsnprintf
100 # define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
101 # else /* HAVE__VSNPRINTF_S */
102 # if defined(HAVE__VSNPRINTF)
103 # undef vsnprintf
104 # define vsnprintf _vsnprintf
105 # else
106 # if !defined(HAVE_VSNPRINTF)
107 # error "No vsnprintf compatible function found"
108 # endif /* HAVE_VSNPRINTF */
109 # endif /* HAVE__VSNPRINTF */
110 # endif /* HAVE__VSNPRINTF_S */
111 
112 # endif /* _MSC_VER */
113 
114 struct timeval;
115 int gettimeofday(struct timeval *__p, void *__t);
116 
117 #define _XCLOSESOCKET closesocket
118 
119 #else /* _WIN32 */
120 
121 #include <unistd.h>
122 #define PRIdS "zd"
123 
124 #define _XCLOSESOCKET close
125 
126 #endif /* _WIN32 */
127 
128 #include "libssh/libssh.h"
129 #include "libssh/callbacks.h"
130 
131 /* some constants */
132 #ifndef MAX_PACKAT_LEN
133 #define MAX_PACKET_LEN 262144
134 #endif
135 #ifndef ERROR_BUFFERLEN
136 #define ERROR_BUFFERLEN 1024
137 #endif
138 #ifndef CLIENTBANNER1
139 #define CLIENTBANNER1 "SSH-1.5-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
140 #endif
141 #ifndef CLIENTBANNER2
142 #define CLIENTBANNER2 "SSH-2.0-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
143 #endif
144 #ifndef KBDINT_MAX_PROMPT
145 #define KBDINT_MAX_PROMPT 256 /* more than openssh's :) */
146 #endif
147 #ifndef MAX_BUF_SIZE
148 #define MAX_BUF_SIZE 4096
149 #endif
150 
151 #ifndef HAVE_COMPILER__FUNC__
152 # ifdef HAVE_COMPILER__FUNCTION__
153 # define __func__ __FUNCTION__
154 # else
155 # error "Your system must provide a __func__ macro"
156 # endif
157 #endif
158 
159 #if defined(HAVE_GCC_THREAD_LOCAL_STORAGE)
160 # define LIBSSH_THREAD __thread
161 #elif defined(HAVE_MSC_THREAD_LOCAL_STORAGE)
162 # define LIBSSH_THREAD __declspec(thread)
163 #else
164 # define LIBSSH_THREAD
165 #endif
166 
167 /*
168  * This makes sure that the compiler doesn't optimize out the code
169  *
170  * Use it in a macro where the provided variable is 'x'.
171  */
172 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
173 # define LIBSSH_MEM_PROTECTION __asm__ volatile("" : : "r"(&(x)) : "memory")
174 #else
175 # define LIBSSH_MEM_PROTECTION
176 #endif
177 
178 #ifdef HAVE_SYS_TIME_H
179 #include <sys/time.h>
180 #endif
181 
182 /* forward declarations */
183 struct ssh_common_struct;
184 struct ssh_kex_struct;
185 
186 int ssh_get_key_params(ssh_session session, ssh_key *privkey);
187 
188 /* LOGGING */
189 void ssh_log_function(int verbosity,
190  const char *function,
191  const char *buffer);
192 #define SSH_LOG(priority, ...) \
193  _ssh_log(priority, __func__, __VA_ARGS__)
194 
195 /* LEGACY */
196 void ssh_log_common(struct ssh_common_struct *common,
197  int verbosity,
198  const char *function,
199  const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
200 
201 
202 /* ERROR HANDLING */
203 
204 /* error handling structure */
205 struct error_struct {
206  int error_code;
207  char error_buffer[ERROR_BUFFERLEN];
208 };
209 
210 #define ssh_set_error(error, code, ...) \
211  _ssh_set_error(error, code, __func__, __VA_ARGS__)
212 void _ssh_set_error(void *error,
213  int code,
214  const char *function,
215  const char *descr, ...) PRINTF_ATTRIBUTE(4, 5);
216 
217 #define ssh_set_error_oom(error) \
218  _ssh_set_error_oom(error, __func__)
219 void _ssh_set_error_oom(void *error, const char *function);
220 
221 #define ssh_set_error_invalid(error) \
222  _ssh_set_error_invalid(error, __func__)
223 void _ssh_set_error_invalid(void *error, const char *function);
224 
225 
226 /* server.c */
227 #ifdef WITH_SERVER
228 int ssh_auth_reply_default(ssh_session session,int partial);
229 int ssh_auth_reply_success(ssh_session session, int partial);
230 #endif
231 /* client.c */
232 
233 int ssh_send_banner(ssh_session session, int is_server);
234 
235 /* connect.c */
236 socket_t ssh_connect_host(ssh_session session, const char *host,const char
237  *bind_addr, int port, long timeout, long usec);
238 socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
239  const char *bind_addr, int port);
240 
241 /* in base64.c */
242 ssh_buffer base64_to_bin(const char *source);
243 unsigned char *bin_to_base64(const unsigned char *source, int len);
244 
245 /* gzip.c */
246 int compress_buffer(ssh_session session,ssh_buffer buf);
247 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
248 
249 /* match.c */
250 int match_hostname(const char *host, const char *pattern, unsigned int len);
251 
252 #ifndef MIN
253 #define MIN(a,b) ((a) < (b) ? (a) : (b))
254 #endif
255 
257 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
258 
260 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
261 
263 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
264 
266 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
267 
268 /*
269  * See http://llvm.org/bugs/show_bug.cgi?id=15495
270  */
271 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
272 
273 # define BURN_STRING(x) do { \
274  if ((x) != NULL) \
275  memset((x), '\0', strlen((x))); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
276  } while(0)
277 
279 # define BURN_BUFFER(x, size) do { \
280  if ((x) != NULL) \
281  memset((x), '\0', (size)); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
282  } while(0)
283 #else /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
284 
285 # define BURN_STRING(x) do { \
286  if ((x) != NULL) memset((x), '\0', strlen((x))); \
287  } while(0)
288 
290 # define BURN_BUFFER(x, size) do { \
291  if ((x) != NULL) \
292  memset((x), '\0', (size)); \
293  } while(0)
294 #endif /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
295 
308 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
309 
313 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
314 
318 #ifdef HAVE_GCC_NARG_MACRO
319 /*
320  * Since MSVC 2010 there is a bug in passing __VA_ARGS__ to subsequent
321  * macros as a single token, which results in:
322  * warning C4003: not enough actual parameters for macro '_VA_ARG_N'
323  * and incorrect behavior. This fixes issue.
324  */
325 #define VA_APPLY_VARIADIC_MACRO(macro, tuple) macro tuple
326 
327 #define __VA_NARG__(...) \
328  (__VA_NARG_(_0, ## __VA_ARGS__, __RSEQ_N()) - 1)
329 #define __VA_NARG_(...) \
330  VA_APPLY_VARIADIC_MACRO(__VA_ARG_N, (__VA_ARGS__))
331 #define __VA_ARG_N( \
332  _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
333  _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
334  _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
335  _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
336  _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
337  _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
338  _61,_62,_63,N,...) N
339 #define __RSEQ_N() \
340  63, 62, 61, 60, \
341  59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
342  49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
343  39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
344  29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
345  19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
346  9, 8, 7, 6, 5, 4, 3, 2, 1, 0
347 #else
348 /* clang does not support the above construction */
349 #define __VA_NARG__(...) (-1)
350 #endif
351 
352 #define CLOSE_SOCKET(s) do { if ((s) != SSH_INVALID_SOCKET) { _XCLOSESOCKET(s); (s) = SSH_INVALID_SOCKET;} } while(0)
353 
354 #endif /* _LIBSSH_PRIV_H */
355 /* vim: set ts=4 sw=4 et cindent: */