errsock.c 1.6 KB

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