lsh_types.h 5.11 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
/* lsh_types.h
 *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Niels Möller's avatar
Niels Möller committed
24 25
 */

Niels Möller's avatar
Niels Möller committed
26 27
#ifndef LSH_TYPES_H_INCLUDED
#define LSH_TYPES_H_INCLUDED
Niels Möller's avatar
Niels Möller committed
28 29 30 31 32

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

33 34 35 36 37
/* FIXME: This should probably be set in config.h by autoconf */

/* The crypt function requires _XOPEN_SOURCE, while the initgroups
 * function requires _BSD_SOURCE */
#define _GNU_SOURCE
Niels Möller's avatar
Niels Möller committed
38 39 40 41 42 43 44

#if SIZEOF_SHORT >= 4
#define UINT32 unsigned short
#elif SIZEOF_INT >= 4
#define UINT32 unsigned int
#elif SIZEOF_LONG >= 4
#define UINT32 unsigned long
Niels Möller's avatar
Niels Möller committed
45 46
#else
#error Ledsen error
Niels Möller's avatar
Niels Möller committed
47 48
#endif

Niels Möller's avatar
Niels Möller committed
49 50 51 52 53 54 55 56
#if SIZEOF_SHORT >= 2
#define UINT16 unsigned short
#elif SIZEOF_INT >= 2
#define UINT16 unsigned int
#else
#error Ledsen error
#endif

Niels Möller's avatar
Niels Möller committed
57 58
#define UINT8 unsigned char

59 60 61 62 63 64 65 66
#ifdef __GNUC__
#define NORETURN __attribute__ ((noreturn))
#define PRINTF_STYLE(f, a) __attribute__ ((format(printf, f, a)))
#else
#define NORETURN
#define PRINTF_STYLE(f, a)
#endif

Niels Möller's avatar
Niels Möller committed
67 68 69 70 71
/* Some macros */

/* Reads a 32-bit integer, in network byte order */
#define READ_UINT32(p)				\
((((UINT32) (p)[0]) << 24)			\
72 73 74
 | (((UINT32) (p)[1]) << 16)			\
 | (((UINT32) (p)[2]) << 8)			\
 | ((UINT32) (p)[3]))
Niels Möller's avatar
Niels Möller committed
75 76 77 78

#define WRITE_UINT32(p, i)			\
do {						\
  (p)[0] = ((i) >> 24) & 0xff;			\
79 80 81
  (p)[1] = ((i) >> 16) & 0xff;			\
  (p)[2] = ((i) >> 8) & 0xff;			\
  (p)[3] = (i) & 0xff;				\
Niels Möller's avatar
Niels Möller committed
82 83 84 85 86
} while(0)

#define MIN(a, b) (((a)>(b)) ? (b) : (a))
#define MAX(a, b) (((a)>(b)) ? (b) : (a))

87 88
/* Generic object */

89 90 91 92
#define LSH_ALLOC_HEAP 0
#define LSH_ALLOC_STATIC 1
#define LSH_ALLOC_STACK 2
     
93
#ifdef DEBUG_ALLOC
94
     
95 96
struct lsh_object
{
97
  int size;  /* Zero for objects that are not allocated on the heap. */
98 99
  char alloc_method;
  char marked;
100 101 102 103 104
};

struct lsh_string_header
{
  int magic;
105 106
};

107 108
#define STATIC_HEADER { 0, LSH_ALLOC_STATIC, 0 }
#define STACK_HEADER { 0, LSH_ALLOC_STACK, 0 }
109 110

#else   /* !DEBUG_ALLOC */
111 112 113 114 115 116
struct lsh_object
{
  char alloc_method;
  char marked;
};

117 118
struct lsh_string_header {};

119 120
#define STATIC_HEADER { LSH_ALLOC_STATIC, 0 }
#define STACK_HEADER { LSH_ALLOC_STACK, 0 }
121 122 123

#endif  /* !DEBUG_ALLOC */

Niels Möller's avatar
Niels Möller committed
124 125
struct lsh_string
{
126
  struct lsh_string_header header;
127
  
Niels Möller's avatar
Niels Möller committed
128 129 130
  UINT32 sequence_number;
  /* NOTE: The allocated size may be larger than the string length. */
  UINT32 length; 
Niels Möller's avatar
Niels Möller committed
131 132 133 134 135 136 137 138
  UINT8 data[1];
};

/* A closed function with no arguments */
struct callback;
typedef int (*callback_f)(struct callback *closure);
struct callback
{
139 140 141
  struct lsh_object header;
  
  int (*f)(struct callback *closure);
Niels Möller's avatar
Niels Möller committed
142 143 144 145
};

#define CALLBACK(c) ((c)->f(c))

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/* Return values.
 *
 * Every handler should return one or more of these values, ored together.
 * Zero means everything is ok.
 */

/* Success/fail indication. LSH_FAIL should always be combined with
 * LSH_DIE or LSH_CLOSE. */
#define LSH_OK 0
#define LSH_FAIL 1

#define LSH_FAILUREP(x) ((x) & 1)

/* Everything is ok */
#define LSH_GOON 0

/* Close the associated connection, after flushing buffers. May be
 * combined with LSH_FAIL. */
#define LSH_CLOSE 2

/* Close connection immediately. This is usually combined with
 * LSH_FAIL, but not always. For instance, when forking, the parent
 * process will return this flag in order to have its copy of the
 * filedescriptor closed. */
#define LSH_DIE  4

/* Close all other filedescriptors immediately. MAinly used when forking.
 * Can be combined with LSH_FAIL or LSH_DIE or both. */
#define LSH_KILL_OTHERS 8

/* Not used by the main loop, but is returned by authentication
 * handlers to indicate that the client's authentication was rejected.
 * This can result either in a fatal protocol failure, or in a request
 * to the client to try again. */
#define LSH_AUTH_FAILED 0x10

182 183 184 185 186 187 188
/* Returned by a read handler when it is (temporarily) not able to
 * read more data. Used for flow control. */
#define LSH_HOLD 0x20

/* Returned by channel callback functions when the channel is closed. */
#define LSH_CHANNEL_FINISHED 0x40

189 190 191 192 193 194 195
/* Non-zero if no messages can be sent over the connection. Used when
 * processing error codes from in the middle of the processing a
 * messages. If this is true, processing should stop, and most likely
 * return LSH_FAIL (ored together with the intermediate error code). */
#define LSH_CLOSEDP(x) (x & (LSH_FAIL | LSH_CLOSE | LSH_DIE) )

/* If non-zero, return to main-loop is preferred */
196
#define LSH_ACTIONP(x) ((x) & (LSH_FAIL | LSH_CLOSE | LSH_DIE | LSH_KILL_OTHERS) )
197 198 199 200 201 202 203

/* Are return codes really needed here? */
#if 0
#define LSH_EXIT(x) ((x) << 3)
#define LSH_GET_EXIT(x) ((x) >> 3)
#endif

Niels Möller's avatar
Niels Möller committed
204
#endif /* LSH_TYPES_H_INCLUDED */