schemers@vela.acs.oakland.edu (Roland Schemers III) (10/26/90)
#! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of archive 2 (of 3)." # Contents: Transport/src/DECnetSocket.C Transport/src/InetAddress.C # Transport/src/InetAddress.h Transport/src/InetSocket.C # Transport/src/Transport.C Transport/src/UnixSocket.C # Transport/src/Util.C Transport/tests/makefile # Wrapped by schemers@vela.acs.oakland.edu on Thu Oct 25 15:44:47 1990 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f 'Transport/src/DECnetSocket.C' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Transport/src/DECnetSocket.C'\" else echo shar: Extracting \"'Transport/src/DECnetSocket.C'\" \(2552 characters\) sed "s/^X//" >'Transport/src/DECnetSocket.C' <<'END_OF_FILE' X#ifdef USE_DECNET X X#include "Transport.h" X#include "Util.h" X X#include <stdio.h> X#include <osfcn.h> X#include <libc.h> X#include <string.h> X Xextern "C" { X int dnet_conn( X const char *node, X const char *obj, X int type, X u_char *opt_out, X int opt_outl, X u_char *opt_in, X int *opt_inl X ); X struct dn_naddr *dnet_addr(const char *cp); X struct nodeent *getnodebyname(const char *node); X} X Xint DECnetSocket::open(int obj) X{ X object=obj; X if (inuse()) close(); X if (!socket()) goto error; X X bzero((char *) &server_addr, sizeof(struct sockaddr_dn)); X server_addr.sdn_family = address_family(); X server_addr.sdn_objnum = object; X X if (!bind((struct sockaddr*)&server_addr,sizeof(server_addr))) X goto error; X X if (!listen(SOMAXCONN)) goto error; X X status=0; X sex=Server; X return 1; Xerror: X if (!return_on_error) X Util::crash_and_burn("DECnetSocket","open",sys_errlist[status]); X return 0; X} X Xint DECnetSocket::open(const char *node,int obj) X{ X object=obj; X if (inuse()) close(); X if (!socket()) goto error; // error creating socket X X bzero((char *) &client_addr , sizeof(client_addr)); X client_addr.sdn_family = address_family(); X client_addr.sdn_objnum = object; X if ( (node_addr = dnet_addr(node)) == NULL) { X if ((nodep=getnodebyname(node))==NULL) { X status=-1; X if (return_on_error) return 0; X Util::crash_and_burn("DECnetSocket","open","host not found"); X } else { X bcopy(nodep->n_addr,client_addr.sdn_nodeaddr, X nodep->n_length); X client_addr.sdn_nodeaddrl = nodep->n_length; X } X } else client_addr.sdn_add = *node_addr; X X if (!connect((struct sockaddr*)&client_addr,sizeof(client_addr))) X goto error; X X status=0; X sex=Client; X return 1; Xerror: X if (!return_on_error) X Util::crash_and_burn("DECnetSocket","open",sys_errlist[status]); X return 0; X} X Xint DECnetSocket::open(const char *node,const char *obj) X{ X td=dnet_conn(node,obj,type,NULL,NULL,NULL,NULL); X if (td<0) status=errno; X else status=0; X sex = Client; X return (status==0); X} X Xint DECnetSocket::accept(DECnetSocket &newsocket) X{ X if (!ok() || (socket_sex()!=Server)) { X newsocket.td = -1; X newsocket.status = -1; X return 0; X } X X if (newsocket.inuse()) newsocket.close(); X X newsocket = *this; X X clen=sizeof(client_addr); X newsocket.td = Socket::accept((struct sockaddr *) &client_addr,clen); X if (newsocket.td < 0 ) { X newsocket.status=errno; X if (return_on_error) return 0; X Util::crash_and_burn("DECnetSocket","accept",sys_errlist[errno]); X } else { X newsocket.status=0; X return 1; X } X} X X X#endif END_OF_FILE if test 2552 -ne `wc -c <'Transport/src/DECnetSocket.C'`; then echo shar: \"'Transport/src/DECnetSocket.C'\" unpacked with wrong size! fi # end of 'Transport/src/DECnetSocket.C' fi if test -f 'Transport/src/InetAddress.C' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Transport/src/InetAddress.C'\" else echo shar: Extracting \"'Transport/src/InetAddress.C'\" \(2519 characters\) sed "s/^X//" >'Transport/src/InetAddress.C' <<'END_OF_FILE' X#include <stdio.h> X#include <osfcn.h> X#include <libc.h> X#include <errno.h> X#include <string.h> X#include <netdb.h> X#include <iostream.h> X X#include "Transport.h" X Xextern "C" { X unsigned long htonl(unsigned long); X unsigned short htons(unsigned short); X unsigned short ntohs(unsigned short); X struct hostent *gethostbyname(const char *host); X} X Xchar *InetAddress::Any="0.0.0.0"; Xchar *InetAddress::Broadcast="255.255.255.255"; X Xostream &operator<<(ostream &os, const InetAddress &ia) X{ X if (!ia.ok()) os <<"(UnKnownHost)"; X else os << "(" << ia.hostname() << "," << ia.port() << ")"; X return os; X} X XInetAddress::InetAddress() X{ X stat=NoHostGiven; // no address looked up X addrlen=sizeof(address); X} X XInetAddress::InetAddress(int port) X{ X lookup(Any,port); X} X X XInetAddress::InetAddress(const char *hostname, int port) X{ X lookup(hostname,port); X} X XInetAddress::InetAddress(const char *hostname, X const char *service, X const char *proto) X{ X lookup(hostname,service,proto); X} X X XInetAddress::InetAddress(const char *service, X const char *proto) X{ X lookup(Any,service,proto); X} X Xint InetAddress::lookup(const char *hostname, int port) X{ X stat=HostFound; X addrlen=sizeof(address); X bzero((char *) &address , addrlen); X address.sin_family = Socket::AfInet; X address.sin_port = htons(port); X if (hostname==Any) address.sin_addr.s_addr = htonl(INADDR_ANY); X else if (hostname==Broadcast) address.sin_addr.s_addr = htonl(INADDR_BROADCAST); X else if ((address.sin_addr.s_addr =inet_addr(hostname))==-1) { X struct hostent *hp=gethostbyname(hostname); X if (hp==NULL) stat=UnknownHost; X else bcopy(hp->h_addr,(char*)&address.sin_addr,hp->h_length); X } X any_flag = X (address.sin_addr.s_addr==htonl(INADDR_ANY)) ? 1 : 0; X broadcast_flag = X (address.sin_addr.s_addr==htonl(INADDR_BROADCAST)) ? 1 : 0; X return stat==HostFound; X} X Xint InetAddress::lookup(const char *hostname, X const char *service, X const char *proto) X{ X struct servent *se=getservbyname(service,proto); X if (se==NULL) { X stat=UnKnownService; X return 0; X } X X return lookup(hostname,ntohs(se->s_port)); X} X Xconst char *InetAddress::hostname() const X{ X struct hostent *hp; X hp=gethostbyaddr((char *)&address.sin_addr,addrlen,Socket::AfInet); X if (hp==NULL) return inet_ntoa( address.sin_addr); X else return hp->h_name; X} X Xconst char *InetAddress::numeric_hostname() const X{ X return inet_ntoa( address.sin_addr); X} X Xint InetAddress::port() const X{ X return ntohs(address.sin_port); X} END_OF_FILE if test 2519 -ne `wc -c <'Transport/src/InetAddress.C'`; then echo shar: \"'Transport/src/InetAddress.C'\" unpacked with wrong size! fi # end of 'Transport/src/InetAddress.C' fi if test -f 'Transport/src/InetAddress.h' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Transport/src/InetAddress.h'\" else echo shar: Extracting \"'Transport/src/InetAddress.h'\" \(1445 characters\) sed "s/^X//" >'Transport/src/InetAddress.h' <<'END_OF_FILE' X#ifndef INETADDRESS_CLASS_H X#define INETADDRESS_CLASS_H X X#include <sys/socket.h> X#include <sys/types.h> X#include <netinet/in.h> X#include <arpa/inet.h> X#include <iostream.h> X Xclass InetAddress { X friend ostream &operator<<(ostream &os, const InetAddress &ia); Xpublic: X static char *Any; // any address X static char *Broadcast; // broadcast address X enum Status {NoHostGiven,UnknownHost,UnKnownService,HostFound}; X InetAddress(); X InetAddress(const char *hostname,int port); X InetAddress(int port); X InetAddress(const char *hostname,const char *service,const char *proto); X InetAddress(const char *service,const char *proto); X lookup(const char *hostname,int port); X lookup(int port) { X return lookup(Any,port); X } X lookup(const char *hostname,const char *service,const char *proto); X lookup(const char *service,const char *proto) { X return lookup(Any,service,proto); X } X int ok() const { return stat==HostFound; } X Status status() const { return stat; } X const char *hostname() const; X const char *numeric_hostname() const; X int size() const { return sizeof(address); } X int port() const; X int any() const { return any_flag; } X typedef struct sockaddr *SOCKADDR_PTR; X typedef const struct sockaddr *CONST_SOCKADDR_PTR; X operator SOCKADDR_PTR() { return (SOCKADDR_PTR) &address; } X operator CONST_SOCKADDR_PTR() const { return (SOCKADDR_PTR) &address; } X X struct sockaddr_in address; X int addrlen; X Status stat; X int any_flag; X}; X#endif END_OF_FILE if test 1445 -ne `wc -c <'Transport/src/InetAddress.h'`; then echo shar: \"'Transport/src/InetAddress.h'\" unpacked with wrong size! fi # end of 'Transport/src/InetAddress.h' fi if test -f 'Transport/src/InetSocket.C' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Transport/src/InetSocket.C'\" else echo shar: Extracting \"'Transport/src/InetSocket.C'\" \(1889 characters\) sed "s/^X//" >'Transport/src/InetSocket.C' <<'END_OF_FILE' X X#include <stdio.h> X#include <osfcn.h> X#include <libc.h> X#include <errno.h> X#include <string.h> X#include <netdb.h> X#include <iostream.h> X X#include "Transport.h" X#include "Util.h" X Xextern "C" { X unsigned long htonl(unsigned long); X unsigned short htons(unsigned short); X struct hostent *gethostbyname(const char *host); X} X Xint InetSocket::connect(const InetAddress &ia) X{ X return Socket::connect(ia,ia.size()); X} X Xint InetSocket::bind(const InetAddress &ia) X{ X return Socket::bind(ia,ia.size()); X} X Xint InetSocket::getpeername(InetAddress &ia) X{ X ia.addrlen=ia.size(); X int stat=Socket::getpeername(ia,ia.addrlen); X if (stat) ia.stat=InetAddress::HostFound; X else ia.stat=InetAddress::UnknownHost; X return stat; X} X Xint InetSocket::getsockname(InetAddress &ia) X{ X ia.addrlen=ia.size(); X int stat=Socket::getsockname(ia,ia.addrlen); X if (stat) ia.stat=InetAddress::HostFound; X else ia.stat=InetAddress::UnknownHost; X return stat; X} X Xint InetSocket::accept(InetSocket &newsocket) X { X if (!ok() || (socket_sex()!=Server)) { // socket is bad! X newsocket.td = -1; X newsocket.status = -1; X return 0; X } X X if (newsocket.inuse()) newsocket.close(); X X newsocket = *this; // copy current socket X InetAddress remote; X remote.addrlen=remote.size(); X newsocket.td = Socket::accept(remote,remote.addrlen); X X if (newsocket.td < 0 ) { X newsocket.status=errno; X if (return_on_error) return 0; X Util::crash_and_burn("InetSocket","accept",sys_errlist[errno]); X } else { X newsocket.status=0; X return 1; X } X X} X Xint InetSocket::recvfrom(char *buffer,int maxbuf, InetAddress &ia) X{ X ia.addrlen=ia.size(); X int stat=Socket::recvfrom(buffer,maxbuf,ia,ia.addrlen); X if (stat) ia.stat=InetAddress::HostFound; X else ia.stat=InetAddress::NoHostGiven; X return stat; X} X Xint InetSocket::sendto(char *buffer,int len,const InetAddress &ia) X{ X return Socket::sendto(buffer,len,ia,ia.addrlen); X} X X END_OF_FILE if test 1889 -ne `wc -c <'Transport/src/InetSocket.C'`; then echo shar: \"'Transport/src/InetSocket.C'\" unpacked with wrong size! fi # end of 'Transport/src/InetSocket.C' fi if test -f 'Transport/src/Transport.C' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Transport/src/Transport.C'\" else echo shar: Extracting \"'Transport/src/Transport.C'\" \(1756 characters\) sed "s/^X//" >'Transport/src/Transport.C' <<'END_OF_FILE' X#include <libc.h> X#include <osfcn.h> X#include <sys/uio.h> X#include <iostream.h> X#include <errno.h> X X#include "Transport.h" X#include "Util.h" X XTransport::Transport() X{ X td = -1; X status = -1; X return_on_error=0; X} X Xint Transport::close() X{ X if (td == -1) { status=0; return 1; } X int s = ::close(td); X if (s==-1) { X status=errno; X if (return_on_error) return 0; X Util::crash_and_burn("Transport","close",sys_errlist[status]); X } X else { X status=0; X td=-1; X return 1; X } X} X Xint Transport::ok() X{ X if (td==-1) return 0; X else return status==0; X} X Xint Transport::inuse() X{ X return (td!=-1); X} X Xint Transport::read(char *buffer, int mlen) X{ X if (td == -1) return -1; X int nbr = ::read(td,buffer,mlen); X if (nbr==-1) { X status=errno; X if (return_on_error) return nbr; X Util::crash_and_burn("Transport","read",sys_errlist[status]); X } X else status=0; X return nbr; X} X Xint Transport::readv(const struct iovec *iov, int iovlen) X{ X if (td == -1) return -1; X int nbr = ::readv(td,iov,iovlen); X if (nbr==-1) { X status=errno; X if (return_on_error) return nbr; X Util::crash_and_burn("Transport","readv",sys_errlist[status]); X } X else status=0; X return nbr; X} X Xint Transport::write(const char *buffer, int len) X{ X if (td == -1) return -1; X int nbw = ::write(td,buffer,len); X if (nbw==-1) { X status=errno; X if (return_on_error) return nbw; X Util::crash_and_burn("Transport","write",sys_errlist[status]); X } X else status=0; X return nbw; X} X X X Xint Transport::writev(const struct iovec *iov, int iovlen) X{ X if (td == -1) return -1; X int nbw = ::writev(td,iov,iovlen); X if (nbw==-1) { X status=errno; X if (return_on_error) return nbw; X Util::crash_and_burn("Transport","writev",sys_errlist[status]); X } X else status=0; X return nbw; X} X X END_OF_FILE if test 1756 -ne `wc -c <'Transport/src/Transport.C'`; then echo shar: \"'Transport/src/Transport.C'\" unpacked with wrong size! fi # end of 'Transport/src/Transport.C' fi if test -f 'Transport/src/UnixSocket.C' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Transport/src/UnixSocket.C'\" else echo shar: Extracting \"'Transport/src/UnixSocket.C'\" \(1521 characters\) sed "s/^X//" >'Transport/src/UnixSocket.C' <<'END_OF_FILE' X#include "Transport.h" X#include "Util.h" X X#include <stdio.h> X#include <osfcn.h> X#include <libc.h> X#include <string.h> X//#include <iostream.h> X X Xint UnixSocket::open(const UnixAddress &ua, X Socket::SocketSex sx) X{ X X sex=sx; X if (inuse()) close(); X if (!socket()) goto error; X X switch (socket_sex()) { X case Client: X remote = ua; X if (!connect(remote,remote.addrlen)) goto error; X break; X case Server: X local = ua; X if (!bind(local,local.addrlen)) goto error; X unlink_socket=1; X if (!listen(SOMAXCONN)) goto error; X break; X } X status=0; X return 1; Xerror: X if (return_on_error) return 1; X Util::crash_and_burn("UnixSocket","open",sys_errlist[errno]); X} X XUnixSocket::~UnixSocket() X{ X if (inuse()) close(); X} X Xint UnixSocket::close() X{ X X if ( unlink_socket) unlink(local.pathname()); X X int s = ::close(td); X if (s==-1) { X status=errno; X if (return_on_error) return 0; X Util::crash_and_burn("UnixSocket","close",sys_errlist[status]); X } X X status=0; X td = -1; X return 1; X} X Xint UnixSocket::accept(UnixSocket &newsocket) X{ X if (!ok() || (socket_sex()!=Server)) { X newsocket.td = -1; X newsocket.status = -1; X return 0; X } X X if (newsocket.inuse()) newsocket.close(); X X newsocket = *this; X remote.addrlen=remote.size(); X newsocket.td = Socket::accept(remote,remote.addrlen); X X if (newsocket.td < 0 ) { X newsocket.status=errno; X if (return_on_error) return 0; X Util::crash_and_burn("UnixSocket","accept",sys_errlist[errno]); X } else { X newsocket.status=0; X return 1; X } X} END_OF_FILE if test 1521 -ne `wc -c <'Transport/src/UnixSocket.C'`; then echo shar: \"'Transport/src/UnixSocket.C'\" unpacked with wrong size! fi # end of 'Transport/src/UnixSocket.C' fi if test -f 'Transport/src/Util.C' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Transport/src/Util.C'\" else echo shar: Extracting \"'Transport/src/Util.C'\" \(1507 characters\) sed "s/^X//" >'Transport/src/Util.C' <<'END_OF_FILE' X#include <sys/signal.h> X#include <iostream.h> X#include <libc.h> X#include <osfcn.h> X#include <sys/wait.h> X#include <sys/param.h> X#include <errno.h> X#include <sys/file.h> X#include <sys/ioctl.h> X X#include "Util.h" X Xvoid Util::crash_and_burn(char *module, char *func, char *mess) X{ X cerr << module << "(" << func << ") : " << mess << endl; X exit(1); X} X Xstatic void sig_child(int sig) X{ Xint pid; Xunion wait stat; X Xwhile ((pid = wait3(&stat, WNOHANG, (struct rusage *) 0)) >0); X X} X Xvoid Util::daemon_start(int ignore_sigcld) X{ Xint childpid,fd; X X#ifdef SIGTTOU X signal(SIGTTOU,SIG_IGN); X#endif X#ifdef SIGTTIN X signal(SIGTTIN,SIG_IGN); X#endif X#ifdef SIGTSTP X signal(SIGTSTP,SIG_IGN); X#endif X X /* X * If we were not started in the background, fork and let the X * parent exit. This also guarantees the first child is not a X * process group leader. X */ X X if ( (childpid=fork()) <0) { X perror("Can't fork first child"); X exit(1); X } else if (childpid>0) exit(0); // parent X X /* X * First child process X * X * Disassociate from controlling terminal and process group. X * Ensure the process can't reacquire a controlling terminal X */ X X if (setpgrp(0, getpid()) == -1) { X perror("can't change process group"); X exit(1); X } X X if (fd=open("/dev/tty",O_RDWR) >=0 ) { X ioctl(fd,TIOCNOTTY, (char *) NULL); X close(fd); X } X X // close any open files X X for (fd=0; fd < NOFILE; fd++) close(fd); X X errno=0; X X chdir("/"); // be nice to mounted file systems X X umask(0); X X if (ignore_sigcld) signal(SIGCLD,sig_child); X X} END_OF_FILE if test 1507 -ne `wc -c <'Transport/src/Util.C'`; then echo shar: \"'Transport/src/Util.C'\" unpacked with wrong size! fi # end of 'Transport/src/Util.C' fi if test -f 'Transport/tests/makefile' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Transport/tests/makefile'\" else echo shar: Extracting \"'Transport/tests/makefile'\" \(1445 characters\) sed "s/^X//" >'Transport/tests/makefile' <<'END_OF_FILE' X# X# X# Makefile X# X XCC=CC XYFLAGS = -d XCFLAGS = -I../../lib X X.SUFFIXES: .o .c .y .l .s .C X# XPROG=qtest X# X# C++ rules X# X.C.o: X $(CC) $(CFLAGS) -c $*.C X X.C.a: X $(CC) -c $(CFLAGS) $< X ar rv $@ $*.o X rm -f $*.o X X XTLIB = ../../lib/$(MACHINE)/libTransport.a X Xitest: $(MACHINE)/iserver $(MACHINE)/iclient X X$(MACHINE)/iserver : iserver.o $(TLIB) X CC -o $(MACHINE)/iserver iserver.o $(TLIB) -ldnet X X$(MACHINE)/iclient : iclient.o $(TLIB) X CC -o $(MACHINE)/iclient iclient.o $(TLIB) -ldnet X Xdtest: $(MACHINE)/dserver $(MACHINE)/dclient X X$(MACHINE)/dserver : dserver.o $(TLIB) X CC -o $(MACHINE)/dserver dserver.o $(TLIB) -ldnet X X$(MACHINE)/dclient : dclient.o $(TLIB) X CC -o $(MACHINE)/dclient dclient.o $(TLIB) -ldnet X Xutest: $(MACHINE)/userver $(MACHINE)/uclient X X$(MACHINE)/userver : userver.o $(TLIB) X CC -o $(MACHINE)/userver userver.o $(TLIB) -ldnet X X$(MACHINE)/uclient : uclient.o $(TLIB) X CC -o $(MACHINE)/uclient uclient.o $(TLIB) -ldnet X Xstest: $(MACHINE)/sserver $(MACHINE)/sclient X X$(MACHINE)/sserver : sserver.o $(TLIB) X CC -o $(MACHINE)/sserver sserver.o $(TLIB) -ldnet X X$(MACHINE)/sclient : sclient.o $(TLIB) X CC -o $(MACHINE)/sclient sclient.o $(TLIB) -ldnet X X$(MACHINE)/qtest : qtest.o $(TLIB) X CC -o $(MACHINE)/qtest qtest.o $(TLIB) -ldnet X X$(PROG): $(MACHINE)/$(PROG) X X$(MACHINE)/$(PROG) : $(PROG).o $(TLIB) X CC -o $(MACHINE)/$(PROG) $(PROG).o $(TLIB) -ldnet X Xbackup: X (cd ..; tar cf - src | compress > backup.tar) Xclean: X rm -f *.o END_OF_FILE if test 1445 -ne `wc -c <'Transport/tests/makefile'`; then echo shar: \"'Transport/tests/makefile'\" unpacked with wrong size! fi # end of 'Transport/tests/makefile' fi echo shar: End of archive 2 \(of 3\). cp /dev/null ark2isdone MISSING="" for I in 1 2 3 ; do if test ! -f ark${I}isdone ; then MISSING="${MISSING} ${I}" fi done if test "${MISSING}" = "" ; then echo You have unpacked all 3 archives. rm -f ark[1-9]isdone else echo You still need to unpack the following archives: echo " " ${MISSING} fi ## End of shell archive. exit 0 -- Roland J. Schemers III Systems Programmer schemers@vela.acs.oakland.edu (Ultrix) Oakland University schemers@argo.acs.oakland.edu (VMS) Rochester, MI 48309-4401 "As long as the systems are up, I get to sleep in!" (313)-370-4323