summaryrefslogtreecommitdiff
path: root/src/port/netdb.c
blob: ea4458e7c9fc5db1f35f23e7872ee4ad78b9ea9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "port/netdb.h"

#if !defined HAVE_GETADDRINFO || defined TEST_GETADDRINFO

#include "port/string.h"			/* for memset, memcpy */
#include "port/stdlib.h"			/* for atoi, itoa, malloc, free, NULL */
#include "port/stdio.h"				/* for snprintf */
#include "port/unused.h"

#include <netinet/in.h>				/* for struct sockaddr_in */

int wolf_port_getaddrinfo(			const char *host_name,
						const char *service_name,
						const struct wolf_port_addrinfo *hintp,
						struct wolf_port_addrinfo **result ) {
	struct wolf_port_addrinfo *ai;
	struct sockaddr_in sin,
			   *psin;
	struct wolf_port_addrinfo hints;

	if( hintp == NULL ) {
		memset( &hints, 0, sizeof( struct wolf_port_addrinfo ) );
		hints.ai_family = AF_INET;
		hints.ai_socktype = SOCK_STREAM;
	} else {
		memcpy( &hints, hintp, sizeof( struct wolf_port_addrinfo ) );
	}

	if( hints.ai_family != AF_INET && hints.ai_family != AF_UNSPEC ) {
		return EAI_FAMILY;
	}

	if( hints.ai_socktype == 0 ) {
		hints.ai_socktype = SOCK_STREAM;
	}

	if( ( host_name == NULL ) && ( service_name == NULL ) ) {
		return EAI_NONAME;
	}

	memset( &sin, 0, sizeof( struct sockaddr_in) );

	sin.sin_family = AF_INET;

	if( host_name != NULL ) {
		if( host_name[0] == '\0' ) {
			sin.sin_addr.s_addr = htonl( INADDR_ANY );
		} else if( hints.ai_flags & AI_NUMERICHOST ) {
			if( !inet_aton( host_name, &sin.sin_addr ) ) {
				return EAI_FAIL;
			}
		} else {
			struct hostent *hp;

			hp = gethostbyname( host_name );
			
			if( hp == NULL ) {
				switch( h_errno ) {
					case HOST_NOT_FOUND:
					case NO_DATA:
						return EAI_NONAME;

					case TRY_AGAIN:
						return EAI_AGAIN;

					case NO_RECOVERY:
					default:
						return EAI_FAIL;
				}
			}

			if( hp->h_addrtype != AF_INET ) {
				return EAI_FAIL;
			}

			memcpy( &(sin.sin_addr), hp->h_addr, hp->h_length );
		}
	} else {
		if( hints.ai_flags & AI_PASSIVE ) {
			sin.sin_addr.s_addr = htonl( INADDR_ANY );
		} else {
			sin.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
		}
	}

	if( service_name != NULL ) {
		sin.sin_port = htons( (unsigned short)atoi( service_name ) );
	}

	ai = (struct wolf_port_addrinfo *)malloc( sizeof( struct wolf_port_addrinfo ) );
	if( ai == NULL ) {
		return EAI_MEMORY;
	}

	psin = (struct sockaddr_in *)malloc( sizeof( struct sockaddr_in ) );
	if (!psin)
	{
		free(ai);
		return EAI_MEMORY;
	}

	memcpy( psin, &sin, sizeof( struct sockaddr_in ) );

	ai->ai_flags = 0;
	ai->ai_family = AF_INET;
	ai->ai_socktype = hints.ai_socktype;
	ai->ai_protocol = hints.ai_protocol;
	ai->ai_addrlen = sizeof(*psin);
	ai->ai_addr = (struct sockaddr *) psin;
	ai->ai_canonname = NULL;
	ai->ai_next = NULL;

	*result = ai;

	return 0;
}

void wolf_port_freeaddrinfo(			struct wolf_port_addrinfo *result ) {
	/* stub doesn't have more than one element in list, adapt if you change that! */
	if( result != NULL ) {
		if( result->ai_addr != NULL ) {
			free( result->ai_addr );
		}
		free( result );
	}
}

/*
 * Convert an ipv4 address to a hostname.
 *
 * Bugs:	- Only supports NI_NUMERICHOST and NI_NUMERICSERV
 *		  It will never resolv a hostname.
 *		- No IPv6 support.
 */
extern int getnameinfo(				const struct sockaddr *sa, socklen_t salen,
						char *host, size_t hostlen,
						char *serv, size_t servlen,	
						int flags ) {
	WOLF_UNUSED( salen );

	/* Invalid arguments. */
	if( sa == NULL || ( host == NULL && serv == NULL ) ) {
		return EAI_FAIL;
	}

	/* We don't support those. */
	if( ( host && !( flags & NI_NUMERICHOST ) )
		|| ( serv && !( flags & NI_NUMERICSERV ) ) ) {
		return EAI_FAIL;
	}

#ifdef	HAVE_IPV6
	/* no IPV6 here */
	if( sa->sa_family == AF_INET6 ) {
		return EAI_FAMILY;
	}
#endif

	if( host != NULL ) {
		if( sa->sa_family == AF_INET ) {
			char *p;

			/* maximal size of an IP4 addres 255.255.255.255.255 */
			if( hostlen < 16 ) {
				return EAI_MEMORY;
			}

			p = inet_ntoa( ( (const struct sockaddr_in * ) sa)->sin_addr );
			
			strlcpy( host, p, hostlen );
		}
	}

	if( serv != NULL ) {
		if (sa->sa_family == AF_INET ) {
			/* maximal number of digits in an unsigned short */
			if( servlen < 10 ) {
				return EAI_MEMORY;
			}
			(void)itoa( ntohs( ( (const struct sockaddr_in * )sa)->sin_port ), serv, 10 );
		}
	}

	return 0;
}

const char *wolf_port_gai_strerror(		int errcode ) {
	static char buf[256];

	switch( errcode ) {
		case EAI_NONAME:
			return "host nor service provided, or not known";

		case EAI_AGAIN:
			return "temporary failure in name resolution";

		case EAI_FAIL:
			return "non-recoverable failure in name resultion";

		case EAI_FAMILY:
			return "socket family not supported";

		case EAI_MEMORY:
			return "memory allocation failure";

		default:
			snprintf( buf, 256, "Unknown GAI error %d", errcode );
			return buf;
	}
}

#endif /* !defined HAVE_GETADDRINFO || defined TEST_GETADDRINFO */

#if !defined HAVE_GAI_STRERROR_R || defined TEST_GAI_STRERROR_R

#include "threads/mutex.h"	/* for mutexes */
#include "port/stdio.h"		/* for snprintf */
#include "port/stdbool.h"	/* for bool */
#include "port/string.h"	/* for strncmp */ 
#include <errno.h>		/* for errno */

#ifdef HAVE_THREADS
static bool mutex_initialized = false;
static wolf_mutex_t mutex;
#endif

int wolf_port_gai_strerror_r( int errcode, char *buf, size_t buflen ) {
	int safe_errno = errno;
	const char *msg;

	/* critical section as gai_strerror is not thread-safe */
#ifdef HAVE_THREADS
	if( !mutex_initialized ) {
		wolf_mutex_init( &mutex );
		mutex_initialized = true;
	}
	wolf_mutex_lock( &mutex );
#endif

	msg = gai_strerror( errcode );
	if( strncmp( msg, "Unknown GAI errror", strlen( "Unknown GAI error" ) ) == 0 /* our own stub implementation */ ) {
		(void)snprintf( buf, buflen, "Unknown GAI error %d", errcode );
		errno = EINVAL;
#ifdef HAVE_THREADS
		wolf_mutex_unlock( &mutex );
#endif
		return -1;
	}

	strncpy( buf, msg, buflen-1 );
	errno = safe_errno;
#ifdef HAVE_THREADS
	wolf_mutex_unlock( &mutex );
#endif
	return 0;
}

#endif /* !defined HAVE_GAI_STRERROR_R || defined TEST_GAI_STRERROR_R */