xref: /original-bsd/usr.bin/talk/get_addrs.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)get_addrs.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <protocols/talkd.h>
16 #include <netdb.h>
17 #include <stdio.h>
18 #include "talk_ctl.h"
19 
20 get_addrs(my_machine_name, his_machine_name)
21 	char *my_machine_name, *his_machine_name;
22 {
23 	struct hostent *hp;
24 	struct servent *sp;
25 
26 	msg.pid = htonl(getpid());
27 	/* look up the address of the local host */
28 	hp = gethostbyname(my_machine_name);
29 	if (hp == NULL) {
30 		fprintf(stderr, "talk: %s: ", my_machine_name);
31 		herror((char *)NULL);
32 		exit(-1);
33 	}
34 	bcopy(hp->h_addr, (char *)&my_machine_addr, hp->h_length);
35 	/*
36 	 * If the callee is on-machine, just copy the
37 	 * network address, otherwise do a lookup...
38 	 */
39 	if (strcmp(his_machine_name, my_machine_name)) {
40 		hp = gethostbyname(his_machine_name);
41 		if (hp == NULL) {
42 			fprintf(stderr, "talk: %s: ", his_machine_name);
43 			herror((char *)NULL);
44 			exit(-1);
45 		}
46 		bcopy(hp->h_addr, (char *) &his_machine_addr, hp->h_length);
47 	} else
48 		his_machine_addr = my_machine_addr;
49 	/* find the server's port */
50 	sp = getservbyname("ntalk", "udp");
51 	if (sp == 0) {
52 		fprintf(stderr, "talk: %s/%s: service is not registered.\n",
53 		     "ntalk", "udp");
54 		exit(-1);
55 	}
56 	daemon_port = sp->s_port;
57 }
58