errsock.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. .plug = sk_error_plug,
  39. .close = sk_error_close,
  40. .socket_error = sk_error_socket_error,
  41. .peer_info = sk_error_peer_info,
  42. /* other methods are NULL */
  43. };
  44. Socket *new_error_socket_consume_string(Plug *plug, char *errmsg)
  45. {
  46. ErrorSocket *es = snew(ErrorSocket);
  47. es->sock.vt = &ErrorSocket_sockvt;
  48. es->plug = plug;
  49. es->error = errmsg;
  50. return &es->sock;
  51. }
  52. Socket *new_error_socket_fmt(Plug *plug, const char *fmt, ...)
  53. {
  54. va_list ap;
  55. char *msg;
  56. va_start(ap, fmt);
  57. msg = dupvprintf(fmt, ap);
  58. va_end(ap);
  59. return new_error_socket_consume_string(plug, msg);
  60. }