BSD 4_4 development
authorCSRG <csrg@ucbvax.Berkeley.EDU>
Mon, 5 Jan 1987 02:11:39 +0000 (18:11 -0800)
committerCSRG <csrg@ucbvax.Berkeley.EDU>
Mon, 5 Jan 1987 02:11:39 +0000 (18:11 -0800)
Work on file usr/src/contrib/xns/xnslib/courierconnection.h
Work on file usr/src/contrib/xns/xnslib/courierdb.h
Work on file usr/src/contrib/xns/xnslib/except.h
Work on file usr/src/contrib/xns/xnslib/except.c
Work on file usr/src/contrib/xns/xnslib/getcourierent.c
Work on file usr/src/contrib/xns/xnslib/getcservice.c
Work on file usr/src/contrib/xns/xnslib/options.c
Work on file usr/src/contrib/xns/xnslib/server.c

Synthesized-from: CSRG/cd3/4.4

usr/src/contrib/xns/xnslib/courierconnection.h [new file with mode: 0644]
usr/src/contrib/xns/xnslib/courierdb.h [new file with mode: 0644]
usr/src/contrib/xns/xnslib/except.c [new file with mode: 0644]
usr/src/contrib/xns/xnslib/except.h [new file with mode: 0644]
usr/src/contrib/xns/xnslib/getcourierent.c [new file with mode: 0644]
usr/src/contrib/xns/xnslib/getcservice.c [new file with mode: 0644]
usr/src/contrib/xns/xnslib/options.c [new file with mode: 0644]
usr/src/contrib/xns/xnslib/server.c [new file with mode: 0644]

diff --git a/usr/src/contrib/xns/xnslib/courierconnection.h b/usr/src/contrib/xns/xnslib/courierconnection.h
new file mode 100644 (file)
index 0000000..3db3c54
--- /dev/null
@@ -0,0 +1,12 @@
+/*
+ * CourierConnection as seen by everyone else except the runtime library,
+ * effectively hiding the internal structure
+ */
+
+#ifndef CourierConnectionHeader
+#define CourierConnectionHeader
+
+typedef int CourierConnection;
+extern CourierConnection *CourierOpen();
+
+#endif CourierConnectionHeader
diff --git a/usr/src/contrib/xns/xnslib/courierdb.h b/usr/src/contrib/xns/xnslib/courierdb.h
new file mode 100644 (file)
index 0000000..47a98e1
--- /dev/null
@@ -0,0 +1,16 @@
+/*     $Header: courierdb.h,v 1.1 87/01/05 12:09:41 ed Exp $   */
+
+/* description for the database of courier services, normally
+ * in the file /etc/Courierservices
+ */
+
+struct courierdbent {
+       char *cr_programname;   /* the name of the Courier program */
+       unsigned long cr_programnumber; /* official number of program */
+       unsigned short cr_version;      /* version number of this server */
+       char *cr_description;   /* file containing the Courier description */
+       char *cr_serverbin;     /* file containing the server binary */
+};
+
+extern struct courierdbent *getcourierdbent();
+extern struct courierdbent *getcourierservice();
diff --git a/usr/src/contrib/xns/xnslib/except.c b/usr/src/contrib/xns/xnslib/except.c
new file mode 100644 (file)
index 0000000..50a8364
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * except.c
+ *
+ * Support routines for C exceptions
+ *
+ (c) Jeffrey Mogul     Stanford        18 February 1983
+ */
+
+#include <stdio.h>
+#include "except.h"
+
+extern int errno;
+extern int sys_nerr;
+extern char *sys_errlist[];
+
+_Except_Buf *_Except_Header = 0;
+int ExceptMode = 0;
+
+raise(code, msg)
+int code;
+char *msg;
+{
+       register _Except_Buf *EBp = _Except_Header;
+       
+       if (EBp == 0) { /* uncaught exception */
+           if (ExceptMode&EX_MODE_REPORT) {
+               fprintf(stderr,"Uncaught exception: %d, %s\n",
+                       code, msg);
+           }
+           if (ExceptMode&EX_MODE_ABORT)
+               abort();
+           else
+               exit(code);
+       }
+
+       EBp->Code = code;
+       EBp->Message = msg;
+       
+       _Except_Header = EBp->Prev;
+       
+       longjmp(EBp->Environ, 1);
+}
+
+raise_sys()
+{
+       register int errnum = errno;
+
+       if ((errnum < 1) || (errnum >= sys_nerr)) {
+           raise(-1, "Unknown Unix error code");
+       }
+       else {
+           raise(errnum, sys_errlist[errnum]);
+       }
+}
diff --git a/usr/src/contrib/xns/xnslib/except.h b/usr/src/contrib/xns/xnslib/except.h
new file mode 100644 (file)
index 0000000..ad6bcd7
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * except.h
+ *
+ * Definitions and macros for C exception mechanism
+ *
+ (c) Jeffrey Mogul     Stanford        18 February 1983
+ */
+
+#include <setjmp.h>
+
+typedef struct _Except_buf_x {
+       struct _Except_buf_x *Prev;     /* exception chain back-pointer */
+       jmp_buf Environ;                /* saved environment */
+       char *Message;                  /* Human-readable cause */
+       int Code;                       /* Exception code */
+} _Except_Buf;
+
+extern _Except_Buf *_Except_Header;    /* global exception chain header */
+
+/*
+ * "syntax":
+ *     DURING statement HANDLER statement END_HANDLER
+ */
+
+#define        _E_RESTORE      _Except_Header = Exception.Prev
+
+#define        DURING {_Except_Buf Exception;\
+                Exception.Prev=_Except_Header;\
+                _Except_Header= &Exception;\
+                if (!setjmp(Exception.Environ)) {
+
+#define        HANDLER _E_RESTORE;} else
+
+#define        END_HANDLER }
+
+#define        E_RETURN(x) {_E_RESTORE; return(x);}
+
+#define        E_RETURN_VOID {_E_RESTORE; return;}
+
+#define        RERAISE raise(Exception.Code, Exception.Message)
+
+/*
+ * Exception modes (combined with ||):
+ */
+#define        EX_MODE_REPORT  1       /* report uncaught errors on stderr */
+#define        EX_MODE_ABORT   2       /* abort if uncaught error */
+
+extern int ExceptMode;
diff --git a/usr/src/contrib/xns/xnslib/getcourierent.c b/usr/src/contrib/xns/xnslib/getcourierent.c
new file mode 100644 (file)
index 0000000..84727ac
--- /dev/null
@@ -0,0 +1,108 @@
+/*     $Header: getcourierent.c,v 2.0 85/11/21 07:22:07 jqj Exp $      */
+
+#include <stdio.h>
+#include "courierdb.h"
+#include <ctype.h>
+
+/*
+ *
+ */
+#ifdef CSERVICES
+static char *COURIERDB = CSERVICES;
+#else
+static char *COURIERDB = "/usr/new/lib/xnscourier/Courierservices";
+#endif
+static FILE *courierdbf = NULL;
+static char line[BUFSIZ+1];
+static struct courierdbent service;
+int _courier_stayopen;
+static char *skipspace(), *skipitem();
+
+setcourierdbent(f)
+       int f;
+{
+       if (courierdbf != NULL)
+               rewind(courierdbf);
+       _courier_stayopen |= f;
+}
+
+endcourierdbent()
+{
+       if (courierdbf != NULL) {
+               fclose(courierdbf);
+               courierdbf = NULL;
+       }
+       _courier_stayopen = 0;
+}
+
+struct courierdbent *
+getcourierdbent()
+{
+       char *p;
+       register char *cp, c;
+
+       if (courierdbf == NULL 
+           && (courierdbf = fopen(COURIERDB, "r" )) == NULL)
+               return (NULL);
+
+       do {
+               if ((p = fgets(line, BUFSIZ, courierdbf)) == NULL)
+                       return (NULL);
+               p = skipspace(p);
+               cp = p;         /* end of whitespace */
+               while ((c = *cp) != '\0' && c != '\n' && c != '#')
+                       cp++;
+               *cp = '\0';     /* end of data */
+       } while (*p == '\0');
+
+       service.cr_programname = p;             /* a string */
+       cp = skipitem(p);
+       if (*cp != '\0') {
+               *cp = '\0';
+               cp = skipspace(++cp);
+       }
+       service.cr_programnumber = (unsigned long) atol(cp);    /* a long */
+       cp = skipitem(cp);  cp = skipspace(cp);
+       service.cr_version = (unsigned short) atoi(cp);         /* an int */
+       cp = skipitem(cp);  cp = skipspace(cp);
+       service.cr_description = (*cp) ? cp : (char*) 0;
+       cp = skipitem(cp);
+       if (*cp != '\0') {
+               *cp = '\0';
+               cp = skipspace(++cp);
+       }
+       service.cr_serverbin = (*cp) ? cp : (char*) 0;
+       cp = skipitem(cp);
+       if (*cp != '\0') {
+               *cp = '\0';
+               /* etc. for more fields */
+       }
+       return (&service);
+}
+
+static char * skipspace(p)
+/* move the pointer past leading whitespace, returning the updated ptr */
+       register char *p;
+{
+       register char c;
+       while ((c = *p) == ' ' || c == '\t')
+               p++;
+       return(p);
+}
+
+static char* skipitem(p)
+/* move the pointer, p, past non-whitespace */
+       register char *p;
+{
+       register char c;
+       while ((c = *p) && c != ' ' && c != '\t')
+               p++;
+       return(p);
+}
+
+setcourierdbfile(file)
+       char *file;
+{
+       COURIERDB = file;
+}
+
diff --git a/usr/src/contrib/xns/xnslib/getcservice.c b/usr/src/contrib/xns/xnslib/getcservice.c
new file mode 100644 (file)
index 0000000..0f25232
--- /dev/null
@@ -0,0 +1,19 @@
+/*     $Header: getcservice.c,v 2.0 85/11/21 07:22:09 jqj Exp $        */
+
+#include "courierdb.h"
+
+struct courierdbent *
+getcourierservice(prognum,vernum)
+       register long unsigned prognum;
+       register short unsigned vernum;
+{
+       register struct courierdbent *p;
+
+       setcourierdbent(0);
+       while (p = getcourierdbent()) {
+               if (p->cr_programnumber == prognum && p->cr_version == vernum)
+                       break;
+       }
+       endcourierdbent();
+       return (p);
+}
diff --git a/usr/src/contrib/xns/xnslib/options.c b/usr/src/contrib/xns/xnslib/options.c
new file mode 100644 (file)
index 0000000..d42f21d
--- /dev/null
@@ -0,0 +1,24 @@
+
+/*
+ *temporary file implementing a setsockopt call
+ * until I find out how Maryland intends to do it.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <sys/socket.h>
+
+SetSPPoptions(s,stream,eom,attn)
+       int s;          /* SPP socket */
+       u_char stream;  /* datastream type */
+       char eom;       /* Boolean EOM */
+                               /* can't set ATTN -- use MSG_OOB instead */
+{
+       /*
+       setsockopt(s, SOL_PROTO, SPPOPT_DATASTREAMTYPE, &stream,
+                  sizeof(stream));
+       setsockopt(s, SOL_PROTO, SPPOPT_EOMBIT, &eom, sizeof(eom));
+       */
+       fprintf(stderr, "SetSPPoptions called , now obsoleted\n");
+       abort();
+}
diff --git a/usr/src/contrib/xns/xnslib/server.c b/usr/src/contrib/xns/xnslib/server.c
new file mode 100644 (file)
index 0000000..698a936
--- /dev/null
@@ -0,0 +1,202 @@
+/*
+ * This file implements server functions for the XNS courier library
+ */
+
+/*
+ $Log: server.c,v $
+ * Revision 2.2  86/12/10  14:21:10  ed
+ * Reset _serverConnection->abortseen in ReceiveCallMessage
+ * 
+ * Revision 2.1  86/11/11  09:33:42  jqj
+ * In ReceiveCallMessage, set state to inprogress so server BDT will work
+ * right.
+ * 
+ * Revision 2.0  85/11/21  07:22:19  jqj
+ * 4.3BSD standard release
+ * 
+ * Revision 1.3  85/03/11  16:37:39  jqj
+ * *** empty log message ***
+ * 
+ * Revision 1.3  85/03/11  16:37:39  jqj
+ * Public alpha-test version, released 11 March 1985
+ * 
+ * Revision 1.2  85/01/27  07:37:43  jqj
+ * finished but undebugged version
+ * 
+ * Revision 1.1  85/1/4  2:40:00  jqj
+ * Initial revision -- Mogul's tcp-based version
+ */
+#ifndef lint
+static char rcsid[] = "$Header: server.c,v 2.2 86/12/10 14:21:10 ed Exp $";
+#endif
+
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/types.h>         /* for ns.h */
+#include <sys/socket.h>
+#include <netns/ns.h>          /* for XNS addresses and courierconnectin.h */
+#include <netns/sp.h>          /* for spphdr */
+#include "courier.h"
+#include "realcourierconnection.h"
+#include <except.h>
+#include <ctype.h>
+
+#if DEBUG
+int CourierServerDebuggingFlag = 0;
+#endif
+
+/*
+ * Message stream handle.
+ */
+CourierConnection *_serverConnection = 0;
+Unspecified tid;                               /* transaction ID */
+
+\f
+/* CALL, transaction id, prognumh, prognuml, version, procedurenum */
+#define CALLHDRLEN 6
+
+
+Unspecified *
+ReceiveCallMessage(procp, skipcount, skippedwords)
+       Cardinal *procp;
+       int skipcount;
+       Unspecified *skippedwords;
+{
+       Cardinal msgtype, version;
+       LongCardinal programnumber;
+       Unspecified *buf, *bp, hdrbuf[CALLHDRLEN];
+       int i;
+
+       if (skipcount > 1 && _serverConnection->state == wantversion) {
+               skipcount -= 2;
+               _serverConnection->state = inprogress;  /* per Ed Flint */
+               skippedwords += 2;
+       }
+
+       if (skipcount > CALLHDRLEN) {
+               fprintf(stderr,"ReceiveCallMessage:  skipcount=%d, too big\n",
+                       skipcount);
+               exit(1);
+       }
+
+       _serverConnection->abortseen= FALSE;            /* reset */
+
+       for (i=0; i < skipcount; i++)
+               hdrbuf[i] = skippedwords[i];
+       buf = ReadMessage(_serverConnection, hdrbuf+skipcount,
+                         CALLHDRLEN-skipcount);
+       bp = hdrbuf;
+       bp += internalize_Cardinal(&msgtype, bp);
+       bp += internalize_Unspecified(&tid, bp);
+       bp += internalize_LongCardinal(&programnumber, bp);
+       bp += internalize_Cardinal(&version, bp);
+       bp += internalize_Cardinal(procp, bp);
+#if DEBUG
+       if (CourierServerDebuggingFlag)
+               fprintf(stderr, "[ReceiveCallMessage %D %d %d]\n",
+                               programnumber, version, *procp);
+#endif
+       return(buf);
+}
+
+
+SendReturnMessage(nwords, results)
+       Cardinal nwords;
+       Unspecified *results;
+{
+#define RETHDRLEN 2
+       Unspecified *bp, buf[RETHDRLEN];
+       static Cardinal msgtype = RETURN;
+
+#if DEBUG
+       if (CourierServerDebuggingFlag)
+               fprintf(stderr, "[SendReturnMessage %d]\n", nwords);
+#endif
+       bp = buf;
+       bp += externalize_Cardinal(&msgtype, bp);
+       bp += externalize_Unspecified(&tid, bp);
+       CourierWrite(_serverConnection, (bp-buf), buf, nwords, results);
+       _serverConnection->bdtstate = wantdata;
+}
+
+\f
+static int
+ServerInit(argc, argv, skippedwords)
+       int argc;
+       char *argv[];
+       Unspecified skippedwords[];
+{
+       extern char *malloc();
+       int skipcount;
+#if DEBUG
+       int namelen;
+#endif
+       int i;
+
+       _serverConnection = (CourierConnection *)
+                       malloc(sizeof(CourierConnection));
+       _serverConnection->bdtstate = wantdata;
+       /* we normally don't bother to set up host, since the server will
+        * never reopen a closed connection
+        */
+#if DEBUG
+       namelen = sizeof(struct sockaddr_ns);
+       getpeername(_serverConnection->fd, &_serverConnection->host, &namelen);
+       fprintf(stderr,"[ServerInit: argc=%d]\n",argc);
+       for (i=0; i<argc; i++) fprintf(stderr,"\targv[%d]=%s\n", i,argv[i]);
+
+#endif
+       skipcount = -1;
+       while (argc-- > 0) {
+#if DEBUG
+               if (strcmp(argv[0],"-d") == 0)
+                       CourierServerDebuggingFlag = 1;
+               else
+#endif
+               if (isdigit(*argv[0])) {
+                       if (skipcount < 0) {
+                               _serverConnection->fd = atoi(argv[0]);
+                               skipcount++;
+                       }
+                       else if (skipcount < 8)
+                               skippedwords[skipcount++] = atoi(argv[0]);
+               }
+               argv++;
+       }
+       if (skipcount < 0 || skipcount == 1) {
+               fprintf(stderr,"in ServerInit, skipcount=%d\n",skipcount);
+               exit(1);
+       }
+       _serverConnection->state = wantversion;
+       return(skipcount);
+}
+
+
+main(argc, argv)
+       int argc;
+       char *argv[];
+{
+       /*
+        * The caller may need to read a packet before getting to the
+        * program/version which it needs for dispatching.  Data so read
+        * is passed in the argv list, and used to set skipcount and
+        * skippedwords.
+        */
+       int skipcount;  /* actual length of skippedwords */
+       Unspecified skippedwords[8];
+
+       /* ServerInit() contains server-independent startup code */
+       skipcount = ServerInit(argc, argv, skippedwords);
+
+       /* Server() may terminate in 2 ways:
+        * (1)  normally, with a return(0) and a closed connection,
+        *      either from our timeout or from END sent by client.
+        * (2)  abnormally, with an exit(1) indicating a protocol
+        *      violation.  We do not currently close down the
+        *      connection in all such cases, but we should.
+        * Note that Server may also exec() a different server if
+        *      a remote procedure for a different program arrives.
+        */
+       Server(skipcount, skippedwords);
+       exit(0);
+}