1
0

errsock.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 {
  11. char *error;
  12. Plug plug;
  13. const Socket_vtable *sockvt;
  14. } ErrorSocket;
  15. static Plug sk_error_plug(Socket s, Plug p)
  16. {
  17. ErrorSocket *es = FROMFIELD(s, ErrorSocket, sockvt);
  18. Plug ret = es->plug;
  19. if (p)
  20. es->plug = p;
  21. return ret;
  22. }
  23. static void sk_error_close(Socket s)
  24. {
  25. ErrorSocket *es = FROMFIELD(s, ErrorSocket, sockvt);
  26. sfree(es->error);
  27. sfree(es);
  28. }
  29. static const char *sk_error_socket_error(Socket s)
  30. {
  31. ErrorSocket *es = FROMFIELD(s, ErrorSocket, sockvt);
  32. return es->error;
  33. }
  34. static char *sk_error_peer_info(Socket s)
  35. {
  36. return NULL;
  37. }
  38. static const Socket_vtable ErrorSocket_sockvt = {
  39. sk_error_plug,
  40. sk_error_close,
  41. NULL /* write */,
  42. NULL /* write_oob */,
  43. NULL /* write_eof */,
  44. NULL /* flush */,
  45. NULL /* set_frozen */,
  46. sk_error_socket_error,
  47. sk_error_peer_info,
  48. };
  49. Socket new_error_socket(const char *errmsg, Plug plug)
  50. {
  51. ErrorSocket *es = snew(ErrorSocket);
  52. es->sockvt = &ErrorSocket_sockvt;
  53. es->plug = plug;
  54. es->error = dupstr(errmsg);
  55. return &es->sockvt;
  56. }