Subversion Repositories SvarDOS

Rev

Rev 207 | Rev 333 | 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.
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
 
329 mateuszvis 71
int net_isconnected(struct net_tcpsocket *s) {
207 mateuszvis 72
  if (tcp_tick(s->sock) == 0) return(-1);
73
  if (sock_established(s->sock) == 0) return(0);
74
  return(1);
75
}
76
 
77
 
78
/* Sends data on socket 'socket'.
79
   Returns the number of bytes sent on success, and < 0 otherwise */
329 mateuszvis 80
int net_send(struct net_tcpsocket *socket, const void *line, long len) {
207 mateuszvis 81
  /* call this to let Watt-32 handle its internal stuff */
82
  if (tcp_tick(socket->sock) == 0) return(-1);
83
  /* send bytes */
329 mateuszvis 84
  return(sock_write(socket->sock, line, len));
207 mateuszvis 85
}
86
 
87
 
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.
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(). */
329 mateuszvis 90
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) {
207 mateuszvis 91
  /* call this to let WatTCP handle its internal stuff */
92
  if (tcp_tick(socket->sock) == 0) return(-1);
329 mateuszvis 93
  return(sock_fastread(socket->sock, buff, maxlen));
207 mateuszvis 94
}
95
 
96
 
97
/* Close the 'sock' socket. */
98
void net_close(struct net_tcpsocket *socket) {
99
  /* 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
101
   * 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);
109
  free(socket);
110
}
111
 
112
 
113
const char *net_engine(void) {
114
  return(wattcpVersion());
115
}