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 | |
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 | |
17 | long BIO_debug_callback(BIO *bio, int cmd, const char *argp, |
18 | int argi, long argl, long ret) |
19 | { |
20 | BIO *b; |
21 | char buf[256]; |
22 | char *p; |
23 | long r = 1; |
24 | int len, left; |
25 | |
26 | if (BIO_CB_RETURN & cmd) |
27 | r = ret; |
28 | |
29 | len = BIO_snprintf(buf, sizeof(buf), "BIO[%p]: " , (void *)bio); |
30 | |
31 | /* Ignore errors and continue printing the other information. */ |
32 | if (len < 0) |
33 | len = 0; |
34 | p = buf + len; |
35 | left = sizeof(buf) - len; |
36 | |
37 | switch (cmd) { |
38 | case BIO_CB_FREE: |
39 | BIO_snprintf(p, left, "Free - %s\n" , bio->method->name); |
40 | break; |
41 | case BIO_CB_READ: |
42 | if (bio->method->type & BIO_TYPE_DESCRIPTOR) |
43 | BIO_snprintf(p, left, "read(%d,%lu) - %s fd=%d\n" , |
44 | bio->num, (unsigned long)argi, |
45 | bio->method->name, bio->num); |
46 | else |
47 | BIO_snprintf(p, left, "read(%d,%lu) - %s\n" , |
48 | bio->num, (unsigned long)argi, bio->method->name); |
49 | break; |
50 | case BIO_CB_WRITE: |
51 | if (bio->method->type & BIO_TYPE_DESCRIPTOR) |
52 | BIO_snprintf(p, left, "write(%d,%lu) - %s fd=%d\n" , |
53 | bio->num, (unsigned long)argi, |
54 | bio->method->name, bio->num); |
55 | else |
56 | BIO_snprintf(p, left, "write(%d,%lu) - %s\n" , |
57 | bio->num, (unsigned long)argi, bio->method->name); |
58 | break; |
59 | case BIO_CB_PUTS: |
60 | BIO_snprintf(p, left, "puts() - %s\n" , bio->method->name); |
61 | break; |
62 | case BIO_CB_GETS: |
63 | BIO_snprintf(p, left, "gets(%lu) - %s\n" , (unsigned long)argi, |
64 | bio->method->name); |
65 | break; |
66 | case BIO_CB_CTRL: |
67 | BIO_snprintf(p, left, "ctrl(%lu) - %s\n" , (unsigned long)argi, |
68 | bio->method->name); |
69 | break; |
70 | case BIO_CB_RETURN | BIO_CB_READ: |
71 | BIO_snprintf(p, left, "read return %ld\n" , ret); |
72 | break; |
73 | case BIO_CB_RETURN | BIO_CB_WRITE: |
74 | BIO_snprintf(p, left, "write return %ld\n" , ret); |
75 | break; |
76 | case BIO_CB_RETURN | BIO_CB_GETS: |
77 | BIO_snprintf(p, left, "gets return %ld\n" , ret); |
78 | break; |
79 | case BIO_CB_RETURN | BIO_CB_PUTS: |
80 | BIO_snprintf(p, left, "puts return %ld\n" , ret); |
81 | break; |
82 | case BIO_CB_RETURN | BIO_CB_CTRL: |
83 | BIO_snprintf(p, left, "ctrl return %ld\n" , ret); |
84 | break; |
85 | default: |
86 | BIO_snprintf(p, left, "bio callback - unknown type (%d)\n" , cmd); |
87 | break; |
88 | } |
89 | |
90 | b = (BIO *)bio->cb_arg; |
91 | if (b != NULL) |
92 | BIO_write(b, buf, strlen(buf)); |
93 | #if !defined(OPENSSL_NO_STDIO) |
94 | else |
95 | fputs(buf, stderr); |
96 | #endif |
97 | return r; |
98 | } |
99 | |