File: ec_poll.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 (161 lines) | stat: -rw-r--r-- 3,457 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
/*
    ettercap -- poll events (use poll or select)

    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>

#ifdef HAVE_SYS_POLL_H
   #include <sys/poll.h>
#elif defined (HAVE_POLL_H)
   #include <poll.h>
#endif

#ifdef HAVE_SYS_SELECT_H
   #include <sys/select.h>
#endif

/* protos */

int ec_poll_in(int fd, u_int msec);
int ec_poll_out(int fd, u_int msec);
int ec_poll_buffer(char *buf);

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

/*
 * looks for event on INPUT
 */
int ec_poll_in(int fd, u_int msec)
{
#if defined(HAVE_POLL) && !defined(OS_DARWIN)
   int poll_result;
   struct pollfd poll_fd;
   
   /* set the correct fd */
   poll_fd.fd = fd;
   poll_fd.events = POLLIN;
         
   /* execute the syscall */
   poll_result = poll(&poll_fd, 1, msec);
   
   /* the event has occurred, return 1 */
   if (poll_result > 0 && poll_fd.revents & POLLIN)
      return 1;
  
   return 0;
   
#elif defined(HAVE_SELECT)

   fd_set msk_fd;
   struct timeval to;
     
   memset(&to, 0, sizeof(struct timeval));
   /* timeval uses microseconds */
   to.tv_usec = MILLI2MICRO(msec);
   
   FD_ZERO(&msk_fd);
  
   /* set the correct fd */
   FD_SET(fd, &msk_fd);

   /* execute the syscall */
   int fds = select(FOPEN_MAX, &msk_fd, (fd_set *) 0, (fd_set *) 0, &to);
  
   if (fds <= 0) {
      return 0;
   } 
   /* the even has occurred */
   if (FD_ISSET(0, &msk_fd))
      return 1;
               
   return 0;
#else
   #error "you don't have neither poll nor select"
#endif
}


/*
 * looks for event on OUTPUT
 */
int ec_poll_out(int fd, u_int msec)
{
#if defined(HAVE_POLL) && !defined(OS_DARWIN)
   struct pollfd poll_fd;
   
   /* set the correct fd */
   poll_fd.fd = fd;
   poll_fd.events = POLLOUT;
         
   /* execute the syscall */
   poll(&poll_fd, 1, msec);

   /* the event has occurred, return 1 */
   if (poll_fd.revents & POLLOUT)
      return 1;
  
   return 0;
   
#elif defined(HAVE_SELECT)

   fd_set msk_fd;
   struct timeval to;
     
   memset(&to, 0, sizeof(struct timeval));
   /* timeval uses microseconds */
   to.tv_usec = MILLI2MICRO(msec);
   
   FD_ZERO(&msk_fd);
  
   /* set the correct fd */
   FD_SET(fd, &msk_fd);

   /* execute the syscall */
   int fds = select(FOPEN_MAX, (fd_set *) 0, &msk_fd, (fd_set *) 0, &to);
    
   if (fds <=0)
      return 0; 
   /* the even has occurred */
   if (FD_ISSET(0, &msk_fd))
      return 1;
               
   return 0;
#else
   #error "you don't have neither poll nor select"
#endif
}

/*
 * a fake poll implementation on string buffers
 * it returns != 0 if there are char(s) to read,
 * 0 if the first char is null
 */
int ec_poll_buffer(char *buf)
{
   if (buf)
      return *buf;
   else
      return 0;
}

/* EOF */

// vim:ts=3:expandtab