Subversion Repositories SvarDOS

Rev

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

Rev 329 Rev 333
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
struct net_tcpsocket {
-
 
18
  tcp_Socket *sock;
-
 
19
  tcp_Socket _sock; /* watt32 socket */
-
 
20
};
-
 
21
 
-
 
22
 
17
int net_dnsresolve(char *ip, const char *name) {
23
int net_dnsresolve(char *ip, const char *name) {
18
  unsigned long ipnum;
24
  unsigned long ipnum;
19
  ipnum = resolve(name); /* I could use WatTCP's lookup_host() here to do all
25
  ipnum = resolve(name); /* I could use WatTCP's lookup_host() here to do all
20
                            the job for me, unfortunately lookup_host() issues
26
                            the job for me, unfortunately lookup_host() issues
21
                            wild outs() calls putting garbage on screen... */
27
                            wild outs() calls putting garbage on screen... */
22
  if (ipnum == 0) return(-1);
28
  if (ipnum == 0) return(-1);
23
  _inet_ntoa(ip, ipnum); /* convert to string */
29
  _inet_ntoa(ip, ipnum); /* convert to string */
24
  return(0);
30
  return(0);
25
}
31
}
26
 
32
 
27
 
33
 
28
static int dummy_printf(const char * format, ...) {
34
static int dummy_printf(const char * format, ...) {
29
  if (format == NULL) return(-1);
35
  if (format == NULL) return(-1);
30
  return(0);
36
  return(0);
31
}
37
}
32
 
38
 
33
 
39
 
34
/* must be called before using libtcp. returns 0 on success, or non-zero if network subsystem is not available. */
40
/* must be called before using libtcp. returns 0 on success, or non-zero if network subsystem is not available. */
35
int net_init(void) {
41
int net_init(void) {
36
  tzset();
42
  tzset();
37
  _printf = dummy_printf;  /* this is to avoid watt32 printing its stuff to console */
43
  _printf = dummy_printf;  /* this is to avoid watt32 printing its stuff to console */
38
  return(sock_init());
44
  return(sock_init());
39
}
45
}
40
 
46
 
41
 
47
 
42
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port) {
48
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port) {
43
  struct net_tcpsocket *resultsock;
49
  struct net_tcpsocket *resultsock;
44
  unsigned long ipaddr;
50
  unsigned long ipaddr;
45
 
51
 
46
  /* convert ip to value */
52
  /* convert ip to value */
47
  ipaddr = _inet_addr(ipstr);
53
  ipaddr = _inet_addr(ipstr);
48
  if (ipaddr == 0) return(NULL);
54
  if (ipaddr == 0) return(NULL);
49
 
55
 
50
  resultsock = calloc(sizeof(struct net_tcpsocket) + sizeof(tcp_Socket), 1);
56
  resultsock = calloc(sizeof(struct net_tcpsocket), 1);
51
  if (resultsock == NULL) return(NULL);
57
  if (resultsock == NULL) return(NULL);
52
  resultsock->sock = resultsock->buffer;
58
  resultsock->sock = &(resultsock->_sock);
53
  if (resultsock->sock == NULL) {
-
 
54
    free(resultsock);
-
 
55
    return(NULL);
-
 
56
  }
-
 
57
 
59
 
58
  /* explicitely set user-managed buffer to none (watt32 will use its own internal buffer) */
60
  /* explicitely set user-managed buffer to none (watt32 will use its own internal buffer) */
59
  sock_setbuf(resultsock->sock, NULL, 0);
61
  sock_setbuf(resultsock->sock, NULL, 0);
60
 
62
 
61
  if (!tcp_open(resultsock->sock, 0, ipaddr, port, NULL)) {
63
  if (!tcp_open(resultsock->sock, 0, ipaddr, port, NULL)) {
62
    sock_abort(resultsock->sock);
64
    sock_abort(resultsock->sock);
63
    free(resultsock);
65
    free(resultsock);
64
    return(NULL);
66
    return(NULL);
65
  }
67
  }
66
 
68
 
67
  return(resultsock);
69
  return(resultsock);
68
}
70
}
69
 
71
 
70
 
72
 
71
int net_isconnected(struct net_tcpsocket *s) {
73
int net_isconnected(struct net_tcpsocket *s) {
72
  if (tcp_tick(s->sock) == 0) return(-1);
74
  if (tcp_tick(s->sock) == 0) return(-1);
73
  if (sock_established(s->sock) == 0) return(0);
75
  if (sock_established(s->sock) == 0) return(0);
74
  return(1);
76
  return(1);
75
}
77
}
76
 
78
 
77
 
79
 
78
/* Sends data on socket 'socket'.
80
/* Sends data on socket 'socket'.
79
   Returns the number of bytes sent on success, and < 0 otherwise */
81
   Returns the number of bytes sent on success, and < 0 otherwise */
80
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
82
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
81
  /* call this to let Watt-32 handle its internal stuff */
83
  /* call this to let Watt-32 handle its internal stuff */
82
  if (tcp_tick(socket->sock) == 0) return(-1);
84
  if (tcp_tick(socket->sock) == 0) return(-1);
83
  /* send bytes */
85
  /* send bytes */
84
  return(sock_write(socket->sock, line, len));
86
  return(sock_write(socket->sock, line, len));
85
}
87
}
86
 
88
 
87
 
89
 
88
/* 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.
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.
89
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(). */
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(). */
90
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
92
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
91
  /* call this to let WatTCP handle its internal stuff */
93
  /* call this to let WatTCP handle its internal stuff */
92
  if (tcp_tick(socket->sock) == 0) return(-1);
94
  if (tcp_tick(socket->sock) == 0) return(-1);
93
  return(sock_fastread(socket->sock, buff, maxlen));
95
  return(sock_fastread(socket->sock, buff, maxlen));
94
}
96
}
95
 
97
 
96
 
98
 
97
/* Close the 'sock' socket. */
99
/* Close the 'sock' socket. */
98
void net_close(struct net_tcpsocket *socket) {
100
void net_close(struct net_tcpsocket *socket) {
99
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
101
  /* I could use sock_close() and sock_wait_closed() if I'd want to be
100
   * friendly, but it's much easier on the tcp stack to send a single RST and
102
   * friendly, but it's much easier on the tcp stack to send a single RST and
101
   * forget about the connection (esp. if the peer is misbehaving) */
103
   * forget about the connection (esp. if the peer is misbehaving) */
102
  net_abort(socket);
-
 
103
}
-
 
104
 
-
 
105
 
-
 
106
/* Close the 'sock' socket immediately (to be used when the peer is behaving wrongly) - this is much faster than net_close(). */
-
 
107
void net_abort(struct net_tcpsocket *socket) {
-
 
108
  sock_abort(socket->sock);
104
  sock_abort(socket->sock);
109
  free(socket);
105
  free(socket);
110
}
106
}
111
 
107
 
112
 
108
 
113
const char *net_engine(void) {
109
const char *net_engine(void) {
114
  return(wattcpVersion());
110
  return(wattcpVersion());
115
}
111
}
116
 
112