new version from sam@monet.berkeley.edu
authorKirk McKusick <mckusick@ucbvax.Berkeley.EDU>
Fri, 14 Mar 1986 09:22:35 +0000 (01:22 -0800)
committerKirk McKusick <mckusick@ucbvax.Berkeley.EDU>
Fri, 14 Mar 1986 09:22:35 +0000 (01:22 -0800)
SCCS-vsn: libexec/talkd/announce.c 5.2
SCCS-vsn: libexec/talkd/print.c 5.2
SCCS-vsn: libexec/talkd/process.c 5.3
SCCS-vsn: libexec/talkd/table.c 5.2
SCCS-vsn: libexec/talkd/talkd.c 5.2
SCCS-vsn: libexec/talkd/Makefile 5.2
SCCS-vsn: include/protocols/talkd.h 5.2

usr/src/include/protocols/talkd.h
usr/src/libexec/talkd/Makefile
usr/src/libexec/talkd/announce.c
usr/src/libexec/talkd/print.c
usr/src/libexec/talkd/process.c
usr/src/libexec/talkd/table.c
usr/src/libexec/talkd/talkd.c

index 1881d68..d536a95 100644 (file)
@@ -3,55 +3,82 @@
  * All rights reserved.  The Berkeley software License Agreement
  * specifies the terms and conditions for redistribution.
  *
  * All rights reserved.  The Berkeley software License Agreement
  * specifies the terms and conditions for redistribution.
  *
- *     @(#)talkd.h     5.1 (Berkeley) %G%
- */
-
-/*
- * ctl.h describes the structure that
- * talk and talkd pass back and forth.
+ *     @(#)talkd.h     5.2 (Berkeley) %G%
  */
 
 #include <sys/types.h>
 #include <sys/socket.h>
  */
 
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
+/*
+ * This describes the protocol used by the talk server and clients.
+ *
+ * The talk server acts a repository of invitations, responding to
+ * requests by clients wishing to rendezvous for the purpose of
+ * holding a conversation.  In normal operation, a client, the caller,
+ * initiates a rendezvous by sending a CTL_MSG to the server of
+ * type LOOK_UP.  This causes the server to search its invitation
+ * tables to check if an invitation currently exists for the caller
+ * (to speak to the callee specified in the message).  If the lookup
+ * fails, the caller then sends an ANNOUNCE message causing the server
+ * to broadcast an announcement on the callee's login ports requesting
+ * contact.  When the callee responds, the local server uses the
+ * recorded invitation to respond with the appropriate rendezvous
+ * address and the caller and callee client programs establish a
+ * stream connection through which the conversation takes place.
+ */
 
 
-#define        NAME_SIZE       9
+/*
+ * Client->server request message format.
+ */
+typedef struct {
+       u_char  vers;           /* protocol version */
+       u_char  type;           /* request type, see below */
+       u_char  answer;         /* not used */
+       u_char  pad;
+       u_long  id_num;         /* message id */
+       struct  sockaddr addr;
+       struct  sockaddr ctl_addr;
+       long    pid;            /* caller's process id */
+#define        NAME_SIZE       12
+       char    l_name[NAME_SIZE];/* caller's name */
+       char    r_name[NAME_SIZE];/* callee's name */
 #define        TTY_SIZE        16
 #define        TTY_SIZE        16
-#define        HOST_NAME_LENGTH 256
+       char    r_tty[TTY_SIZE];/* callee's tty name */
+} CTL_MSG;
 
 
-#define MAX_LIFE       60      /* max time daemon saves invitations */
-/* RING_WAIT should be 10's of seconds less than MAX_LIFE */
-#define RING_WAIT      30      /* time to wait before refreshing invitation */
+/*
+ * Server->client response message format.
+ */
+typedef struct {
+       u_char  vers;           /* protocol version */
+       u_char  type;           /* type of request message, see below */
+       u_char  answer;         /* respose to request message, see below */
+       u_char  pad;
+       u_long  id_num;         /* message id */
+       struct  sockaddr addr;  /* address for establishing conversation */
+} CTL_RESPONSE;
 
 
-/* type values */
-#define LEAVE_INVITE   0
-#define LOOK_UP                1
-#define DELETE         2
-#define ANNOUNCE       3
+#define        TALK_VERSION    1               /* protocol version */
 
 
-/* answer values */
-#define SUCCESS                0
-#define NOT_HERE       1
-#define FAILED         2
-#define MACHINE_UNKNOWN        3
-#define PERMISSION_DENIED 4
-#define UNKNOWN_REQUEST        5
+/* message type values */
+#define LEAVE_INVITE   0       /* leave invitation with server */
+#define LOOK_UP                1       /* check for invitation by callee */
+#define DELETE         2       /* delete invitation by caller */
+#define ANNOUNCE       3       /* announce invitation by caller */
 
 
-typedef struct ctl_response {
-       char    type;
-       char    answer;
-       int     id_num;
-       struct  sockaddr_in addr;
-} CTL_RESPONSE;
+/* answer values */
+#define SUCCESS                0       /* operation completed properly */
+#define NOT_HERE       1       /* callee not logged in */
+#define FAILED         2       /* operation failed for unexplained reason */
+#define MACHINE_UNKNOWN        3       /* caller's machine name unknown */
+#define PERMISSION_DENIED 4    /* callee's tty doesn't permit announce */
+#define UNKNOWN_REQUEST        5       /* request has invalid type value */
+#define        BADVERSION      6       /* request has invalid protocol version */
+#define        BADADDR         7       /* request has invalid addr value */
+#define        BADCTLADDR      8       /* request has invalid ctl_addr value */
 
 
-typedef struct ctl_msg {
-       char    type;
-       char    l_name[NAME_SIZE];
-       char    r_name[NAME_SIZE];
-       int     id_num;
-       int     pid;
-       char    r_tty[TTY_SIZE];
-       struct  sockaddr_in addr;
-       struct  sockaddr_in ctl_addr;
-} CTL_MSG;
+/*
+ * Operational parameters.
+ */
+#define MAX_LIFE       60      /* max time daemon saves invitations */
+/* RING_WAIT should be 10's of seconds less than MAX_LIFE */
+#define RING_WAIT      30      /* time to wait before resending invitation */
index 7fbfd5b..6283e2c 100644 (file)
@@ -3,39 +3,23 @@
 # All rights reserved.  The Berkeley software License Agreement
 # specifies the terms and conditions for redistribution.
 #
 # All rights reserved.  The Berkeley software License Agreement
 # specifies the terms and conditions for redistribution.
 #
-#      @(#)Makefile    5.1 (Berkeley) %G%
+#      @(#)Makefile    5.2 (Berkeley) %G%
 #
 DESTDIR=
 #
 DESTDIR=
-DOBJECT=talkd.o announce.o process.o table.o print.o
-TOBJECT=talk.o get_names.o display.o io.o ctl.o init_disp.o \
-       msgs.o get_addrs.o ctl_transact.o invite.o look_up.o
-DSOURCE=talkd.c announce.c process.c table.c print.c
-TSOURCE=talk.c get_names.c display.c io.c ctl.c init_disp.c \
-       msgs.c get_addrs.c ctl_transact.c invite.c look_up.c
-INCLUDE=talk.h ctl.h talk_ctl.h
-SOURCE= ${DSOURCE} ${TSOURCE}
+OBJS=  talkd.o announce.o process.o table.o print.o
+SRCS=  talkd.c announce.c process.c table.c print.c
 CFLAGS= -O
 
 CFLAGS= -O
 
-all:   talk talkd
+all:   talkd
 
 
-talkd: ${DOBJECT}
-       cc ${CFLAGS} -o talkd ${DOBJECT}
+talkd: ${OBJS}
+       cc ${CFLAGS} -o talkd ${OBJS}
 
 
-talk:  ${TOBJECT}
-       cc -o talk ${TOBJECT} -lcurses -ltermlib
-
-${DOBJECT}: ctl.h
-${TOBJECT}: talk.h ctl.h talk_ctl.h
-
-install: talk talkd
-       install -s talk ${DESTDIR}/usr/ucb/talk
+install: talkd
        install -s talkd ${DESTDIR}/etc/talkd
 
        install -s talkd ${DESTDIR}/etc/talkd
 
-lint:
-       lint ${DSOURCE}
-
 clean:
 clean:
-       rm -f *.o talk talkd
+       rm -f ${OBJS} errs core a.out talkd
 
 
-tags:  ${SOURCE} ${INCLUDE}
-       ctags ${SOURCE} ${INCLUDE}
+tags:  ${SRCS}
+       ctags ${SOURCE}
index fe4d177..cbdb47a 100644 (file)
@@ -5,11 +5,10 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)announce.c 5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)announce.c 5.2 (Berkeley) %G%";
 #endif not lint
 
 #endif not lint
 
-#include "ctl.h"
-
+#include <sys/types.h>
 #include <sys/stat.h>
 #include <sgtty.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sgtty.h>
 #include <sys/ioctl.h>
@@ -17,38 +16,37 @@ static char sccsid[] = "@(#)announce.c      5.1 (Berkeley) %G%";
 #include <stdio.h>
 #include <sys/wait.h>
 #include <errno.h>
 #include <stdio.h>
 #include <sys/wait.h>
 #include <errno.h>
+#include <syslog.h>
 
 
-char *sprintf();
+#include <protocols/talkd.h>
 
 
-extern int errno;
-extern char hostname[];
-int nofork = 0;                /* to be set from the debugger */
+extern int errno;
+extern char hostname[];
 
 /*
 
 /*
+ * Announce an invitation to talk.
+ *
  * Because the tty driver insists on attaching a terminal-less
  * process to any terminal that it writes on, we must fork a child
  * to protect ourselves
  */
  * Because the tty driver insists on attaching a terminal-less
  * process to any terminal that it writes on, we must fork a child
  * to protect ourselves
  */
-
 announce(request, remote_machine)
        CTL_MSG *request;
        char *remote_machine;
 {
        int pid, val, status;
 
 announce(request, remote_machine)
        CTL_MSG *request;
        char *remote_machine;
 {
        int pid, val, status;
 
-       if (nofork)
-               return(announce_proc(request, remote_machine));
        if (pid = fork()) {
                /* we are the parent, so wait for the child */
                if (pid == -1)          /* the fork failed */
        if (pid = fork()) {
                /* we are the parent, so wait for the child */
                if (pid == -1)          /* the fork failed */
-                       return(FAILED);
+                       return (FAILED);
                do {
                        val = wait(&status);
                        if (val == -1) {
                                if (errno == EINTR)
                                        continue;
                                /* shouldn't happen */
                do {
                        val = wait(&status);
                        if (val == -1) {
                                if (errno == EINTR)
                                        continue;
                                /* shouldn't happen */
-                               perror("wait");
+                               syslog(LOG_WARNING, "announce: wait: %m");
                                return (FAILED);
                        }
                } while (val != pid);
                                return (FAILED);
                        }
                } while (val != pid);
@@ -74,15 +72,15 @@ announce_proc(request, remote_machine)
        FILE *tf;
        struct stat stbuf;
 
        FILE *tf;
        struct stat stbuf;
 
-       (void) sprintf(full_tty, "/dev/%s", request->r_tty);
+       sprintf(full_tty, "/dev/%s", request->r_tty);
        if (access(full_tty, 0) != 0)
                return (FAILED);
        if ((tf = fopen(full_tty, "w")) == NULL)
                return (PERMISSION_DENIED);
        /*
        if (access(full_tty, 0) != 0)
                return (FAILED);
        if ((tf = fopen(full_tty, "w")) == NULL)
                return (PERMISSION_DENIED);
        /*
-        * Open gratuitously attaches the talkd to
-        * any tty it opens, so disconnect us from the
-        * tty before we catch a signal
+        * On first tty open, the server will have
+        * it's pgrp set, so disconnect us from the
+        * tty before we catch a signal.
         */
        ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
        if (fstat(fileno(tf), &stbuf) < 0)
         */
        ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
        if (fstat(fileno(tf), &stbuf) < 0)
@@ -147,9 +145,9 @@ print_mesg(tf, request, remote_machine)
        max_size = max(max_size, sizes[i]);
        i++;
        bptr = big_buf;
        max_size = max(max_size, sizes[i]);
        i++;
        bptr = big_buf;
-       *(bptr++) = '\a'; /* send something to wake them up */
-       *(bptr++) = '\r';       /* add a \r in case of raw mode */
-       *(bptr++) = '\n';
+       *bptr++ = '\a'; /* send something to wake them up */
+       *bptr++ = '\r'; /* add a \r in case of raw mode */
+       *bptr++ = '\n';
        for (i = 0; i < N_LINES; i++) {
                /* copy the line into the big buffer */
                lptr = line_buf[i];
        for (i = 0; i < N_LINES; i++) {
                /* copy the line into the big buffer */
                lptr = line_buf[i];
index d73bd6a..b3f4689 100644 (file)
@@ -5,34 +5,54 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)print.c    5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)print.c    5.2 (Berkeley) %G%";
 #endif not lint
 
 /* debug print routines */
 
 #include <stdio.h>
 #endif not lint
 
 /* debug print routines */
 
 #include <stdio.h>
-#include "ctl.h"
+#include <syslog.h>
 
 
-print_request(request)
-       CTL_MSG *request;
+#include <protocols/talkd.h>
+
+static char *types[] =
+    { "leave_invite", "look_up", "delete", "announce" };
+#define        NTYPES  (sizeof (types) / sizeof (types[0]))
+static char *answers[] = 
+    { "success", "not_here", "failed", "machine_unknown", "permission_denied",
+      "unknown_request", "badversion", "badaddr", "badctladdr" };
+#define        NANSWERS        (sizeof (answers) / sizeof (answers[0]))
+
+print_request(cp, mp)
+       char *cp;
+       register CTL_MSG *mp;
 {
 {
-       extern FILE *debugout;
+       char tbuf[80], *tp;
        
        
-       fprintf(debugout
-               , "type is %d, l_user %s, r_user %s, r_tty %s\n"
-               , request->type, request->l_name, request->r_name
-               , request->r_tty);
-       fprintf(debugout, "             id = %d\n", request->id_num);
-       fflush(debugout);
+       if (mp->type > NTYPES) {
+               sprintf(tbuf, "type %d", mp->type);
+               tp = tbuf;
+       } else
+               tp = types[mp->type];
+       syslog(LOG_DEBUG, "%s: %s: id %d, l_user %s, r_user %s, r_tty %s",
+           cp, tp, mp->id_num, mp->l_name, mp->r_name, mp->r_tty);
 }
 
 }
 
-print_response(response)
-       CTL_RESPONSE *response;
+print_response(cp, rp)
+       char *cp;
+       register CTL_RESPONSE *rp;
 {
 {
-       extern FILE *debugout;
+       char tbuf[80], *tp, abuf[80], *ap;
        
        
-       printf(debugout
-               , "type is %d, answer is %d, id = %d\n\n", response->type
-               , response->answer, response->id_num);
-       fflush(debugout);
+       if (rp->type > NTYPES) {
+               sprintf(tbuf, "type %d", rp->type);
+               tp = tbuf;
+       } else
+               tp = types[rp->type];
+       if (rp->answer > NANSWERS) {
+               sprintf(abuf, "answer %d", rp->answer);
+               ap = abuf;
+       } else
+               ap = answers[rp->answer];
+       syslog(LOG_DEBUG, "%s: %s: %s, id %d", cp, tp, ap, ntohl(rp->id_num));
 }
 }
index 0a48da5..f3483dd 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)process.c  5.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)process.c  5.3 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -16,98 +16,130 @@ static char sccsid[] = "@(#)process.c      5.2 (Berkeley) %G%";
  *               in the table for the local user
  *     DELETE - delete invitation
  */
  *               in the table for the local user
  *     DELETE - delete invitation
  */
-#include "ctl.h"
+#include <sys/types.h>
 #include <sys/stat.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <stdio.h>
+#include <syslog.h>
+#include <netdb.h>
+#include <netinet/in.h>
 
 
-char *strcpy();
+#include <protocols/talkd.h>
+
+char   *strcpy();
 CTL_MSG *find_request();
 CTL_MSG *find_match();
 
 CTL_MSG *find_request();
 CTL_MSG *find_match();
 
-process_request(request, response)
-       CTL_MSG *request;
-       CTL_RESPONSE *response;
+process_request(mp, rp)
+       register CTL_MSG *mp;
+       register CTL_RESPONSE *rp;
 {
 {
-       CTL_MSG *ptr;
-
-       response->type = request->type;
-       response->id_num = 0;
-
-       switch (request->type) {
+       register CTL_MSG *ptr;
+       extern int debug;
+
+       rp->vers = TALK_VERSION;
+       rp->type = mp->type;
+       rp->id_num = htonl(0);
+       if (mp->vers != TALK_VERSION) {
+               syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
+               rp->answer = BADVERSION;
+               return;
+       }
+       mp->id_num = ntohl(mp->id_num);
+       mp->addr.sa_family = ntohs(mp->addr.sa_family);
+       if (mp->addr.sa_family != AF_INET) {
+               syslog(LOG_WARNING, "Bad address, family %d",
+                   mp->addr.sa_family);
+               rp->answer = BADADDR;
+               return;
+       }
+       mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
+       if (mp->ctl_addr.sa_family != AF_INET) {
+               syslog(LOG_WARNING, "Bad control address, family %d",
+                   mp->ctl_addr.sa_family);
+               rp->answer = BADCTLADDR;
+               return;
+       }
+       mp->pid = ntohl(mp->pid);
+       if (debug)
+               print_request("process_request", mp);
+       switch (mp->type) {
 
 
-       case ANNOUNCE :
-               do_announce(request, response);
+       case ANNOUNCE:
+               do_announce(mp, rp);
                break;
 
                break;
 
-       case LEAVE_INVITE :
-               ptr = find_request(request);
-               if (ptr != (CTL_MSG *) 0) {
-                       response->id_num = ptr->id_num;
-                       response->answer = SUCCESS;
+       case LEAVE_INVITE:
+               ptr = find_request(mp);
+               if (ptr != (CTL_MSG *)0) {
+                       rp->id_num = htonl(ptr->id_num);
+                       rp->answer = SUCCESS;
                } else
                } else
-                       insert_table(request, response);
+                       insert_table(mp, rp);
                break;
 
                break;
 
-       case LOOK_UP :
-               ptr = find_match(request);
-               if (ptr != (CTL_MSG *) 0) {
-                       response->id_num = ptr->id_num;
-                       response->addr = ptr->addr;
-                       response->answer = SUCCESS;
+       case LOOK_UP:
+               ptr = find_match(mp);
+               if (ptr != (CTL_MSG *)0) {
+                       rp->id_num = htonl(ptr->id_num);
+                       rp->addr = ptr->addr;
+                       rp->addr.sa_family = htons(ptr->addr.sa_family);
+                       rp->answer = SUCCESS;
                } else
                } else
-                       response->answer = NOT_HERE;
+                       rp->answer = NOT_HERE;
                break;
 
                break;
 
-       case DELETE :
-               response->answer = delete_invite(request->id_num);
+       case DELETE:
+               rp->answer = delete_invite(mp->id_num);
                break;
 
                break;
 
-       default :
-               response->answer = UNKNOWN_REQUEST;
+       default:
+               rp->answer = UNKNOWN_REQUEST;
                break;
        }
                break;
        }
+       if (debug)
+               print_response("process_request", rp);
 }
 
 }
 
-struct hostent *gethostbyaddr();
-
-do_announce(request, response)
-       CTL_MSG *request;
-       CTL_RESPONSE *response;
+do_announce(mp, rp)
+       register CTL_MSG *mp;
+       CTL_RESPONSE *rp;
 {
        struct hostent *hp;
        CTL_MSG *ptr;
        int result;
 
        /* see if the user is logged */
 {
        struct hostent *hp;
        CTL_MSG *ptr;
        int result;
 
        /* see if the user is logged */
-       result = find_user(request->r_name, request->r_tty);
+       result = find_user(mp->r_name, mp->r_tty);
        if (result != SUCCESS) {
        if (result != SUCCESS) {
-               response->answer = result;
+               rp->answer = result;
                return;
        }
                return;
        }
-       hp = gethostbyaddr(&request->ctl_addr.sin_addr,
-               sizeof(struct in_addr), AF_INET);
+#define        satosin(sa)     ((struct sockaddr_in *)(sa))
+       hp = gethostbyaddr(&satosin(&mp->ctl_addr)->sin_addr,
+               sizeof (struct in_addr), AF_INET);
        if (hp == (struct hostent *)0) {
        if (hp == (struct hostent *)0) {
-               response->answer = MACHINE_UNKNOWN;
+               rp->answer = MACHINE_UNKNOWN;
                return;
        }
                return;
        }
-       ptr = find_request(request);
+       ptr = find_request(mp);
        if (ptr == (CTL_MSG *) 0) {
        if (ptr == (CTL_MSG *) 0) {
-               insert_table(request,response);
-               response->answer = announce(request, hp->h_name);
+               insert_table(mp, rp);
+               rp->answer = announce(mp, hp->h_name);
                return;
        }
                return;
        }
-       if (request->id_num > ptr->id_num) {
+       if (mp->id_num > ptr->id_num) {
                /*
                /*
-                * this is an explicit re-announce, so update the id_num
-                * field to avoid duplicates and re-announce the talk 
+                * This is an explicit re-announce, so update the id_num
+                * field to avoid duplicates and re-announce the talk.
                 */
                 */
-               ptr->id_num = response->id_num = new_id();
-               response->answer = announce(request, hp->h_name);
-               return;
+               ptr->id_num = new_id();
+               rp->id_num = htonl(ptr->id_num);
+               rp->answer = announce(mp, hp->h_name);
+       } else {
+               /* a duplicated request, so ignore it */
+               rp->id_num = htonl(ptr->id_num);
+               rp->answer = SUCCESS;
        }
        }
-       /* a duplicated request, so ignore it */
-       response->id_num = ptr->id_num;
-       response->answer = SUCCESS;
 }
 
 #include <utmp.h>
 }
 
 #include <utmp.h>
@@ -116,8 +148,7 @@ do_announce(request, response)
  * Search utmp for the local user
  */
 find_user(name, tty)
  * Search utmp for the local user
  */
 find_user(name, tty)
-       char *name;
-       char *tty;
+       char *name, *tty;
 {
        struct utmp ubuf;
        int status;
 {
        struct utmp ubuf;
        int status;
index aaf2855..d1d9e23 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)table.c    5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)table.c    5.2 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -18,8 +18,9 @@ static char sccsid[] = "@(#)table.c   5.1 (Berkeley) %G%";
  */
 #include <stdio.h>
 #include <sys/time.h>
  */
 #include <stdio.h>
 #include <sys/time.h>
+#include <syslog.h>
 
 
-#include "ctl.h"
+#include <protocols/talkd.h>
 
 #define MAX_ID 16000   /* << 2^15 so I don't have sign troubles */
 
 
 #define MAX_ID 16000   /* << 2^15 so I don't have sign troubles */
 
@@ -38,7 +39,7 @@ struct table_entry {
        TABLE_ENTRY *last;
 };
 
        TABLE_ENTRY *last;
 };
 
-TABLE_ENTRY    *table = NIL;
+TABLE_ENTRY *table = NIL;
 CTL_MSG *find_request();
 CTL_MSG *find_match();
 char   *malloc();
 CTL_MSG *find_request();
 CTL_MSG *find_match();
 char   *malloc();
@@ -49,31 +50,26 @@ char        *malloc();
  */
 CTL_MSG *
 find_match(request)
  */
 CTL_MSG *
 find_match(request)
-       CTL_MSG *request;
+       register CTL_MSG *request;
 {
 {
-       TABLE_ENTRY *ptr;
-       extern FILE *debugout;
+       register TABLE_ENTRY *ptr;
        long current_time;
 
        gettimeofday(&tp, &txp);
        current_time = tp.tv_sec;
        long current_time;
 
        gettimeofday(&tp, &txp);
        current_time = tp.tv_sec;
-       if (debug) {
-               fprintf(debugout, "Entering Look-Up with : \n");
-               print_request(request);
-       }
+       if (debug)
+               print_request("find_match", request);
        for (ptr = table; ptr != NIL; ptr = ptr->next) {
                if ((ptr->time - current_time) > MAX_LIFE) {
                        /* the entry is too old */
        for (ptr = table; ptr != NIL; ptr = ptr->next) {
                if ((ptr->time - current_time) > MAX_LIFE) {
                        /* the entry is too old */
-                       if (debug) {
-                               fprintf(debugout
-                                       ,"Deleting expired entry : \n");
-                               print_request(&ptr->request);
-                       }
+                       if (debug)
+                               print_request("deleting expired entry",
+                                   &ptr->request);
                        delete(ptr);
                        continue;
                }
                if (debug)
                        delete(ptr);
                        continue;
                }
                if (debug)
-                       print_request(&ptr->request);
+                       print_request("", &ptr->request);
                if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
                    strcmp(request->r_name, ptr->request.l_name) == 0 &&
                     ptr->request.type == LEAVE_INVITE)
                if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
                    strcmp(request->r_name, ptr->request.l_name) == 0 &&
                     ptr->request.type == LEAVE_INVITE)
@@ -88,10 +84,9 @@ find_match(request)
  */
 CTL_MSG *
 find_request(request)
  */
 CTL_MSG *
 find_request(request)
-       CTL_MSG *request;
+       register CTL_MSG *request;
 {
 {
-       TABLE_ENTRY *ptr;
-       extern FILE *debugout;
+       register TABLE_ENTRY *ptr;
        long current_time;
 
        gettimeofday(&tp, &txp);
        long current_time;
 
        gettimeofday(&tp, &txp);
@@ -100,23 +95,19 @@ find_request(request)
         * See if this is a repeated message, and check for
         * out of date entries in the table while we are it.
         */
         * See if this is a repeated message, and check for
         * out of date entries in the table while we are it.
         */
-       if (debug) {
-               fprintf(debugout, "Entering find_request with : \n");
-               print_request(request);
-       }
+       if (debug)
+               print_request("find_request", request);
        for (ptr = table; ptr != NIL; ptr = ptr->next) {
                if ((ptr->time - current_time) > MAX_LIFE) {
                        /* the entry is too old */
        for (ptr = table; ptr != NIL; ptr = ptr->next) {
                if ((ptr->time - current_time) > MAX_LIFE) {
                        /* the entry is too old */
-                       if (debug) {
-                               fprintf(debugout
-                                       , "Deleting expired entry : \n");
-                               print_request(&ptr->request);
-                       }
+                       if (debug)
+                               print_request("deleting expired entry",
+                                   &ptr->request);
                        delete(ptr);
                        continue;
                }
                if (debug)
                        delete(ptr);
                        continue;
                }
                if (debug)
-                       print_request(&ptr->request);
+                       print_request("", &ptr->request);
                if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
                    strcmp(request->l_name, ptr->request.l_name) == 0 &&
                    request->type == ptr->request.type &&
                if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
                    strcmp(request->l_name, ptr->request.l_name) == 0 &&
                    request->type == ptr->request.type &&
@@ -133,17 +124,18 @@ insert_table(request, response)
        CTL_MSG *request;
        CTL_RESPONSE *response;
 {
        CTL_MSG *request;
        CTL_RESPONSE *response;
 {
-       TABLE_ENTRY *ptr;
+       register TABLE_ENTRY *ptr;
        long current_time;
 
        gettimeofday(&tp, &txp);
        current_time = tp.tv_sec;
        long current_time;
 
        gettimeofday(&tp, &txp);
        current_time = tp.tv_sec;
-       response->id_num = request->id_num = new_id();
+       request->id_num = new_id();
+       response->id_num = htonl(request->id_num);
        /* insert a new entry into the top of the list */
        ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
        if (ptr == NIL) {
        /* insert a new entry into the top of the list */
        ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
        if (ptr == NIL) {
-               fprintf(stderr, "malloc in insert_table");
-               exit(1);
+               syslog(LOG_ERR, "insert_table: Out of memory");
+               _exit(1);
        }
        ptr->time = current_time;
        ptr->request = *request;
        }
        ptr->time = current_time;
        ptr->request = *request;
@@ -174,18 +166,16 @@ new_id()
 delete_invite(id_num)
        int id_num;
 {
 delete_invite(id_num)
        int id_num;
 {
-       TABLE_ENTRY *ptr;
-       extern FILE *debugout;
+       register TABLE_ENTRY *ptr;
 
        ptr = table;
 
        ptr = table;
-
        if (debug)
        if (debug)
-               fprintf(debugout,"Entering delete_invite with %d\n", id_num);
+               syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
        for (ptr = table; ptr != NIL; ptr = ptr->next) {
                if (ptr->request.id_num == id_num)
                        break;
                if (debug)
        for (ptr = table; ptr != NIL; ptr = ptr->next) {
                if (ptr->request.id_num == id_num)
                        break;
                if (debug)
-                       print_request(&ptr->request);
+                       print_request("", &ptr->request);
        }
        if (ptr != NIL) {
                delete(ptr);
        }
        if (ptr != NIL) {
                delete(ptr);
@@ -198,14 +188,11 @@ delete_invite(id_num)
  * Classic delete from a double-linked list
  */
 delete(ptr)
  * Classic delete from a double-linked list
  */
 delete(ptr)
-       TABLE_ENTRY *ptr;
+       register TABLE_ENTRY *ptr;
 {
 {
-       extern FILE *debugout;
 
 
-       if (debug) {
-               fprintf(debugout, "Deleting : ");
-               print_request(&ptr->request);
-       }
+       if (debug)
+               print_request("delete", &ptr->request);
        if (table == ptr)
                table = ptr->next;
        else if (ptr->last != NIL)
        if (table == ptr)
                table = ptr->next;
        else if (ptr->last != NIL)
index 2d849f9..26ae4a7 100644 (file)
@@ -11,7 +11,7 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)talkd.c    5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)talkd.c    5.2 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -23,17 +23,15 @@ static char sccsid[] = "@(#)talkd.c 5.1 (Berkeley) %G%";
 #include <stdio.h>
 #include <errno.h>
 #include <signal.h>
 #include <stdio.h>
 #include <errno.h>
 #include <signal.h>
+#include <syslog.h>
 
 
-#include "ctl.h"
-
-struct sockaddr_in sin = { AF_INET };
+#include <protocols/talkd.h>
 
 CTL_MSG                request;
 CTL_RESPONSE   response;
 
 int    sockt;
 int    debug = 0;
 
 CTL_MSG                request;
 CTL_RESPONSE   response;
 
 int    sockt;
 int    debug = 0;
-FILE   *debugout;
 int    timeout();
 long   lastmsgtime;
 
 int    timeout();
 long   lastmsgtime;
 
@@ -46,41 +44,42 @@ main(argc, argv)
        int argc;
        char *argv[];
 {
        int argc;
        char *argv[];
 {
-       struct sockaddr_in from;
-       int fromlen, cc;
-       
-       if (debug)
-               debugout = (FILE *)fopen ("/usr/tmp/talkd.msgs", "w");
+       register CTL_MSG *mp = &request;
+       int cc;
 
        if (getuid()) {
 
        if (getuid()) {
-               fprintf(stderr, "Talkd : not super user\n");
+               fprintf(stderr, "%s: getuid: not super-user", argv[0]);
                exit(1);
        }
                exit(1);
        }
-       gethostname(hostname, sizeof (hostname));
-       (void) chdir("/dev");
+       openlog("talkd", LOG_PID, LOG_DAEMON);
+       if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
+               syslog(LOG_ERR, "gethostname: %m");
+               _exit(1);
+       }
+       if (chdir("/dev") < 0) {
+               syslog(LOG_ERR, "chdir: /dev: %m");
+               _exit(1);
+       }
+       if (argc > 1 && strcmp(argv[1], "-d") == 0)
+               debug = 1;
        signal(SIGALRM, timeout);
        alarm(TIMEOUT);
        for (;;) {
                extern int errno;
 
        signal(SIGALRM, timeout);
        alarm(TIMEOUT);
        for (;;) {
                extern int errno;
 
-               fromlen = sizeof(from);
-               cc = recvfrom(0, (char *)&request, sizeof (request), 0,
-                   &from, &fromlen);
-               if (cc != sizeof(request)) {
+               cc = recv(0, (char *)mp, sizeof (*mp), 0);
+               if (cc != sizeof (*mp)) {
                        if (cc < 0 && errno != EINTR)
                        if (cc < 0 && errno != EINTR)
-                       perror("recvfrom");
+                               syslog(LOG_WARNING, "recv: %m");
                        continue;
                }
                lastmsgtime = time(0);
                        continue;
                }
                lastmsgtime = time(0);
-               swapmsg(&request);
-               if (debug) print_request(&request);
-               process_request(&request, &response);
+               process_request(mp, &response);
                /* can block here, is this what I want? */
                /* can block here, is this what I want? */
-               cc = sendto(sockt, (char *) &response,
-                   sizeof (response), 0, &request.ctl_addr,
-                   sizeof (request.ctl_addr));
-               if (cc != sizeof(response))
-                       perror("sendto");
+               cc = sendto(sockt, (char *)&response,
+                   sizeof (response), 0, &mp->ctl_addr, sizeof (mp->ctl_addr));
+               if (cc != sizeof (response))
+                       syslog(LOG_WARNING, "sendto: %m");
        }
 }
 
        }
 }
 
@@ -88,22 +87,6 @@ timeout()
 {
 
        if (time(0) - lastmsgtime >= MAXIDLE)
 {
 
        if (time(0) - lastmsgtime >= MAXIDLE)
-               exit(0);
+               _exit(0);
        alarm(TIMEOUT);
 }
        alarm(TIMEOUT);
 }
-
-/*  
- * heuristic to detect if need to swap bytes
- */
-
-swapmsg(req)
-       CTL_MSG *req;
-{
-       if (req->ctl_addr.sin_family == ntohs(AF_INET)) {
-               req->id_num = ntohl(req->id_num);
-               req->pid = ntohl(req->pid);
-               req->addr.sin_family = ntohs(req->addr.sin_family);
-               req->ctl_addr.sin_family =
-                       ntohs(req->ctl_addr.sin_family);
-       }
-}