]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - errsock.c
first pass
[PuTTY.git] / errsock.c
1 /*
2  * A dummy Socket implementation which just holds an error message.
3  */
4
5 #include <stdio.h>
6 #include <assert.h>
7
8 #define DEFINE_PLUG_METHOD_MACROS
9 #include "tree234.h"
10 #include "putty.h"
11 #include "network.h"
12
13 typedef struct Socket_error_tag *Error_Socket;
14
15 struct Socket_error_tag {
16     const struct socket_function_table *fn;
17     /* the above variable absolutely *must* be the first in this structure */
18
19     char *error;
20     Plug plug;
21 };
22
23 static Plug sk_error_plug(Socket s, Plug p)
24 {
25     Error_Socket ps = (Error_Socket) s;
26     Plug ret = ps->plug;
27     if (p)
28         ps->plug = p;
29     return ret;
30 }
31
32 static void sk_error_close(Socket s)
33 {
34     Error_Socket ps = (Error_Socket) s;
35
36     sfree(ps->error);
37     sfree(ps);
38 }
39
40 static const char *sk_error_socket_error(Socket s)
41 {
42     Error_Socket ps = (Error_Socket) s;
43     return ps->error;
44 }
45
46 static char *sk_error_peer_info(Socket s)
47 {
48     return NULL;
49 }
50
51 Socket new_error_socket(const char *errmsg, Plug plug)
52 {
53     static const struct socket_function_table socket_fn_table = {
54         sk_error_plug,
55         sk_error_close,
56         NULL /* write */,
57         NULL /* write_oob */,
58         NULL /* write_eof */,
59         NULL /* flush */,
60         NULL /* set_frozen */,
61         sk_error_socket_error,
62         sk_error_peer_info,
63     };
64
65     Error_Socket ret;
66
67     ret = snew(struct Socket_error_tag);
68     ret->fn = &socket_fn_table;
69     ret->plug = plug;
70     ret->error = dupstr(errmsg);
71
72     return (Socket) ret;
73 }