Subversion Repositories SvarDOS

Rev

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

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