File:  [Power 6/32 Unix Tahoe 4.2BSD] / cci / usr / src / ucb / talk / get_addrs.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Sun Jul 28 12:29:26 2019 UTC (7 years ago) by root
Branches: bsd, MAIN
CVS tags: v121, HEAD
Power 6/32 Unix version 1.21

/* $Header: /var/lib/cvsd/repos/cci/cci/usr/src/ucb/talk/get_addrs.c,v 1.1.1.1 2019/07/28 12:29:26 root Exp $ */

#include "talk_ctl.h"

struct hostent *gethostbyname();
struct servent *getservbyname();
int	debug;

get_addrs(my_machine_name, his_machine_name)
char *my_machine_name;
char *his_machine_name;
{
    struct hostent *hp;
    struct servent *sp;

    msg.ctlm_pid = getpid();

	/* look up the address of the local host */

    hp = gethostbyname(my_machine_name);

    if (hp == (struct hostent *) 0) {
	printf("This machine doesn't exist. Boy, am I confused!\n");
	exit(-1);
    }

    if (hp->h_addrtype != AF_INET) {
	printf("Protocal mix up with local machine address\n");
	exit(-1);
    }

    /* bcopy(hp->h_addr, (char *)&my_machine_addr, hp->h_length); */
    

	/* if he is on the same machine, then simply copy */

    if ( bcmp((char *)&his_machine_name, (char *)&my_machine_name,
		sizeof(his_machine_name)) == 0) {
        bcopy(hp->h_addr, (char *)&my_machine_addr, hp->h_length);
    	bcopy((char *)&my_machine_addr, (char *)&his_machine_addr,
    		sizeof(his_machine_name));
    
    } else {

	/* look up the address of the recipient's machine */

	hp = gethostbyname(his_machine_name);

	if (hp == (struct hostent *) 0 ) {
	    printf("%s is an unknown host\n", his_machine_name);
	    exit(-1);
	}

	if (hp->h_addrtype != AF_INET) {
	    printf("Protocol mix up with remote machine address\n");
	    exit(-1);
	}

	bcopy(hp->h_addr, (char *) &his_machine_addr, hp->h_length);
    }

	/* find the daemon portal */

#ifdef NTALK
    sp = getservbyname("ntalk", "udp");
#else
    sp = getservbyname("talk", "udp");
#endif

    if (strcmp(sp->s_proto, "udp") != 0) {
	printf("Protocol mix up with talk daemon\n");
	exit(-1);
    }

    if (sp == 0) {
	    p_error("This machine doesn't support a tcp talk daemon");
	    exit(-1);
    }

    daemon_port = sp->s_port;

   
    /* figure the default address by connecting to the remote host.
       in case of multi-homed host, default address is not same 
       as host name */
    {
	int	tsock;
	int	length;
	struct sockaddr_in tdaemon;


        tdaemon.sin_family = AF_INET;
        tdaemon.sin_addr= his_machine_addr;
	tdaemon.sin_port = daemon_port;

	tsock = socket(AF_INET, SOCK_DGRAM, 0, 0);
	if (tsock <= 0) {
		p_error("Bad temporary socket");
	}

	if (connect(tsock, &tdaemon, sizeof(tdaemon)) < 0)
			p_error("Error on connection to the other machine");

        length = sizeof(tdaemon);
        if (getsockname(tsock, &tdaemon, &length) == -1) {
		p_error("Bad address for temporary socket");
        }
	my_machine_addr = tdaemon.sin_addr;
	if (debug)
		printf("my machine address is %lx", my_machine_addr.s_addr);
	close(tsock);
    }

}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.