libssh  0.7.2
include/libssh/priv.h
00001 /*
00002  * This file is part of the SSH Library
00003  *
00004  * Copyright (c) 2003-2009 by Aris Adamantiadis
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 /*
00022  * priv.h file
00023  * This include file contains everything you shouldn't deal with in
00024  * user programs. Consider that anything in this file might change
00025  * without notice; libssh.h file will keep backward compatibility
00026  * on binary & source
00027  */
00028 
00029 #ifndef _LIBSSH_PRIV_H
00030 #define _LIBSSH_PRIV_H
00031 
00032 #include "config.h"
00033 
00034 #if !defined(HAVE_STRTOULL)
00035 # if defined(HAVE___STRTOULL)
00036 #  define strtoull __strtoull
00037 # elif defined(HAVE__STRTOUI64)
00038 #  define strtoull _strtoui64
00039 # elif defined(__hpux) && defined(__LP64__)
00040 #  define strtoull strtoul
00041 # else
00042 #  error "no strtoull function found"
00043 # endif
00044 #endif /* !defined(HAVE_STRTOULL) */
00045 
00046 #ifdef _WIN32
00047 
00048 /* Imitate define of inttypes.h */
00049 # ifndef PRIdS
00050 #  define PRIdS "Id"
00051 # endif
00052 
00053 # ifndef PRIu64
00054 #  if __WORDSIZE == 64
00055 #   define PRIu64 "lu"
00056 #  else
00057 #   define PRIu64 "llu"
00058 #  endif /* __WORDSIZE */
00059 # endif /* PRIu64 */
00060 
00061 # ifdef _MSC_VER
00062 #  include <stdio.h>
00063 #  include <stdarg.h> /* va_copy define check */
00064 
00065 /* On Microsoft compilers define inline to __inline on all others use inline */
00066 #  undef inline
00067 #  define inline __inline
00068 
00069 #  ifndef va_copy
00070 #   define va_copy(dest, src) (dest = src)
00071 #  endif
00072 
00073 #  define strcasecmp _stricmp
00074 #  define strncasecmp _strnicmp
00075 #  if ! defined(HAVE_ISBLANK)
00076 #   define isblank(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n' || (ch) == '\r')
00077 #  endif
00078 
00079 #  define usleep(X) Sleep(((X)+1000)/1000)
00080 
00081 #  undef strtok_r
00082 #  define strtok_r strtok_s
00083 
00084 #  if defined(HAVE__SNPRINTF_S)
00085 #   undef snprintf
00086 #   define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
00087 #  else /* HAVE__SNPRINTF_S */
00088 #   if defined(HAVE__SNPRINTF)
00089 #     undef snprintf
00090 #     define snprintf _snprintf
00091 #   else /* HAVE__SNPRINTF */
00092 #    if !defined(HAVE_SNPRINTF)
00093 #     error "no snprintf compatible function found"
00094 #    endif /* HAVE_SNPRINTF */
00095 #   endif /* HAVE__SNPRINTF */
00096 #  endif /* HAVE__SNPRINTF_S */
00097 
00098 #  if defined(HAVE__VSNPRINTF_S)
00099 #   undef vsnprintf
00100 #   define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
00101 #  else /* HAVE__VSNPRINTF_S */
00102 #   if defined(HAVE__VSNPRINTF)
00103 #    undef vsnprintf
00104 #    define vsnprintf _vsnprintf
00105 #   else
00106 #    if !defined(HAVE_VSNPRINTF)
00107 #     error "No vsnprintf compatible function found"
00108 #    endif /* HAVE_VSNPRINTF */
00109 #   endif /* HAVE__VSNPRINTF */
00110 #  endif /* HAVE__VSNPRINTF_S */
00111 
00112 # endif /* _MSC_VER */
00113 
00114 struct timeval;
00115 int gettimeofday(struct timeval *__p, void *__t);
00116 
00117 #define _XCLOSESOCKET closesocket
00118 
00119 #else /* _WIN32 */
00120 
00121 #include <unistd.h>
00122 #define PRIdS "zd"
00123 
00124 #define _XCLOSESOCKET close
00125 
00126 #endif /* _WIN32 */
00127 
00128 #include "libssh/libssh.h"
00129 #include "libssh/callbacks.h"
00130 
00131 /* some constants */
00132 #ifndef MAX_PACKAT_LEN
00133 #define MAX_PACKET_LEN 262144
00134 #endif
00135 #ifndef ERROR_BUFFERLEN
00136 #define ERROR_BUFFERLEN 1024
00137 #endif
00138 #ifndef CLIENTBANNER1
00139 #define CLIENTBANNER1 "SSH-1.5-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
00140 #endif
00141 #ifndef CLIENTBANNER2
00142 #define CLIENTBANNER2 "SSH-2.0-libssh-" SSH_STRINGIFY(LIBSSH_VERSION)
00143 #endif
00144 #ifndef KBDINT_MAX_PROMPT
00145 #define KBDINT_MAX_PROMPT 256 /* more than openssh's :) */
00146 #endif
00147 #ifndef MAX_BUF_SIZE
00148 #define MAX_BUF_SIZE 4096
00149 #endif
00150 
00151 #ifndef HAVE_COMPILER__FUNC__
00152 # ifdef HAVE_COMPILER__FUNCTION__
00153 #  define __func__ __FUNCTION__
00154 # else
00155 #  error "Your system must provide a __func__ macro"
00156 # endif
00157 #endif
00158 
00159 #if defined(HAVE_GCC_THREAD_LOCAL_STORAGE)
00160 # define LIBSSH_THREAD __thread
00161 #elif defined(HAVE_MSC_THREAD_LOCAL_STORAGE)
00162 # define LIBSSH_THREAD __declspec(thread)
00163 #else
00164 # define LIBSSH_THREAD
00165 #endif
00166 
00167 /*
00168  * This makes sure that the compiler doesn't optimize out the code
00169  *
00170  * Use it in a macro where the provided variable is 'x'.
00171  */
00172 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
00173 # define LIBSSH_MEM_PROTECTION __asm__ volatile("" : : "r"(&(x)) : "memory")
00174 #else
00175 # define LIBSSH_MEM_PROTECTION
00176 #endif
00177 
00178 #ifdef HAVE_SYS_TIME_H
00179 #include <sys/time.h>
00180 #endif
00181 
00182 /* forward declarations */
00183 struct ssh_common_struct;
00184 struct ssh_kex_struct;
00185 
00186 int ssh_get_key_params(ssh_session session, ssh_key *privkey);
00187 
00188 /* LOGGING */
00189 void ssh_log_function(int verbosity,
00190                       const char *function,
00191                       const char *buffer);
00192 #define SSH_LOG(priority, ...) \
00193     _ssh_log(priority, __func__, __VA_ARGS__)
00194 
00195 /* LEGACY */
00196 void ssh_log_common(struct ssh_common_struct *common,
00197                     int verbosity,
00198                     const char *function,
00199                     const char *format, ...) PRINTF_ATTRIBUTE(4, 5);
00200 
00201 
00202 /* ERROR HANDLING */
00203 
00204 /* error handling structure */
00205 struct error_struct {
00206     int error_code;
00207     char error_buffer[ERROR_BUFFERLEN];
00208 };
00209 
00210 #define ssh_set_error(error, code, ...) \
00211     _ssh_set_error(error, code, __func__, __VA_ARGS__)
00212 void _ssh_set_error(void *error,
00213                     int code,
00214                     const char *function,
00215                     const char *descr, ...) PRINTF_ATTRIBUTE(4, 5);
00216 
00217 #define ssh_set_error_oom(error) \
00218     _ssh_set_error_oom(error, __func__)
00219 void _ssh_set_error_oom(void *error, const char *function);
00220 
00221 #define ssh_set_error_invalid(error) \
00222     _ssh_set_error_invalid(error, __func__)
00223 void _ssh_set_error_invalid(void *error, const char *function);
00224 
00225 
00226 /* server.c */
00227 #ifdef WITH_SERVER
00228 int ssh_auth_reply_default(ssh_session session,int partial);
00229 int ssh_auth_reply_success(ssh_session session, int partial);
00230 #endif
00231 /* client.c */
00232 
00233 int ssh_send_banner(ssh_session session, int is_server);
00234 
00235 /* connect.c */
00236 socket_t ssh_connect_host(ssh_session session, const char *host,const char
00237         *bind_addr, int port, long timeout, long usec);
00238 socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
00239     const char *bind_addr, int port);
00240 
00241 /* in base64.c */
00242 ssh_buffer base64_to_bin(const char *source);
00243 unsigned char *bin_to_base64(const unsigned char *source, int len);
00244 
00245 /* gzip.c */
00246 int compress_buffer(ssh_session session,ssh_buffer buf);
00247 int decompress_buffer(ssh_session session,ssh_buffer buf, size_t maxlen);
00248 
00249 /* match.c */
00250 int match_hostname(const char *host, const char *pattern, unsigned int len);
00251 
00252 #ifndef MIN
00253 #define MIN(a,b) ((a) < (b) ? (a) : (b))
00254 #endif
00255 
00257 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
00258 
00260 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
00261 
00263 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
00264 
00266 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
00267 
00268 /*
00269  * See http://llvm.org/bugs/show_bug.cgi?id=15495
00270  */
00271 #if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
00272 
00273 # define BURN_STRING(x) do { \
00274     if ((x) != NULL) \
00275         memset((x), '\0', strlen((x))); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
00276   } while(0)
00277 
00279 # define BURN_BUFFER(x, size) do { \
00280     if ((x) != NULL) \
00281         memset((x), '\0', (size)); __asm__ volatile("" : : "r"(&(x)) : "memory"); \
00282   } while(0)
00283 #else /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
00284 
00285 # define BURN_STRING(x) do { \
00286     if ((x) != NULL) memset((x), '\0', strlen((x))); \
00287   } while(0)
00288 
00290 # define BURN_BUFFER(x, size) do { \
00291     if ((x) != NULL) \
00292         memset((x), '\0', (size)); \
00293   } while(0)
00294 #endif /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
00295 
00308 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
00309 
00313 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
00314 
00318 #ifdef HAVE_GCC_NARG_MACRO
00319 /*
00320  * Since MSVC 2010 there is a bug in passing __VA_ARGS__ to subsequent
00321  * macros as a single token, which results in:
00322  *    warning C4003: not enough actual parameters for macro '_VA_ARG_N'
00323  *  and incorrect behavior. This fixes issue.
00324  */
00325 #define VA_APPLY_VARIADIC_MACRO(macro, tuple) macro tuple
00326 
00327 #define __VA_NARG__(...) \
00328         (__VA_NARG_(_0, ## __VA_ARGS__, __RSEQ_N()) - 1)
00329 #define __VA_NARG_(...) \
00330         VA_APPLY_VARIADIC_MACRO(__VA_ARG_N, (__VA_ARGS__))
00331 #define __VA_ARG_N( \
00332          _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
00333         _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
00334         _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
00335         _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
00336         _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
00337         _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
00338         _61,_62,_63,N,...) N
00339 #define __RSEQ_N() \
00340         63, 62, 61, 60,                         \
00341         59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
00342         49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
00343         39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
00344         29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
00345         19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
00346          9,  8,  7,  6,  5,  4,  3,  2,  1,  0
00347 #else
00348 /* clang does not support the above construction */
00349 #define __VA_NARG__(...) (-1)
00350 #endif
00351 
00352 #define CLOSE_SOCKET(s) do { if ((s) != SSH_INVALID_SOCKET) { _XCLOSESOCKET(s); (s) = SSH_INVALID_SOCKET;} } while(0)
00353 
00354 #endif /* _LIBSSH_PRIV_H */
00355 /* vim: set ts=4 sw=4 et cindent: */