Subversion Repositories SvarDOS

Rev

Rev 333 | Rev 616 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 333 Rev 335
1
/*
1
/*
2
 * This file is part of the pkgnet package - the SvarDOS package manager.
2
 * This file is part of the pkgnet package - the SvarDOS package manager.
3
 * Copyright (C) Mateusz Viste 2013-2021
3
 * Copyright (C) Mateusz Viste 2013-2021
4
 *
4
 *
5
 * Provides all network functions used by pkgnet, wrapped around the
5
 * Provides all network functions used by pkgnet, wrapped around the
6
 * Watt-32 TCP/IP stack.
6
 * Watt-32 TCP/IP stack.
7
 */
7
 */
8
 
8
 
9
#include <stdlib.h>
9
#include <stdlib.h>
10
 
10
 
11
/* Watt32 */
11
/* Watt32 */
12
#include <tcp.h>
12
#include <tcp.h>
13
 
13
 
14
#include "net.h" /* include self for control */
14
#include "net.h" /* include self for control */
15
 
15
 
16
 
16
 
-
 
17
/* if there is enough memory, net_connect() will try setting up a tcp window
-
 
18
 * larger than the WATT32's default of only 2K
-
 
19
 * on my system's DOSEMU install the relation between tcp buffer and download
-
 
20
 * speed were measured as follows (this is on a 4 Mbps ADSL link):
-
 
21
 *  2K = 20 KiB/s
-
 
22
 *  4K = 51 KiB/s
-
 
23
 *  8K = 67 KiB/s
-
 
24
 *  9K = 98 KiB/s
-
 
25
 * 10K = 98 KiB/s
-
 
26
 * 16K = 96 KiB/s
-
 
27
 * 32K = 98 KiB/s
-
 
28
 * 60K = 98 KiB/s
-
 
29
 */
-
 
30
#define TCPBUFF_SIZE (16 * 1024)
-
 
31
 
-
 
32
 
17
struct net_tcpsocket {
33
struct net_tcpsocket {
18
  tcp_Socket *sock;
34
  tcp_Socket *sock;
19
  tcp_Socket _sock; /* watt32 socket */
35
  tcp_Socket _sock; /* watt32 socket */
-
 
36
  char tcpbuff[1];
20
};
37
};
21
 
38
 
22
 
39
 
23
int net_dnsresolve(char *ip, const char *name) {
40
int net_dnsresolve(char *ip, const char *name) {
24
  unsigned long ipnum;
41
  unsigned long ipnum;
25
  ipnum = resolve(name); /* I could use WatTCP's lookup_host() here to do all
42
  ipnum = resolve(name); /* I could use WatTCP's lookup_host() here to do all
26
                            the job for me, unfortunately lookup_host() issues
43
                            the job for me, unfortunately lookup_host() issues
27
                            wild outs() calls putting garbage on screen... */
44
                            wild outs() calls putting garbage on screen... */
28
  if (ipnum == 0) return(-1);
45
  if (ipnum == 0) return(-1);
29
  _inet_ntoa(ip, ipnum); /* convert to string */
46
  _inet_ntoa(ip, ipnum); /* convert to string */
30
  return(0);
47
  return(0);
31
}
48
}
32
 
49
 
33
 
50
 
34
static int dummy_printf(const char * format, ...) {
51
static int dummy_printf(const char * format, ...) {
35
  if (format == NULL) return(-1);
52
  if (format == NULL) return(-1);
36
  return(0);
53
  return(0);
37
}
54
}
38
 
55
 
39
 
56
 
40
/* must be called before using libtcp. returns 0 on success, or non-zero if network subsystem is not available. */
57
/* must be called before using libtcp. returns 0 on success, or non-zero if network subsystem is not available. */
41
int net_init(void) {
58
int net_init(void) {
42
  tzset();
59
  tzset();
43
  _printf = dummy_printf;  /* this is to avoid watt32 printing its stuff to console */
60
  _printf = dummy_printf;  /* this is to avoid watt32 printing its stuff to console */
44
  return(sock_init());
61
  return(sock_init());
45
}
62
}
46
 
63
 
47
 
64
 
48
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port) {
65
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port) {
49
  struct net_tcpsocket *resultsock;
66
  struct net_tcpsocket *resultsock, *resizsock;
50
  unsigned long ipaddr;
67
  unsigned long ipaddr;
51
 
68
 
52
  /* convert ip to value */
69
  /* convert ip to value */
53
  ipaddr = _inet_addr(ipstr);
70
  ipaddr = _inet_addr(ipstr);
54
  if (ipaddr == 0) return(NULL);
71
  if (ipaddr == 0) return(NULL);
55
 
72
 
56
  resultsock = calloc(sizeof(struct net_tcpsocket), 1);
73
  resultsock = calloc(sizeof(struct net_tcpsocket), 1);
57
  if (resultsock == NULL) return(NULL);
74
  if (resultsock == NULL) return(NULL);
58
  resultsock->sock = &(resultsock->_sock);
75
  resultsock->sock = &(resultsock->_sock);
59
 
76
 
60
  /* explicitely set user-managed buffer to none (watt32 will use its own internal buffer) */
-
 
61
  sock_setbuf(resultsock->sock, NULL, 0);
-
 
62
 
-
 
63
  if (!tcp_open(resultsock->sock, 0, ipaddr, port, NULL)) {
77
  if (!tcp_open(resultsock->sock, 0, ipaddr, port, NULL)) {
64
    sock_abort(resultsock->sock);
78
    sock_abort(resultsock->sock);
65
    free(resultsock);
79
    free(resultsock);
66
    return(NULL);
80
    return(NULL);
67
  }
81
  }
-
 
82
 
-
 
83
  /* set user-managed buffer if possible (watt32's default is only 2K)
-
 
84
   * this must be set AFTER tcp_open(), since the latter rewrites the tcp
-
 
85
   * rx buffer */
-
 
86
  resizsock = realloc(resultsock, sizeof(struct net_tcpsocket) + TCPBUFF_SIZE);
-
 
87
  if (resizsock != NULL) {
-
 
88
    resultsock = resizsock;
-
 
89
    sock_setbuf(resultsock->sock, resultsock->tcpbuff, TCPBUFF_SIZE);
-
 
90
  }
68
 
91
 
69
  return(resultsock);
92
  return(resultsock);
70
}
93
}
71
 
94
 
72
 
95
 
73
int net_isconnected(struct net_tcpsocket *s) {
96
int net_isconnected(struct net_tcpsocket *s) {
74
  if (tcp_tick(s->sock) == 0) return(-1);
97
  if (tcp_tick(s->sock) == 0) return(-1);
75
  if (sock_established(s->sock) == 0) return(0);
98
  if (sock_established(s->sock) == 0) return(0);
76
  return(1);
99
  return(1);
77
}
100
}
78
 
101
 
79
 
102
 
80
/* Sends data on socket 'socket'.
103
/* Sends data on socket 'socket'.
81
   Returns the number of bytes sent on success, and < 0 otherwise */
104
   Returns the number of bytes sent on success, and < 0 otherwise */
82
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
105
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
83
  /* call this to let Watt-32 handle its internal stuff */
106
  /* call this to let Watt-32 handle its internal stuff */
84
  if (tcp_tick(socket->sock) == 0) return(-1);
107
  if (tcp_tick(socket->sock) == 0) return(-1);
85
  /* send bytes */
108
  /* send bytes */
86
  return(sock_write(socket->sock, line, len));
109
  return(sock_write(socket->sock, line, len));
87
}
110
}
88
 
111
 
89
 
112
 
90
/* Reads data from socket 'sock' and write it into buffer 'buff', until end of connection. Will fall into error if the amount of data is bigger than 'maxlen' bytes.
113
/* Reads data from socket 'sock' and write it into buffer 'buff', until end of connection. Will fall into error if the amount of data is bigger than 'maxlen' bytes.
91
Returns the amount of data read (in bytes) on success, or a negative value otherwise. The error code can be translated into a human error message via libtcp_strerr(). */
114
Returns the amount of data read (in bytes) on success, or a negative value otherwise. The error code can be translated into a human error message via libtcp_strerr(). */
92
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
115
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
93
  /* call this to let WatTCP handle its internal stuff */
116
  /* call this to let WatTCP handle its internal stuff */
94
  if (tcp_tick(socket->sock) == 0) return(-1);
117
  if (tcp_tick(socket->sock) == 0) return(-1);
95
  return(sock_fastread(socket->sock, buff, maxlen));
118
  return(sock_fastread(socket->sock, buff, maxlen));
96
}
119
}
97
 
120
 
98
 
121
 
99
/* Close the 'sock' socket. */
122
/* Close the 'sock' socket. */
100
void net_close(struct net_tcpsocket *socket) {
123
void net_close(struct net_tcpsocket *socket) {
101
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
124
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
102
   * friendly, but it's much easier on the tcp stack to send a single RST and
125
   * friendly, but it's much easier on the tcp stack to send a single RST and
103
   * forget about the connection (esp. if the peer is misbehaving) */
126
   * forget about the connection (esp. if the peer is misbehaving) */
104
  sock_abort(socket->sock);
127
  sock_abort(socket->sock);
105
  free(socket);
128
  free(socket);
106
}
129
}
107
 
130
 
108
 
131
 
109
const char *net_engine(void) {
132
const char *net_engine(void) {
110
  return(wattcpVersion());
133
  return(wattcpVersion());
111
}
134
}
112
 
135