xref: /openbsd-src/regress/lib/libpthread/getaddrinfo/getaddrinfo.c (revision 5fb35c8ffbe5300260a0c09ed7b4fa4cd5d92947)
1*5fb35c8fSmarc /*	$OpenBSD: getaddrinfo.c,v 1.3 2003/01/18 01:48:21 marc Exp $	*/
229d817a9Stodd /*
329d817a9Stodd  * Copyright (c) 2002 Todd T. Fries <todd@OpenBSD.org>
429d817a9Stodd  * All rights reserved.
529d817a9Stodd  *
629d817a9Stodd  * Redistribution and use in source and binary forms, with or without
729d817a9Stodd  * modification, are permitted provided that the following conditions
829d817a9Stodd  * are met:
929d817a9Stodd  * 1. Redistributions of source code must retain the above copyright
1029d817a9Stodd  *    notice, this list of conditions and the following disclaimer.
1129d817a9Stodd  * 2. The name of the author may not be used to endorse or promote products
1229d817a9Stodd  *    derived from this software without specific prior written permission.
1329d817a9Stodd  *
1429d817a9Stodd  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1529d817a9Stodd  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
1629d817a9Stodd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
1729d817a9Stodd  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1829d817a9Stodd  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1929d817a9Stodd  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2029d817a9Stodd  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2129d817a9Stodd  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2229d817a9Stodd  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2329d817a9Stodd  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2429d817a9Stodd  */
2529d817a9Stodd 
2629d817a9Stodd #include <unistd.h>
2729d817a9Stodd #include <sys/types.h>
2829d817a9Stodd #include <sys/socket.h>
2929d817a9Stodd #include <netinet/in.h>
3029d817a9Stodd #include <pthread.h>
3129d817a9Stodd #include <netdb.h>
3229d817a9Stodd #include <resolv.h>
3329d817a9Stodd 
3429d817a9Stodd #include "test.h"
3529d817a9Stodd 
36*5fb35c8fSmarc #define STACK_SIZE	(2 * 1024 * 1024)
37*5fb35c8fSmarc 
3829d817a9Stodd void	*func(void *);
3929d817a9Stodd 
4029d817a9Stodd int
main(argc,argv)4129d817a9Stodd main(argc, argv)
4229d817a9Stodd 	int argc;
4329d817a9Stodd 	char **argv;
4429d817a9Stodd {
45*5fb35c8fSmarc 	pthread_attr_t attr;
46cbc44f4fSfgsch 	pthread_t threads[2];
4729d817a9Stodd 	int i;
4829d817a9Stodd 
49*5fb35c8fSmarc 	CHECKr(pthread_attr_init(&attr));
50*5fb35c8fSmarc 	CHECKr(pthread_attr_setstacksize(&attr, (size_t) STACK_SIZE));
5129d817a9Stodd 	for (i = 0; i < 2; i++) {
52*5fb35c8fSmarc 		CHECKr(pthread_create(&threads[i], &attr, func, NULL));
5329d817a9Stodd 	}
5429d817a9Stodd 
5529d817a9Stodd 	pthread_yield();
56cbc44f4fSfgsch 	for (i = 0; i < 2; i++) {
57cbc44f4fSfgsch 		CHECKr(pthread_join(threads[i], NULL));
58cbc44f4fSfgsch 	}
5929d817a9Stodd 
6029d817a9Stodd 	SUCCEED;
6129d817a9Stodd }
6229d817a9Stodd 
6329d817a9Stodd void *
func(arg)6429d817a9Stodd func(arg)
6529d817a9Stodd 	void *arg;
6629d817a9Stodd {
6729d817a9Stodd 	struct addrinfo hints, *res;
6829d817a9Stodd 	char h[NI_MAXHOST];
69cbc44f4fSfgsch 	int i;
7029d817a9Stodd 
7129d817a9Stodd 	memset(&hints, 0, sizeof(hints));
7229d817a9Stodd 	hints.ai_family = PF_UNSPEC;
7329d817a9Stodd 	hints.ai_socktype = SOCK_DGRAM;
7429d817a9Stodd 	hints.ai_flags = AI_CANONNAME;
7529d817a9Stodd 
76cbc44f4fSfgsch 	printf("Starting thread %p\n", pthread_self());
77cbc44f4fSfgsch 
7829d817a9Stodd 	for(i = 0; i < 50; i++) {
7929d817a9Stodd 		if (getaddrinfo("www.openbsd.org", "0", &hints, &res))
8029d817a9Stodd 			printf("error on thread %p\n", pthread_self());
8129d817a9Stodd 		else {
8229d817a9Stodd 			getnameinfo(res->ai_addr, res->ai_addrlen, h, sizeof h,
8329d817a9Stodd 			    NULL, 0, NI_NUMERICHOST);
84cbc44f4fSfgsch 			printf("success on thread %p: %s is %s\n",
85cbc44f4fSfgsch 			    pthread_self(), res->ai_canonname, h);
8629d817a9Stodd 			freeaddrinfo(res);
8729d817a9Stodd 		}
8829d817a9Stodd 	}
89cbc44f4fSfgsch 	return (NULL);
9029d817a9Stodd }
9129d817a9Stodd 
92