testgetgateway.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* $Id: testgetgateway.c,v 1.4 2008/07/02 22:33:06 nanard Exp $ */
  2. /* libnatpmp
  3. * Copyright (c) 2007, Thomas BERNARD <miniupnp@free.fr>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  16. #include <stdio.h>
  17. #ifdef WIN32
  18. #include <winsock2.h>
  19. #else
  20. #include <netinet/in.h>
  21. #include <arpa/inet.h>
  22. #endif
  23. #include "getgateway.h"
  24. int main(int argc, char * * argv)
  25. {
  26. struct in_addr gatewayaddr;
  27. int r;
  28. #ifdef WIN32
  29. uint32_t temp = 0;
  30. r = getdefaultgateway(&temp);
  31. gatewayaddr.S_un.S_addr = temp;
  32. #else
  33. r = getdefaultgateway(&(gatewayaddr.s_addr));
  34. #endif
  35. if(r>=0)
  36. printf("default gateway : %s\n", inet_ntoa(gatewayaddr));
  37. else
  38. fprintf(stderr, "getdefaultgateway() failed\n");
  39. return 0;
  40. }