read_data.c 2.13 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* read_data.c
 *
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
26
 */

#include "read_data.h"
Niels Möller's avatar
Niels Möller committed
27
28
#include "werror.h"
#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
29

30
31
32
33
34
35
36
37
38
39
40
41
struct read_data
{
  struct read_handler super; /* Super type */

  UINT32 block_size;

  /* Where to send the data */
  struct abstract_write *handler;

  struct callback *close_callback;
};

Niels Möller's avatar
Niels Möller committed
42
43
static int do_read_data(struct read_handler **h,
			struct abstract_read *read)
Niels Möller's avatar
Niels Möller committed
44
{
Niels Möller's avatar
Niels Möller committed
45
  struct read_data *closure = (struct read_data *) *h;
46
47

  MDEBUG(closure);
Niels Möller's avatar
Niels Möller committed
48
  
Niels Möller's avatar
Niels Möller committed
49
50
  while(1)
    {
Niels Möller's avatar
Niels Möller committed
51
      struct lsh_string *packet = lsh_string_alloc(closure->block_size);
52
      int n = A_READ(read, packet->length, packet->data);
Niels Möller's avatar
Niels Möller committed
53
54
55
56
57
58
59
60
61
      
      switch(n)
	{
	case 0:
	  lsh_string_free(packet);
	  break;
	case A_FAIL:
	  /* Fall through */
	case A_EOF:
Niels Möller's avatar
Niels Möller committed
62
	  CALLBACK(closure->close_callback);
Niels Möller's avatar
Niels Möller committed
63
	  return LSH_OK | LSH_CLOSE;
Niels Möller's avatar
Niels Möller committed
64
	default:
65
66
67
68
69
	  {
	    int res;
	    packet->length = n;
	    /* FIXME: Use returned value */
	    res = A_WRITE(closure->handler, packet);
Niels Möller's avatar
Niels Möller committed
70
	    if (LSH_PROBLEMP(res))
71
72
73
	      return res;
	    break;
	  }
Niels Möller's avatar
Niels Möller committed
74
75
76
77
	}
    }
}

Niels Möller's avatar
Niels Möller committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
struct read_handler *make_read_data(struct abstract_write *handler,
				    struct callback *close_callback,
				    UINT32 block_size)
{
  struct read_data *closure = xalloc(sizeof(struct read_data));

  closure->super.handler = do_read_data;
  closure->block_size = block_size;

  closure->handler = handler;
  closure->close_callback = close_callback;

  return &closure->super;
}