aboutsummaryrefslogtreecommitdiffstats
path: root/netopen.c
blob: cf17cda51387424170e865c6ee83b7cd66a5c4cb (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
/*
 * netopen.c
 *
 * Open a TCP network connection to the specified hostname and port.
 * This might need some porting work to work on Win32.
 */

#include "network.h"
#include <stdio.h>

#ifdef __WIN32__

#define _WIN32_WINNT 0x0501	/* Windows XP */
#include <windows.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <winsock2.h>
#include <ws2tcpip.h>

/* This is basically a lame subset of stdio functionality */
#define NETCON_BUF	65536
struct netcon {
  int is_output;
  int buffill;
  char *bufdata;
  SOCKET sock;
  unsigned char buffer[NETCON_BUF];
};

static int winsock_started = 0;
int socket_errno;		/* For debuggability */

static void end_winsock(void)
{
  if ( winsock_started ) {
    WSACleanup();
    winsock_started = 0;
  }
}

static void start_winsock(void)
{
  WSADATA wd;

  if ( !winsock_started && !WSAStartup(MAKEWORD(2,0), &wd) ) {
    winsock_started = 1;
    atexit(end_winsock);
  }
}

static int input_get(netcon_t nc)
{
  int rv;

  rv = recv(nc->sock, nc->buffer, NETCON_BUF, 0);
  if ( rv == SOCKET_ERROR )
    socket_errno = WSAGetLastError();

  nc->bufdata = nc->buffer;
  nc->buffill = (rv == SOCKET_ERROR) ? 0 : rv;

  return rv;
}

static int output_send(netcon_t nc)
{
  char *p = nc->buffer;
  int count = nc->buffill;
  int oc = count;
  int rv;

  while ( count ) {
    rv = send(nc->sock, p, count, 0);
    if ( rv == 0 || rv == SOCKET_ERROR ) {
      socket_errno = WSAGetLastError();
      return -1;
    }

    p += rv;
    count -= rv;
  }

  nc->bufdata = nc->buffer;
  nc->buffill = 0;

  return oc;
}

int fgetc_network(netcon_t nc)
{
  int rv;

  if ( !nc->buffill ) {
    rv = input_get(nc);
    if ( rv == 0 || rv == SOCKET_ERROR )
      return EOF;
  }

  rv = *nc->bufdata;

  nc->bufdata++;
  nc->buffill--;

  return rv;
}

char *fgets_network(char *s, int size, netcon_t nc)
{
  int ch;
  char *p = s;
  int cr = 0;

  if ( size == 0 )
    return NULL;

  while ( size > 1 ) {
    ch = fgetc_network(nc);
    if ( ch == EOF )
      break;

    *p++ = ch;
    cr++;

    if ( ch == '\n' )
      break;
  }

  *p = '\0';

  return cr ? s : NULL;
}

int fputs_network(const char *s, netcon_t nc)
{
  int b = strlen(s);
  int sl;

  while ( b ) {
    sl = NETCON_BUF - nc->buffill; /* Space left */

    if ( !sl ) {
      if ( output_send(nc) <= 0 )
	return EOF;		/* Error */
      else
	continue;
    }

    if ( sl > b )
      sl = b;

    memcpy(nc->bufdata, s, sl);

    nc->buffill += sl;
    nc->bufdata += sl;
    s += sl;
    b -= sl;
  }

  return 0;
}

netcon_t sock_to_netcon(SOCKET sock, int is_write)
{
  struct netcon *nc = malloc(sizeof(struct netcon));

  if ( !nc ) {
    closesocket(sock);
    return NULL;
  }

  /* Construct a struct netcon object */
  nc->bufdata = nc->buffer;
  nc->buffill = 0;
  nc->sock = sock;
  nc->is_output = is_write;

  return nc;
}

int fclose_network(netcon_t nc)
{
  int rv = 0;

  if ( nc->is_output && nc->buffill ) {
    if ( output_send(nc) <= 0 )
      rv = EOF;
  }

  closesocket(nc->sock);
  free(nc);

  return rv;
}

#else
/* Unix, or something else which implements BSD sockets correctly */

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

/* Winsuck-compatibility */
typedef int SOCKET;
#define INVALID_SOCKET (-1)
#define start_winsock() do { } while (0)
#define closesocket(x) close(x)

static netcon_t sock_to_netcon(int sock, int is_write)
{
  FILE *f = fdopen(sock, is_write ? "w" : "r");
  if ( !f ) {
    close(sock);
    return NULL;
  }

  return f;
}

#endif

#ifdef AF_INET6

#ifndef AI_ADDRCONFIG
# define AI_ADDRCONFIG 0
#endif

static SOCKET open_socket(const char *name, int port)
{
  SOCKET sock;
  struct addrinfo *ai, *ap;
  struct addrinfo hints;
  char port_str[32];

  start_winsock();

  memset(&hints, 0, sizeof hints);
  hints.ai_family   = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags    = AI_ADDRCONFIG;

  sprintf(port_str, "%d", port);
  if (getaddrinfo(name, port_str, &hints, &ai))
    return INVALID_SOCKET;

  sock = INVALID_SOCKET;

  for (ap = ai; ap; ap = ap->ai_next) {
    sock = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
    if ( sock == INVALID_SOCKET )
      continue;

    if ( !connect(sock, ap->ai_addr, ap->ai_addrlen) )
      break;

    closesocket(sock);
    sock = INVALID_SOCKET;
  }

  freeaddrinfo(ai);

  return sock;
}

#else

/* Legacy IPv4-only API */

static SOCKET open_socket(const char *name, int port)
{
  struct hostent *he;
  SOCKET sock;
  struct in_addr **haddr;
  struct sockaddr_in si;

  start_winsock();

  he = gethostbyname(name);
  if ( !he || he->h_addrtype != AF_INET )
    return INVALID_SOCKET;

  sock = socket(PF_INET, SOCK_STREAM, 0);
  if ( sock == INVALID_SOCKET )
    return INVALID_SOCKET;

  for ( haddr = (struct in_addr **)he->h_addr_list ; *haddr ; haddr++ ) {
    memset(&si, 0, sizeof si);
    si.sin_family = AF_INET;
    si.sin_port = htons(port);
    memcpy(&si.sin_addr, *haddr, sizeof si.sin_addr);

    if ( !connect(sock, (struct sockaddr *)&si, sizeof si) )
      break;
  }
  if ( !*haddr ) {
    closesocket(sock);
    return INVALID_SOCKET;
  }

  return sock;
}

#endif

netcon_t fopen_network(const char *name, int port, int is_write)
{
  SOCKET sock;

  sock = open_socket(name, port);
  if (sock == INVALID_SOCKET)
    return NULL;

  return sock_to_netcon(sock, is_write);
}