1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/** -*-C-*-ish
Kaya standard library
Copyright (C) 2004, 2005 Edwin Brady
This file is distributed under the terms of the GNU Lesser General
Public Licence. See COPYING for licence.
*/
module Net;
import Builtins;
import System;
import Prelude;
import TLS;
// TODO: TLS currently available for client only, server support needed
%include "network_glue.h";
%include "unistd.h";
%imported "network_glue";
// %link "wsock32";
foreign "network_glue.o" {
Void do_net_init(Ptr vm) = net_init;
String net_recv(Ptr vm, Ptr handle, a len) = net_recv;
Ptr net_connect(Ptr vm, Int proto, String server, Int port) = net_connect;
Int net_listen(Ptr vm, Int proto, Int port, Int backlog) = net_listen;
Ptr net_accept(Ptr vm, Int socket) = net_accept;
Void net_shutdown(Ptr h) = net_shutdown;
Void net_close(Ptr h) = net_close;
Void close(Int x) = close;
Void net_send(Ptr vm, Ptr h, String d) = net_send;
Bool net_pending(Ptr vm, Ptr h, Int timeout) = net_pending;
String net_getaddr(Ptr h) = net_getaddr;
}
abstract data NetHandle = NetH(Ptr handle, Maybe<TLSsession> tls);
Int MAXCONN = 128;
public data Protocol = TCP | TCP_IPv4 | TCP_IPv6;
"Initialises networking libraries. Not required on Posix-based systems,
but should be called anyway for compatibility."
public Void netInit() {
do_net_init(getVM());
}
"Connect to a server (optionally using TLS).
Returns connection handle."
public NetHandle connect(Protocol proto, String server, Int port, Bool usetls=false)
{
h = net_connect(getVM(),protocolToInt(proto),server,port);
if (usetls) {
tls = makeTLS(h);
return NetH(h,just(tls));
} else {
return NetH(h,nothing);
}
}
"Listen on a port.
Returns a socket id."
public Int listen(Protocol proto, Int port, Int backlog)
= net_listen(getVM(),protocolToInt(proto),port,backlog);
"Return the Int representation of protocol needed by the C glue library."
Int protocolToInt(Protocol proto) = case proto of {
TCP -> 1;
| TCP_IPv4 -> 1;
| TCP_IPv6 -> 2;
};
"Accept a connection on a socket.
Returns a connection handle."
public NetHandle accept(Int socket) {
h = net_accept(getVM(),socket);
return NetH(h,nothing);
}
"Shutdown a handle."
public Void shutdown(NetHandle h) {
net_shutdown(h.handle);
}
"Close a connection handle."
public Void closeConnection(NetHandle h) {
case h.tls of {
nothing -> ;
| just(tls) -> closeTLS(tls);
}
net_close(h.handle);
}
"Close a socket."
public Void closeSocket(Int x) {
close(x);
}
"Send data across a connection."
public Void send(NetHandle h, String d) {
case h.tls of {
nothing -> net_send(getVM(),h.handle,d);
| just(tls) -> putTLS(tls,d);
}
}
"Recieve data from a connection.
Reads up to <em>maxlen</em> bytes, and times out after <em>timeout</em>
seconds. If <em>maxlen</em> is negative (the default), reads as much as
possible. <em>maxlen</em> and <em>timeout</em> are currently ignored for
TLS connections"
public String recv(NetHandle h, Int maxlen = -1, Int timeout = 0)
{
case h.tls of {
nothing -> return dorecv(h, maxlen, timeout*1000000);
| just(tls) -> return getTLS(tls);
}
}
"Recieve data from a connection. As recv() but with a timeout measured
in microseconds."
public String microRecv(NetHandle h, Int maxlen = -1, Int microtimeout = 0)
{
case h.tls of {
nothing -> return dorecv(h, maxlen, microtimeout);
| just(tls) -> return getTLS(tls);
}
}
// timeout in microseconds
String dorecv(NetHandle h, Int maxlen = -1, Int timeout = 0) {
len = maxlen;
rpt = false;
if (maxlen==-1) {
len = 65535; // Repeatedly read in 64k chunks.
rpt = true;
}
str="";
do {
if (timeout==0) {
str += net_recv(getVM(),h.handle,len);
// putStrLn(str);
}
else {
if (microPending(h,timeout)) {
str += net_recv(getVM(),h.handle,len);
}
else {
throw(Exception("Nothing to receive",1));
}
}
if (len==0) { rpt=false; }
}
while (rpt);
return str;
}
"Return whether data is waiting at a socket.
Times out by default after 5 seconds."
public Bool pending(NetHandle h, Int timeout = 5) {
return net_pending(getVM(),h.handle,timeout*1000000);
}
"Return whether data is waiting at a socket.
Times out by default after 100 microseconds."
public Bool microPending(NetHandle h, Int timeout = 100) {
return net_pending(getVM(),h.handle,timeout);
}
"Get the host name from a connection handle."
public String getHost(NetHandle h) = net_getaddr(h.handle);
|