aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/net
diff options
context:
space:
mode:
authorWilliam Harrington <kb0iic@berzerkula.org>2025-01-14 16:06:02 -0600
committerWilliam Harrington <kb0iic@berzerkula.org>2025-01-14 16:06:02 -0600
commit0cc9b20c15460213e488bf5e70963b941482f628 (patch)
treebb0143245583ec846630f39bfa2258dba640ccd7 /sdk/net
parent0e084ade5069756d487b5c948c48b777e37c00c9 (diff)
Add source.
Diffstat (limited to 'sdk/net')
-rw-r--r--sdk/net/Makefile.in19
-rw-r--r--sdk/net/bind.conf0
-rw-r--r--sdk/net/gethost.c39
-rw-r--r--sdk/net/getservice.c45
-rw-r--r--sdk/net/getsocket.c43
-rw-r--r--sdk/net/init.c20
-rw-r--r--sdk/net/make.conf4
-rw-r--r--sdk/net/msgport.c84
-rw-r--r--sdk/net/msgport.h47
-rw-r--r--sdk/net/netaddr.c28
-rw-r--r--sdk/net/snmptrap.c165
-rw-r--r--sdk/net/socket.h61
-rw-r--r--sdk/net/sockname.c62
-rw-r--r--sdk/net/stream.h45
-rw-r--r--sdk/net/tcpsocket.c42
-rw-r--r--sdk/net/tcpstream.c88
-rw-r--r--sdk/net/trap.h30
-rw-r--r--sdk/net/udpsocket.c36
18 files changed, 858 insertions, 0 deletions
diff --git a/sdk/net/Makefile.in b/sdk/net/Makefile.in
new file mode 100644
index 0000000..bc890f5
--- /dev/null
+++ b/sdk/net/Makefile.in
@@ -0,0 +1,19 @@
+#
+# Template to build net makefile.
+# $Id$
+# Copyright (c) 1997 by Tycho Softworks.
+#
+
+OBJS = snmptrap.o gethost.o getservice.o getsocket.o sockname.o \
+ tcpsocket.o udpsocket.o init.o tcpstream.o msgport.o netaddr.o
+
+.c.o:
+ $(CC) $(CFLAGS) $(OPTIMIZE) -I.. -o $@ -c $<
+ $(AR) r ../lib/libnet.a $@
+
+all: $(OBJS)
+ ranlib ../lib/libnet.a
+
+clean:
+ rm *.o
+
diff --git a/sdk/net/bind.conf b/sdk/net/bind.conf
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sdk/net/bind.conf
diff --git a/sdk/net/gethost.c b/sdk/net/gethost.c
new file mode 100644
index 0000000..f74997c
--- /dev/null
+++ b/sdk/net/gethost.c
@@ -0,0 +1,39 @@
+/*
+ * Portable and simple host name lookup fuction.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/socket.h>
+
+struct hostent *gethost(const char *hostname)
+{
+ static struct in_addr saddr;
+ struct hostent *hp;
+ static struct hostent np;
+ static char host[24];
+ static char *alias[] = {NULL};
+ static struct in_addr *alist[] = {&saddr, NULL};
+
+ if(!hostname)
+ return NULL;
+
+ if(isdigit(*hostname))
+ {
+ saddr.s_addr = inet_addr(hostname);
+ hp = gethostbyaddr((void *)&saddr, sizeof(struct in_addr), AF_INET);
+ if(hp)
+ return hp;
+
+ strcpy(host, hostname);
+ np.h_name = host;
+ np.h_aliases = alias;
+ np.h_addrtype = AF_INET;
+ np.h_length = 4;
+ np.h_addr_list = (char **)alist;
+ return &np;
+ }
+ else
+ return gethostbyname(hostname);
+}
diff --git a/sdk/net/getservice.c b/sdk/net/getservice.c
new file mode 100644
index 0000000..fed448e
--- /dev/null
+++ b/sdk/net/getservice.c
@@ -0,0 +1,45 @@
+/*
+ * Simple portable service name lookup.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/socket.h>
+#include <std/string.h>
+
+int getservice(const char *service)
+{
+ char servname[32];
+ char *p;
+ struct servent *svc;
+ struct protoent *proto;
+
+ if(isdigit(*service))
+ return atoi(service);
+
+ strcpy(servname, service);
+ p = strchr(servname, '/');
+ if(!p)
+ strcat(servname, "/tcp");
+
+ p = strchr(servname, '/');
+ *(p++) = 0;
+ if(isdigit(*p))
+ {
+ proto = getprotobynumber(atoi(p));
+ if(!proto)
+ return 0;
+
+ p = proto->p_name;
+ }
+ svc = getservbyname(servname, p);
+ if(!svc)
+ return 0;
+
+ return ntohs(svc->s_port);
+}
+
+
+
+
diff --git a/sdk/net/getsocket.c b/sdk/net/getsocket.c
new file mode 100644
index 0000000..83ca68a
--- /dev/null
+++ b/sdk/net/getsocket.c
@@ -0,0 +1,43 @@
+#include <net/socket.h>
+#include <std/string.h>
+
+SOCKET getsocket(const char *host, int port, int socktype)
+{
+ SOCKET so;
+ struct hostent *hp;
+ struct sockaddr_in saddr;
+ struct in_addr *aptr;
+ char *sp, *p;
+ char *dup = strdup(host);
+
+ so = socket(AF_INET, socktype, 0);
+ if(so == INVALID_SOCKET)
+ {
+ free(dup);
+ return so;
+ }
+
+ sp = dup;
+ while(NULL != (p = strtok(sp, " ;:\t")))
+ {
+ sp = NULL;
+ hp = gethost(p);
+ if(!hp)
+ continue;
+
+ saddr.sin_family = hp->h_addrtype;
+ saddr.sin_port = htons(port);
+ while( (aptr = (struct in_addr *)*(hp->h_addr_list)++) != NULL)
+ saddr.sin_addr = *aptr;
+
+ if(connect(so, (struct sockaddr *)&saddr, sizeof(saddr)))
+ continue;
+
+ free(dup);
+ return so;
+ }
+ endsocket(so);
+ free(dup);
+ return INVALID_SOCKET;
+}
+
diff --git a/sdk/net/init.c b/sdk/net/init.c
new file mode 100644
index 0000000..02575a7
--- /dev/null
+++ b/sdk/net/init.c
@@ -0,0 +1,20 @@
+/*
+ * Needed for "winsock" compatibility only.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/socket.h>
+
+bool init_socket(void)
+{
+ return TRUE;
+}
+
+void endsocket(SOCKET so)
+{
+ shutdown(so, 2);
+ close(so);
+}
+
diff --git a/sdk/net/make.conf b/sdk/net/make.conf
new file mode 100644
index 0000000..36fbb70
--- /dev/null
+++ b/sdk/net/make.conf
@@ -0,0 +1,4 @@
+if test -f $CONFIG_HOST/lib/libsocket.a ; then
+ CONFIG_LIBS=$CONFIG_LIBS' -lsocket'
+fi
+
diff --git a/sdk/net/msgport.c b/sdk/net/msgport.c
new file mode 100644
index 0000000..39c1109
--- /dev/null
+++ b/sdk/net/msgport.c
@@ -0,0 +1,84 @@
+/*
+ * Portable network messaging routines.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/msgport.h>
+#include <std/time.h>
+
+static ushort sequence = 0;
+
+SOCKET create_msgport(char *mask, int port, int backlog)
+{
+ if(backlog)
+ return tcpsocket(mask, port, backlog);
+ else
+ return udpsocket(mask, port);
+}
+
+size_t send_msgport(SOCKET so, SOCKMSG *buf, size_t len, bool flag)
+{
+ size_t stat;
+
+ memset(buf, 0, sizeof(SOCKMSG));
+ if(flag)
+ ++sequence;
+ buf->header.len = htonl(len);
+ buf->sequence = sequence;
+
+ stat = send(so, buf, len + sizeof(SOCKMSG), 0);
+ if (stat > 0)
+ return stat - sizeof(SOCKMSG);
+ return stat;
+}
+
+size_t recv_msgport(SOCKET so, SOCKMSG *buf, size_t len, bool any)
+{
+ int alen;
+ size_t mlen;
+ size_t stat;
+ SOCKMSG saddr;
+
+ for(;;)
+ {
+ alen = recv(so, &saddr, sizeof(saddr), MSG_PEEK);
+ if(alen < 0)
+ return stat;
+
+ mlen = ntohl(saddr.header.len);
+ if (mlen > len)
+ mlen = len;
+
+ stat = recvfrom(so, buf, mlen + sizeof(saddr), 0, (struct sockaddr *)&saddr, &alen);
+ memcpy(buf, &saddr, alen);
+ if (stat > 0)
+ {
+ if(any && buf->sequence != sequence)
+ continue;
+
+ return stat - sizeof(SOCKMSG);
+ }
+ return stat;
+ }
+}
+
+size_t reply_msgport(SOCKET so, SOCKMSG *buf, SOCKMSG *org, size_t len)
+{
+ size_t stat;
+ SOCKMSG saddr;
+
+ if(!org)
+ org = buf;
+
+ memcpy(&saddr, org, sizeof(saddr));
+ buf->header.len = htonl(len);
+ stat = sendto(so, buf, len + sizeof(SOCKMSG), 0, (struct sockaddr *)&saddr, sizeof(saddr));
+ if (stat > 0)
+ return stat - sizeof(SOCKMSG);
+ return stat;
+}
+
+
+
diff --git a/sdk/net/msgport.h b/sdk/net/msgport.h
new file mode 100644
index 0000000..f7817e4
--- /dev/null
+++ b/sdk/net/msgport.h
@@ -0,0 +1,47 @@
+/*
+ * Portable network messaging routines.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#ifndef __NET_MSGPORT_H__
+#define __NET_MSGPORT_H__
+
+#ifndef __NET_SOCKET_H__
+#include <net/socket.h>
+#endif
+
+typedef struct
+{
+ union
+ {
+ struct sockaddr_in addr;
+ long len;
+ } header;
+ ushort sequence;
+ uchar body[ EMPTY ];
+} SOCKMSG;
+
+/* msgport control options */
+
+enum
+{
+ MSGPORT_TIMEOUT
+};
+
+#define attach_tcp(host, port) getsocket(host, port, SOCK_STREAM)
+#define attach_udp(host, port) getsocket(host, port, SOCK_DGRAM)
+
+#ifdef __NAMESPACE
+#define create_msgport __NAMESPACE(create_msgport)
+#define send_msgport __NAMESPACE(send_msgport)
+#define recv_msgport __NAMESPACE(recv_msgport)
+#define reply_msgport __NAMESPACE(reply_msgport)
+#endif
+
+SOCKET create_msgport(char *mask, int port, int backlog);
+size_t send_msgport(SOCKET so, SOCKMSG *buf, size_t len, bool inc);
+size_t recv_msgport(SOCKET so, SOCKMSG *buf, size_t maxlen, bool any);
+size_t reply_msgport(SOCKET so, SOCKMSG *buf, SOCKMSG *org, size_t len);
+#endif
diff --git a/sdk/net/netaddr.c b/sdk/net/netaddr.c
new file mode 100644
index 0000000..c156505
--- /dev/null
+++ b/sdk/net/netaddr.c
@@ -0,0 +1,28 @@
+/*
+ * Convert network mask or address string into a "BIND" filter mask.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/socket.h>
+#include <std/string.h>
+
+struct in_addr *getnetaddr(const char *mask)
+{
+ static struct in_addr addr;
+ struct in_addr *aptr;
+ struct hostent *hp;
+
+ if(!mask)
+ mask = "0.0.0.0";
+
+ hp = gethost(mask);
+ if(!hp)
+ return NULL;
+
+ while((aptr = (struct in_addr *)*(hp->h_addr_list)++) != NULL)
+ addr = *aptr;
+
+ return &addr;
+}
diff --git a/sdk/net/snmptrap.c b/sdk/net/snmptrap.c
new file mode 100644
index 0000000..17c9f7c
--- /dev/null
+++ b/sdk/net/snmptrap.c
@@ -0,0 +1,165 @@
+/*
+ * Generic snmp trap generation routine.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/trap.h>
+#include <std/string.h>
+
+int snmptrap(char *host, char *agent, char *community, int id, char *enterprise, char *describe, long ut)
+{
+ static uchar sysvar[] = {0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x04};
+
+ uchar pkt[256] = {0x30, 0x82, 0x00, 0x00, 0x02, 0x01, 0x00, 0x04};
+ char console[129];
+ int len;
+ int pos;
+ int l1 = 45;
+ int l2 = 39;
+ int l3 = 16;
+ int l2pos;
+ int l3pos;
+ int v1;
+ char *p;
+ int v2;
+ ulong hostip;
+ struct hostent *hp;
+ struct sockaddr_in target;
+ struct in_addr *aptr;
+ int i1, i2;
+ SOCKET so;
+
+ if(!describe)
+ describe = "";
+
+ if(!host)
+ host = "localhost";
+
+ if(!agent)
+ agent = "localhost";
+
+ hp = gethost(agent);
+ if(!hp)
+ return -1;
+
+ while( (aptr = (struct in_addr *)*(hp->h_addr_list)++) != NULL)
+ memcpy(&hostip, aptr, 4);
+
+ if(!community)
+ community = "PUBLIC";
+
+ if(!enterprise)
+ enterprise = "1.3.6.1.4.1.3.1.1";
+
+
+ so = socket(AF_INET, SOCK_DGRAM, 0);
+ if(so == INVALID_SOCKET)
+ return -1;
+
+ len = strlen(community);
+ l1 = l1 + len + 1;
+ pkt[8] = (uchar)len;
+ strcpy((char *)pkt + 9, community);
+ pos = 9 + len;
+ pkt[pos++] = 0xa4;
+ l2pos = pos++;
+ pkt[pos++] = 0x06;
+ len = 0;
+
+ v1 = atoi(enterprise);
+ enterprise = strchr(enterprise, '.');
+ v2 = atoi(++enterprise);
+ len = 1;
+ pkt[pos + len] = (uchar)(v1 * 40 + v2);
+
+ while(NULL !=(enterprise = strchr(enterprise, '.')))
+ pkt[pos + (++len)] = (uchar)atoi(++enterprise);
+
+ pkt[pos] = len;
+ pos += len + 1;
+ l1 += len;
+ l2 += len;
+
+ pkt[pos++] = 0x40;
+ pkt[pos++] = 0x04;
+ memcpy(pkt + pos, &hostip, 4);
+ pos += 4;
+
+ i2 = 0;
+ i1 = id;
+ if(id > 5 || id < 0)
+ {
+ i1 = 6;
+ i2 = abs(id);
+ }
+
+ pkt[pos++] = 0x02;
+ pkt[pos++] = 0x01;
+ pkt[pos++] = (uchar)i1;
+ pkt[pos++] = 0x02;
+ if(i2 > -1 && i2 < 128)
+ {
+ pkt[pos++] = 0x01;
+ pkt[pos++] = (uchar)i2;
+ }
+ else
+ {
+ pkt[pos++] = 0x02;
+ pkt[pos++] = (uchar)(i2 / 256);
+ pkt[pos++] = (uchar)(i2 % 256);
+ ++l1;
+ ++l2;
+ }
+ pkt[pos++] = 0x43;
+ pkt[pos++] = 0x03;
+ pkt[pos++] = (uchar)((ut / 0x10000) % 256);
+ pkt[pos++] = (uchar)((ut / 0x100) % 256);
+ pkt[pos++] = (uchar)(ut % 256);
+ pkt[pos++] = 0x30;
+ pkt[pos++] = 0x82;
+ pkt[pos++] = 0;
+ l3pos = pos++;
+
+ len = strlen(describe);
+ pkt[pos++] = 0x30;
+ pkt[pos++] = 0x82;
+ pkt[pos++] = 0x00;
+ pkt[pos++] = len + 12;
+ memcpy(pkt + pos, sysvar, 11);
+ pos += 11;
+ pkt[pos++] = (uchar)len;
+ strcpy((char *)pkt + pos, describe);
+
+ l1 += len;
+ l2 += len;
+ l3 += len;
+ pos += len;
+
+ pkt[2] = (uchar)(l1 / 256);
+ pkt[3] = (uchar)(l1 % 256);
+ pkt[l2pos] = (uchar)l2;
+ pkt[l3pos] = (uchar)l3;
+
+
+ strcpy(console, host);
+ host = strtok(console, ":\t; \t\n");
+ while(NULL != host)
+ {
+ hp = gethost(host);
+ if(!hp)
+ return -1;
+
+ while( (aptr = (struct in_addr *)*(hp->h_addr_list)++) != NULL)
+ target.sin_addr = *aptr;
+
+ target.sin_family = hp->h_addrtype;
+ target.sin_port = htons(SNMP_TRAP_PORT);
+ host = strtok(NULL, ":\t;");
+ sendto(so, pkt, pos, 0, (struct sockaddr *)&target, sizeof(struct sockaddr));
+ }
+ endsocket(so);
+ return 0;
+}
+
diff --git a/sdk/net/socket.h b/sdk/net/socket.h
new file mode 100644
index 0000000..b9bd190
--- /dev/null
+++ b/sdk/net/socket.h
@@ -0,0 +1,61 @@
+/*
+ * Portable include for access of socket services.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#ifndef __NET_SOCKET_H__
+#define __NET_SOCKET_H__
+
+#ifndef __STD_FILES_H__
+#include <std/files.h>
+#endif
+
+#ifndef __STD_TYPES_H__
+#include <std/types.h>
+#endif
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+typedef int SOCKET;
+#define INVALID_SOCKET -1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __NAMESPACE
+#define init_sockets __NAMESPACE(init_sockets)
+#define gethost __NAMESPACE(gethost)
+#define getservice __NAMESPACE(getservice)
+#define peername __NAMESPACE(peername)
+#define peeraddr __NAMESPACE(peeraddr)
+#define homename __NAMESPACE(homename)
+#define homeaddr __NAMESPACE(homeaddr)
+#define endsocket __NAMESPACE(endsocket)
+#define udpsocket __NAMESPACE(udpsocket)
+#define tcpsocket __NAMESPACE(tcpsocket)
+#define getsocket __NAMESPACE(getsocket)
+#endif
+
+bool init_sockets(void);
+struct hostent *gethost(const char *host);
+struct in_addr *getnetaddr(const char *addr);
+int getservice(const char *service);
+char *peername(SOCKET so);
+char *peeraddr(SOCKET so);
+char *homename(SOCKET so);
+char *homeaddr(SOCKET so);
+void endsocket(SOCKET so);
+SOCKET udpsocket(char *mask, int port);
+SOCKET tcpsocket(char *mask, int port, int backlog);
+SOCKET getsocket(const char *host, int port, int socktype);
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/sdk/net/sockname.c b/sdk/net/sockname.c
new file mode 100644
index 0000000..a8e9aad
--- /dev/null
+++ b/sdk/net/sockname.c
@@ -0,0 +1,62 @@
+/*
+ * Portable socket and peer name services.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/socket.h>
+
+char *homename(SOCKET so)
+{
+ struct hostent *hp;
+ struct sockaddr_in sin;
+ int len = sizeof(sin);
+
+ if(getsockname(so, (struct sockaddr *)&sin, &len) < 0)
+ return NULL;
+
+ hp = gethostbyaddr((char *)&sin.sin_addr, sizeof(struct in_addr), AF_INET);
+ if(hp)
+ return (char *)hp->h_name;
+ else
+ return inet_ntoa(sin.sin_addr);
+}
+
+char *homeaddr(SOCKET so)
+{
+ struct sockaddr_in sin;
+ int len = sizeof(sin);
+
+ if(getsockname(so, (struct sockaddr *)&sin, &len) < 0)
+ return NULL;
+
+ return inet_ntoa(sin.sin_addr);
+}
+
+char *peername(SOCKET so)
+{
+ struct hostent *hp;
+ struct sockaddr_in sin;
+ int len = sizeof(sin);
+
+ if(getpeername(so, (struct sockaddr *)&sin, &len) < 0)
+ return NULL;
+
+ hp = gethostbyaddr((char *)&sin.sin_addr, sizeof(struct in_addr), AF_INET);
+ if(hp)
+ return (char *)hp->h_name;
+ else
+ return inet_ntoa(sin.sin_addr);
+}
+
+char *peeraddr(SOCKET so)
+{
+ struct sockaddr_in sin;
+ int len = sizeof(sin);
+
+ if(getpeername(so, (struct sockaddr *)&sin, &len) < 0)
+ return NULL;
+
+ return inet_ntoa(sin.sin_addr);
+}
diff --git a/sdk/net/stream.h b/sdk/net/stream.h
new file mode 100644
index 0000000..268d881
--- /dev/null
+++ b/sdk/net/stream.h
@@ -0,0 +1,45 @@
+/*
+ * Stream oriented TCP session routines.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#ifndef __NET_STREAM_H__
+#define __NET_STREAM_H__
+
+#ifndef __NET_SOCKET_H__
+#include <net/socket.h>
+#endif
+
+#define tcppeer(fp) peername(fileno(fp))
+#define tcphome(fp) homename(fileno(fp))
+
+typedef FILE *STREAM;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __NAMESPACE
+#define opentcp __NAMESPACE(opentcp)
+#define accepttcp __NAMESPACE(accepttcp)
+#define gettcp __NAMESPACE(gettcp)
+#define puttcp __NAMESPACE(puttcp)
+#define closetcp __NAMESPACE(closetcp)
+#define buftcp __NAMESPACE(buftcp)
+#endif
+
+STREAM opentcp(const char *hostname, int port);
+STREAM accepttcp(SOCKET so);
+char *gettcp(char *buf, size_t count, STREAM fp);
+int puttcp(char *str, STREAM fp);
+void closetcp(STREAM fp);
+int buftcp(STREAM fp, int size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
diff --git a/sdk/net/tcpsocket.c b/sdk/net/tcpsocket.c
new file mode 100644
index 0000000..b5e61bf
--- /dev/null
+++ b/sdk/net/tcpsocket.c
@@ -0,0 +1,42 @@
+/*
+ * Create and bind a tcp socket for a server.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/socket.h>
+
+SOCKET tcpsocket(char *mask, int port, int backlog)
+{
+ SOCKET so;
+ struct sockaddr_in saddr;
+ struct in_addr *aptr;
+
+ so = socket(AF_INET, SOCK_STREAM, 0);
+ if(so == INVALID_SOCKET)
+ return so;
+
+ memset(&saddr, 0, sizeof(saddr));
+ saddr.sin_family = AF_INET;
+ saddr.sin_port = htons(port);
+ aptr = getnetaddr(mask);
+ if(!aptr)
+ {
+ endsocket(so);
+ return INVALID_SOCKET;
+ }
+ saddr.sin_addr = *aptr;
+
+ if(bind(so, (struct sockaddr *)&saddr, sizeof(saddr)))
+ {
+ endsocket(so);
+ return INVALID_SOCKET;
+ }
+ if(listen(so, backlog))
+ {
+ endsocket(so);
+ return INVALID_SOCKET;
+ }
+ return so;
+}
diff --git a/sdk/net/tcpstream.c b/sdk/net/tcpstream.c
new file mode 100644
index 0000000..b39645d
--- /dev/null
+++ b/sdk/net/tcpstream.c
@@ -0,0 +1,88 @@
+/*
+ * Portable TCP session stream functions.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/stream.h>
+#include <std/string.h>
+
+STREAM opentcp(const char *host, int port)
+{
+ SOCKET so = getsocket(host, port, SOCK_STREAM);
+ FILE *fp;
+
+ if(so == INVALID_SOCKET)
+ return NULL;
+
+ fp = fdopen(so, "r+");
+ if(!fp)
+ {
+ endsocket(so);
+ return NULL;
+ }
+ buftcp(fp, 0);
+ return fp;
+}
+
+char *gettcp(char *buf, size_t count, STREAM fp)
+{
+ memset(buf, 0, count);
+ if(!fgets(buf, count, fp))
+ return NULL;
+
+ return buf;
+}
+
+int puttcp(char *buf, STREAM fp)
+{
+ int err;
+ err = fputs(buf, fp);
+ fflush(fp);
+ return err;
+}
+
+void closetcp(STREAM fp)
+{
+ SOCKET so = fileno(fp);
+
+ fflush(fp);
+ shutdown(so, 2);
+ fclose(fp);
+}
+
+int buftcp(STREAM fp, int size)
+{
+ fflush(fp);
+
+ if(!size)
+ return setvbuf(fp, NULL, _IOLBF, 1024);
+ else
+ return setvbuf(fp, NULL, _IOFBF, size);
+}
+
+STREAM accepttcp(SOCKET so)
+{
+ struct sockaddr addr;
+ int len;
+ int si;
+ FILE *fp;
+
+ len = sizeof(addr);
+ si = accept(so, &addr, &len);
+ if(si < 0)
+ return NULL;
+
+ fp = fdopen(si, "r+");
+ if(!fp)
+ {
+ shutdown(si, 2);
+ close(si);
+ return NULL;
+ }
+
+ buftcp(fp, 0);
+ return fp;
+}
+
diff --git a/sdk/net/trap.h b/sdk/net/trap.h
new file mode 100644
index 0000000..b903d5d
--- /dev/null
+++ b/sdk/net/trap.h
@@ -0,0 +1,30 @@
+/*
+ * Define SNMP Traps.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#ifndef __NET_TRAP_H__
+
+#ifndef __NET_SOCKET_H__
+#include <net/socket.h>
+#endif
+
+#define SNMP_TRAP_PORT 162
+#define SNMP_TRAP_COLDSTART 0
+#define SNMP_TRAP_WARMSTART 1
+#define SNMP_TRAP_LINKDOWN 2
+#define SNMP_TRAP_LINKUP 3
+#define SNMP_TRAP_AUTHFAIL 4
+#define SNMP_TRAP_EGPNEIGHBORLOSS 5
+#define SNMP_TRAP_ENTERPRISESPECIFIC 6
+
+#ifdef __NAMESPACE
+#define snmptrap __NAMESPACE(snmptrap)
+#endif
+
+int snmptrap(char *target, char *host, char *community, int id, char *enterprise, char *describe, long uptime);
+
+
+#endif
diff --git a/sdk/net/udpsocket.c b/sdk/net/udpsocket.c
new file mode 100644
index 0000000..2127ac3
--- /dev/null
+++ b/sdk/net/udpsocket.c
@@ -0,0 +1,36 @@
+/*
+ * Create and bind a udp socket for a server.
+ * $Id$
+ * Copyright (c) 1997 by Tycho Softworks.
+ * For conditions of distribution and reuse see product license.
+ */
+
+#include <net/socket.h>
+
+SOCKET udpsocket(char *mask, int port)
+{
+ SOCKET so;
+ struct sockaddr_in saddr;
+ struct in_addr *aptr;
+
+ so = socket(AF_INET, SOCK_DGRAM, 0);
+ if(so == INVALID_SOCKET)
+ return so;
+
+ saddr.sin_family = AF_INET;
+ saddr.sin_port = htons(port);
+ aptr = getnetaddr(mask);
+ if(!aptr)
+ {
+ endsocket(so);
+ return INVALID_SOCKET;
+ }
+ saddr.sin_addr = *aptr;
+
+ if(bind(so, (struct sockaddr *)&saddr, sizeof(saddr)))
+ {
+ endsocket(so);
+ return INVALID_SOCKET;
+ }
+ return so;
+}