libssh: priv.h Source File

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