home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / talk / invite.c,v < prev    next >
Encoding:
Text File  |  1994-07-16  |  6.1 KB  |  214 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks
  5.     florian:1.1; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    94.07.16.09.52.42;    author florian;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19. 1.1
  20. log
  21. @Initial revision
  22. @
  23. text
  24. @/*
  25.  * Copyright (c) 1983 Regents of the University of California.
  26.  * All rights reserved.
  27.  *
  28.  * Redistribution and use in source and binary forms, with or without
  29.  * modification, are permitted provided that the following conditions
  30.  * are met:
  31.  * 1. Redistributions of source code must retain the above copyright
  32.  *    notice, this list of conditions and the following disclaimer.
  33.  * 2. Redistributions in binary form must reproduce the above copyright
  34.  *    notice, this list of conditions and the following disclaimer in the
  35.  *    documentation and/or other materials provided with the distribution.
  36.  * 3. All advertising materials mentioning features or use of this software
  37.  *    must display the following acknowledgement:
  38.  *    This product includes software developed by the University of
  39.  *    California, Berkeley and its contributors.
  40.  * 4. Neither the name of the University nor the names of its contributors
  41.  *    may be used to endorse or promote products derived from this software
  42.  *    without specific prior written permission.
  43.  *
  44.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  45.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  48.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  54.  * SUCH DAMAGE.
  55.  */
  56.  
  57. #ifndef lint
  58. /*static char sccsid[] = "from: @@(#)invite.c    5.8 (Berkeley) 3/1/91";*/
  59. static char rcsid[] = "$Id: invite.c,v 1.2 1993/08/01 18:07:49 mycroft Exp $";
  60. #endif /* not lint */
  61.  
  62. #include <sys/types.h>
  63. #include <sys/socket.h>
  64. #include <sys/time.h>
  65. #include <signal.h>
  66. #include <netinet/in.h>
  67. #include <protocols/talkd.h>
  68. #include <errno.h>
  69. #include <setjmp.h>
  70. #include "talk_ctl.h"
  71. #include "talk.h"
  72.  
  73. /*
  74.  * There wasn't an invitation waiting, so send a request containing
  75.  * our sockt address to the remote talk daemon so it can invite
  76.  * him 
  77.  */
  78.  
  79. /*
  80.  * The msg.id's for the invitations
  81.  * on the local and remote machines.
  82.  * These are used to delete the 
  83.  * invitations.
  84.  */
  85. int    local_id, remote_id;
  86. void    re_invite();
  87. jmp_buf invitebuf;
  88.  
  89. invite_remote()
  90. {
  91.     int nfd, read_mask, template, new_sockt;
  92.     struct itimerval itimer;
  93.     CTL_RESPONSE response;
  94.  
  95.     itimer.it_value.tv_sec = RING_WAIT;
  96.     itimer.it_value.tv_usec = 0;
  97.     itimer.it_interval = itimer.it_value;
  98.     if (listen(sockt, 5) != 0)
  99.         p_error("Error on attempt to listen for caller");
  100. #ifdef MSG_EOR
  101.     /* copy new style sockaddr to old, swap family (short in old) */
  102.     msg.addr = *(struct osockaddr *)&my_addr;  /* XXX new to old  style*/
  103.     msg.addr.sa_family = htons(my_addr.sin_family);
  104. #else
  105.     msg.addr = *(struct sockaddr *)&my_addr;
  106. #endif
  107.     msg.id_num = htonl(-1);        /* an impossible id_num */
  108.     invitation_waiting = 1;
  109.     announce_invite();
  110.     /*
  111.      * Shut off the automatic messages for a while,
  112.      * so we can use the interupt timer to resend the invitation
  113.      */
  114.     end_msgs();
  115.     setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
  116.     message("Waiting for your party to respond");
  117.     signal(SIGALRM, re_invite);
  118.     (void) setjmp(invitebuf);
  119.     while ((new_sockt = accept(sockt, 0, 0)) < 0) {
  120.         if (errno == EINTR)
  121.             continue;
  122.         p_error("Unable to connect with your party");
  123.     }
  124.     close(sockt);
  125.     sockt = new_sockt;
  126.  
  127.     /*
  128.      * Have the daemons delete the invitations now that we
  129.      * have connected.
  130.      */
  131.     current_state = "Waiting for your party to respond";
  132.     start_msgs();
  133.  
  134.     msg.id_num = htonl(local_id);
  135.     ctl_transact(my_machine_addr, msg, DELETE, &response);
  136.     msg.id_num = htonl(remote_id);
  137.     ctl_transact(his_machine_addr, msg, DELETE, &response);
  138.     invitation_waiting = 0;
  139. }
  140.  
  141. /*
  142.  * Routine called on interupt to re-invite the callee
  143.  */
  144. void
  145. re_invite()
  146. {
  147.  
  148.     message("Ringing your party again");
  149.     current_line++;
  150.     /* force a re-announce */
  151.     msg.id_num = htonl(remote_id + 1);
  152.     announce_invite();
  153.     longjmp(invitebuf, 1);
  154. }
  155.  
  156. static    char *answers[] = {
  157.     "answer #0",                    /* SUCCESS */
  158.     "Your party is not logged on",            /* NOT_HERE */
  159.     "Target machine is too confused to talk to us",    /* FAILED */
  160.     "Target machine does not recognize us",        /* MACHINE_UNKNOWN */
  161.     "Your party is refusing messages",        /* PERMISSION_REFUSED */
  162.     "Target machine can not handle remote talk",    /* UNKNOWN_REQUEST */
  163.     "Target machine indicates protocol mismatch",    /* BADVERSION */
  164.     "Target machine indicates protocol botch (addr)",/* BADADDR */
  165.     "Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
  166. };
  167. #define    NANSWERS    (sizeof (answers) / sizeof (answers[0]))
  168.  
  169. /*
  170.  * Transmit the invitation and process the response
  171.  */
  172. announce_invite()
  173. {
  174.     CTL_RESPONSE response;
  175.  
  176.     current_state = "Trying to connect to your party's talk daemon";
  177.     ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
  178.     remote_id = response.id_num;
  179.     if (response.answer != SUCCESS) {
  180.         if (response.answer < NANSWERS)
  181.             message(answers[response.answer]);
  182.         quit();
  183.     }
  184.     /* leave the actual invitation on my talk daemon */
  185.     ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
  186.     local_id = response.id_num;
  187. }
  188.  
  189. /*
  190.  * Tell the daemon to remove your invitation
  191.  */
  192. send_delete()
  193. {
  194.  
  195.     msg.type = DELETE;
  196.     /*
  197.      * This is just a extra clean up, so just send it
  198.      * and don't wait for an answer
  199.      */
  200.     msg.id_num = htonl(remote_id);
  201.     daemon_addr.sin_addr = his_machine_addr;
  202.     if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
  203.         (struct sockaddr *)&daemon_addr,
  204.         sizeof (daemon_addr)) != sizeof(msg))
  205.         perror("send_delete (remote)");
  206.     msg.id_num = htonl(local_id);
  207.     daemon_addr.sin_addr = my_machine_addr;
  208.     if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
  209.         (struct sockaddr *)&daemon_addr,
  210.         sizeof (daemon_addr)) != sizeof (msg))
  211.         perror("send_delete (local)");
  212. }
  213. @
  214.