PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/libc/example.c

#
C | 74 lines | 31 code | 8 blank | 35 comment | 3 complexity | 7916a21c02a34b83a9645e8b0b70c811 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, LGPL-2.0, BSD-3-Clause
  1. /* example.c --- Example code showing how to use IDN enabled getaddrinfo().
  2. * Copyright (C) 2003-2012 Simon Josefsson
  3. *
  4. * This file is part of GNU Libidn.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #define _GNU_SOURCE 1
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netdb.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #include <locale.h> /* setlocale() */
  28. /*
  29. * Compiling against IDN enabled Libc:
  30. *
  31. * $ 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
  32. * $ CHARSET=iso-8859-1 ./example
  33. * locale charset `iso-8859-1'
  34. * gettaddrinfo(räksmörg?s.josefsson.org):
  35. * address `217.13.230.178'
  36. * canonical name `178.230.13.217.in-addr.dgcsystems.net'
  37. * $
  38. *
  39. * Internally the name iesg--rksmrgsa-0zap8p.josefsson.org is looked
  40. * up in DNS.
  41. */
  42. int
  43. main(int argc, char *argv[])
  44. {
  45. char *in = argc > 1 ? argv[1] : "räksmörg?s.josefsson.org";
  46. struct addrinfo hints;
  47. struct addrinfo *res = NULL;
  48. int rc;
  49. setlocale (LC_ALL, "");
  50. //printf("locale charset `%s'\n", stringprep_locale_charset());
  51. memset(&hints, 0, sizeof(hints));
  52. hints.ai_flags = AI_CANONNAME|AI_IDN;
  53. printf("gettaddrinfo(%s):\n", in);
  54. rc = getaddrinfo(in, NULL, &hints, &res);
  55. if (rc)
  56. printf("gai err %d: %s\n", rc, gai_strerror(rc));
  57. else if (res)
  58. printf("address `%s'\ncanonical name `%s'\n",
  59. res->ai_addr ?
  60. /* FIXME: Use inet_ntop, so it works for IPv6 too. */
  61. inet_ntoa(((struct sockaddr_in*)res->ai_addr)->sin_addr) : "ERROR",
  62. res->ai_canonname ? res->ai_canonname : "ERROR");
  63. else
  64. printf("Bad magic\n");
  65. return 0;
  66. }