summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Robert S. French <rfrench@mit.edu>1987-06-10 08:34:32 +0000
committerGravatar Robert S. French <rfrench@mit.edu>1987-06-10 08:34:32 +0000
commit16720e5ffa2f10c5a969c4027ac570ce46832ea3 (patch)
treeaeabd7f4502221a5fafd224d52e7c6487b896d29 /lib
parenta004e96fce359c109eae47d8f2ddbfcbb808f0cd (diff)
Initial revision
Diffstat (limited to 'lib')
-rw-r--r--lib/ZClosePort.c27
-rw-r--r--lib/ZOpenPort.c64
-rw-r--r--lib/ZParseNot.c60
-rw-r--r--lib/ZPeekNot.c33
-rw-r--r--lib/ZPeekPkt.c45
-rw-r--r--lib/ZPending.c27
-rw-r--r--lib/ZRecvNot.c33
-rw-r--r--lib/ZRecvPkt.c59
-rw-r--r--lib/ZSendList.c42
-rw-r--r--lib/ZSendNot.c41
-rw-r--r--lib/ZSendPkt.c47
-rw-r--r--lib/ZSetFD.c27
12 files changed, 505 insertions, 0 deletions
diff --git a/lib/ZClosePort.c b/lib/ZClosePort.c
new file mode 100644
index 0000000..0853d00
--- /dev/null
+++ b/lib/ZClosePort.c
@@ -0,0 +1,27 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for the ZClosePort function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+Code_t ZClosePort()
+{
+ if (__Zephyr_fd >= 0)
+ close(__Zephyr_fd);
+
+ __Zephyr_fd = -1;
+
+ return (ZERR_NONE);
+}
diff --git a/lib/ZOpenPort.c b/lib/ZOpenPort.c
new file mode 100644
index 0000000..bdf3709
--- /dev/null
+++ b/lib/ZOpenPort.c
@@ -0,0 +1,64 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for the ZOpenPort function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+#include <sys/socket.h>
+
+Code_t ZOpenPort(port)
+ int *port;
+{
+ int retval;
+ struct sockaddr_in bindin;
+
+ ZClosePort();
+
+ if ((__Zephyr_fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
+ __Zephyr_fd = -1;
+ return (ZERR_UNIX);
+ }
+
+ bindin.sin_family = AF_INET;
+
+ if (port && *port)
+ bindin.sin_port = htons(*port);
+ else
+ bindin.sin_port = htons(((getpid()*8)&0xfff)+
+ ((random()>>4)&0xf)+1024);
+
+ bindin.sin_addr.s_addr = INADDR_ANY;
+
+ do {
+ if ((retval = bind(__Zephyr_fd,&bindin,sizeof(bindin))) < 0) {
+ if (errno == EADDRINUSE) {
+ if (port)
+ return (ZERR_PORTINUSE);
+ else
+ bindin.sin_port = htons(ntohs(bindin.
+ sin_port)
+ +1);
+ }
+ else
+ return (ZERR_UNIX);
+ }
+ } while (retval < 0 && port);
+
+ __Zephyr_port = ntohs(bindin.sin_port);
+
+ if (port)
+ *port = ntohs(bindin.sin_port);
+
+ return (ZERR_NONE);
+}
diff --git a/lib/ZParseNot.c b/lib/ZParseNot.c
new file mode 100644
index 0000000..394e89a
--- /dev/null
+++ b/lib/ZParseNot.c
@@ -0,0 +1,60 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for the ZParseNotice function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+Code_t ZParseNotice(buffer,len,notice,auth)
+ ZPacket_t buffer;
+ int len;
+ ZNotice_t *notice;
+ int *auth;
+{
+ int hdrlen;
+ char *ptr;
+
+ hdrlen = *((short *)buffer);
+
+ ptr = buffer+2;
+
+ if (*ptr++ != ZVERSION)
+ return (ZERR_VERS);
+
+ notice->z_kind = (ZNotice_Kind_t)*ptr++;
+ bcopy(ptr,notice->z_checksum,sizeof(ZChecksum_t));
+ ptr += sizeof(ZChecksum_t);
+ bcopy(ptr,&notice->z_uid,sizeof(ZUnique_Id_t));
+ ptr += sizeof(ZUnique_Id_t);
+ notice->z_port = *((short *)ptr);
+ ptr += sizeof(short);
+ notice->z_class = ptr;
+ ptr += strlen(ptr)+1;
+ notice->z_class_inst = ptr;
+ ptr += strlen(ptr)+1;
+ notice->z_opcode = ptr;
+ ptr += strlen(ptr)+1;
+ notice->z_sender = ptr;
+ ptr += strlen(ptr)+1;
+ notice->z_recipient = ptr;
+ ptr += strlen(ptr)+1;
+
+ if (ptr-buffer != hdrlen)
+ return(ZERR_BADPKT);
+
+ notice->z_message = (caddr_t) ptr;
+ notice->z_message_len = len-hdrlen;
+
+ *auth = 0;
+}
diff --git a/lib/ZPeekNot.c b/lib/ZPeekNot.c
new file mode 100644
index 0000000..9168214
--- /dev/null
+++ b/lib/ZPeekNot.c
@@ -0,0 +1,33 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for ZPeekNotice function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+Code_t ZPeekNotice(buffer,buffer_len,notice,auth)
+ ZPacket_t buffer;
+ int buffer_len;
+ ZNotice_t *notice;
+ int *auth;
+{
+ int len;
+ Code_t retval;
+
+ if ((retval = ZPeekPacket(buffer,buffer_len,&len)) !=
+ ZERR_NONE)
+ return (retval);
+
+ return (ZParseNotice(buffer,len,notice,auth));
+}
diff --git a/lib/ZPeekPkt.c b/lib/ZPeekPkt.c
new file mode 100644
index 0000000..ff338b6
--- /dev/null
+++ b/lib/ZPeekPkt.c
@@ -0,0 +1,45 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for ZPeekPacket function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+Code_t ZPeekPacket(buffer,buffer_len,ret_len)
+ ZPacket_t buffer;
+ int buffer_len;
+ int *ret_len;
+{
+ int retval;
+
+ if (ZGetFD() < 0)
+ return (ZERR_NOPORT);
+
+ if (!Z_QLength())
+ if ((retval = Z_ReadWait()) != ZERR_NONE)
+ return (retval);
+
+ if (buffer_len < __Q_Head->packet_len) {
+ *ret_len = buffer_len;
+ retval = ZERR_PKTLEN;
+ }
+ else {
+ *ret_len = __Q_Head->packet_len;
+ retval = ZERR_NONE;
+ }
+
+ bcopy(__Q_Head->packet,buffer,*ret_len);
+
+ return (retval);
+}
diff --git a/lib/ZPending.c b/lib/ZPending.c
new file mode 100644
index 0000000..ff9093b
--- /dev/null
+++ b/lib/ZPending.c
@@ -0,0 +1,27 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for the ZPending function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+int ZPending()
+{
+ if (ZGetFD() < 0)
+ return (-1);
+
+ Z_ReadEnqueue();
+
+ return(Z_QLength());
+}
diff --git a/lib/ZRecvNot.c b/lib/ZRecvNot.c
new file mode 100644
index 0000000..65b9514
--- /dev/null
+++ b/lib/ZRecvNot.c
@@ -0,0 +1,33 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for ZReceiveNotice function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+Code_t ZReceiveNotice(buffer,buffer_len,notice,auth)
+ ZPacket_t buffer;
+ int buffer_len;
+ ZNotice_t *notice;
+ int *auth;
+{
+ int len;
+ Code_t retval;
+
+ if ((retval = ZReceivePacket(buffer,buffer_len,&len)) !=
+ ZERR_NONE)
+ return (retval);
+
+ return (ZParseNotice(buffer,len,notice,auth));
+}
diff --git a/lib/ZRecvPkt.c b/lib/ZRecvPkt.c
new file mode 100644
index 0000000..0f1829e
--- /dev/null
+++ b/lib/ZRecvPkt.c
@@ -0,0 +1,59 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for ZReceivePacket function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+#include <sys/socket.h>
+
+#define min(a,b) ((a)<(b)?(a):(b))
+
+Code_t ZReceivePacket(buffer,buffer_len,ret_len)
+ ZPacket_t buffer;
+ int buffer_len;
+ int *ret_len;
+{
+ int retval;
+ struct _Z_InputQ *old_head;
+
+ if (ZGetFD() < 0)
+ return (ZERR_NOPORT);
+
+ if (!Z_QLength())
+ if ((retval = Z_ReadWait()) != ZERR_NONE)
+ return (retval);
+
+ if (buffer_len < __Q_Head->packet_len) {
+ *ret_len = buffer_len;
+ retval = ZERR_PKTLEN;
+ }
+ else {
+ *ret_len = __Q_Head->packet_len;
+ retval = ZERR_NONE;
+ }
+
+ bcopy(__Q_Head->packet,buffer,*ret_len);
+
+ __Q_Length--;
+
+ old_head = __Q_Head;
+ if (__Q_Length)
+ __Q_Head = __Q_Head->next;
+ else
+ __Q_Head = __Q_Tail = NULL;
+
+ free (old_head);
+
+ return (retval);
+}
diff --git a/lib/ZSendList.c b/lib/ZSendList.c
new file mode 100644
index 0000000..dc21d63
--- /dev/null
+++ b/lib/ZSendList.c
@@ -0,0 +1,42 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for the ZSendList function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+Code_t ZSendList(notice,list,nitems)
+ ZNotice_t *notice;
+ char *list[];
+ int nitems;
+{
+ Code_t retval;
+ char *buffer;
+ int len;
+
+ buffer = (char *)malloc(BUFSIZ);
+ if (!buffer)
+ return (ZERR_NOMEM);
+
+ if ((retval = ZFormatNoticeList(notice,list,nitems,buffer,
+ BUFSIZ,&len)) < 0) {
+ free(buffer);
+ return (retval);
+ }
+
+ retval = ZSendPacket(buffer,len);
+ free(buffer);
+
+ return (retval);
+}
diff --git a/lib/ZSendNot.c b/lib/ZSendNot.c
new file mode 100644
index 0000000..683b666
--- /dev/null
+++ b/lib/ZSendNot.c
@@ -0,0 +1,41 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for the ZSendNotice function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+Code_t ZSendNotice(notice)
+ ZNotice_t *notice;
+{
+ Code_t retval;
+ char *buffer;
+ int len;
+
+ buffer = (char *)malloc(BUFSIZ);
+ if (!buffer)
+ return (ZERR_NOMEM);
+
+ len = BUFSIZ;
+
+ if ((retval = ZFormatNotice(notice,buffer,sizeof buffer,&len)) < 0) {
+ free(buffer);
+ return (retval);
+ }
+
+ retval = ZSendPacket(buffer,len);
+ free(buffer);
+
+ return (retval);
+}
diff --git a/lib/ZSendPkt.c b/lib/ZSendPkt.c
new file mode 100644
index 0000000..215f548
--- /dev/null
+++ b/lib/ZSendPkt.c
@@ -0,0 +1,47 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for the ZSendPacket function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+#include <sys/socket.h>
+
+Code_t ZSendPacket(packet,len)
+ ZPacket_t packet;
+ int len;
+{
+ Code_t retval;
+ struct sockaddr_in sin;
+
+ if (!packet || len < 0 || len > Z_MAXPKTLEN)
+ return (ZERR_ILLVAL);
+
+ if (ZGetFD() < 0)
+ if ((retval = ZOpenPort(0)) != ZERR_NONE)
+ return (retval);
+
+ if ((retval = Z_GetHMPortAddr()) != ZERR_NONE)
+ return (retval);
+
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(__HM_port);
+ bcopy(__HM_addr,&sin.sin_addr,__HM_length);
+
+ printf("Internal: Sending to port %d\n",__HM_port);
+
+ if (sendto(ZGetFD(),packet,len,0,&sin,sizeof(sin)) < 0)
+ return (ZERR_UNIX);
+
+ return (ZERR_NONE);
+}
diff --git a/lib/ZSetFD.c b/lib/ZSetFD.c
new file mode 100644
index 0000000..0837cb1
--- /dev/null
+++ b/lib/ZSetFD.c
@@ -0,0 +1,27 @@
+/* This file is part of the Project Athena Zephyr Notification System.
+ * It contains source for the ZSetFD function.
+ *
+ * Created by: Robert French
+ *
+ * $Source$
+ * $Author$
+ *
+ * Copyright (c) 1987 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, see the file
+ * "mit-copyright.h".
+ */
+/* $Header$ */
+
+#include <zephyr/mit-copyright.h>
+
+#include <zephyr/zephyr_internal.h>
+
+Code_t ZSetFD(port)
+ int port;
+{
+ ZClosePort();
+
+ __Zephyr_fd = port;
+
+ return (ZERR_NONE);
+}