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