Advertisement
Guest User

Untitled

a guest
Jul 4th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. [ainozama@notebook sandbox]$ cat ipv6util_storage.c
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netdb.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <arpa/inet.h>
  11. #include <bits/socket.h>
  12. #include <errno.h>
  13. #include <unistd.h>
  14.  
  15. static int cached_getipbyname(char *host_name, struct sockaddr_storage *endereco) {
  16. struct addrinfo *res;
  17. getaddrinfo(host_name, "80", NULL, &res);
  18. memcpy(endereco, res->ai_addr, res->ai_addrlen);
  19. return(0);
  20. }
  21.  
  22. int main(int argc, char *argv[]){
  23. struct sockaddr_storage endereco;
  24. char *t = NULL;
  25.  
  26. cached_getipbyname(argv[1], &endereco);
  27. if (endereco.ss_family == AF_INET6) {
  28. int fd;
  29. struct sockaddr_in6 *addr;
  30.  
  31. //memset(&addr,0,sizeof(addr));
  32. addr = (struct sockaddr_in6 *)&endereco;
  33. t = malloc (INET6_ADDRSTRLEN);
  34. inet_ntop(AF_INET6, &addr->sin6_addr, t, INET6_ADDRSTRLEN);
  35. printf("DEST IP: %s\n", t);
  36. printf("PORT: %d\n", addr->sin6_port);
  37.  
  38. fd = socket(AF_INET6, SOCK_STREAM, 0);
  39. if (connect(fd, (struct sockaddr *)&addr, sizeof(addr->sin6_addr)) == -1) {
  40. printf("NOK\n");
  41. if(errno != EINPROGRESS) {
  42. printf("connect error: %s\n",strerror(errno));
  43. close(fd);
  44. }
  45. } else
  46. printf("OK\n");
  47. free(t);
  48. }
  49. return(0);
  50. }
  51.  
  52. [ainozama@notebook sandbox]$ make storage
  53. gcc -g -Wall ipv6util_storage.c -o ipv6util_storage
  54.  
  55. [ainozama@notebook sandbox]$ ./ipv6util_storage ipv6lab
  56. DEST IP: fef::6f:ec:b5
  57. PORT: 20480
  58. NOK
  59. connect error: Invalid argument
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement