errsock.c 1.4 KB

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