Undo commit d420ac2
[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
26     if (BIO_CB_RETURN & cmd)
27         r = ret;
28
29     len = sprintf(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
36     switch (cmd) {
37     case BIO_CB_FREE:
38         sprintf(p, "Free - %s\n", bio->method->name);
39         break;
40     case BIO_CB_READ:
41         if (bio->method->type & BIO_TYPE_DESCRIPTOR)
42             sprintf(p, "read(%d,%lu) - %s fd=%d\n",
43                     bio->num, (unsigned long)argi,
44                     bio->method->name, bio->num);
45         else
46             sprintf(p, "read(%d,%lu) - %s\n",
47                     bio->num, (unsigned long)argi, bio->method->name);
48         break;
49     case BIO_CB_WRITE:
50         if (bio->method->type & BIO_TYPE_DESCRIPTOR)
51             sprintf(p, "write(%d,%lu) - %s fd=%d\n",
52                     bio->num, (unsigned long)argi,
53                     bio->method->name, bio->num);
54         else
55             sprintf(p, "write(%d,%lu) - %s\n",
56                     bio->num, (unsigned long)argi, bio->method->name);
57         break;
58     case BIO_CB_PUTS:
59         sprintf(p, "puts() - %s\n", bio->method->name);
60         break;
61     case BIO_CB_GETS:
62         sprintf(p, "gets(%lu) - %s\n", (unsigned long)argi,
63                 bio->method->name);
64         break;
65     case BIO_CB_CTRL:
66         sprintf(p, "ctrl(%lu) - %s\n", (unsigned long)argi,
67                 bio->method->name);
68         break;
69     case BIO_CB_RETURN | BIO_CB_READ:
70         sprintf(p, "read return %ld\n", ret);
71         break;
72     case BIO_CB_RETURN | BIO_CB_WRITE:
73         sprintf(p, "write return %ld\n", ret);
74         break;
75     case BIO_CB_RETURN | BIO_CB_GETS:
76         sprintf(p, "gets return %ld\n", ret);
77         break;
78     case BIO_CB_RETURN | BIO_CB_PUTS:
79         sprintf(p, "puts return %ld\n", ret);
80         break;
81     case BIO_CB_RETURN | BIO_CB_CTRL:
82         sprintf(p, "ctrl return %ld\n", ret);
83         break;
84     default:
85         sprintf(p, "bio callback - unknown type (%d)\n", cmd);
86         break;
87     }
88
89     b = (BIO *)bio->cb_arg;
90     if (b != NULL)
91         BIO_write(b, buf, strlen(buf));
92 #if !defined(OPENSSL_NO_STDIO)
93     else
94         fputs(buf, stderr);
95 #endif
96     return (r);
97 }