/lib/libidn/libidn-1.5/libc/example.c

https://bitbucket.org/thelearninglabs/uclinux-distro-tll-public · C · 75 lines · 31 code · 8 blank · 36 comment · 3 complexity · a6f22b04de3b3e96215dc208abe9449f MD5 · raw file

  1. /* example.c --- Example code showing how to use IDN enabled getaddrinfo().
  2. * Copyright (C) 2003, 2004 Simon Josefsson
  3. *
  4. * This file is part of GNU Libidn.
  5. *
  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.
  10. *
  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.
  15. *
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. */
  21. #define _GNU_SOURCE 1
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netdb.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. #include <locale.h> /* setlocale() */
  29. /*
  30. * Compiling against IDN enabled Libc:
  31. *
  32. * $ 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
  33. * $ CHARSET=iso-8859-1 ./example
  34. * locale charset `iso-8859-1'
  35. * gettaddrinfo(räksmörgås.josefsson.org):
  36. * address `217.13.230.178'
  37. * canonical name `178.230.13.217.in-addr.dgcsystems.net'
  38. * $
  39. *
  40. * Internally the name iesg--rksmrgsa-0zap8p.josefsson.org is looked
  41. * up in DNS.
  42. */
  43. int
  44. main(int argc, char *argv[])
  45. {
  46. char *in = argc > 1 ? argv[1] : "räksmörgås.josefsson.org";
  47. struct addrinfo hints;
  48. struct addrinfo *res = NULL;
  49. int rc;
  50. setlocale (LC_ALL, "");
  51. //printf("locale charset `%s'\n", stringprep_locale_charset());
  52. memset(&hints, 0, sizeof(hints));
  53. hints.ai_flags = AI_CANONNAME|AI_IDN;
  54. printf("gettaddrinfo(%s):\n", in);
  55. rc = getaddrinfo(in, NULL, &hints, &res);
  56. if (rc)
  57. printf("gai err %d: %s\n", rc, gai_strerror(rc));
  58. else if (res)
  59. printf("address `%s'\ncanonical name `%s'\n",
  60. res->ai_addr ?
  61. /* FIXME: Use inet_ntop, so it works for IPv6 too. */
  62. inet_ntoa(((struct sockaddr_in*)res->ai_addr)->sin_addr) : "ERROR",
  63. res->ai_canonname ? res->ai_canonname : "ERROR");
  64. else
  65. printf("Bad magic\n");
  66. return 0;
  67. }