aboutsummaryrefslogtreecommitdiff
path: root/src/net.c
blob: c78dc055285565d9260d46972b1f1d2d0eb44de1 (plain)
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
   net.c

   This file is part of GNU Anubis.
   Copyright (C) 2001, 2002, 2003, 2004 The Anubis Team.

   GNU Anubis 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.

   GNU Anubis 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 GNU Anubis; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   GNU Anubis is released under the GPL with the additional exemption that
   compiling, linking, and/or using OpenSSL is allowed.
*/

#include "headers.h"
#include "extern.h"

static int connect_directly_to (char *, unsigned int);

static struct _debug_cache
{
  int method;
  int output;
  int newline;
  size_t count;
}
debug_cache =
{
-1, -1, 0, 0};

static void
_debug_printer (int method, int output, unsigned long nleft, char *ptr)
{
  int i;
  char *mode = "?";

  switch (method)
    {
    case CLIENT:
      mode = _("SERVER");
      break;
    case SERVER:
      mode = _("CLIENT");
    }

  if (debug_cache.newline
      || method != debug_cache.method || output != debug_cache.output)
    {
      if (!debug_cache.newline && debug_cache.count)
	fprintf (stderr, "(%lu)\n", (unsigned long) debug_cache.count);
      debug_cache.method = method;
      debug_cache.output = output;
      debug_cache.newline = 0;
      debug_cache.count = 0;
      fprintf (stderr, "%s %s ", mode, output ? "<<<" : ">>>");
    }

  for (i = 0; i < nleft; i++, ptr++)
    {
      debug_cache.count++;
      debug_cache.newline = 0;
      if (*ptr == '\r')
	continue;
      if (*ptr == '\n')
	{
	  fprintf (stderr, "(%ld)\n", (unsigned long) debug_cache.count);
	  debug_cache.count = 0;
	  if (i != nleft - 1)
	    {
	      fprintf (stderr, "%s %s ", mode, output ? "<<<" : ">>>");
	      debug_cache.newline = 0;
	    }
	  else
	    debug_cache.newline = 1;
	}
      else
	fputc (*ptr, stderr);
    }
}

#define DPRINTF(method, output, nleft, ptr) do {\
  if (options.termlevel == DEBUG) \
     _debug_printer(method, output, nleft, ptr);\
  } while (0)

NET_STREAM
make_remote_connection (char *host, unsigned int port)
{
  int sd;
  NET_STREAM str;

#ifdef USE_SOCKS_PROXY
  if (topt & T_SOCKS)
    {				/* SOCKS proxy */
      host = session.socks;
      port = session.socks_port;
    }
#endif /* USE_SOCKS_PROXY */

  if ((sd = connect_directly_to (host, port)) == -1)
    return NULL;

  net_create_stream (&str, sd);
  return str;
}

static int
connect_directly_to (char *host, unsigned int port)
{
  int sd = 0;
  unsigned long inaddr;
  struct sockaddr_in addr;

  /*
     Find out the IP address.
   */

  memset (&addr, 0, sizeof (addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons (port);
  info (VERBOSE, _("Getting remote host information..."));

  inaddr = inet_addr (host);
  if (inaddr != INADDR_NONE)
    memcpy (&addr.sin_addr, &inaddr, sizeof (inaddr));
  else
    {
      struct hostent *hp = 0;
      hp = gethostbyname (host);
      if (hp == 0)
	{
	  hostname_error (host);
	  return -1;
	}
      else
	{
	  if (hp->h_length != 4 && hp->h_length != 8)
	    {
	      anubis_error (EXIT_FAILURE, 0,
			    _("Illegal address length received for host %s"),
			    host);
	      return -1;
	    }
	  else
	    {
	      memcpy ((char *) &addr.sin_addr.s_addr, hp->h_addr,
		      hp->h_length);
	    }
	}
    }

  /*
     Create socket, and connect.
   */

  if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
    {
      anubis_error (EXIT_FAILURE, errno, _("Cannot create stream socket."));
      return -1;
    }
  if (connect (sd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
    {
      anubis_error (EXIT_FAILURE, errno, _("Couldn't connect to %s:%u. %s."),
		    host, port);
      return -1;
    }
  else
    info (NORMAL, _("Connected to %s:%u"), host, port);

  return sd;
}

/*****************
 Bind and listen.
******************/

int
bind_and_listen (char *host, unsigned int port)
{
  int sd = 0;
  unsigned long inaddr;
  struct sockaddr_in addr;
  int true = 1;

  memset (&addr, 0, sizeof (addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons (port);

  if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
    anubis_error (EXIT_FAILURE, errno, _("Cannot create stream socket"));

  if (topt & T_NAMES)
    {
      inaddr = inet_addr (host);
      if (inaddr != INADDR_NONE)
	memcpy (&addr.sin_addr, &inaddr, sizeof (inaddr));
      else
	{
	  struct hostent *hp = 0;
	  hp = gethostbyname (host);
	  if (hp == 0)
	    hostname_error (host);
	  else
	    {
	      if (hp->h_length != 4 && hp->h_length != 8)
		anubis_error (EXIT_FAILURE, 0,
			      _("Illegal address length received for host %s"),
			      host);
	      else
		{
		  memcpy ((char *) &addr.sin_addr.s_addr, hp->h_addr,
			  hp->h_length);
		}
	    }
	}
    }
  else
    addr.sin_addr.s_addr = htonl (INADDR_ANY);

  setsockopt (sd, SOL_SOCKET, SO_REUSEADDR, &true, sizeof (true));

  if (bind (sd, (struct sockaddr *) &addr, sizeof (addr)))
    anubis_error (EXIT_FAILURE, errno, _("bind() failed"));
  info (VERBOSE, _("GNU Anubis bound to %s:%u"), inet_ntoa (addr.sin_addr),
	ntohs (addr.sin_port));
  if (listen (sd, 5))
    anubis_error (EXIT_FAILURE, errno, _("listen() failed"));
  return sd;
}

/**************
  Send a data
***************/

void
swrite (int method, NET_STREAM sd, char *ptr)
{
  int rc;
  size_t nleft, nwritten = 0;

  if (ptr == NULL || (nleft = strlen (ptr)) == 0)
    return;
  
  rc = stream_write (sd, ptr, nleft, &nwritten);
  if (rc)
    socket_error (stream_strerror (sd, rc));
  DPRINTF (method, 1, nwritten, ptr);
  if (nwritten != nleft)
    {
      /* Should not happen */
      anubis_error (EXIT_FAILURE, 0, _("Short write"));
    }
}

/**************
  Read data
***************/

int
recvline (int method, NET_STREAM sd, void *vptr, size_t maxlen)
{
  int rc;
  size_t nbytes;

  rc = stream_readline (sd, vptr, maxlen, &nbytes);
  if (rc)
    socket_error (stream_strerror (sd, rc));
  DPRINTF (method, 0, nbytes, (char *) vptr);
  return nbytes;
}

#define INIT_RECVLINE_SIZE 81

int
recvline_ptr (int method, NET_STREAM sd, char **vptr, size_t * maxlen)
{
  int rc;
  size_t off = 0;

  *vptr = NULL;
  *maxlen = 0;
  while (1)
    {
      size_t nbytes;

      if (*maxlen - off <= 1)
	{
	  *maxlen += INIT_RECVLINE_SIZE;
	  *vptr = xrealloc (*vptr, *maxlen);
	}
      rc = stream_readline (sd, *vptr + off, *maxlen - off, &nbytes);
      if (rc)
        socket_error (stream_strerror (sd, rc));
      if (nbytes == 0)
	break;
      off += nbytes;
      if ((*vptr)[off - 1] == '\n')
	break;
    }
  (*vptr)[off] = 0;
  DPRINTF (method, 0, off, *vptr);
  return off;
}

/*****************
  Get a response
******************/

void
get_response_smtp (int method, NET_STREAM sd, char *buf, int size)
{
  int n;
  char line[LINEBUFFER + 1];
  int overflow = 0; /* This will be removed when we finally get rid of
		       static buffers */
  
  if (buf != 0)
    memset (buf, 0, size);
  do
    {
      n = recvline (method, sd, line, LINEBUFFER);
      if (buf != 0)
	{
	  if (size == 0)
	    {
	      if (!overflow)
		{
		  anubis_error (0, 0,
			      _("INTERNAL ERROR (get_response_smtp): buffer exhausted. Please report."));
		  overflow = 1;
		}
	    }
	  else
	    {
	      strncat (buf, line, size);
	      size -= n;
	    }
	}
    }
  while (line[3] == '-');
}

/**************************
 Close a socket descriptor
***************************/
void
close_socket (int sd)
{
  if (sd)
    close (sd);
  return;
}

void
net_close_stream (NET_STREAM *sd)
{
  stream_close (*sd);
  stream_destroy (sd);
  return;
}


void
net_create_stream (NET_STREAM * str, int fd)
{
  stream_create (str);
  stream_set_io (*str, (void *) fd, NULL, NULL, NULL, NULL, NULL);
}


/* EOF */

Return to:

Send suggestions and report system problems to the System administrator.