Subversion Repositories SvarDOS

Rev

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

Rev 885 Rev 1132
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
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
18
 * larger than the WATT32's default of only 2K
19
 * on my system's DOSEMU install the relation between tcp buffer and download
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):
20
 * speed were measured as follows (this is on a 4 Mbps ADSL link):
21
 *  2K = 20 KiB/s
21
 *  2K = 20 KiB/s
22
 *  4K = 51 KiB/s
22
 *  4K = 51 KiB/s
23
 *  8K = 67 KiB/s
23
 *  8K = 67 KiB/s
24
 *  9K = 98 KiB/s
24
 *  9K = 98 KiB/s
25
 * 10K = 98 KiB/s
25
 * 10K = 98 KiB/s
26
 * 16K = 96 KiB/s
26
 * 16K = 96 KiB/s
27
 * 32K = 98 KiB/s
27
 * 32K = 98 KiB/s
28
 * 60K = 98 KiB/s
28
 * 60K = 98 KiB/s
29
 */
29
 */
30
#define TCPBUFF_SIZE (16 * 1024)
-
 
31
 
-
 
32
 
30
 
33
struct net_tcpsocket {
31
struct net_tcpsocket {
34
  tcp_Socket *sock;
32
  tcp_Socket *sock;
35
  tcp_Socket _sock; /* watt32 socket */
33
  tcp_Socket _sock; /* watt32 socket */
36
  char tcpbuff[1];
34
  char tcpbuff[1];
37
};
35
};
38
 
36
 
39
 
37
 
40
int net_dnsresolve(char *ip, const char *name, int retries) {
38
int net_dnsresolve(char *ip, const char *name, int retries) {
41
  unsigned long ipnum;
39
  unsigned long ipnum;
42
  do {
40
  do {
43
    ipnum = resolve(name); /* I could use WatTCP's lookup_host() to do all the
41
    ipnum = resolve(name); /* I could use WatTCP's lookup_host() to do all the
44
                              job for me, unfortunately lookup_host() issues
42
                              job for me, unfortunately lookup_host() issues
45
                              wild outs() calls putting garbage on screen... */
43
                              wild outs() calls putting garbage on screen... */
46
  } while ((ipnum == 0) && (retries-- > 0));
44
  } while ((ipnum == 0) && (retries-- > 0));
47
  if (ipnum == 0) return(-1);
45
  if (ipnum == 0) return(-1);
48
  _inet_ntoa(ip, ipnum); /* convert to string */
46
  _inet_ntoa(ip, ipnum); /* convert to string */
49
  return(0);
47
  return(0);
50
}
48
}
51
 
49
 
52
 
50
 
53
static int dummy_printf(const char * format, ...) {
51
static int dummy_printf(const char * format, ...) {
54
  if (format == NULL) return(-1);
52
  if (format == NULL) return(-1);
55
  return(0);
53
  return(0);
56
}
54
}
57
 
55
 
58
 
56
 
59
/* 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. */
60
int net_init(void) {
58
int net_init(void) {
61
  tzset();
59
  tzset();
62
  _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 */
63
  return(sock_init());
61
  return(sock_init());
64
}
62
}
65
 
63
 
66
 
64
 
67
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port) {
65
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port, unsigned short buffsz) {
68
  struct net_tcpsocket *resultsock, *resizsock;
66
  struct net_tcpsocket *resultsock;
69
  unsigned long ipaddr;
67
  unsigned long ipaddr;
70
 
68
 
71
  /* convert ip to value */
69
  /* convert ip to value */
72
  ipaddr = _inet_addr(ipstr);
70
  ipaddr = _inet_addr(ipstr);
73
  if (ipaddr == 0) return(NULL);
71
  if (ipaddr == 0) return(NULL);
74
 
72
 
-
 
73
  /* ignore buffsz smaller than 2K (wattcp already has a 2K buffer) */
-
 
74
  if (buffsz <= 2048) buffsz = 0;
-
 
75
 
75
  resultsock = calloc(sizeof(struct net_tcpsocket), 1);
76
  resultsock = calloc(sizeof(struct net_tcpsocket) + buffsz, 1);
76
  if (resultsock == NULL) return(NULL);
77
  if (resultsock == NULL) return(NULL);
77
  resultsock->sock = &(resultsock->_sock);
78
  resultsock->sock = &(resultsock->_sock);
78
 
79
 
79
  if (!tcp_open(resultsock->sock, 0, ipaddr, port, NULL)) {
80
  if (!tcp_open(resultsock->sock, 0, ipaddr, port, NULL)) {
80
    sock_abort(resultsock->sock);
81
    sock_abort(resultsock->sock);
81
    free(resultsock);
82
    free(resultsock);
82
    return(NULL);
83
    return(NULL);
83
  }
84
  }
84
 
85
 
85
  /* set user-managed buffer if possible (watt32's default is only 2K)
86
  /* set user-managed buffer if requested (watt32's default is only 2K)
86
   * this must be set AFTER tcp_open(), since the latter rewrites the tcp
87
   * this must be set AFTER tcp_open(), since the latter rewrites the tcp
87
   * rx buffer */
88
   * rx buffer */
88
  resizsock = realloc(resultsock, sizeof(struct net_tcpsocket) + TCPBUFF_SIZE);
-
 
89
  if (resizsock != NULL) {
-
 
90
    resultsock = resizsock;
-
 
91
    sock_setbuf(resultsock->sock, resultsock->tcpbuff, TCPBUFF_SIZE);
89
  if (buffsz > 0) sock_setbuf(resultsock->sock, resultsock->tcpbuff, buffsz);
92
  }
-
 
93
 
90
 
94
  return(resultsock);
91
  return(resultsock);
95
}
92
}
96
 
93
 
97
 
94
 
98
int net_isconnected(struct net_tcpsocket *s) {
95
int net_isconnected(struct net_tcpsocket *s) {
99
  if (tcp_tick(s->sock) == 0) return(-1);
96
  if (tcp_tick(s->sock) == 0) return(-1);
100
  if (sock_established(s->sock) == 0) return(0);
97
  if (sock_established(s->sock) == 0) return(0);
101
  return(1);
98
  return(1);
102
}
99
}
103
 
100
 
104
 
101
 
105
/* Sends data on socket 'socket'.
102
/* Sends data on socket 'socket'.
106
   Returns the number of bytes sent on success, and < 0 otherwise */
103
   Returns the number of bytes sent on success, and < 0 otherwise */
107
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
104
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
108
  /* call this to let Watt-32 handle its internal stuff */
105
  /* call this to let Watt-32 handle its internal stuff */
109
  if (tcp_tick(socket->sock) == 0) return(-1);
106
  if (tcp_tick(socket->sock) == 0) return(-1);
110
  /* send bytes */
107
  /* send bytes */
111
  return(sock_write(socket->sock, line, len));
108
  return(sock_write(socket->sock, line, len));
112
}
109
}
113
 
110
 
114
 
111
 
115
/* 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.
112
/* 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.
116
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(). */
113
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(). */
117
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
114
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
118
  /* call this to let WatTCP handle its internal stuff */
115
  /* call this to let WatTCP handle its internal stuff */
119
  if (tcp_tick(socket->sock) == 0) return(-1);
116
  if (tcp_tick(socket->sock) == 0) return(-1);
120
  return(sock_fastread(socket->sock, buff, maxlen));
117
  return(sock_fastread(socket->sock, buff, maxlen));
121
}
118
}
122
 
119
 
123
 
120
 
124
/* Close the 'sock' socket. */
121
/* Close the 'sock' socket. */
125
void net_close(struct net_tcpsocket *socket) {
122
void net_close(struct net_tcpsocket *socket) {
126
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
123
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
127
   * friendly, but it's much easier on the tcp stack to send a single RST and
124
   * friendly, but it's much easier on the tcp stack to send a single RST and
128
   * forget about the connection (esp. if the peer is misbehaving) */
125
   * forget about the connection (esp. if the peer is misbehaving) */
129
  sock_abort(socket->sock);
126
  sock_abort(socket->sock);
130
  free(socket);
127
  free(socket);
131
}
128
}
132
 
129
 
133
 
130
 
134
const char *net_engine(void) {
131
const char *net_engine(void) {
135
  return(wattcpVersion());
132
  return(wattcpVersion());
136
}
133
}
137
 
134