1/*	$OpenBSD: getaddrinfo.c,v 1.3 2003/01/18 01:48:21 marc Exp $	*/
2/*
3 * Copyright (c) 2002 Todd T. Fries <todd@OpenBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 *    derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
16 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
17 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <unistd.h>
27#include <sys/types.h>
28#include <sys/socket.h>
29#include <netinet/in.h>
30#include <pthread.h>
31#include <netdb.h>
32#include <resolv.h>
33
34#include "test.h"
35
36#define STACK_SIZE	(2 * 1024 * 1024)
37
38void	*func(void *);
39
40int
41main(argc, argv)
42	int argc;
43	char **argv;
44{
45	pthread_attr_t attr;
46	pthread_t threads[2];
47	int i;
48
49	CHECKr(pthread_attr_init(&attr));
50	CHECKr(pthread_attr_setstacksize(&attr, (size_t) STACK_SIZE));
51	for (i = 0; i < 2; i++) {
52		CHECKr(pthread_create(&threads[i], &attr, func, NULL));
53	}
54
55	pthread_yield();
56	for (i = 0; i < 2; i++) {
57		CHECKr(pthread_join(threads[i], NULL));
58	}
59
60	SUCCEED;
61}
62
63void *
64func(arg)
65	void *arg;
66{
67	struct addrinfo hints, *res;
68	char h[NI_MAXHOST];
69	int i;
70
71	memset(&hints, 0, sizeof(hints));
72	hints.ai_family = PF_UNSPEC;
73	hints.ai_socktype = SOCK_DGRAM;
74	hints.ai_flags = AI_CANONNAME;
75
76	printf("Starting thread %p\n", pthread_self());
77
78	for(i = 0; i < 50; i++) {
79		if (getaddrinfo("www.openbsd.org", "0", &hints, &res))
80			printf("error on thread %p\n", pthread_self());
81		else {
82			getnameinfo(res->ai_addr, res->ai_addrlen, h, sizeof h,
83			    NULL, 0, NI_NUMERICHOST);
84			printf("success on thread %p: %s is %s\n",
85			    pthread_self(), res->ai_canonname, h);
86			freeaddrinfo(res);
87		}
88	}
89	return (NULL);
90}
91
92