Subversion Repositories SvarDOS

Rev

Rev 1502 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
207 mateuszvis 1
/*
2
 * This file is part of the pkgnet package - the SvarDOS package manager.
1502 mateusz.vi 3
 * Copyright (C) Mateusz Viste 2013-2023
207 mateuszvis 4
 *
5
 * Provides all network functions used by pkgnet, wrapped around the
6
 * Watt-32 TCP/IP stack.
7
 */
8
 
9
#include <stdlib.h>
10
 
11
/* Watt32 */
12
#include <tcp.h>
13
 
14
#include "net.h" /* include self for control */
15
 
16
 
335 mateuszvis 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
 
333 mateuszvis 31
struct net_tcpsocket {
1133 mateusz.vi 32
  tcp_Socket sock; /* watt32 socket */
335 mateuszvis 33
  char tcpbuff[1];
333 mateuszvis 34
};
35
 
36
 
207 mateuszvis 37
static int dummy_printf(const char * format, ...) {
38
  if (format == NULL) return(-1);
39
  return(0);
40
}
41
 
42
 
43
/* must be called before using libtcp. returns 0 on success, or non-zero if network subsystem is not available. */
44
int net_init(void) {
45
  tzset();
46
  _printf = dummy_printf;  /* this is to avoid watt32 printing its stuff to console */
47
  return(sock_init());
48
}
49
 
50
 
1132 mateusz.vi 51
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port, unsigned short buffsz) {
52
  struct net_tcpsocket *resultsock;
207 mateuszvis 53
  unsigned long ipaddr;
54
 
55
  /* convert ip to value */
56
  ipaddr = _inet_addr(ipstr);
57
  if (ipaddr == 0) return(NULL);
58
 
1132 mateusz.vi 59
  /* ignore buffsz smaller than 2K (wattcp already has a 2K buffer) */
60
  if (buffsz <= 2048) buffsz = 0;
61
 
62
  resultsock = calloc(sizeof(struct net_tcpsocket) + buffsz, 1);
207 mateuszvis 63
  if (resultsock == NULL) return(NULL);
64
 
1133 mateusz.vi 65
  if (!tcp_open(&(resultsock->sock), 0, ipaddr, port, NULL)) {
66
    sock_abort(&(resultsock->sock));
207 mateuszvis 67
    free(resultsock);
68
    return(NULL);
69
  }
70
 
1132 mateusz.vi 71
  /* set user-managed buffer if requested (watt32's default is only 2K)
335 mateuszvis 72
   * this must be set AFTER tcp_open(), since the latter rewrites the tcp
73
   * rx buffer */
1133 mateusz.vi 74
  if (buffsz > 0) sock_setbuf(&(resultsock->sock), resultsock->tcpbuff, buffsz);
335 mateuszvis 75
 
207 mateuszvis 76
  return(resultsock);
77
}
78
 
79
 
329 mateuszvis 80
int net_isconnected(struct net_tcpsocket *s) {
1133 mateusz.vi 81
  if (tcp_tick(&(s->sock)) == 0) return(-1);
82
  if (sock_established(&(s->sock)) == 0) return(0);
207 mateuszvis 83
  return(1);
84
}
85
 
86
 
87
/* Sends data on socket 'socket'.
88
   Returns the number of bytes sent on success, and < 0 otherwise */
329 mateuszvis 89
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
207 mateuszvis 90
  /* call this to let Watt-32 handle its internal stuff */
1133 mateusz.vi 91
  if (tcp_tick(&(socket->sock)) == 0) return(-1);
207 mateuszvis 92
  /* send bytes */
1133 mateusz.vi 93
  return(sock_write(&(socket->sock), line, len));
207 mateuszvis 94
}
95
 
96
 
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.
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(). */
329 mateuszvis 99
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
207 mateuszvis 100
  /* call this to let WatTCP handle its internal stuff */
1133 mateusz.vi 101
  if (tcp_tick(&(socket->sock)) == 0) return(-1);
102
  return(sock_fastread(&(socket->sock), buff, maxlen));
207 mateuszvis 103
}
104
 
105
 
106
/* Close the 'sock' socket. */
107
void net_close(struct net_tcpsocket *socket) {
108
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
109
   * friendly, but it's much easier on the tcp stack to send a single RST and
110
   * forget about the connection (esp. if the peer is misbehaving) */
1133 mateusz.vi 111
  sock_abort(&(socket->sock));
207 mateuszvis 112
  free(socket);
113
}
114
 
115
 
1502 mateusz.vi 116
void net_shut(void) {
117
  sock_exit();
118
}
119
 
120
 
207 mateuszvis 121
const char *net_engine(void) {
122
  return(wattcpVersion());
123
}