1 /*
2  * SIE Remote Access (SRA) ASCII tool definitions
3  *
4  *  Copyright (c) 2014-2018 by Farsight Security, Inc.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 #ifndef SRATOOL_H
20 #define SRATOOL_H
21 
22 #include <wdns.h>
23 
24 #include <config.h>
25 #include <axa/axa_endian.h>
26 #include <axa/wire.h>
27 #include <axa/fields.h>
28 #include <axa/dns_walk.h>
29 #include <axa/client.h>
30 #include <axa/client_config.h>
31 #include <axa/open_nmsg_out.h>
32 
33 #include <nmsg/vendors.h>
34 #include <nmsg/sie/defs.h>
35 #include <nmsg/base/defs.h>
36 #include <nmsg/base/dnsqr.pb-c.h>
37 #include <nmsg/sie/newdomain.pb-c.h>
38 #include <nmsg/base/packet.pb-c.h>
39 
40 #include <arpa/nameser.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <net/ethernet.h>
44 #ifdef __linux
45 #include <netinet/ether.h>
46 #endif
47 #include <limits.h>
48 #include <signal.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #ifdef __linux
52 #include <bsd/string.h>                 /* for strlcpy() */
53 #endif
54 #include <sysexits.h>
55 #include <sys/stat.h>
56 #include <unistd.h>
57 
58 #include <histedit.h>
59 #include <pwd.h>
60 #include <math.h>
61 
62 
63 extern axa_p_hdr_t recv_hdr;
64 extern axa_p_body_t *recv_body;
65 extern size_t recv_len;
66 
67 extern uint verbose;
68 
69 #define NMSG_LEADER  "  "
70 #define NMSG_LEADER2 "   "
71 
72 
73 typedef struct cmd_tbl_entry cmd_tbl_entry_t;
74 
75 /* -1=display help message, 0=command failed, 1=success */
76 typedef int cmd_t (axa_tag_t tag, const char *arg, const cmd_tbl_entry_t *ce);
77 
78 extern void clear_prompt(void);
79 
80 /* cmd.c */
81 #define MAX_IN_FILES 10
82 
83 /*
84  *  Simple way to subtract timeval based timers, subtracts `uvp` from `tvp`.
85  *
86  *  \param[in] tvp pointer to timeval structure (first value)
87  *  \param[in] uvp pointer to timeval structure (second value)
88  *  \param[out] vvp pointer to timeval (result timeval)
89  */
90 #define PTIMERSUB(tvp, uvp, vvp)                                \
91 do {                                                            \
92 	(vvp)->tv_sec  = (tvp)->tv_sec  - (uvp)->tv_sec;        \
93 	(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
94 	if ((vvp)->tv_usec < 0) {                               \
95 		(vvp)->tv_sec--;                                \
96 		(vvp)->tv_usec += 1000000;                      \
97 	}                                                       \
98 } while (0)
99 
100 typedef enum {
101 	SRA,
102 	RAD,
103 	BOTH
104 } axa_mode_t;
105 
106 bool do_cmds(const char *cmd_buf);
107 void history_get_savefile(void);
108 const char *el_prompt(EditLine *e AXA_UNUSED);
109 void clear_prompt(void);
110 void reprompt(void);
111 #if LIBEDIT_IS_UNICODE
112 int getcfn(EditLine *e AXA_UNUSED, wchar_t *buf);
113 #else
114 int getcfn(EditLine *e AXA_UNUSED, char *buf);
115 #endif
116 void AXA_NORETURN usage(void);
117 int version_cmd(axa_tag_t tag AXA_UNUSED, const char *arg  AXA_UNUSED,
118 		const cmd_tbl_entry_t *ce AXA_UNUSED);
119 void error_help_cmd(axa_tag_t tag, const char *arg);
120 
121 /* error.c */
122 void error_close(bool cmd_error);
123 void error_msg(const char *p, ...)  AXA_PF(1,2);
124 
125 /* infile.c */
126 typedef struct input_files {
127 	uint    lineno;
128 	FILE    *f;
129 	char    *name;
130 	char    *buf;
131 	size_t  buf_size;
132 }in_files_t;
133 void close_in_file_cur(void);
134 void close_in_files(void);
135 
136 /* io.c */
137 void io_wait(bool cmds_ok, bool once, time_t wait_ms);
138 
139 /* main.c */
140 void AXA_NORETURN stop(int status);
141 
142 /* output.c */
143 time_t out_flush_ck(const struct timeval *now, time_t delay);
144 void out_flush(void);
145 void out_close(bool announce);
146 bool out_error_ok(void);
147 void AXA_PF(1,2) out_error(const char *p, ...);
148 bool out_whit_nmsg(axa_p_whit_t *whit, size_t whit_len);
149 void out_ip_pcap_file(const uint8_t *pkt, size_t caplen, size_t len,
150 		const struct timeval *tv);
151 void out_ip_pcap_inject(const uint8_t *pkt, size_t caplen);
152 bool out_whit_pcap(axa_p_whit_t *whit, size_t whit_len);
153 axa_w2n_res_t whit2nmsg(nmsg_message_t *msgp, axa_p_whit_t *whit,
154 		size_t whit_len);
155 
156 /* print.c */
157 void count_print(bool always);
158 void print_whit(axa_p_whit_t *whit, size_t whit_len, const char *title_sep,
159 		const char *title);
160 void print_ahit(void);
161 void print_channel(void);
162 void wlist_alist(void);
163 void print_raw(const uint8_t *pkt, size_t pkt_len);
164 bool print_dns_pkt(const uint8_t *data, size_t data_len, const char *str);
165 void print_raw_ip(const uint8_t *data, size_t data_len, axa_p_ch_t ch);
166 void print_stats(_axa_p_stats_rsp_t *stats, uint32_t len);
167 void print_kill(_axa_p_kill_t *kill, size_t len);
168 
169 /* server.c */
170 void read_srvr(void);
171 int srvr_send(axa_tag_t tag, axa_p_op_t op, const void *b, size_t b_len);
172 void disconnect(bool announce);
173 
174 /* signal.c */
175 void sigint(int sig AXA_UNUSED);
176 void sigterm(int sig AXA_UNUSED);
177 
178 /* timer.c */
179 void convert_seconds(uint32_t seconds, uint32_t *d, uint32_t *h, uint32_t *m,
180 		uint32_t *s);
181 const char *convert_timeval(struct timeval *t);
182 
183 #endif /* SRATOOL_H */
184