File: ec_socket.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 (187 lines) | stat: -rw-r--r-- 4,486 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
/*
    ettercap -- socket handling module

    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_signals.h>
#include <ec_poll.h>
#include <ec_socket.h>
#include <ec_sleep.h>

#ifndef OS_WINDOWS
   #include <netdb.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <arpa/inet.h>
#endif

#include <fcntl.h>

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

/* 
 * set or unset blocking flag on a socket
 */
void set_blocking(int s, int set)
{
#ifdef OS_WINDOWS
   u_long on = set;
   ioctlsocket(s, FIONBIO, &on);
#else
   int ret;

   /* get the current flags */
   if ((ret = fcntl(s, F_GETFL, 0)) == -1)
      return;
   
   if (set) 
      ret &= ~O_NONBLOCK;
   else
      ret |= O_NONBLOCK;
   
   /* set the flag */
//   fcntl (s, F_SETFL, F_SETFD, FD_CLOEXEC, ret); //this solution BREAKS the socket (ssl mitm will not work)
   fcntl(s, F_SETFL, ret);

#endif   
}


/*
 * open a socket to the specified host and port
 */
int open_socket(const char *host, u_int16 port)
{
   struct hostent *infh;
   struct sockaddr_in sa_in;
   int sh, ret, err = 0;
#define TSLEEP (50*1000) /* 50 milliseconds */
   int loops = (GBL_CONF->connect_timeout * 10e5) / TSLEEP;

   DEBUG_MSG("open_socket -- [%s]:[%d]", host, port);

   /* prepare the structures */
   memset((char*)&sa_in, 0, sizeof(sa_in));
   sa_in.sin_family = AF_INET;
   sa_in.sin_port = htons(port);

   /* resolve the hostname */
   if ( (infh = gethostbyname(host)) != NULL )
      memcpy(&sa_in.sin_addr, infh->h_addr, infh->h_length);
   else {
      if ( inet_aton(host, (struct in_addr *)&sa_in.sin_addr.s_addr) == 0 )
         return -E_NOADDRESS;
   }

   /* open the socket */
   if ( (sh = socket(AF_INET, SOCK_STREAM, 0)) < 0)
      return -E_FATAL;
 
   /* set nonblocking socket */
   set_blocking(sh, 0);
  
   do {
      /* connect to the server */
      ret = connect(sh, (struct sockaddr *)&sa_in, sizeof(sa_in));
      
      /* connect is in progress... */
      if (ret < 0) {
         err = GET_SOCK_ERRNO();
         if (err == EINPROGRESS || err == EALREADY || err == EWOULDBLOCK || err == EAGAIN) {
            /* sleep a quirk of time... */
            DEBUG_MSG("open_socket: connect() retrying: %d", err);
            ec_usleep(TSLEEP); /* 50000 microseconds */
         }
      } else { 
         /* there was an error or the connect was successful */
         break;
      }
   } while(loops--);
 
   /* 
    * we cannot recall get_sock_errno because under windows
    * calling it twice would not return the same result
    */
   err = ret < 0 ? err : 0;
   
   /* reached the timeout */
   if (ret < 0 && (err == EINPROGRESS || err == EALREADY || err == EAGAIN)) {
      DEBUG_MSG("open_socket: connect() timeout: %d", err);
      close_socket(sh);
      return -E_TIMEOUT;
   }

   /* error while connecting */
   if (ret < 0 && err != EISCONN) {
      DEBUG_MSG("open_socket: connect() error: %d", err);
      close_socket(sh);
      return -E_INVALID;
   }
      
   DEBUG_MSG("open_socket: connect() connected.");
   
   /* reset the state to blocking socket */
   set_blocking(sh, 1);
   
   
   DEBUG_MSG("open_socket: %d", sh);
   
   return sh;
}

/*
 * close the given socket 
 */
int close_socket(int s)
{
   DEBUG_MSG("close_socket: %d", s);

   /* close the socket */
#ifdef OS_WINDOWS
   return closesocket(s);
#else   
   return close(s);
#endif   
}


/* 
 * send a buffer throught the socket
 */
int socket_send(int s, const u_char *payload, size_t size)
{
   /* send data to the socket */
   return send(s, payload, size, 0);
}

/*
 * receive data from the socket
 */
int socket_recv(int sh, u_char *payload, size_t size)
{
   /* read up to size byte */
   return recv(sh, payload, size, 0);
}


/* EOF */

// vim:ts=3:expandtab