Move unicode utility functions from stringprep to own chapter.
[libidn.git] / libc / example.c
blob9604da7d057eb115e57821fc9bfb2d6028ac6655
1 /* example.c Example code showing how to use IDN enabled getaddrinfo().
2 * Copyright (C) 2003 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>
31 * Compiling against IDN enabled Libc:
33 * $ 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/i386-linux/2.95.4/include
34 * $ CHARSET=iso-8859-1 ./example
35 * locale charset `iso-8859-1'
36 * gettaddrinfo(räksmörgås.josefsson.org):
37 * address `217.13.230.178'
38 * canonical name `178.230.13.217.in-addr.dgcsystems.net'
39 * $
41 * Internally the name iesg--rksmrgsa-0zap8p.josefsson.org is looked
42 * up in DNS.
45 int
46 main(int argc, char *argv[])
48 char *in = argc > 1 ? argv[1] : "räksmörgås.josefsson.org";
49 struct addrinfo hints;
50 struct addrinfo *res = NULL;
51 int rc;
53 printf("locale charset `%s'\n", stringprep_locale_charset());
55 memset(&hints, 0, sizeof(hints));
56 hints.ai_flags = AI_CANONNAME|AI_IDN;
58 printf("gettaddrinfo(%s):\n", in);
59 rc = getaddrinfo(in, NULL, &hints, &res);
60 if (rc)
61 printf("gai err %d: %s\n", rc, gai_strerror(rc));
62 else if (res)
63 printf("address `%s'\ncanonical name `%s'\n",
64 res->ai_addr ?
65 inet_ntoa(((struct sockaddr_in*)res->ai_addr)->sin_addr) : "ERROR",
66 res->ai_canonname ? res->ai_canonname : "ERROR");
67 else
68 printf("Bad magic\n");
70 return 0;