ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libukcprog/src/ip.c
Revision: 1.2
Committed: Sat Sep 24 13:31:30 2005 UTC (19 years, 3 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +27 -4 lines
Log Message:
Add WIN32 support via MINGW. We'll need to add stuff to the README file
about what this requires to build.

All the hard work done by: skel

File Contents

# User Rev Content
1 tdb 1.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 tdb 1.2 #ifndef WIN32
20 tdb 1.1 #include <sys/socket.h>
21     #include <netinet/in.h>
22     #include <netdb.h>
23 tdb 1.2 #else
24     #include <winsock2.h>
25     #endif
26 tdb 1.1
27     #include <stdlib.h>
28     #include <string.h>
29     #include <stdio.h>
30    
31     #include "ukcprog.h"
32    
33     /* Map a hostname to an IP address. The address is returned in network
34     * byte order. Return 0 for success, -1 and an error message on failure.
35     *
36     * If the hostname looks like an IP quad we convert that to an IP address.
37     */
38     int
39     get_host_addr(hostname, p_addr)
40     const char *hostname;
41     struct in_addr *p_addr;
42     {
43     struct hostent *h;
44     int b3, b2, b1, b0;
45     char c;
46    
47     if (sscanf(hostname, "%d.%d.%d.%d%c", &b3, &b2, &b1, &b0, &c) == 4) {
48 tdb 1.2 unsigned long val;
49 tdb 1.1
50 tdb 1.2 val = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
51     p_addr->s_addr = htonl(val);
52     return 0;
53 tdb 1.1 }
54    
55 tdb 1.2 #ifdef WIN32
56     /* Init the winsock, because we all love windows */
57     WSADATA wsaData;
58    
59     if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
60     errf("Unable to init a winsock");
61     return -1;
62     }
63     #endif
64 tdb 1.1 if ((h = gethostbyname(hostname)) == NULL) {
65     errf("Can't find address of %s", hostname);
66     return -1;
67     }
68    
69     if (h->h_addrtype != AF_INET) {
70     errf("%s has non-IP address (addrtype=%d)",
71     hostname, h->h_addrtype);
72     return -1;
73     }
74    
75     memcpy((char *)p_addr, (char *)h->h_addr_list[0], sizeof(*p_addr));
76     return 0;
77     }
78    
79     /* Map a service name to a port number in network byte order.
80     * If the service name looks like a number we return that.
81     */
82     int
83     get_service_port(servname, p_port)
84     const char *servname;
85     int *p_port;
86     {
87     struct servent *sp;
88     char *endstr;
89     unsigned short hport;
90    
91     hport = strtol(servname, &endstr, 0);
92     if (endstr != servname && *endstr == '\0') {
93     *p_port = htons(hport);
94     return 0;
95     }
96 tdb 1.2
97     #ifdef WIN32
98     /* Init the winsock, because we all love windows */
99     WSADATA wsaData;
100    
101     if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
102     errf("Unable to init a winsock");
103     return -1;
104     }
105     #endif
106 tdb 1.1
107     if ((sp = getservbyname(servname, "tcp")) == NULL) {
108     errf("Unknown service `%s'", servname);
109     return -1;
110     }
111    
112     *p_port = sp->s_port;
113    
114     return 0;
115     }