File: ec_ui.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,468 kB
  • ctags: 6,333
  • sloc: ansic: 47,337; yacc: 310; lex: 204; makefile: 121; xml: 31; sh: 24
file content (359 lines) | stat: -rw-r--r-- 7,688 bytes parent folder | download | duplicates (4)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
    ettercap -- user interface stuff

    Copyright (C) ALoR & NaGA

    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.

*/

#include <ec.h>
#include <ec_ui.h>

#include <stdarg.h>
#include <pthread.h>

/* globals */

struct ui_message {
   char *message;
   STAILQ_ENTRY(ui_message) next;
};

static STAILQ_HEAD(, ui_message) messages_queue = STAILQ_HEAD_INITIALIZER(messages_queue);

/* global mutex on interface */

static pthread_mutex_t ui_msg_mutex = PTHREAD_MUTEX_INITIALIZER;
#define UI_MSG_LOCK     do{ pthread_mutex_lock(&ui_msg_mutex); }while(0)
#define UI_MSG_UNLOCK   do{ pthread_mutex_unlock(&ui_msg_mutex); }while(0)

/*******************************************/

/* called to initialize the user interface */

void ui_init(void)
{
   DEBUG_MSG("ui_init");

   EXECUTE(GBL_UI->init);

   GBL_UI->initialized = 1;
}

/* called to run the user interface */

void ui_start(void)
{
   DEBUG_MSG("ui_start");

   if (GBL_UI->initialized)
      EXECUTE(GBL_UI->start);
   else
   {
      DEBUG_MSG("ui_start called initialized");
   }
}

/* called to end the user interface */

void ui_cleanup(void)
{
   if (GBL_UI->initialized) {
      DEBUG_MSG("ui_cleanup");
      EXECUTE(GBL_UI->cleanup);
      GBL_UI->initialized = 0;
   }
}

/* implement the progress bar */
int ui_progress(char *title, int value, int max)
{
   if (GBL_UI->progress)
      return GBL_UI->progress(title, value, max);

   return UI_PROGRESS_UPDATED;
}

/* send an update notification */
void ui_update(int target)
{
   if(GBL_UI->update) {
      DEBUG_MSG("ui_update");
      GBL_UI->update(target);
   }
}

/*
 * the FATAL_MSG error handling function
 */
void ui_error(const char *fmt, ...)
{
   va_list ap;
   int n;
   size_t size = 50;
   char *msg;

   /* 
    * we hope the message is shorter
    * than 'size', else realloc it
    */
    
   SAFE_CALLOC(msg, size, sizeof(char));

   while (1) {
      /* Try to print in the allocated space. */
      va_start(ap, fmt);
      n = vsnprintf (msg, size, fmt, ap);
      va_end(ap);
      
      /* If that worked, we have finished. */
      if (n > -1 && (size_t)n < size)
         break;
   
      /* Else try again with more space. */
      if (n > -1)    /* glibc 2.1 */
         size = n+1; /* precisely what is needed */
      else           /* glibc 2.0 */
         size *= 2;  /* twice the old size */
      
      SAFE_REALLOC(msg, size);
   }

   /* dump the error in the debug file */
   DEBUG_MSG("%s", msg);
   
   /* call the function */
   if (GBL_UI->error)
      EXECUTE(GBL_UI->error, msg);
   /* the interface is not yet initialized */
   else
      fprintf(stderr, "\n%s\n", msg);
   
   /* free the message */
   SAFE_FREE(msg);
}


/*
 * the FATAL_ERROR error handling function
 */
void ui_fatal_error(const char *msg)
{
   /* 
    * call the function 
    * make sure that the globals have been alloc'd
    */
   if (GBLS && GBL_UI && GBL_UI->fatal_error && GBL_UI->initialized)
      EXECUTE(GBL_UI->fatal_error, msg);
   /* the interface is not yet initialized */
   else {
      fprintf(stderr, "\n%s\n\n", msg);
      exit(-1);
   }
   
}


/*
 * this fuction enqueues the messages displayed by
 * ui_msg_flush()
 */

void ui_msg(const char *fmt, ...)
{
   va_list ap;
   struct ui_message *msg;
   int n;
   size_t size = 50;

   SAFE_CALLOC(msg, 1, sizeof(struct ui_message));

   /* 
    * we hope the message is shorter
    * than 'size', else realloc it
    */
    
   SAFE_CALLOC(msg->message, size, sizeof(char));

   while (1) {
      /* Try to print in the allocated space. */
      va_start(ap, fmt);
      n = vsnprintf (msg->message, size, fmt, ap);
      va_end(ap);
      
      /* If that worked, we have finished. */
      if (n > -1 && (size_t)n < size)
         break;
   
      /* Else try again with more space. */
      if (n > -1)    /* glibc 2.1 */
         size = n+1; /* precisely what is needed */
      else           /* glibc 2.0 */
         size *= 2;  /* twice the old size */
      
      SAFE_REALLOC(msg->message, size);
   }

   /* log the messages if needed */
   if (GBL_OPTIONS->msg_fd) {
      fprintf(GBL_OPTIONS->msg_fd, "%s", msg->message);
      fflush(GBL_OPTIONS->msg_fd);
   }
   
   /* 
    * MUST use the mutex.
    * this MAY be a different thread !!
    */
   UI_MSG_LOCK;
   
   /* add the message to the queue */
   STAILQ_INSERT_TAIL(&messages_queue, msg, next);

   UI_MSG_UNLOCK;
   
}


/*
 * get the user input
 */
void ui_input(const char *title, char *input, size_t n, void (*callback)(void))
{
   DEBUG_MSG("ui_input");

   EXECUTE(GBL_UI->input, title, input, n, callback);
}

/* 
 * this function is used to display up to 'max' messages.
 * a user interface MUST use this to empty the message queue.
 */

int ui_msg_flush(int max)
{
   int i = 0;
   int old = 0;
   struct ui_message *msg;
  

   /* sanity checks */
   if (!GBL_UI->initialized)
      return 0;
     
   if (STAILQ_EMPTY(&messages_queue))
	return 0; 

   // don't allow the thread to cancel while holding the ui mutex
   pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);

   /* the queue is updated by other threads */
   UI_MSG_LOCK;
   

   while ( (msg = STAILQ_FIRST(&messages_queue)) != NULL) {

      /* diplay the message */
      GBL_UI->msg(msg->message);

      STAILQ_REMOVE_HEAD(&messages_queue, msg, next);
      /* free the message */
      SAFE_FREE(msg->message);
      SAFE_FREE(msg);
      
      /* do not display more then 'max' messages */
      if (++i == max)
         break;
   }
   
   UI_MSG_UNLOCK;

   pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);

   /* returns the number of displayed messages */
   return i;
   
}

/*
 * empty all the message queue
 */
int ui_msg_purge_all(void)
{
   int i = 0;
   struct ui_message *msg;

   /* the queue is updated by other threads */
   UI_MSG_LOCK;
      
   while ( (msg = STAILQ_FIRST(&messages_queue)) != NULL) {
      STAILQ_REMOVE_HEAD(&messages_queue, msg, next);
      /* free the message */
      SAFE_FREE(msg->message);
      SAFE_FREE(msg);
   }
   
   UI_MSG_UNLOCK;
   
   /* returns the number of purgeded messages */
   return i;
   
}

/*
 * register the function pointer for
 * the user interface.
 * a new user interface MUST implement this 
 * three function and use this function 
 * to hook in the right place.
 */

void ui_register(struct ui_ops *ops)
{
        
   BUG_IF(ops->init == NULL);
   GBL_UI->init = ops->init;
   
   BUG_IF(ops->cleanup == NULL);
   GBL_UI->cleanup = ops->cleanup;
   
   BUG_IF(ops->start == NULL);
   GBL_UI->start = ops->start;
        
   BUG_IF(ops->msg == NULL);
   GBL_UI->msg = ops->msg;
   
   BUG_IF(ops->error == NULL);
   GBL_UI->error = ops->error;
   
   BUG_IF(ops->fatal_error == NULL);
   GBL_UI->fatal_error = ops->fatal_error;
   
   BUG_IF(ops->input == NULL);
   GBL_UI->input = ops->input;
   
   BUG_IF(ops->progress == NULL);
   GBL_UI->progress = ops->progress;

   GBL_UI->update = ops->update;

   GBL_UI->type = ops->type;
}


/* EOF */

// vim:ts=3:expandtab