errsock.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * A dummy Socket implementation which just holds an error message.
  3. */
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include "tree234.h"
  7. #include "putty.h"
  8. #include "network.h"
  9. typedef struct {
  10. char *error;
  11. Plug *plug;
  12. Socket sock;
  13. } ErrorSocket;
  14. static Plug *sk_error_plug(Socket *s, Plug *p)
  15. {
  16. ErrorSocket *es = container_of(s, ErrorSocket, sock);
  17. Plug *ret = es->plug;
  18. if (p)
  19. es->plug = p;
  20. return ret;
  21. }
  22. static void sk_error_close(Socket *s)
  23. {
  24. ErrorSocket *es = container_of(s, ErrorSocket, sock);
  25. sfree(es->error);
  26. sfree(es);
  27. }
  28. static const char *sk_error_socket_error(Socket *s)
  29. {
  30. ErrorSocket *es = container_of(s, ErrorSocket, sock);
  31. return es->error;
  32. }
  33. static SocketPeerInfo *sk_error_peer_info(Socket *s)
  34. {
  35. return NULL;
  36. }
  37. static const SocketVtable ErrorSocket_sockvt = {
  38. sk_error_plug,
  39. sk_error_close,
  40. NULL /* write */,
  41. NULL /* write_oob */,
  42. NULL /* write_eof */,
  43. NULL /* flush */,
  44. NULL /* set_frozen */,
  45. sk_error_socket_error,
  46. sk_error_peer_info,
  47. };
  48. static Socket *new_error_socket_internal(char *errmsg, Plug *plug)
  49. {
  50. ErrorSocket *es = snew(ErrorSocket);
  51. es->sock.vt = &ErrorSocket_sockvt;
  52. es->plug = plug;
  53. es->error = errmsg;
  54. return &es->sock;
  55. }
  56. Socket *new_error_socket_fmt(Plug *plug, const char *fmt, ...)
  57. {
  58. va_list ap;
  59. char *msg;
  60. va_start(ap, fmt);
  61. msg = dupvprintf(fmt, ap);
  62. va_end(ap);
  63. return new_error_socket_internal(msg, plug);
  64. }