]>
Commit | Line | Data |
---|---|---|
75072239 AJ |
1 | #include "config.h" |
2 | ||
eb1f6bfa | 3 | // |
7962bc6a | 4 | // $Id$ |
eb1f6bfa | 5 | // |
0b96a9b3 | 6 | // Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de> |
eb1f6bfa AJ |
7 | // |
8 | // File: convert.cc | |
9 | // Thu Oct 30 1997 | |
10 | // | |
11 | // (c) 1997 Lehrgebiet Rechnernetze und Verteilte Systeme | |
0b96a9b3 | 12 | // Universit?t Hannover, Germany |
eb1f6bfa AJ |
13 | // |
14 | // Permission to use, copy, modify, distribute, and sell this software | |
15 | // and its documentation for any purpose is hereby granted without fee, | |
16 | // provided that (i) the above copyright notices and this permission | |
17 | // notice appear in all copies of the software and related documentation, | |
18 | // and (ii) the names of the Lehrgebiet Rechnernetze und Verteilte | |
19 | // Systeme and the University of Hannover may not be used in any | |
20 | // advertising or publicity relating to the software without the | |
21 | // specific, prior written permission of Lehrgebiet Rechnernetze und | |
22 | // Verteilte Systeme and the University of Hannover. | |
23 | // | |
24 | // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, | |
25 | // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY | |
26 | // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | |
27 | // | |
28 | // IN NO EVENT SHALL THE LEHRGEBIET RECHNERNETZE UND VERTEILTE SYSTEME OR | |
29 | // THE UNIVERSITY OF HANNOVER BE LIABLE FOR ANY SPECIAL, INCIDENTAL, | |
30 | // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES | |
31 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT | |
32 | // ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, | |
33 | // ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
34 | // SOFTWARE. | |
35 | // | |
eb1f6bfa AJ |
36 | // Revision 1.3 2000/06/20 09:43:01 voeckler |
37 | // added FreeBSD related fixes and support. | |
38 | // | |
39 | // Revision 1.2 1999/01/19 11:00:50 voeckler | |
40 | // Linux glibc2 fixes for sockets. | |
41 | // | |
42 | // Revision 1.1 1998/08/13 21:38:04 voeckler | |
43 | // Initial revision | |
44 | // | |
45 | // | |
46 | #if defined(__GNUC__) || defined(__GNUG__) | |
47 | #pragma implementation | |
48 | #endif | |
49 | ||
50 | #include "convert.hh" | |
51 | #include <string.h> | |
52 | #include <netinet/in.h> | |
53 | #include <arpa/inet.h> | |
54 | #include <netdb.h> | |
55 | #include <stdlib.h> | |
56 | #include <stdio.h> | |
57 | ||
58 | #ifndef SA | |
59 | #define SA struct sockaddr | |
60 | #endif | |
61 | ||
eb1f6bfa AJ |
62 | const char* |
63 | my_inet_ntoa( const struct in_addr& a, HostAddress output ) | |
feec68a0 A |
64 | // purpose: thread-safely convert IPv4 address -> ASCII representation |
65 | // paramtr: a (IN): networked representation of IPv4 address | |
66 | // buffer (OUT): storage area to store representation into. | |
67 | // returns: pointer to buffer | |
68 | // goodies: INADDR_ANY will be converted to "*" | |
eb1f6bfa | 69 | { |
feec68a0 A |
70 | if ( a.s_addr == ntohl(INADDR_ANY) ) { |
71 | // 'default' or '*' or ... | |
72 | output[0] = '*'; | |
73 | output[1] = '\0'; | |
74 | } else { | |
75 | // ANSI C++ forbids casting to an array type, nag, nag, nag... | |
76 | unsigned char s[sizeof(a.s_addr)]; | |
77 | memcpy( s, &a.s_addr, sizeof(a.s_addr) ); | |
eb1f6bfa | 78 | |
75072239 | 79 | snprintf(output, sizeof(HostAddress), "%d.%d.%d.%d", s[0], s[1], s[2], s[3] ); |
feec68a0 A |
80 | } |
81 | return output; | |
eb1f6bfa AJ |
82 | } |
83 | ||
84 | const char* | |
85 | my_sock_ntoa( const struct sockaddr_in& a, SockAddress buffer ) | |
feec68a0 A |
86 | // purpose: thread-safely convert IPv4 socket pair into ASCII rep. |
87 | // paramtr: a (IN): sockaddr_in address | |
88 | // buffer (OUT): storage area to store representation into. | |
89 | // returns: pointer to buffer | |
eb1f6bfa | 90 | { |
feec68a0 | 91 | HostAddress host; |
75072239 | 92 | snprintf( buffer, sizeof(SockAddress), "%s:%u", |
b038cb7b | 93 | my_inet_ntoa(a.sin_addr,host), ntohs(a.sin_port) ); |
feec68a0 | 94 | return buffer; |
eb1f6bfa AJ |
95 | } |
96 | ||
97 | const char* | |
98 | my_sock_fd2a( int fd, SockAddress buffer, bool peer ) | |
feec68a0 A |
99 | // purpose: thread-safely convert IPv4 socket FD associated address |
100 | // to ASCII representation | |
101 | // paramtr: fd (IN): open socket FD | |
102 | // buffer (OUT): storage area | |
103 | // peer (IN): true, use peer (remote) socket pair | |
104 | // false, use own (local) socket pair | |
105 | // returns: NULL in case of error, or pointer to buffer otherwise | |
106 | // Refer to errno in case of error (usually unconnected fd...) | |
eb1f6bfa | 107 | { |
feec68a0 | 108 | struct sockaddr_in socket; |
2ccf2eb2 | 109 | socklen_t len = sizeof(socket); |
eb1f6bfa | 110 | |
feec68a0 A |
111 | if ( (peer ? getpeername( fd, (SA*) &socket, &len ) : |
112 | getsockname( fd, (SA*) &socket, &len )) == -1 ) | |
113 | return NULL; | |
114 | else | |
115 | return my_sock_ntoa( socket, buffer ); | |
eb1f6bfa AJ |
116 | } |
117 | ||
118 | int | |
119 | convertHostname( const char* host, in_addr& dst ) | |
feec68a0 A |
120 | // purpose: convert a numeric or symbolic hostname |
121 | // paramtr: host (IN): host description to convert | |
122 | // dst (OUT): the internet address in network byteorder. | |
123 | // returns: -1 in case of error, see h_errno; 0 otherwise. | |
eb1f6bfa | 124 | { |
feec68a0 A |
125 | if ( host == 0 ) return -1; |
126 | unsigned long int h = inet_addr(host); | |
127 | if ( h == 0xFFFFFFFF && strncmp(host,"255.255.255.255",15) != 0 ) { | |
128 | // symbolic host | |
129 | struct hostent* dns = gethostbyname(host); | |
130 | if ( dns == NULL ) return -1; | |
131 | else memcpy( &dst.s_addr, dns->h_addr, dns->h_length ); | |
132 | } else { | |
133 | // numeric host | |
134 | dst.s_addr = h; | |
135 | } | |
136 | return 0; | |
eb1f6bfa AJ |
137 | } |
138 | ||
139 | int | |
140 | convertPortname( const char* port, unsigned short& dst ) | |
feec68a0 A |
141 | // purpose: convert a numeric or symbolic port number |
142 | // paramtr: port (IN): port description to convert | |
143 | // dst (OUT): port number in network byteorder. | |
144 | // returns: -1 in case of error, see errno; 0 otherwise. | |
eb1f6bfa | 145 | { |
feec68a0 | 146 | int p = strtoul(port,0,0); |
eb1f6bfa | 147 | |
feec68a0 A |
148 | if ( p == 0 ) { |
149 | // symbolic port | |
150 | struct servent* proto = getservbyname( port, "tcp" ); | |
151 | if ( proto == NULL ) return -1; | |
152 | else dst = proto->s_port; | |
153 | } else { | |
154 | // numeric port | |
155 | dst = htons(p); | |
156 | } | |
157 | return 0; | |
eb1f6bfa | 158 | } |