1 /*
2  *  OpenVPN -- An application to securely tunnel IP networks
3  *             over a single TCP/UDP port, with support for SSL/TLS-based
4  *             session authentication and key exchange,
5  *             packet encryption, packet authentication, and
6  *             packet compression.
7  *
8  *  Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License version 2
12  *  as published by the Free Software Foundation.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef STATUS_H
25 #define STATUS_H
26 
27 #include "interval.h"
28 
29 /*
30  * virtual function interface for status output
31  */
32 struct virtual_output {
33     void *arg;
34     unsigned int flags_default;
35     void (*func) (void *arg, const unsigned int flags, const char *str);
36 };
37 
38 static inline void
virtual_output_print(const struct virtual_output * vo,const unsigned int flags,const char * str)39 virtual_output_print(const struct virtual_output *vo, const unsigned int flags, const char *str)
40 {
41     (*vo->func)(vo->arg, flags, str);
42 }
43 
44 /*
45  * printf-style interface for inputting/outputting status info
46  */
47 
48 struct status_output
49 {
50 #define STATUS_OUTPUT_READ  (1<<0)
51 #define STATUS_OUTPUT_WRITE (1<<1)
52     unsigned int flags;
53 
54     char *filename;
55     int fd;
56     int msglevel;
57     const struct virtual_output *vout;
58 
59     struct buffer read_buf;
60 
61     struct event_timeout et;
62 
63     bool errors;
64 };
65 
66 struct status_output *status_open(const char *filename,
67                                   const int refresh_freq,
68                                   const int msglevel,
69                                   const struct virtual_output *vout,
70                                   const unsigned int flags);
71 
72 bool status_trigger(struct status_output *so);
73 
74 void status_reset(struct status_output *so);
75 
76 void status_flush(struct status_output *so);
77 
78 bool status_close(struct status_output *so);
79 
80 void status_printf(struct status_output *so, const char *format, ...)
81 #ifdef __GNUC__
82 #if __USE_MINGW_ANSI_STDIO
83 __attribute__ ((format(gnu_printf, 2, 3)))
84 #else
85 __attribute__ ((format(__printf__, 2, 3)))
86 #endif
87 #endif
88 ;
89 
90 bool status_read(struct status_output *so, struct buffer *buf);
91 
92 static inline unsigned int
status_rw_flags(const struct status_output * so)93 status_rw_flags(const struct status_output *so)
94 {
95     if (so)
96     {
97         return so->flags;
98     }
99     else
100     {
101         return 0;
102     }
103 }
104 
105 #endif /* ifndef STATUS_H */
106