Subversion Repositories SvarDOS

Rev

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

Rev 1502 Rev 1559
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-2023
3
 * Copyright (C) Mateusz Viste 2013-2023
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
 
30
 
31
struct net_tcpsocket {
31
struct net_tcpsocket {
32
  tcp_Socket sock; /* watt32 socket */
32
  tcp_Socket sock; /* watt32 socket */
33
  char tcpbuff[1];
33
  char tcpbuff[1];
34
};
34
};
35
 
35
 
36
 
36
 
37
int net_dnsresolve(char *ip, const char *name, int retries) {
-
 
38
  unsigned long ipnum;
-
 
39
  do {
-
 
40
    ipnum = resolve(name); /* I could use WatTCP's lookup_host() to do all the
-
 
41
                              job for me, unfortunately lookup_host() issues
-
 
42
                              wild outs() calls putting garbage on screen... */
-
 
43
  } while ((ipnum == 0) && (retries-- > 0));
-
 
44
  if (ipnum == 0) return(-1);
-
 
45
  _inet_ntoa(ip, ipnum); /* convert to string */
-
 
46
  return(0);
-
 
47
}
-
 
48
 
-
 
49
 
-
 
50
static int dummy_printf(const char * format, ...) {
37
static int dummy_printf(const char * format, ...) {
51
  if (format == NULL) return(-1);
38
  if (format == NULL) return(-1);
52
  return(0);
39
  return(0);
53
}
40
}
54
 
41
 
55
 
42
 
56
/* must be called before using libtcp. returns 0 on success, or non-zero if network subsystem is not available. */
43
/* must be called before using libtcp. returns 0 on success, or non-zero if network subsystem is not available. */
57
int net_init(void) {
44
int net_init(void) {
58
  tzset();
45
  tzset();
59
  _printf = dummy_printf;  /* this is to avoid watt32 printing its stuff to console */
46
  _printf = dummy_printf;  /* this is to avoid watt32 printing its stuff to console */
60
  return(sock_init());
47
  return(sock_init());
61
}
48
}
62
 
49
 
63
 
50
 
64
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port, unsigned short buffsz) {
51
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port, unsigned short buffsz) {
65
  struct net_tcpsocket *resultsock;
52
  struct net_tcpsocket *resultsock;
66
  unsigned long ipaddr;
53
  unsigned long ipaddr;
67
 
54
 
68
  /* convert ip to value */
55
  /* convert ip to value */
69
  ipaddr = _inet_addr(ipstr);
56
  ipaddr = _inet_addr(ipstr);
70
  if (ipaddr == 0) return(NULL);
57
  if (ipaddr == 0) return(NULL);
71
 
58
 
72
  /* ignore buffsz smaller than 2K (wattcp already has a 2K buffer) */
59
  /* ignore buffsz smaller than 2K (wattcp already has a 2K buffer) */
73
  if (buffsz <= 2048) buffsz = 0;
60
  if (buffsz <= 2048) buffsz = 0;
74
 
61
 
75
  resultsock = calloc(sizeof(struct net_tcpsocket) + buffsz, 1);
62
  resultsock = calloc(sizeof(struct net_tcpsocket) + buffsz, 1);
76
  if (resultsock == NULL) return(NULL);
63
  if (resultsock == NULL) return(NULL);
77
 
64
 
78
  if (!tcp_open(&(resultsock->sock), 0, ipaddr, port, NULL)) {
65
  if (!tcp_open(&(resultsock->sock), 0, ipaddr, port, NULL)) {
79
    sock_abort(&(resultsock->sock));
66
    sock_abort(&(resultsock->sock));
80
    free(resultsock);
67
    free(resultsock);
81
    return(NULL);
68
    return(NULL);
82
  }
69
  }
83
 
70
 
84
  /* set user-managed buffer if requested (watt32's default is only 2K)
71
  /* set user-managed buffer if requested (watt32's default is only 2K)
85
   * this must be set AFTER tcp_open(), since the latter rewrites the tcp
72
   * this must be set AFTER tcp_open(), since the latter rewrites the tcp
86
   * rx buffer */
73
   * rx buffer */
87
  if (buffsz > 0) sock_setbuf(&(resultsock->sock), resultsock->tcpbuff, buffsz);
74
  if (buffsz > 0) sock_setbuf(&(resultsock->sock), resultsock->tcpbuff, buffsz);
88
 
75
 
89
  return(resultsock);
76
  return(resultsock);
90
}
77
}
91
 
78
 
92
 
79
 
93
int net_isconnected(struct net_tcpsocket *s) {
80
int net_isconnected(struct net_tcpsocket *s) {
94
  if (tcp_tick(&(s->sock)) == 0) return(-1);
81
  if (tcp_tick(&(s->sock)) == 0) return(-1);
95
  if (sock_established(&(s->sock)) == 0) return(0);
82
  if (sock_established(&(s->sock)) == 0) return(0);
96
  return(1);
83
  return(1);
97
}
84
}
98
 
85
 
99
 
86
 
100
/* Sends data on socket 'socket'.
87
/* Sends data on socket 'socket'.
101
   Returns the number of bytes sent on success, and < 0 otherwise */
88
   Returns the number of bytes sent on success, and < 0 otherwise */
102
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
89
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
103
  /* call this to let Watt-32 handle its internal stuff */
90
  /* call this to let Watt-32 handle its internal stuff */
104
  if (tcp_tick(&(socket->sock)) == 0) return(-1);
91
  if (tcp_tick(&(socket->sock)) == 0) return(-1);
105
  /* send bytes */
92
  /* send bytes */
106
  return(sock_write(&(socket->sock), line, len));
93
  return(sock_write(&(socket->sock), line, len));
107
}
94
}
108
 
95
 
109
 
96
 
110
/* 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.
97
/* 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.
111
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(). */
98
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(). */
112
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
99
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
113
  /* call this to let WatTCP handle its internal stuff */
100
  /* call this to let WatTCP handle its internal stuff */
114
  if (tcp_tick(&(socket->sock)) == 0) return(-1);
101
  if (tcp_tick(&(socket->sock)) == 0) return(-1);
115
  return(sock_fastread(&(socket->sock), buff, maxlen));
102
  return(sock_fastread(&(socket->sock), buff, maxlen));
116
}
103
}
117
 
104
 
118
 
105
 
119
/* Close the 'sock' socket. */
106
/* Close the 'sock' socket. */
120
void net_close(struct net_tcpsocket *socket) {
107
void net_close(struct net_tcpsocket *socket) {
121
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
108
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
122
   * friendly, but it's much easier on the tcp stack to send a single RST and
109
   * friendly, but it's much easier on the tcp stack to send a single RST and
123
   * forget about the connection (esp. if the peer is misbehaving) */
110
   * forget about the connection (esp. if the peer is misbehaving) */
124
  sock_abort(&(socket->sock));
111
  sock_abort(&(socket->sock));
125
  free(socket);
112
  free(socket);
126
}
113
}
127
 
114
 
128
 
115
 
129
void net_shut(void) {
116
void net_shut(void) {
130
  sock_exit();
117
  sock_exit();
131
}
118
}
132
 
119
 
133
 
120
 
134
const char *net_engine(void) {
121
const char *net_engine(void) {
135
  return(wattcpVersion());
122
  return(wattcpVersion());
136
}
123
}
137
 
124