|
|
1.1 ! root 1: /* $Header: get_addrs.c,v 1.2 85/01/09 13:04:49 rcs Exp $ */ ! 2: ! 3: #include "talk_ctl.h" ! 4: ! 5: struct hostent *gethostbyname(); ! 6: struct servent *getservbyname(); ! 7: int debug; ! 8: ! 9: get_addrs(my_machine_name, his_machine_name) ! 10: char *my_machine_name; ! 11: char *his_machine_name; ! 12: { ! 13: struct hostent *hp; ! 14: struct servent *sp; ! 15: ! 16: msg.ctlm_pid = getpid(); ! 17: ! 18: /* look up the address of the local host */ ! 19: ! 20: hp = gethostbyname(my_machine_name); ! 21: ! 22: if (hp == (struct hostent *) 0) { ! 23: printf("This machine doesn't exist. Boy, am I confused!\n"); ! 24: exit(-1); ! 25: } ! 26: ! 27: if (hp->h_addrtype != AF_INET) { ! 28: printf("Protocal mix up with local machine address\n"); ! 29: exit(-1); ! 30: } ! 31: ! 32: /* bcopy(hp->h_addr, (char *)&my_machine_addr, hp->h_length); */ ! 33: ! 34: ! 35: /* if he is on the same machine, then simply copy */ ! 36: ! 37: if ( bcmp((char *)&his_machine_name, (char *)&my_machine_name, ! 38: sizeof(his_machine_name)) == 0) { ! 39: bcopy(hp->h_addr, (char *)&my_machine_addr, hp->h_length); ! 40: bcopy((char *)&my_machine_addr, (char *)&his_machine_addr, ! 41: sizeof(his_machine_name)); ! 42: ! 43: } else { ! 44: ! 45: /* look up the address of the recipient's machine */ ! 46: ! 47: hp = gethostbyname(his_machine_name); ! 48: ! 49: if (hp == (struct hostent *) 0 ) { ! 50: printf("%s is an unknown host\n", his_machine_name); ! 51: exit(-1); ! 52: } ! 53: ! 54: if (hp->h_addrtype != AF_INET) { ! 55: printf("Protocol mix up with remote machine address\n"); ! 56: exit(-1); ! 57: } ! 58: ! 59: bcopy(hp->h_addr, (char *) &his_machine_addr, hp->h_length); ! 60: } ! 61: ! 62: /* find the daemon portal */ ! 63: ! 64: #ifdef NTALK ! 65: sp = getservbyname("ntalk", "udp"); ! 66: #else ! 67: sp = getservbyname("talk", "udp"); ! 68: #endif ! 69: ! 70: if (strcmp(sp->s_proto, "udp") != 0) { ! 71: printf("Protocol mix up with talk daemon\n"); ! 72: exit(-1); ! 73: } ! 74: ! 75: if (sp == 0) { ! 76: p_error("This machine doesn't support a tcp talk daemon"); ! 77: exit(-1); ! 78: } ! 79: ! 80: daemon_port = sp->s_port; ! 81: ! 82: ! 83: /* figure the default address by connecting to the remote host. ! 84: in case of multi-homed host, default address is not same ! 85: as host name */ ! 86: { ! 87: int tsock; ! 88: int length; ! 89: struct sockaddr_in tdaemon; ! 90: ! 91: ! 92: tdaemon.sin_family = AF_INET; ! 93: tdaemon.sin_addr= his_machine_addr; ! 94: tdaemon.sin_port = daemon_port; ! 95: ! 96: tsock = socket(AF_INET, SOCK_DGRAM, 0, 0); ! 97: if (tsock <= 0) { ! 98: p_error("Bad temporary socket"); ! 99: } ! 100: ! 101: if (connect(tsock, &tdaemon, sizeof(tdaemon)) < 0) ! 102: p_error("Error on connection to the other machine"); ! 103: ! 104: length = sizeof(tdaemon); ! 105: if (getsockname(tsock, &tdaemon, &length) == -1) { ! 106: p_error("Bad address for temporary socket"); ! 107: } ! 108: my_machine_addr = tdaemon.sin_addr; ! 109: if (debug) ! 110: printf("my machine address is %lx", my_machine_addr.s_addr); ! 111: close(tsock); ! 112: } ! 113: ! 114: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.