aboutsummaryrefslogtreecommitdiff
path: root/lib/libRTV/netclient.c
blob: 1a9c4de175b1007f82c3a7f12abf0f4c5b7c6c3b (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
#ifndef __GNUC__
#pragma code_seg( "RTV_TEXT" )
#pragma data_seg( "RTV_DATA" )
#pragma bss_seg( "RTV_BSS" )
#pragma const_seg( "RTV_RD" )
#endif

/*
 * Copyright (C) 2002 John Todd Larason <jtl@molehill.org>
 *
 * Parts based on ReplayPC 0.3 by Matthew T. Linehan and others
 *
 *    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.
 */

#include "netclient.h"
#include "rtv.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>
//#include <netdb.h>

#ifdef _XBOX
#	include <Xtl.h>

	typedef SOCKET socket_fd;

	struct  hostent {
		char    * h_name;			/* official name of host */
		char    * * h_aliases;		/* alias list */
		short   h_addrtype;			/* host address type */
		short   h_length;			/* length of address */
		char    * * h_addr_list;	/* list of addresses */
	#define h_addr  h_addr_list[0]	/* address, for backward compat */
	};

	struct hostent * gethostbyname(const char * name);
#elif defined _WIN32
#  include <winsock2.h>
#  pragma comment(lib, "ws2_32.lib")
   typedef SOCKET socket_fd;
#else
#  include <unistd.h>
#  include <sys/socket.h>
#  include <netinet/in.h>
#  include <arpa/inet.h>
#  define closesocket(fd) close(fd)
   typedef int socket_fd;
#  define INVALID_SOCKET (-1)
#include <netdb.h>
#endif

#ifndef MIN
#define MIN(x,y) ((x)<(y) ? (x) : (y))
#endif

static int nc_do_dump = 0;
static int nc_initted = 0;

struct nc
{
    u8        rcv_buf[32768];
    size_t    low, high;
    socket_fd fd;
};

static void nc_fini(void)
{
    nc_initted = 0;

#ifdef _WIN32
    WSACleanup();
#endif
}

static void nc_dump(char * tag, unsigned char * buf, size_t sz)
{
#if 0
    unsigned int rows, row, col, i, c;
    if (!nc_do_dump)
        return;
    //fprintf(stderr, "NC DUMP: %s %lu\n", tag, (unsigned long)sz);
    if (buf == NULL)
        return;
    rows = (sz + 15)/16;
    for (row = 0; row < rows; row++) {
        //fprintf(stderr, "| ");
        for (col = 0; col < 16; col++) {
            i = row * 16 + col;
            //if (i < sz)
              //  fprintf(stderr, "%02x", buf[i]);
            //else
              //  fprintf(stderr, "  ");
            //if ((i & 3) == 3)
              //  fprintf(stderr, " ");
        }
        fprintf(stderr, "  |  ");
        for (col = 0; col < 16; col++) {
            i = row * 16 + col;
            if (i < sz) {
                c = buf[i];
                fprintf(stderr, "%c", (c >= ' ' && c <= '~') ? c : '.');
            } else {
                fprintf(stderr, " ");
            }
            if ((i & 3) == 3)
                fprintf(stderr, " ");
        }
        fprintf(stderr, " |\n");
    }
#endif
}

void nc_error(char * where)
{
#ifdef _WIN32
    //fprintf(stderr, "NC error:%s:%d/%d\n", where, errno, WSAGetLastError());
#else
    //fprintf(stderr, "NC error:%s:%d:%s\n", where, errno, strerror(errno));
#endif
}

static int nc_init(void)
{
#ifdef _WIN32
    WSADATA wd;
    
    nc_dump("initting", NULL, 0);
    
    if (WSAStartup(MAKEWORD(2,2), &wd) == -1) {
        nc_error("WSAStartup");
        return -1;
    }
#endif
    nc_initted = 1;

    //if (getenv("NC_DUMP"))
    //    nc_do_dump = 1;

    atexit(nc_fini);
    return 0;
}

static unsigned long parse_addr(const char *address_str)
{
    unsigned long addr;
    struct hostent *hent;

    addr = inet_addr(address_str);
    if (addr != INADDR_NONE)
        return addr;

    nc_dump("looking up address", NULL, 0);

    hent = gethostbyname(address_str);
    if (!hent) {
        //herror(address_str);
        return INADDR_NONE;
    }

    if (hent->h_addrtype == AF_INET && hent->h_addr_list[0] != NULL) {
        memcpy(&addr, hent->h_addr_list[0], sizeof(addr));
        return addr;
    }

    //fprintf(stderr, "No IPv4 address for %s\n", address_str);
    return INADDR_NONE;
}

struct nc * nc_open(char * address_str, short port)
{
    struct nc * nc;
    struct sockaddr_in address;

    if (!nc_initted) {
        if (nc_init()) {
            //fprintf(stderr, "Couldn't initialize netclient library\n");
            return NULL;
        }
    }
    
    nc = malloc(sizeof *nc);
    if (!nc) {
        //fprintf(stderr, "Couldn't allocate memory for struct nc\n");
        return NULL;
    }
    memset(nc, 0, sizeof *nc);
    nc->fd = -1;

    memset(&address, 0, sizeof address);
    address.sin_family      = AF_INET;
    address.sin_port        = htons(port);
    address.sin_addr.s_addr = parse_addr(address_str);

    if (address.sin_addr.s_addr == INADDR_NONE) {
        //fprintf(stderr, "Couldn't determine address for \"%s\"\n", address_str);
        return NULL;
    }

    nc_dump("creating socket", NULL, 0);
    nc->fd = socket(AF_INET, SOCK_STREAM, 0);
    if (nc->fd == INVALID_SOCKET) {
        nc_error("open_nc socket");
        return NULL;
    }
    nc_dump("created", NULL, 0);

    nc_dump("connecting", NULL, 0);
    if (connect(nc->fd, (struct sockaddr *)&address, sizeof(address)) == -1) {
        nc_error("open_nc connect");
        return NULL;
    }
    nc_dump("connected", NULL, 0);

    return nc;
}

int nc_close(struct nc * nc)
{
    int r = 0;
    
    if (nc->fd >= 0) {
        nc_dump("closing", NULL, 0);
        r = closesocket(nc->fd);
        nc_dump("closed", NULL, 0);
    }
    free(nc);

    return r;
}

static void fill_rcv_buf(struct nc * nc)
{
    int r;
    
    nc_dump("receiving...", NULL, 0);
    r = recv(nc->fd, nc->rcv_buf, sizeof(nc->rcv_buf), 0);
    nc->low  = 0;
    if (r <= 0) {
        if (r < 0)
            nc_error("fill_rcv_buf recv");
        /* XXX -- error or closed, return whatever we got*/
        nc->high = 0;
    } else {
        nc->high = r;
    }
    nc_dump("received", nc->rcv_buf, nc->high);
}

int nc_read(struct nc * nc, unsigned char * buf, size_t len)
{
    size_t l;
    int r = 0;

    nc_dump("Reading", NULL, len);
    while (len) {
        if (nc->high == nc->low) {
            fill_rcv_buf(nc);
        }
        if (nc->high == 0) {
            nc_dump("Read", buf, r);
            return r;
        }
        l = MIN(len, nc->high - nc->low);
        memcpy(buf + r, nc->rcv_buf + nc->low, l);
        len     -= l;
        r       += l;
        nc->low += l;
    }
    nc_dump("Read", buf, r);
    return r;
}

int nc_read_line(struct nc * nc, unsigned char * buf, size_t maxlen)
{
    size_t r = 0;

    nc_dump("Reading line", NULL, maxlen);
    while (r < maxlen) {
        if (nc_read(nc, buf + r, 1) <= 0) {
            return r;
        }
        if (buf[r] == '\012') {
            buf[r] = '\0';
            if (r > 0 && buf[r-1] == '\015') {
                r--;
                buf[r] = '\0';
            }
            nc_dump("Read line", buf, r);
            return r;
        }
        r++;
    }
    nc_dump("Read line", buf, r);
    return r;
}

int nc_write(struct nc * nc, unsigned char * buf, size_t len)
{
    int r;
    nc_dump("sending...", buf, len);
    r = send(nc->fd, buf, len, 0);
    if (r < 0)
        nc_error("nc_write");
    nc_dump("sent", NULL, 0);
    return r;
}