home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2375 / netwk.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  7KB  |  339 lines

  1. /* 
  2.  *   Copyright (c) 1989 Oki Electric Industry Co., Ltd.
  3.  *
  4.  *   This file is part of vtalk.
  5.  *
  6.  *   Permission to use, copy, modify and distribute this program without
  7.  *   fee is grtanted,  provided that  the above  copyright notice appear
  8.  *   in all copies.
  9.  *
  10.  */
  11. /*
  12.  *  netwk.c    network control routine for vtalk
  13.  *
  14.  *  vtalk is a command for 
  15.  *  Voice TALK with the user on another machine.
  16.  *  
  17.  */
  18.  
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <netdb.h>
  23. #include <sys/file.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <sys/param.h>
  28. #include <sys/time.h>
  29. #include "netwk.h"
  30.  
  31.  
  32. /* #define BUFFER_SIZE    256    */
  33. #define BUFFER_SIZE    64    /* record */
  34. #define INT sizeof(int)
  35.  
  36.  
  37. struct daemon_msg msg;
  38.  
  39. struct in_addr machine_addr; /* inet address */
  40. int machine_addrtype;
  41. int dsockt;        /* socket for daemon */
  42. int vsockt;        /* socket for voice talk */
  43.  
  44.  
  45. void
  46.   check_args(argc, argv, flag)
  47. int argc;
  48. char **argv;
  49. int *flag;
  50. {
  51.     char *my_name;
  52.     char his_name[256];
  53.     char *my_host;
  54.     char *his_host;
  55.     char host[MAXHOSTNAMELEN];
  56.     struct in_addr my_host_addr;
  57.     struct in_addr his_host_addr;
  58.     int his_host_addrtype;
  59.     int my_host_addrtype;
  60.     struct hostent *hp;
  61.     char *ap, *np;
  62.     char *getlogin();
  63.     
  64.     /* my name */
  65.     if ((my_name = getlogin()) == NULL) {
  66.         fprintf(stderr,"%s: you don't exist\n", argv[0]);
  67.         exit(1);
  68.     }
  69.     strcpy(msg.caller_name, my_name);
  70.     
  71.     /* my host name */
  72.     gethostname(host, sizeof (host));
  73.     my_host = host;
  74.     strcpy(msg.caller_host_name, host);
  75.     
  76.     if(argc == 1){        /* receiver */
  77.         
  78.     /* my host address */
  79.         if ((hp = gethostbyname(my_host))== (struct hostent *) 0) {
  80.             fprintf(stderr,
  81.                 "%s: %s can't recognize network address.\n",
  82.                 argv[0], my_host);
  83.             exit(1);
  84.         }
  85.         bcopy(hp->h_addr, (char *)&my_host_addr,
  86.               hp->h_length);
  87.         my_host_addrtype = hp->h_addrtype;
  88.         *flag = 0;
  89.         machine_addr = my_host_addr;
  90.         machine_addrtype = my_host_addrtype;
  91.         
  92.         
  93.     }else {                 /* caller */
  94.         
  95.     /* his name & his host name */
  96.         ap = argv[1];
  97.         np = his_name;
  98.         while(*ap != '\0'){
  99.             *np = *ap;
  100.             if (*ap == '@') {
  101.                 *np = '\0';
  102.                 his_host = ++ap;
  103.                 break;
  104.             } ap++;np++;
  105.         }
  106.         if(*ap == '\0') {
  107.             fprintf(stderr,"Usage: vtalk user@machine\n");
  108.             exit(1);
  109.         }
  110.         strcpy(msg.receiver_name, his_name);
  111.         
  112.     /* his host addrs */
  113.         if ((hp = gethostbyname(his_host)) == (struct hostent *) 0) {
  114.             fprintf(stderr,
  115.                 "%s: %s can't recognize network address.\n",
  116.                 argv[0], his_host);
  117.             exit(1);
  118.         }
  119.         bcopy(hp->h_addr, (char *) &his_host_addr, 
  120.               hp->h_length);
  121.         his_host_addrtype = hp->h_addrtype;
  122.         
  123.         machine_addrtype = his_host_addrtype;
  124.         machine_addr = his_host_addr;
  125.         *flag = 1;       /* caller */
  126.         msg.sin.sin_addr = my_host_addr;
  127.     }
  128.     
  129. }
  130.  
  131.  
  132. void
  133.   open_dsocket()
  134. {
  135.     struct sockaddr_in daemon; /* daemon sockname */
  136.     struct servent* sp;        /* server entry */
  137.     
  138. #ifdef DEBUG
  139.     fprintf(stderr,"vtalk:open_dsocket\n");
  140. #endif DEBUG
  141.     
  142.     /* vtalk server entry */
  143.     if((sp = getservbyname("vtalk","tcp")) == NULL){
  144.         fprintf(stderr,
  145.             "vtalk: undefined server entry\n");
  146.         exit(1);
  147.     }
  148.     bzero((char *)&daemon, sizeof(daemon));
  149.     daemon.sin_port = sp->s_port;
  150.     daemon.sin_addr = machine_addr;
  151.     daemon.sin_family = machine_addrtype;
  152.     
  153.     /* SOCKET for server */
  154.     if((dsockt = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  155.         perror("vtalk: can't create socket");
  156.         exit(1);
  157.     }
  158.     
  159.     /* CONNECT to server */
  160.     if(connect(dsockt, (struct sockaddr *)&daemon, sizeof(daemon))
  161.        < 0){
  162.         perror("vtalk: binding local socket");
  163.         exit(1);
  164.     }
  165.     
  166. }
  167.  
  168.  
  169. call_vtalkd(caller)
  170.      int caller;
  171. {
  172.     
  173. #ifdef DEBUG
  174.     fprintf(stderr,"vtalk:call vtalk daemon\n");
  175. #endif DEBUG
  176.     
  177.     /* WRITE msg to server */
  178.     msg.flag = caller;
  179.     if(write(dsockt, (char*)&msg, sizeof(struct daemon_msg)) !=
  180.        sizeof(struct daemon_msg)){
  181.         perror("vtalk: lost the connection with daemon");
  182.         exit(1);
  183.     }
  184.  
  185.     /* READ msg from server */
  186.     if(read(dsockt, (char*)&msg, sizeof(struct daemon_msg)) !=
  187.        sizeof(struct daemon_msg)){
  188.         perror("vtalk: lost the connection with daemon");
  189.         exit(1);
  190.     }
  191.     
  192.     /* CLOSE daemon socket */
  193.     if(close(dsockt)){
  194.         perror("vtalk: can't close socket\n");
  195.     }
  196.  
  197.     return(msg.status);
  198. }
  199.  
  200.  
  201.  
  202. void
  203.   call()
  204. {
  205.     struct sockaddr_in sin;
  206.     int sd;  
  207.  
  208. #ifdef DEBUG 
  209.     fprintf(stderr,"vtalk:open vtalk socket\n");
  210. #endif DEBUG
  211.     
  212.     /* SOCKET for vtalk */
  213.     if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  214.         perror("vtalk: can't create socket");
  215.         exit(1);
  216.     }
  217.  
  218.     /* BIND socket to given port no */
  219.     bzero((char *)&sin, sizeof(sin));
  220.     sin.sin_port = msg.sin.sin_port; 
  221.  
  222. #ifdef DEBUG
  223.     fprintf(stderr,"vtalk:sin=%d %d %d \n",sin.sin_port,
  224.         sin.sin_addr,sin.sin_family);
  225. #endif DEBUG
  226.     
  227.     if(bind(sd, (struct sockaddr*)&sin, sizeof(sin))
  228.        < 0){
  229.  
  230. #ifdef DEBUG
  231.         fprintf(stderr,"vtalk:bind:errno = %d\n",errno);
  232. #endif DEBUG
  233.         
  234.         perror("vtalk: binding local socket");
  235.         exit(1);
  236.     }
  237.     
  238.     /* LISTEN */
  239.     listen(sd, 5);
  240.     
  241. #ifdef DEBUG
  242.     fprintf(stderr,"vtalk:caller:listen\n");
  243. #endif
  244.  
  245.     /* waiting until ACCEPT */ 
  246.     while((vsockt = accept(sd, 0, 0)) < 0){
  247.         if(errno == EINTR)
  248.           continue;
  249.         perror("vtalk: can't accept");
  250.     }
  251.  
  252. #ifdef DEBUG
  253.     fprintf(stderr,"vtalk:caller:accepted\n");
  254. #endif
  255.     /* CLOSE old sockt */
  256.     close(sd);
  257.     
  258. }
  259.  
  260.  
  261. void
  262.   response()
  263. {
  264.     struct sockaddr_in sin;
  265.  
  266. #ifdef DEBUG 
  267.     fprintf(stderr,"vtalk:open vtalk socket\n");
  268. #endif DEBUG
  269.     
  270.     /* SOCKET for vtalk */
  271.     if((vsockt = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  272.         perror("vtalk: can't create socket");
  273.         exit(1);
  274.     }
  275.  
  276.     /* CONNECT to waiting caller */
  277.     sin.sin_family = msg.sin.sin_family;
  278.     sin.sin_port = msg.sin.sin_port;
  279.     sin.sin_addr = msg.sin.sin_addr;
  280.     if(connect(vsockt, (struct sockaddr*)&sin, 
  281.            sizeof(sin)) < 0){
  282.         perror("vtalk: can't connect");
  283.         exit(1);
  284.     }    
  285.         fprintf(stderr,"Connection established, finish with CTRL/C..");
  286. #ifdef DEBUG
  287.     fprintf(stderr,"vtalk:receiver:connected\n");
  288. #endif
  289. }
  290.  
  291.  
  292. void
  293.   vtalk(audio_fd, caller)
  294. int audio_fd;
  295. int caller;
  296. {
  297.     int read_size;
  298.     char audio_buffer[BUFFER_SIZE];
  299.     char sockt_buffer[BUFFER_SIZE];
  300.     
  301.     read_size = BUFFER_SIZE;
  302.     if(caller){
  303.         while(1) {
  304.             read(vsockt, &sockt_buffer[0], read_size);
  305.             write(audio_fd, &sockt_buffer[0], read_size);
  306.             read(audio_fd, &audio_buffer[0], read_size);
  307.             write(vsockt, &audio_buffer[0], read_size);
  308.         }
  309.         }else{
  310.         while(1){
  311.             read(audio_fd, &audio_buffer[0], read_size);
  312.             write(vsockt, &audio_buffer[0], read_size);
  313.             read(vsockt, &sockt_buffer[0], read_size);
  314.             write(audio_fd, &sockt_buffer[0], read_size);
  315.         }
  316.         }
  317. }
  318.  
  319.  
  320. static char *messages[] = {
  321.     "",
  322.     "Receiver is not existing",
  323.     "Receiver machine is too confused to vtalk",
  324.     "Receiver machine cannot recognize us",
  325.     "Receiver is refusing messages",
  326.     "Nobody is requesting your reply",
  327. };
  328. #define NANSWERS  (sizeof(messages) /sizeof (messages[0]))
  329.  
  330.  
  331. output_err(status)
  332.      int status;
  333. {
  334.     if(status < NANSWERS)
  335.       fprintf(stderr,"[%s]\n",messages[status]);
  336.     
  337. }
  338.  
  339.