Update PGP key.
[libidn.git] / libc / example.c
blob9579254b53fd6c7ee8c0f11741a53ab09742a6bc
1 /* example.c Example code showing how to use IDN enabled getaddrinfo().
2 * Copyright (C) 2003, 2004 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * GNU Libidn is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define _GNU_SOURCE 1
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netdb.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <locale.h> /* setlocale() */
32 * Compiling against IDN enabled Libc:
34 * $ gcc -o example example.c -L/usr/local/glibc/lib -Wl,-rpath,/usr/local/glibc/lib -nostdinc -I/usr/local/glibc/include -I/usr/include -I/usr/lib/gcc-lib/i486-linux/3.3.3/include
35 * $ CHARSET=iso-8859-1 ./example
36 * locale charset `iso-8859-1'
37 * gettaddrinfo(räksmörgås.josefsson.org):
38 * address `217.13.230.178'
39 * canonical name `178.230.13.217.in-addr.dgcsystems.net'
40 * $
42 * Internally the name iesg--rksmrgsa-0zap8p.josefsson.org is looked
43 * up in DNS.
46 int
47 main(int argc, char *argv[])
49 char *in = argc > 1 ? argv[1] : "räksmörgås.josefsson.org";
50 struct addrinfo hints;
51 struct addrinfo *res = NULL;
52 int rc;
54 setlocale (LC_ALL, "");
56 //printf("locale charset `%s'\n", stringprep_locale_charset());
58 memset(&hints, 0, sizeof(hints));
59 hints.ai_flags = AI_CANONNAME|AI_IDN;
61 printf("gettaddrinfo(%s):\n", in);
62 rc = getaddrinfo(in, NULL, &hints, &res);
63 if (rc)
64 printf("gai err %d: %s\n", rc, gai_strerror(rc));
65 else if (res)
66 printf("address `%s'\ncanonical name `%s'\n",
67 res->ai_addr ?
68 /* FIXME: Use inet_ntop, so it works for IPv6 too. */
69 inet_ntoa(((struct sockaddr_in*)res->ai_addr)->sin_addr) : "ERROR",
70 res->ai_canonname ? res->ai_canonname : "ERROR");
71 else
72 printf("Bad magic\n");
74 return 0;