2
0

bio_cb.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 1995-2017 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. if (processed != NULL)
  25. l = *processed;
  26. left = BIO_snprintf(buf, sizeof(buf), "BIO[%p]: ", (void *)bio);
  27. /* Ignore errors and continue printing the other information. */
  28. if (left < 0)
  29. left = 0;
  30. p = buf + left;
  31. left = sizeof(buf) - left;
  32. switch (cmd) {
  33. case BIO_CB_FREE:
  34. BIO_snprintf(p, left, "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, left, "read(%d,%zu) - %s fd=%d\n",
  39. bio->num, len,
  40. bio->method->name, bio->num);
  41. else
  42. BIO_snprintf(p, left, "read(%d,%zu) - %s\n",
  43. bio->num, len, bio->method->name);
  44. break;
  45. case BIO_CB_WRITE:
  46. if (bio->method->type & BIO_TYPE_DESCRIPTOR)
  47. BIO_snprintf(p, left, "write(%d,%zu) - %s fd=%d\n",
  48. bio->num, len,
  49. bio->method->name, bio->num);
  50. else
  51. BIO_snprintf(p, left, "write(%d,%zu) - %s\n",
  52. bio->num, len, bio->method->name);
  53. break;
  54. case BIO_CB_PUTS:
  55. BIO_snprintf(p, left, "puts() - %s\n", bio->method->name);
  56. break;
  57. case BIO_CB_GETS:
  58. BIO_snprintf(p, left, "gets(%zu) - %s\n", len,
  59. bio->method->name);
  60. break;
  61. case BIO_CB_CTRL:
  62. BIO_snprintf(p, left, "ctrl(%d) - %s\n", argi,
  63. bio->method->name);
  64. break;
  65. case BIO_CB_RETURN | BIO_CB_READ:
  66. BIO_snprintf(p, left, "read return %d processed: %zu\n", ret, l);
  67. break;
  68. case BIO_CB_RETURN | BIO_CB_WRITE:
  69. BIO_snprintf(p, left, "write return %d processed: %zu\n", ret, l);
  70. break;
  71. case BIO_CB_RETURN | BIO_CB_GETS:
  72. BIO_snprintf(p, left, "gets return %d processed: %zu\n", ret, l);
  73. break;
  74. case BIO_CB_RETURN | BIO_CB_PUTS:
  75. BIO_snprintf(p, left, "puts return %d processed: %zu\n", ret, l);
  76. break;
  77. case BIO_CB_RETURN | BIO_CB_CTRL:
  78. BIO_snprintf(p, left, "ctrl return %d\n", ret);
  79. break;
  80. default:
  81. BIO_snprintf(p, left, "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 ret;
  92. }
  93. #ifndef OPENSSL_NO_DEPRECATED_3_0
  94. long BIO_debug_callback(BIO *bio, int cmd, const char *argp,
  95. int argi, long argl, long ret)
  96. {
  97. size_t processed = 0;
  98. if (ret > 0)
  99. processed = (size_t)ret;
  100. BIO_debug_callback_ex(bio, cmd, argp, (size_t)argi,
  101. argi, argl, ret > 0 ? 1 : (int)ret, &processed);
  102. return ret;
  103. }
  104. #endif