bio_cb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #define OPENSSL_SUPPRESS_DEPRECATED
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "bio_local.h"
  14. #include "internal/cryptlib.h"
  15. #include <openssl/err.h>
  16. long BIO_debug_callback_ex(BIO *bio, int cmd, const char *argp, size_t len,
  17. int argi, long argl, int ret, size_t *processed)
  18. {
  19. BIO *b;
  20. char buf[256];
  21. char *p;
  22. int left;
  23. size_t l = 0;
  24. BIO_MMSG_CB_ARGS *args;
  25. long ret_ = ret;
  26. if (processed != NULL)
  27. l = *processed;
  28. left = BIO_snprintf(buf, sizeof(buf), "BIO[%p]: ", (void *)bio);
  29. /* Ignore errors and continue printing the other information. */
  30. if (left < 0)
  31. left = 0;
  32. p = buf + left;
  33. left = sizeof(buf) - left;
  34. switch (cmd) {
  35. case BIO_CB_FREE:
  36. BIO_snprintf(p, left, "Free - %s\n", bio->method->name);
  37. break;
  38. case BIO_CB_READ:
  39. if (bio->method->type & BIO_TYPE_DESCRIPTOR)
  40. BIO_snprintf(p, left, "read(%d,%zu) - %s fd=%d\n",
  41. bio->num, len,
  42. bio->method->name, bio->num);
  43. else
  44. BIO_snprintf(p, left, "read(%d,%zu) - %s\n",
  45. bio->num, len, bio->method->name);
  46. break;
  47. case BIO_CB_WRITE:
  48. if (bio->method->type & BIO_TYPE_DESCRIPTOR)
  49. BIO_snprintf(p, left, "write(%d,%zu) - %s fd=%d\n",
  50. bio->num, len,
  51. bio->method->name, bio->num);
  52. else
  53. BIO_snprintf(p, left, "write(%d,%zu) - %s\n",
  54. bio->num, len, bio->method->name);
  55. break;
  56. case BIO_CB_PUTS:
  57. BIO_snprintf(p, left, "puts() - %s\n", bio->method->name);
  58. break;
  59. case BIO_CB_GETS:
  60. BIO_snprintf(p, left, "gets(%zu) - %s\n", len,
  61. bio->method->name);
  62. break;
  63. case BIO_CB_CTRL:
  64. BIO_snprintf(p, left, "ctrl(%d) - %s\n", argi,
  65. bio->method->name);
  66. break;
  67. case BIO_CB_RECVMMSG:
  68. args = (BIO_MMSG_CB_ARGS *)argp;
  69. BIO_snprintf(p, left, "recvmmsg(%zu) - %s",
  70. args->num_msg, bio->method->name);
  71. break;
  72. case BIO_CB_SENDMMSG:
  73. args = (BIO_MMSG_CB_ARGS *)argp;
  74. BIO_snprintf(p, left, "sendmmsg(%zu) - %s",
  75. args->num_msg, bio->method->name);
  76. break;
  77. case BIO_CB_RETURN | BIO_CB_READ:
  78. BIO_snprintf(p, left, "read return %d processed: %zu\n", ret, l);
  79. break;
  80. case BIO_CB_RETURN | BIO_CB_WRITE:
  81. BIO_snprintf(p, left, "write return %d processed: %zu\n", ret, l);
  82. break;
  83. case BIO_CB_RETURN | BIO_CB_GETS:
  84. BIO_snprintf(p, left, "gets return %d processed: %zu\n", ret, l);
  85. break;
  86. case BIO_CB_RETURN | BIO_CB_PUTS:
  87. BIO_snprintf(p, left, "puts return %d processed: %zu\n", ret, l);
  88. break;
  89. case BIO_CB_RETURN | BIO_CB_CTRL:
  90. BIO_snprintf(p, left, "ctrl return %d\n", ret);
  91. break;
  92. case BIO_CB_RETURN | BIO_CB_RECVMMSG:
  93. BIO_snprintf(p, left, "recvmmsg processed: %zu\n", len);
  94. ret_ = (long)len;
  95. break;
  96. case BIO_CB_RETURN | BIO_CB_SENDMMSG:
  97. BIO_snprintf(p, left, "sendmmsg processed: %zu\n", len);
  98. ret_ = (long)len;
  99. break;
  100. default:
  101. BIO_snprintf(p, left, "bio callback - unknown type (%d)\n", cmd);
  102. break;
  103. }
  104. b = (BIO *)bio->cb_arg;
  105. if (b != NULL)
  106. BIO_write(b, buf, strlen(buf));
  107. #if !defined(OPENSSL_NO_STDIO)
  108. else
  109. fputs(buf, stderr);
  110. #endif
  111. return ret_;
  112. }
  113. #ifndef OPENSSL_NO_DEPRECATED_3_0
  114. long BIO_debug_callback(BIO *bio, int cmd, const char *argp,
  115. int argi, long argl, long ret)
  116. {
  117. size_t processed = 0;
  118. if (ret > 0)
  119. processed = (size_t)ret;
  120. BIO_debug_callback_ex(bio, cmd, argp, (size_t)argi,
  121. argi, argl, ret > 0 ? 1 : (int)ret, &processed);
  122. return ret;
  123. }
  124. #endif