<*+ CSTDLIB  *>
<*- NOHEADER *>
<*+ M2EXTENSIONS *>

DEFINITION MODULE ["C"] tcp;


PROCEDURE resolv(url:ARRAY OF CHAR; VAR host:CARDINAL):INTEGER;
(*
PROCEDURE opentcp():INTEGER;
*)
PROCEDURE connectto(VAR url,port:ARRAY OF CHAR):INTEGER;
PROCEDURE waitconnect(port:ARRAY OF CHAR; maxconn:CARDINAL):INTEGER;
PROCEDURE acceptconnect(fd:INTEGER; VAR addr:ARRAY OF CHAR; VAR len:INTEGER):INTEGER;
PROCEDURE ipnum2str(sockaddr:ARRAY OF CHAR; VAR s:ARRAY OF CHAR; maxlen:CARDINAL);
PROCEDURE sendsock(fd:INTEGER; VAR buf:ARRAY OF CHAR; len:INTEGER):INTEGER;
PROCEDURE sendmore(fd:INTEGER; VAR buf:ARRAY OF CHAR; len:INTEGER):INTEGER;
PROCEDURE readsock(fd:INTEGER; VAR buf:ARRAY OF CHAR; len:INTEGER):INTEGER;
PROCEDURE getsockipnum(fd:INTEGER; VAR addr:ARRAY OF CHAR; VAR len:INTEGER):INTEGER;
PROCEDURE getpeeripnum(fd:INTEGER; VAR addr:ARRAY OF CHAR; VAR len:INTEGER):INTEGER;
PROCEDURE ipnumport2str(sockaddr:ARRAY OF CHAR; salen:CARDINAL; VAR ip:ARRAY OF CHAR; maxiplen:CARDINAL; VAR port:ARRAY OF CHAR; maxportlen:CARDINAL);
PROCEDURE getunack(fd:INTEGER):INTEGER;
PROCEDURE stoptxrx(fd, how:INTEGER);
(*
PROCEDURE sockreuse(fd:INTEGER):INTEGER;
*)


END tcp.

(*
struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

struct sockaddr_in6 {
    u_int16_t       sin6_family;   // address family, AF_INET6
    u_int16_t       sin6_port;     // port number, Network Byte Order
    u_int32_t       sin6_flowinfo; // IPv6 flow information
    struct in6_addr sin6_addr;     // IPv6 address
    u_int32_t       sin6_scope_id; // Scope ID
};

struct sockaddr_in ip4addr;
int s;

ip4addr.sin_family = AF_INET;
ip4addr.sin_port = htons(3490);
inet_pton(AF_INET, "10.0.0.1", &ip4addr.sin_addr);

s = socket(PF_INET, SOCK_STREAM, 0);
bind(s, (struct sockaddr*)&ip4addr, sizeof ip4addr);

-----------

struct sockaddr_in6 ip6addr;
int s;

ip6addr.sin6_family = AF_INET6;
ip6addr.sin6_port = htons(4950);
inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &ip6addr.sin6_addr);

s = socket(PF_INET6, SOCK_STREAM, 0);
bind(s, (struct sockaddr*)&ip6addr, sizeof ip6addr);

-------
// IPv4 demo of inet_ntop() and inet_pton()

struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];

// store this IP address in sa:
inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));

// now get it back and print it
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);

printf("%s\n", str); // prints "192.0.2.33"
// IPv6 demo of inet_ntop() and inet_pton()
// (basically the same except with a bunch of 6s thrown around)

struct sockaddr_in6 sa;
char str[INET6_ADDRSTRLEN];

// store this IP address in sa:
inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &(sa.sin6_addr));

// now get it back and print it
inet_ntop(AF_INET6, &(sa.sin6_addr), str, INET6_ADDRSTRLEN);

printf("%s\n", str); // prints "2001:db8:8714:3a90::12"


*)