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