libssh  0.8.91
The SSH library
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 <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #if !defined(HAVE_STRTOULL)
37 # if defined(HAVE___STRTOULL)
38 # define strtoull __strtoull
39 # elif defined(HAVE__STRTOUI64)
40 # define strtoull _strtoui64
41 # elif defined(__hpux) && defined(__LP64__)
42 # define strtoull strtoul
43 # else
44 # error "no strtoull function found"
45 # endif
46 #endif /* !defined(HAVE_STRTOULL) */
47 
48 #if !defined(HAVE_STRNDUP)
49 char *strndup(const char *s, size_t n);
50 #endif /* ! HAVE_STRNDUP */
51 
52 #ifdef HAVE_BYTESWAP_H
53 #include <byteswap.h>
54 #endif
55 
56 #ifdef HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
58 #endif
59 
60 #ifndef bswap_32
61 #define bswap_32(x) \
62  ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
63  (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
64 #endif
65 
66 #ifdef _WIN32
67 
68 /* Imitate define of inttypes.h */
69 # ifndef PRIdS
70 # define PRIdS "Id"
71 # endif
72 
73 # ifndef PRIu64
74 # if __WORDSIZE == 64
75 # define PRIu64 "lu"
76 # else
77 # define PRIu64 "llu"
78 # endif /* __WORDSIZE */
79 # endif /* PRIu64 */
80 
81 # ifndef PRIu32
82 # define PRIu32 "u"
83 # endif /* PRIu32 */
84 
85 # ifndef PRIx64
86 # if __WORDSIZE == 64
87 # define PRIx64 "lx"
88 # else
89 # define PRIx64 "llx"
90 # endif /* __WORDSIZE */
91 # endif /* PRIx64 */
92 
93 # ifndef PRIx32
94 # define PRIx32 "x"
95 # endif /* PRIx32 */
96 
97 # ifdef _MSC_VER
98 # include <stdio.h>
99 # include <stdarg.h> /* va_copy define check */
100 
101 /* On Microsoft compilers define inline to __inline on all others use inline */
102 # undef inline
103 # define inline __inline
104 
105 # ifndef va_copy
106 # define va_copy(dest, src) (dest = src)
107 # endif
108 
109 # define strcasecmp _stricmp
110 # define strncasecmp _strnicmp
111 # if ! defined(HAVE_ISBLANK)
112 # define isblank(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n' || (ch) == '\r')
113 # endif
114 
115 # define usleep(X) Sleep(((X)+1000)/1000)
116 
117 # undef strtok_r
118 # define strtok_r strtok_s
119 
120 # if defined(HAVE__SNPRINTF_S)
121 # undef snprintf
122 # define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
123 # else /* HAVE__SNPRINTF_S */
124 # if defined(HAVE__SNPRINTF)
125 # undef snprintf
126 # define snprintf _snprintf
127 # else /* HAVE__SNPRINTF */
128 # if !defined(HAVE_SNPRINTF)
129 # error "no snprintf compatible function found"
130 # endif /* HAVE_SNPRINTF */
131 # endif /* HAVE__SNPRINTF */
132 # endif /* HAVE__SNPRINTF_S */
133 
134 # if defined(HAVE__VSNPRINTF_S)
135 # undef vsnprintf
136 # define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
137 # else /* HAVE__VSNPRINTF_S */
138 # if defined(HAVE__VSNPRINTF)
139 # undef vsnprintf
140 # define vsnprintf _vsnprintf
141 # else
142 # if !defined(HAVE_VSNPRINTF)
143 # error "No vsnprintf compatible function found"
144 # endif /* HAVE_VSNPRINTF */
145 # endif /* HAVE__VSNPRINTF */
146 # endif /* HAVE__VSNPRINTF_S */
147 
148 # ifndef _SSIZE_T_DEFINED
149 # undef ssize_t
150 # include <BaseTsd.h>
151  typedef _W64 SSIZE_T ssize_t;
152 # define _SSIZE_T_DEFINED
153 # endif /* _SSIZE_T_DEFINED */
154 
155 # endif /* _MSC_VER */
156 
157 struct timeval;
158 int gettimeofday(struct timeval *__p, void *__t);
159 
160 #define _XCLOSESOCKET closesocket
161 
162 #else /* _WIN32 */
163 
164 #include <unistd.h>
165 #define PRIdS "zd"
166 
167 #define _XCLOSESOCKET close
168 
169 #endif /* _WIN32 */
170 
171 #include "libssh/libssh.h"
172 #include "libssh/callbacks.h"
173 
174 /* some constants */
175 #ifndef MAX_PACKAT_LEN
176 #define MAX_PACKET_LEN 262144
177 #endif
178 #ifndef ERROR_BUFFERLEN
179 #define ERROR_BUFFERLEN 1024
180 #endif
181 
182 #ifndef CLIENT_BANNER_SSH2
183 #define CLIENT_BANNER_SSH2 "SSH-2.0-libssh_" SSH_STRINGIFY(LIBSSH_VERSION)
184 #endif /* CLIENT_BANNER_SSH2 */
185 
186 #ifndef KBDINT_MAX_PROMPT
187 #define KBDINT_MAX_PROMPT 256 /* more than openssh's :) */
188 #endif
189 #ifndef MAX_BUF_SIZE
190 #define MAX_BUF_SIZE 4096
191 #endif
192 
193 #ifndef HAVE_COMPILER__FUNC__
194 # ifdef HAVE_COMPILER__FUNCTION__
195 # define __func__ __FUNCTION__
196 # else
197 # error "Your system must provide a __func__ macro"
198 # endif
199 #endif
200 
201 #if defined(HAVE_GCC_THREAD_LOCAL_STORAGE)
202 # define LIBSSH_THREAD __thread
203 #elif defined(HAVE_MSC_THREAD_LOCAL_STORAGE)
204 # define LIBSSH_THREAD __declspec(thread)
205 #else
206 # define LIBSSH_THREAD
207 #endif
208 
209 /*
210  * This makes sure that the compiler doesn't optimize out the code
211  *
212  * Use it in a macro where the provided variable is 'x'.
213  */
214 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
215 # define LIBSSH_MEM_PROTECTION __asm__ volatile("" : : "r"(&(x)) : "memory")
216 #else
217 # define LIBSSH_MEM_PROTECTION
218 #endif
219 
220 /* forward declarations */
221 struct ssh_common_struct;
222 struct ssh_kex_struct;
223 
224 int ssh_get_key_params(ssh_session session, ssh_key *privkey);
225 
226 /* LOGGING */
227 void ssh_log_function(int verbosity,
228  const char *function,
229  const char *buffer);
230 #define SSH_LOG(priority, ...) \
231  _ssh_log(priority, __func__, __VA_ARGS__)
232 
233 /* LEGACY */
234 void ssh_log_common(struct ssh_common_struct *common,
235  int verbosity,
236  const char *function,
237  const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
238 
239 
240 /* ERROR HANDLING */
241 
242 /* error handling structure */
243 struct error_struct {
244  int error_code;
245  char error_buffer[ERROR_BUFFERLEN];
246 };
247 
248 #define ssh_set_error(error, code, ...) \
249  _ssh_set_error(error, code, __func__, __VA_ARGS__)
250 void _ssh_set_error(void *error,
251  int code,
252  const char *function,
253  const char *descr, ...) PRINTF_ATTRIBUTE(4, 5);
254 
255 #define ssh_set_error_oom(error) \
256  _ssh_set_error_oom(error, __func__)
257 void _ssh_set_error_oom(void *error, const char *function);
258 
259 #define ssh_set_error_invalid(error) \
260  _ssh_set_error_invalid(error, __func__)
261 void _ssh_set_error_invalid(void *error, const char *function);
262 
263 void ssh_reset_error(void *error);
264 
265 /* server.c */
266 #ifdef WITH_SERVER
267 int ssh_auth_reply_default(ssh_session session,int partial);
268 int ssh_auth_reply_success(ssh_session session, int partial);
269 #endif
270 /* client.c */
271 
272 int ssh_send_banner(ssh_session session, int is_server);
273 
274 /* connect.c */
275 socket_t ssh_connect_host(ssh_session session, const char *host,const char
276  *bind_addr, int port, long timeout, long usec);
277 socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
278  const char *bind_addr, int port);
279 
280 /* in base64.c */
281 ssh_buffer base64_to_bin(const char *source);
282 unsigned char *bin_to_base64(const unsigned char *source, int len);
283 
284 /* gzip.c */
285 int compress_buffer(ssh_session session,ssh_buffer buf);
286 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
287 
288 /* match.c */
289 int match_pattern_list(const char *string, const char *pattern,
290  unsigned int len, int dolower);
291 int match_hostname(const char *host, const char *pattern, unsigned int len);
292 
293 /* connector.c */
294 int ssh_connector_set_event(ssh_connector connector, ssh_event event);
295 int ssh_connector_remove_event(ssh_connector connector);
296 
297 #ifndef MIN
298 #define MIN(a,b) ((a) < (b) ? (a) : (b))
299 #endif
300 
301 #ifndef MAX
302 #define MAX(a,b) ((a) > (b) ? (a) : (b))
303 #endif
304 
306 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
307 
309 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
310 
312 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
313 
315 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
316 
317 #ifndef HAVE_EXPLICIT_BZERO
318 void explicit_bzero(void *s, size_t n);
319 #endif /* !HAVE_EXPLICIT_BZERO */
320 
333 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
334 
338 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
339 
343 /*
344  * Since MSVC 2010 there is a bug in passing __VA_ARGS__ to subsequent
345  * macros as a single token, which results in:
346  * warning C4003: not enough actual parameters for macro '_VA_ARG_N'
347  * and incorrect behavior. This fixes issue.
348  */
349 #define VA_APPLY_VARIADIC_MACRO(macro, tuple) macro tuple
350 
351 #define __VA_NARG__(...) \
352  (__VA_NARG_(__VA_ARGS__, __RSEQ_N()))
353 #define __VA_NARG_(...) \
354  VA_APPLY_VARIADIC_MACRO(__VA_ARG_N, (__VA_ARGS__))
355 #define __VA_ARG_N( \
356  _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
357  _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
358  _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
359  _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
360  _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
361  _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
362  _61,_62,_63,N,...) N
363 #define __RSEQ_N() \
364  63, 62, 61, 60, \
365  59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
366  49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
367  39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
368  29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
369  19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
370  9, 8, 7, 6, 5, 4, 3, 2, 1, 0
371 
372 #define CLOSE_SOCKET(s) do { if ((s) != SSH_INVALID_SOCKET) { _XCLOSESOCKET(s); (s) = SSH_INVALID_SOCKET;} } while(0)
373 
374 #ifndef HAVE_HTONLL
375 # ifdef WORDS_BIGENDIAN
376 # define htonll(x) (x)
377 # else
378 # define htonll(x) \
379  (((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
380 # endif
381 #endif
382 
383 #ifndef HAVE_NTOHLL
384 # ifdef WORDS_BIGENDIAN
385 # define ntohll(x) (x)
386 # else
387 # define ntohll(x) \
388  (((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
389 # endif
390 #endif
391 
392 #ifndef FALL_THROUGH
393 # ifdef HAVE_FALLTHROUGH_ATTRIBUTE
394 # define FALL_THROUGH __attribute__ ((fallthrough))
395 # else /* HAVE_FALLTHROUGH_ATTRIBUTE */
396 # define FALL_THROUGH
397 # endif /* HAVE_FALLTHROUGH_ATTRIBUTE */
398 #endif /* FALL_THROUGH */
399 
400 #ifndef __unused__
401 # ifdef HAVE_UNUSED_ATTRIBUTE
402 # define __unused__ __attribute__((unused))
403 # else /* HAVE_UNUSED_ATTRIBUTE */
404 # define __unused__
405 # endif /* HAVE_UNUSED_ATTRIBUTE */
406 #endif /* __unused__ */
407 
408 #ifndef UNUSED_PARAM
409 #define UNUSED_PARAM(param) param __unused__
410 #endif /* UNUSED_PARAM */
411 
412 #ifndef UNUSED_VAR
413 #define UNUSED_VAR(var) __unused__ var
414 #endif /* UNUSED_VAR */
415 
416 void ssh_agent_state_free(void *data);
417 
418 #endif /* _LIBSSH_PRIV_H */
Definition: kex.h:29
Definition: pki.h:42
Definition: poll.c:745
Definition: session.h:109
Definition: connector.c:52
Definition: priv.h:243
Definition: session.h:103
Definition: buffer.c:47