transport.h 2.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* transport.h
 *
 * ssh transport protocol, and supporting types
 */

#ifndef LSH_TRANSPORT_H_INCLUDED
#define LSH_TRANSPORT_H_INCLUDED

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

/* Generic packet */
struct simple_packet
{
  UINT32 length;
  UINT8 data[1];
}

struct ssh_packet
{
  UINT32 packet_length;  /* In network byteorder */
  UINT8 padding_length;
  UINT8 data[1];  /* Includes payload and padding */
};

/* Allocation */

/* The memory allocation model is as follows:
 *
 * Packets are allocated when the are needed. They may be passed
 * through a chain of processing functions, until it is finally
 * discarded or transmitted, at which time it is deallocated.
 * Processing functions may deallocate their input packets and
 * allocate fresh packets to pass on; therefore, any data from a
 * packet that is needed later must be copied into some other storage.
 *
 * At any time, each packet is own by a a particular processing
 * function. Pointers into a packet are valid only while you own it.
 * */

/* Size is the maximum amount of payload + padding that will be stored
 * in the packet. */

struct simple_packet *simple_packet_alloc(UINT32 size);
void simple_packet_free(struct simple_packet *packet);

/* Simple buffer */
struct simple_buffer
{
  UNIT32 capacity;
  UINT32 pos;
  UINT8 *data;
};

void simple_buffer_init(struct simple_buffer *buffer,
			UINT32 capacity, UINT8 *data);

/* Returns the number of octets that were actually written into the buffer */

UINT32 simple_buffer_write(struct simple_buffer *buffer,
			   UINT32 length, UINT32 *data);

UINT32 simple_buffer_avail(struct simple_buffer *buffer);

/* A packet processing function.
 *
 * Typically, real processors will extend this struct, with fields
 * such as the process parameters, next processor, output socket, etc.
 * */

typedef int (*raw_processor_function)(struct packet_processor *context,
				      struct simple_packet *packet);
struct packet_processor
{
  /* Returns some (so far unspecified) return code */
  raw_processor_function f;
};

int apply_processor(struct packet_processor *closure,
		    struct simple_packet *packet);

/* A processor that passes its result on to another processor */
struct chained_processor
{
  struct packet_processor p;
  struct *packet_processor *next;
};

#endif /* LSH_TRANSPORT_H_INCLUDED */