Subversion Repositories SvarDOS

Rev

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