errsock.c 1.5 KB

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