bio_cb.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include "bio_lcl.h"
  13. #include "internal/cryptlib.h"
  14. #include <openssl/err.h>
  15. long BIO_debug_callback(BIO *bio, int cmd, const char *argp,
  16. int argi, long argl, long ret)
  17. {
  18. BIO *b;
  19. char buf[256];
  20. char *p;
  21. long r = 1;
  22. int len;
  23. size_t p_maxlen;
  24. if (BIO_CB_RETURN & cmd)
  25. r = ret;
  26. len = BIO_snprintf(buf, sizeof buf, "BIO[%p]: ", (void *)bio);
  27. /* Ignore errors and continue printing the other information. */
  28. if (len < 0)
  29. len = 0;
  30. p = buf + len;
  31. p_maxlen = sizeof(buf) - len;
  32. switch (cmd) {
  33. case BIO_CB_FREE:
  34. BIO_snprintf(p, p_maxlen, "Free - %s\n", bio->method->name);
  35. break;
  36. case BIO_CB_READ:
  37. if (bio->method->type & BIO_TYPE_DESCRIPTOR)
  38. BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s fd=%d\n",
  39. bio->num, (unsigned long)argi,
  40. bio->method->name, bio->num);
  41. else
  42. BIO_snprintf(p, p_maxlen, "read(%d,%lu) - %s\n",
  43. bio->num, (unsigned long)argi, bio->method->name);
  44. break;
  45. case BIO_CB_WRITE:
  46. if (bio->method->type & BIO_TYPE_DESCRIPTOR)
  47. BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s fd=%d\n",
  48. bio->num, (unsigned long)argi,
  49. bio->method->name, bio->num);
  50. else
  51. BIO_snprintf(p, p_maxlen, "write(%d,%lu) - %s\n",
  52. bio->num, (unsigned long)argi, bio->method->name);
  53. break;
  54. case BIO_CB_PUTS:
  55. BIO_snprintf(p, p_maxlen, "puts() - %s\n", bio->method->name);
  56. break;
  57. case BIO_CB_GETS:
  58. BIO_snprintf(p, p_maxlen, "gets(%lu) - %s\n", (unsigned long)argi,
  59. bio->method->name);
  60. break;
  61. case BIO_CB_CTRL:
  62. BIO_snprintf(p, p_maxlen, "ctrl(%lu) - %s\n", (unsigned long)argi,
  63. bio->method->name);
  64. break;
  65. case BIO_CB_RETURN | BIO_CB_READ:
  66. BIO_snprintf(p, p_maxlen, "read return %ld\n", ret);
  67. break;
  68. case BIO_CB_RETURN | BIO_CB_WRITE:
  69. BIO_snprintf(p, p_maxlen, "write return %ld\n", ret);
  70. break;
  71. case BIO_CB_RETURN | BIO_CB_GETS:
  72. BIO_snprintf(p, p_maxlen, "gets return %ld\n", ret);
  73. break;
  74. case BIO_CB_RETURN | BIO_CB_PUTS:
  75. BIO_snprintf(p, p_maxlen, "puts return %ld\n", ret);
  76. break;
  77. case BIO_CB_RETURN | BIO_CB_CTRL:
  78. BIO_snprintf(p, p_maxlen, "ctrl return %ld\n", ret);
  79. break;
  80. default:
  81. BIO_snprintf(p, p_maxlen, "bio callback - unknown type (%d)\n", cmd);
  82. break;
  83. }
  84. b = (BIO *)bio->cb_arg;
  85. if (b != NULL)
  86. BIO_write(b, buf, strlen(buf));
  87. #if !defined(OPENSSL_NO_STDIO)
  88. else
  89. fputs(buf, stderr);
  90. #endif
  91. return (r);
  92. }