aboutsummaryrefslogtreecommitdiffstats
path: root/netopen.c
diff options
context:
space:
mode:
Diffstat (limited to 'netopen.c')
-rw-r--r--netopen.c171
1 files changed, 18 insertions, 153 deletions
diff --git a/netopen.c b/netopen.c
index cf17cda..bec777d 100644
--- a/netopen.c
+++ b/netopen.c
@@ -18,16 +18,6 @@
#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 */
@@ -49,148 +39,23 @@ static void start_winsock(void)
}
}
-static int input_get(netcon_t nc)
+static FILE *sock_to_file(SOCKET sock, int is_write)
{
- 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;
+ FILE *f;
+ int fd;
+ int osfhandle = sock;
- 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 )
+ fd = _open_osfhandle(&osfhandle, is_write ? 0 : _O_RDONLY);
+ if (fd < 0) {
+ closesocket(fd);
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;
-}
+ f = fdopen(fd, is_write ? "w" : "r");
+ if (!f)
+ _close(fd);
-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;
+ return f;
}
#else
@@ -209,13 +74,13 @@ typedef int SOCKET;
#define start_winsock() do { } while (0)
#define closesocket(x) close(x)
-static netcon_t sock_to_netcon(int sock, int is_write)
+static FILE *sock_to_file(SOCKET sock, int is_write)
{
- FILE *f = fdopen(sock, is_write ? "w" : "r");
- if ( !f ) {
+ FILE *f;
+
+ f = fdopen(sock, is_write ? "w" : "f");
+ if (!f)
close(sock);
- return NULL;
- }
return f;
}
@@ -305,7 +170,7 @@ static SOCKET open_socket(const char *name, int port)
#endif
-netcon_t fopen_network(const char *name, int port, int is_write)
+FILE *fopen_network(const char *name, int port, int is_write)
{
SOCKET sock;
@@ -313,5 +178,5 @@ netcon_t fopen_network(const char *name, int port, int is_write)
if (sock == INVALID_SOCKET)
return NULL;
- return sock_to_netcon(sock, is_write);
+ return sock_to_file(sock, is_write);
}