ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libukcprog/ip.c
Revision: 1.1
Committed: Fri Mar 8 14:37:29 2002 UTC (22 years, 8 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: IHOST_1_5_3, IHOST_1_5_2, IHOST_1_5_1, IHOST_1_5, IHOST_1_0_RC1
Log Message:
I'm not usually up for putting third party sources in here, but in this
case I'll make an exception. This is ukcprog, a set of useful C functions
which the ihost plugins Pete's writing uses. It's got a pretty free license
too. I've munged the Makefile around, as all it needs to do now is make the
library, not install anything. The idea is to statically compile the other
programs against this library, making the final binary independent of this
code etc.

File Contents

# Content
1 /* ip.c - map hostnames */
2
3 /* Copyright 1992 Mark Russell, University of Kent at Canterbury.
4 *
5 * You can do what you like with this source code as long as
6 * you don't try to make money out of it and you include an
7 * unaltered copy of this message (including the copyright).
8 */
9
10 char ukcprog_ip_sccsid[] = "$Id: ip.c,v 1.7 1995/12/20 15:31:00 mtr Exp $ UKC";
11
12 #undef _POSIX_SOURCE
13
14 #ifndef MSDOS
15 #include <sys/types.h>
16 #else
17 #include <sys/tk_types.h>
18 #endif
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26
27 #include "ukcprog.h"
28
29 /* Map a hostname to an IP address. The address is returned in network
30 * byte order. Return 0 for success, -1 and an error message on failure.
31 *
32 * If the hostname looks like an IP quad we convert that to an IP address.
33 */
34 int
35 get_host_addr(hostname, p_addr)
36 const char *hostname;
37 struct in_addr *p_addr;
38 {
39 struct hostent *h;
40 int b3, b2, b1, b0;
41 char c;
42
43 if (sscanf(hostname, "%d.%d.%d.%d%c", &b3, &b2, &b1, &b0, &c) == 4) {
44 unsigned long val;
45
46 val = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
47 p_addr->s_addr = htonl(val);
48 return 0;
49 }
50
51 if ((h = gethostbyname(hostname)) == NULL) {
52 errf("Can't find address of %s", hostname);
53 return -1;
54 }
55
56 if (h->h_addrtype != AF_INET) {
57 errf("%s has non-IP address (addrtype=%d)",
58 hostname, h->h_addrtype);
59 return -1;
60 }
61
62 memcpy((char *)p_addr, (char *)h->h_addr_list[0], sizeof(*p_addr));
63 return 0;
64 }
65
66 /* Map a service name to a port number in network byte order.
67 * If the service name looks like a number we return that.
68 */
69 int
70 get_service_port(servname, p_port)
71 const char *servname;
72 int *p_port;
73 {
74 struct servent *sp;
75 char *endstr;
76 unsigned short hport;
77
78 hport = strtol(servname, &endstr, 0);
79 if (endstr != servname && *endstr == '\0') {
80 *p_port = htons(hport);
81 return 0;
82 }
83
84 if ((sp = getservbyname(servname, "tcp")) == NULL) {
85 errf("Unknown service `%s'", servname);
86 return -1;
87 }
88
89 *p_port = sp->s_port;
90
91 return 0;
92 }