home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1989 Oki Electric Industry Co., Ltd.
- *
- * This file is part of vtalk.
- *
- * Permission to use, copy, modify and distribute this program without
- * fee is grtanted, provided that the above copyright notice appear
- * in all copies.
- *
- */
- /*
- * netwk.c network control routine for vtalk
- *
- * vtalk is a command for
- * Voice TALK with the user on another machine.
- *
- */
-
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <netdb.h>
- #include <sys/file.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/param.h>
- #include <sys/time.h>
- #include "netwk.h"
-
-
- /* #define BUFFER_SIZE 256 */
- #define BUFFER_SIZE 64 /* record */
- #define INT sizeof(int)
-
-
- struct daemon_msg msg;
-
- struct in_addr machine_addr; /* inet address */
- int machine_addrtype;
- int dsockt; /* socket for daemon */
- int vsockt; /* socket for voice talk */
-
-
- void
- check_args(argc, argv, flag)
- int argc;
- char **argv;
- int *flag;
- {
- char *my_name;
- char his_name[256];
- char *my_host;
- char *his_host;
- char host[MAXHOSTNAMELEN];
- struct in_addr my_host_addr;
- struct in_addr his_host_addr;
- int his_host_addrtype;
- int my_host_addrtype;
- struct hostent *hp;
- char *ap, *np;
- char *getlogin();
-
- /* my name */
- if ((my_name = getlogin()) == NULL) {
- fprintf(stderr,"%s: you don't exist\n", argv[0]);
- exit(1);
- }
- strcpy(msg.caller_name, my_name);
-
- /* my host name */
- gethostname(host, sizeof (host));
- my_host = host;
- strcpy(msg.caller_host_name, host);
-
- if(argc == 1){ /* receiver */
-
- /* my host address */
- if ((hp = gethostbyname(my_host))== (struct hostent *) 0) {
- fprintf(stderr,
- "%s: %s can't recognize network address.\n",
- argv[0], my_host);
- exit(1);
- }
- bcopy(hp->h_addr, (char *)&my_host_addr,
- hp->h_length);
- my_host_addrtype = hp->h_addrtype;
- *flag = 0;
- machine_addr = my_host_addr;
- machine_addrtype = my_host_addrtype;
-
-
- }else { /* caller */
-
- /* his name & his host name */
- ap = argv[1];
- np = his_name;
- while(*ap != '\0'){
- *np = *ap;
- if (*ap == '@') {
- *np = '\0';
- his_host = ++ap;
- break;
- } ap++;np++;
- }
- if(*ap == '\0') {
- fprintf(stderr,"Usage: vtalk user@machine\n");
- exit(1);
- }
- strcpy(msg.receiver_name, his_name);
-
- /* his host addrs */
- if ((hp = gethostbyname(his_host)) == (struct hostent *) 0) {
- fprintf(stderr,
- "%s: %s can't recognize network address.\n",
- argv[0], his_host);
- exit(1);
- }
- bcopy(hp->h_addr, (char *) &his_host_addr,
- hp->h_length);
- his_host_addrtype = hp->h_addrtype;
-
- machine_addrtype = his_host_addrtype;
- machine_addr = his_host_addr;
- *flag = 1; /* caller */
- msg.sin.sin_addr = my_host_addr;
- }
-
- }
-
-
- void
- open_dsocket()
- {
- struct sockaddr_in daemon; /* daemon sockname */
- struct servent* sp; /* server entry */
-
- #ifdef DEBUG
- fprintf(stderr,"vtalk:open_dsocket\n");
- #endif DEBUG
-
- /* vtalk server entry */
- if((sp = getservbyname("vtalk","tcp")) == NULL){
- fprintf(stderr,
- "vtalk: undefined server entry\n");
- exit(1);
- }
- bzero((char *)&daemon, sizeof(daemon));
- daemon.sin_port = sp->s_port;
- daemon.sin_addr = machine_addr;
- daemon.sin_family = machine_addrtype;
-
- /* SOCKET for server */
- if((dsockt = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- perror("vtalk: can't create socket");
- exit(1);
- }
-
- /* CONNECT to server */
- if(connect(dsockt, (struct sockaddr *)&daemon, sizeof(daemon))
- < 0){
- perror("vtalk: binding local socket");
- exit(1);
- }
-
- }
-
-
- call_vtalkd(caller)
- int caller;
- {
-
- #ifdef DEBUG
- fprintf(stderr,"vtalk:call vtalk daemon\n");
- #endif DEBUG
-
- /* WRITE msg to server */
- msg.flag = caller;
- if(write(dsockt, (char*)&msg, sizeof(struct daemon_msg)) !=
- sizeof(struct daemon_msg)){
- perror("vtalk: lost the connection with daemon");
- exit(1);
- }
-
- /* READ msg from server */
- if(read(dsockt, (char*)&msg, sizeof(struct daemon_msg)) !=
- sizeof(struct daemon_msg)){
- perror("vtalk: lost the connection with daemon");
- exit(1);
- }
-
- /* CLOSE daemon socket */
- if(close(dsockt)){
- perror("vtalk: can't close socket\n");
- }
-
- return(msg.status);
- }
-
-
-
- void
- call()
- {
- struct sockaddr_in sin;
- int sd;
-
- #ifdef DEBUG
- fprintf(stderr,"vtalk:open vtalk socket\n");
- #endif DEBUG
-
- /* SOCKET for vtalk */
- if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- perror("vtalk: can't create socket");
- exit(1);
- }
-
- /* BIND socket to given port no */
- bzero((char *)&sin, sizeof(sin));
- sin.sin_port = msg.sin.sin_port;
-
- #ifdef DEBUG
- fprintf(stderr,"vtalk:sin=%d %d %d \n",sin.sin_port,
- sin.sin_addr,sin.sin_family);
- #endif DEBUG
-
- if(bind(sd, (struct sockaddr*)&sin, sizeof(sin))
- < 0){
-
- #ifdef DEBUG
- fprintf(stderr,"vtalk:bind:errno = %d\n",errno);
- #endif DEBUG
-
- perror("vtalk: binding local socket");
- exit(1);
- }
-
- /* LISTEN */
- listen(sd, 5);
-
- #ifdef DEBUG
- fprintf(stderr,"vtalk:caller:listen\n");
- #endif
-
- /* waiting until ACCEPT */
- while((vsockt = accept(sd, 0, 0)) < 0){
- if(errno == EINTR)
- continue;
- perror("vtalk: can't accept");
- }
-
- #ifdef DEBUG
- fprintf(stderr,"vtalk:caller:accepted\n");
- #endif
- /* CLOSE old sockt */
- close(sd);
-
- }
-
-
- void
- response()
- {
- struct sockaddr_in sin;
-
- #ifdef DEBUG
- fprintf(stderr,"vtalk:open vtalk socket\n");
- #endif DEBUG
-
- /* SOCKET for vtalk */
- if((vsockt = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- perror("vtalk: can't create socket");
- exit(1);
- }
-
- /* CONNECT to waiting caller */
- sin.sin_family = msg.sin.sin_family;
- sin.sin_port = msg.sin.sin_port;
- sin.sin_addr = msg.sin.sin_addr;
- if(connect(vsockt, (struct sockaddr*)&sin,
- sizeof(sin)) < 0){
- perror("vtalk: can't connect");
- exit(1);
- }
- fprintf(stderr,"Connection established, finish with CTRL/C..");
- #ifdef DEBUG
- fprintf(stderr,"vtalk:receiver:connected\n");
- #endif
- }
-
-
- void
- vtalk(audio_fd, caller)
- int audio_fd;
- int caller;
- {
- int read_size;
- char audio_buffer[BUFFER_SIZE];
- char sockt_buffer[BUFFER_SIZE];
-
- read_size = BUFFER_SIZE;
- if(caller){
- while(1) {
- read(vsockt, &sockt_buffer[0], read_size);
- write(audio_fd, &sockt_buffer[0], read_size);
- read(audio_fd, &audio_buffer[0], read_size);
- write(vsockt, &audio_buffer[0], read_size);
- }
- }else{
- while(1){
- read(audio_fd, &audio_buffer[0], read_size);
- write(vsockt, &audio_buffer[0], read_size);
- read(vsockt, &sockt_buffer[0], read_size);
- write(audio_fd, &sockt_buffer[0], read_size);
- }
- }
- }
-
-
- static char *messages[] = {
- "",
- "Receiver is not existing",
- "Receiver machine is too confused to vtalk",
- "Receiver machine cannot recognize us",
- "Receiver is refusing messages",
- "Nobody is requesting your reply",
- };
- #define NANSWERS (sizeof(messages) /sizeof (messages[0]))
-
-
- output_err(status)
- int status;
- {
- if(status < NANSWERS)
- fprintf(stderr,"[%s]\n",messages[status]);
-
- }
-
-