-
Notifications
You must be signed in to change notification settings - Fork 117
/
ncurses-gui.c
260 lines (207 loc) · 6.75 KB
/
ncurses-gui.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* ncurses-gui.c
* Implementation for ncurses GUI
*
* Yersinia
* By David Barroso <tomac@yersinia.net> and Alfredo Andres <aandreswork@hotmail.com>
* Copyright 2005-2017 Alfredo Andres and David Barroso
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef _REENTRANT
#define _REENTRANT
#endif
#include <stdio.h>
#include <errno.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include <sys/socket.h>
#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#else
#ifdef HAVE_NETINET_IN_SYSTEM_H
#include <netinet/in_system.h>
#endif
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <time.h>
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_BSTRING_H
#include <bstring.h>
#endif
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#include <stdarg.h>
#include <netinet/tcp.h>
#ifdef SOLARIS
#include <pthread.h>
#include <thread.h>
#else
#include <pthread.h>
#endif
#include "ncurses-gui.h"
#include "ncurses-interface.h"
#include "ncurses-callbacks.h"
/*
* Initialization routines for the GUI
*/
void
ncurses_gui(void *args)
{
int tmp;
WINDOW *my_wins[NCURSES_MAX_WINDOWS];
PANEL *my_panels[NCURSES_MAX_WINDOWS];
struct term_node *term_node = NULL;
time_t this_time;
sigset_t mask;
struct interface_data *iface_data=NULL, *iface;
terms->work_state = RUNNING;
pthread_mutex_lock(&terms->gui_th.finished);
write_log(0,"\n ncurses_gui_th = %d\n",(int)pthread_self());
sigfillset(&mask);
if (pthread_sigmask(SIG_BLOCK, &mask, NULL))
{
thread_error("ncurses_gui_th pthread_sigmask()",errno);
ncurses_gui_th_exit(NULL);
}
if (pthread_mutex_lock(&terms->mutex) != 0) {
thread_error("ncurses_gui_th pthread_mutex_lock",errno);
ncurses_gui_th_exit(NULL);
}
if (term_add_node(&term_node, TERM_CON, 0, pthread_self()) < 0)
{
if (pthread_mutex_unlock(&terms->mutex) != 0)
thread_error("ncurses_gui_th pthread_mutex_unlock",errno);
ncurses_gui_th_exit(NULL);
}
if (term_node == NULL)
{
write_log(0, "Ouch!! No more than %d %s accepted!!\n", term_type[TERM_CON].max, term_type[TERM_CON].name);
if (pthread_mutex_unlock(&terms->mutex) != 0)
thread_error("ncurses_gui_th pthread_mutex_unlock",errno);
ncurses_gui_th_exit(NULL);
}
this_time = time(NULL);
#ifdef HAVE_CTIME_R
#ifdef SOLARIS
ctime_r(&this_time,term_node->since, sizeof(term_node->since));
#else
ctime_r(&this_time,term_node->since);
#endif
#else
pthread_mutex_lock(&mutex_ctime);
strncpy(term_node->since, ctime(&this_time), sizeof(term_node->since) - 1);
pthread_mutex_unlock(&mutex_ctime);
#endif
/* Just to remove the cr+lf...*/
term_node->since[sizeof(term_node->since)-2] = 0;
/* This is a console so, man... ;) */
strncpy(term_node->from_ip, "127.0.0.1", sizeof(term_node->from_ip) - 1);
/* Parse config file */
if ( strlen( tty_tmp->config_file ) )
{
if (parser_read_config_file(tty_tmp, term_node) < 0)
write_log(0, "Error reading configuration file\n");
}
if (pthread_mutex_unlock(&terms->mutex) != 0)
{
thread_error("ncurses_gui_th pthread_mutex_unlock",errno);
ncurses_gui_th_exit(term_node);
}
if (ncurses_i_init(my_wins, my_panels, term_node) < 0)
ncurses_gui_th_exit(term_node);
if (interfaces->list)
iface_data = dlist_data(interfaces->list);
else
{
ncurses_i_error_window(0, "Hmm... you don't have any valid interface. %s is useless. Go and get a life!", PACKAGE);
ncurses_gui_th_exit(term_node);
}
/* take the first valid interface */
if ( iface_data && strlen( iface_data->ifname ) )
{
if ( ncurses_i_error_window(0, "Warning: interface %s selected as the default one", iface_data->ifname) < 0)
ncurses_gui_th_exit(term_node);
if ((tmp = interfaces_enable(iface_data->ifname)) == -1)
{
if (ncurses_i_error_window(1, "Unable to use interface %s!! (Maybe nonexistent?)\n\n", iface_data->ifname) < 0)
ncurses_gui_th_exit(term_node);
}
else
{
iface = (struct interface_data *) calloc(1, sizeof(struct interface_data));
memcpy((void *)iface, (void *)iface_data, sizeof(struct interface_data));
term_node->used_ints->list = dlist_append(term_node->used_ints->list, iface);
}
}
else
{
if (ncurses_i_error_window(1, "Hmm... you don't have any valid interface. %s is useless. Go and get a life!", PACKAGE) < 0)
ncurses_gui_th_exit(term_node);
}
ncurses_c_engine(my_wins, my_panels, term_node);
ncurses_gui_th_exit(term_node);
}
/*
* GUI destroy. End
*/
void
ncurses_gui_th_exit(struct term_node *term_node)
{
dlist_t *p;
struct interface_data *iface_data;
if (endwin() == ERR)
thread_error("Error in endwin", errno);
write_log(0, "\n ncurses_gui_th_exit start...\n");
if (term_node)
{
for (p = term_node->used_ints->list; p; p = dlist_next(term_node->used_ints->list, p)) {
iface_data = (struct interface_data *) dlist_data(p);
interfaces_disable(iface_data->ifname);
}
attack_kill_th(term_node,ALL_ATTACK_THREADS);
if (pthread_mutex_lock(&terms->mutex) != 0)
thread_error("ncurses_gui_th pthread_mutex_lock",errno);
term_delete_node(term_node, NOKILL_THREAD);
if (pthread_mutex_unlock(&terms->mutex) != 0)
thread_error("ncurses_gui_th pthread_mutex_unlock",errno);
}
write_log(0," ncurses_gui_th_exit finish...\n");
if (pthread_mutex_unlock(&terms->gui_th.finished) != 0)
thread_error("ncurses_gui_th pthread_mutex_unlock",errno);
terms->work_state = STOPPED;
terms->gui_th.id = 0;
pthread_exit(NULL);
}