errsock.c 1.4 KB

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