File: msocket.c

package info (click to toggle)
gbuffy 0.2.6-13
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 544 kB
  • ctags: 517
  • sloc: ansic: 6,235; sh: 195; makefile: 163; perl: 44; awk: 2
file content (195 lines) | stat: -rw-r--r-- 4,545 bytes parent folder | download | duplicates (3)
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
static const char rcsid[]="$Id: msocket.c,v 1.2 2003/10/10 09:06:53 blong Exp $";
/*
 * Copyright (C) 1998 Michael R. Elkins <me@cs.hmc.edu>
 * Copyright (C) 1998 Brandon C. Long <blong@fiction.net>
 * 
 *     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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */ 

/* Copied from mutt 0.95 */

#include "gbuffy.h"
#include "msocket.h"

#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>

#ifdef HAVE_SSL
#include <openssl/ssl.h>
#endif

/* support for multiple socket connections */

static CONNECTION *Connections = NULL;

/* simple read buffering to speed things up. */
int msocket_readchar (CONNECTION *conn, char *c)
{
  if (conn->bufpos >= conn->available)
  {
#ifdef HAVE_SSL
    if (conn->ssl_mode) {
	conn->available = SSL_read(conn->ssl, conn->inbuf, LONG_STRING-1);
    } else {
#endif
	conn->available = read (conn->fd, conn->inbuf, LONG_STRING-1);
#ifdef HAVE_SSL
    }
#endif
    conn->bufpos = 0;
    if (conn->available <= 0)
    {
#ifdef DEBUG
      fflush (debugfile);
#endif
      return conn->available; /* returns 0 for EOF or -1 for other error */
    }
  }
  if (conn->available > LONG_STRING || conn->bufpos > LONG_STRING)
    return -1;
  *c = conn->inbuf[conn->bufpos];
  conn->bufpos++;
  return 1;
}

int msocket_read_line (char *buf, size_t buflen, CONNECTION *conn)
{
  char ch;
  int i;

  for (i = 0; i < buflen; i++)
  {
    if (msocket_readchar (conn, &ch) != 1)
      return (-1);
    if (ch == '\n')
      break;
    buf[i] = ch;
  }
  if (i)
    buf[i-1] = 0;
  else
    buf[i] = 0;
  return (i + 1);
}

int msocket_read_line_d (char *buf, size_t buflen, CONNECTION *conn)
{
  int r = msocket_read_line (buf, buflen, conn);
  dprint (1,(debugfile,"msocket_read_line_d():%s\n", buf));
  return r;
}

int msocket_write (CONNECTION *conn, const char *buf)
{
  dprint (1,(debugfile,"msocket_write():%s", buf));
#ifdef HAVE_SSL
  if (conn->ssl_mode) {
	return SSL_write(conn->ssl, buf, strlen(buf));
  } else {
#endif
	return (write (conn->fd, buf, strlen (buf)));
#ifdef HAVE_SSL
  }
#endif
}

CONNECTION *msocket_select_connection (char *host, int port, int flags)
{
  CONNECTION *conn;

  if (flags != M_SOCKET_NEW)
  {
    conn = Connections;
    while (conn)
    {
      if (!strcmp (host, conn->server) && (port == conn->port))
	return conn;
      conn = conn->next;
    }
  }
  conn = (CONNECTION *) safe_calloc (1, sizeof (CONNECTION));
  conn->bufpos = 0;
  conn->available = 0;
  conn->uses = 0;
  conn->server = safe_strdup (host);
  conn->port = port;
  conn->ssl = NULL;
  conn->ssl_mode = 0;
  conn->next = Connections;
  Connections = conn;

  return conn;
}

int msocket_open_connection (CONNECTION *conn)
{
  struct sockaddr_in sin;
  struct hostent *he;
#ifdef HAVE_SSL
  SSL_CTX *ssl_ctx;
  SSL *ssl;
  int retval;
#endif

  memset (&sin, 0, sizeof (sin));
  sin.sin_port = htons (conn->port);
  sin.sin_family = AF_INET;
  if ((he = gethostbyname (conn->server)) == NULL)
  {
    perror (conn->server);
    return (-1);
  }
  memcpy (&sin.sin_addr, he->h_addr_list[0], he->h_length);

  if ((conn->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)
  {
/*    mutt_perror ("socket"); */
    return (-1);
  }

/*  mutt_message (_("Connecting to %s..."), conn->server); */

  if (connect (conn->fd, (struct sockaddr *) &sin, sizeof (sin)) < 0)
  {
/*    mutt_perror ("connect"); */
    close (conn->fd);
  }

#ifdef HAVE_SSL
  if (conn->ssl_mode) {
	  SSL_library_init();
	  ssl_ctx = SSL_CTX_new(SSLv23_client_method());
	  ssl = SSL_new(ssl_ctx);
	  if (!SSL_set_fd(ssl, conn->fd)) {
		close(conn->fd);
		return -1;
	  }

	  if ((retval = SSL_connect(ssl)) < 1) {
		printf("Failed connecting to TLS server: %d\n", SSL_get_error(ssl, retval));
		close(conn->fd);
		return -1;
	  }

	  conn->ssl = ssl;
  }
#endif

  return 0;
}