Subversion Repositories SvarDOS

Rev

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