summaryrefslogtreecommitdiffstats
path: root/netopen.c
blob: 31832c3eda8513c99d999b5d1032c05f2b94b37e (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
/*
 * 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>

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 FILE *sock_to_file(SOCKET sock, int is_write)
{
  FILE *f;
  int fd;
  int osfhandle = sock;

  fd = _open_osfhandle(&osfhandle, is_write ? 0 : _O_RDONLY);
  if (fd < 0) {
    closesocket(fd);
    return NULL;
  }

  f = fdopen(fd, is_write ? "w" : "r");
  if (!f)
    _close(fd);

  return f;
}

#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 FILE *sock_to_file(SOCKET sock, int is_write)
{
  FILE *f;

  f = fdopen(sock, is_write ? "w" : "r");
  if (!f)
    close(sock);

  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

FILE *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_file(sock, is_write);
}