|
|
1.1 ! root 1: #include <sys/types.h> ! 2: #include <sys/socket.h> ! 3: #include <netinet/in.h> ! 4: #include <ctype.h> ! 5: #include <netdb.h> ! 6: #include "get_tcp_conn.h" ! 7: ! 8: extern int errno; ! 9: ! 10: /* ! 11: ** Take the name of an internet host in ASCII (this may either be its ! 12: ** official host name or internet number (with or without enclosing ! 13: ** backets [])), and return the internet address number in 32 bit quantity. ! 14: ** ! 15: ** returns FAIL for failure to find the host name in the local database, ! 16: ** or for a bad internet address spec. ! 17: */ ! 18: u_long ! 19: name_to_address(host) ! 20: register char *host; ! 21: { ! 22: if (host == (char *)NULL) ! 23: return(FAIL); ! 24: ! 25: /* ! 26: ** Is this an ASCII internet address? (either of [10.0.0.78] or ! 27: ** 10.0.0.78). ! 28: */ ! 29: if (*host == '[' || isdigit(*host)) { ! 30: u_long host_address; ! 31: char namebuf[128]; ! 32: register char *cp = namebuf; ! 33: ! 34: /* ! 35: ** strip brackets [] or anything else we don't want. ! 36: */ ! 37: while(*host && cp < &namebuf[sizeof(namebuf)]) { ! 38: if (isdigit(*host) || *host == '.') ! 39: *cp++ = *host++; ! 40: else ! 41: host++; ! 42: } ! 43: ! 44: if ((host_address = inet_addr(namebuf)) == FAIL) ! 45: return(FAIL); /* malformed internet address spec */ ! 46: return(host_address); ! 47: } else { ! 48: struct hostent *hstp = gethostbyname(host); ! 49: ! 50: if (hstp == NULL) ! 51: return(FAIL); /* no such host */ ! 52: return(*(u_long *)hstp->h_addr); /* we assume... */ ! 53: } ! 54: } ! 55: ! 56: /* ! 57: ** given a host name (either name or internet address) and service name ! 58: ** (or port number) (both in ASCII), give us a TCP connection to the ! 59: ** requested service at the requested host (or give us FAIL). ! 60: */ ! 61: get_tcp_conn(host,serv) ! 62: char *host; ! 63: char *serv; ! 64: { ! 65: u_short port; ! 66: struct in_addr host_address; ! 67: ! 68: if ((host_address.s_addr = name_to_address(host)) == FAIL) { ! 69: return(NOHOST); ! 70: } ! 71: ! 72: if (isdigit(*serv)) { ! 73: port = htons((u_short)(atoi(serv))); ! 74: } else { ! 75: struct servent *srvp = getservbyname(serv, "tcp"); ! 76: ! 77: if (srvp == NULL) { ! 78: return(NOSERVICE); ! 79: } ! 80: port = (u_short)srvp->s_port; ! 81: } ! 82: ! 83: return(mkconn(&host_address, port, IPPROTO_TCP, SOCK_STREAM)); ! 84: } ! 85: ! 86: /* ! 87: ** create a socket and connect it to a remote host on the specified ! 88: ** port by the specified protocol. Return FAIL if something goes ! 89: ** wrong somewhere. Since these are exclusively system calls, ! 90: ** errno will have the correct error in it. ! 91: */ ! 92: mkconn(host_address, port, protocol, proto_type) ! 93: struct in_addr *host_address; ! 94: u_short port; ! 95: int protocol, proto_type; ! 96: { ! 97: register int skt; ! 98: struct sockaddr_in sadr; ! 99: ! 100: sadr.sin_family = (u_short)AF_INET; /* Only internet for now */ ! 101: sadr.sin_addr.s_addr = host_address->s_addr; ! 102: sadr.sin_port = (u_short)port; ! 103: ! 104: if ((skt = socket(AF_INET, proto_type, protocol)) < 0) ! 105: return(FAIL); ! 106: ! 107: if (connect(skt, &sadr, sizeof(sadr)) < 0) { ! 108: int save = errno; ! 109: ! 110: close(skt); ! 111: errno = save; ! 112: return(FAIL); ! 113: } ! 114: return(skt); ! 115: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.