Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. // #include <stdlib.h>
  2. // #include <stdio.h>
  3. // #include <sys/types.h>
  4. // #include <sys/socket.h>
  5. // // #include <netdb.h>
  6. // #include "/usr/include/rpc/netdb.h"
  7. // #include <arpa/inet.h>
  8.  
  9. #define _XOPEN_SOURCE 600
  10. #define _POSIX_C_SOURCE 200112L
  11.  
  12. #include "AdresseInternet.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <netdb.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22.  
  23. #define DNS_MAX_SIZE 256
  24. #define SERVICE_NAME_MAX_SIZE 20
  25.  
  26.  
  27.  
  28. AdresseInternet * AdresseInternet_new(const char* nom, uint16_t port) {
  29. struct addrinfo hints;
  30. if (memset(&hints, 0, sizeof(hints)) == NULL) {
  31. perror("memset");
  32. return NULL;
  33. }
  34. char port_string[6];
  35. if (snprintf(port_string, SERVICE_NAME_MAX_SIZE, "%u", port) != 1) {
  36. perror("snprintf");
  37. return NULL;
  38. }
  39. hints.ai_flags = AI_NUMERICSERV;
  40. hints.ai_family = AF_UNSPEC;
  41. struct addrinfo *result;
  42. int s = getaddrinfo(nom, port_string, &hints, &result);
  43. if (s != 0) {
  44. gai_strerror(s);
  45. return NULL;
  46. }
  47. if (result -> ai_family != AF_INET && result -> ai_family != AF_INET6) {
  48. return NULL;
  49. }
  50. AdresseInternet *adresse = malloc(sizeof(AdresseInternet));
  51. if (adresse == NULL) {
  52. return NULL;
  53. }
  54. adresse -> nom[0] = '\0';
  55. adresse -> service[0] = '\0';
  56. memcpy(&(adresse -> sockAddr), result -> ai_addr, result ->ai_addrlen);
  57. return NULL;
  58. }
  59.  
  60. AdresseInternet *AdresseInternet_any (uint16_t port) {
  61. struct addrinfo hints;
  62. if (memset(&hints, 0, sizeof(hints)) == NULL) {
  63. perror("memset");
  64. return NULL;
  65. }
  66. char port_string[6];
  67. if (snprintf(port_string, SERVICE_NAME_MAX_SIZE, "%u", port) != 1) {
  68. perror("snprintf");
  69. return NULL;
  70. }
  71. hints.ai_flags = AI_PASSIVE;
  72. hints.ai_family = AF_UNSPEC;
  73. struct addrinfo *result;
  74. int s = getaddrinfo(NULL, port_string, &hints, &result);
  75. if (s != 0) {
  76. gai_strerror(s);
  77. return NULL;
  78. }
  79. if (result -> ai_family != AF_INET && result -> ai_family != AF_INET6) {
  80. return NULL;
  81. }
  82. AdresseInternet *adresse = malloc(sizeof(AdresseInternet));
  83. if (adresse == NULL) {
  84. return NULL;
  85. }
  86. adresse -> nom[0] = '\0';
  87. adresse -> service[0] = '\0';
  88. memcpy(&(adresse -> sockAddr), result -> ai_addr, result ->ai_addrlen);
  89. freeaddrinfo(result);
  90. return NULL;
  91. }
  92.  
  93. AdresseInternet *AdresseInternet_loopback(uint16_t port) {
  94. struct addrinfo hints;
  95. if (memset(&hints, 0, sizeof(hints)) == NULL) {
  96. perror("memset");
  97. return NULL;
  98. }
  99. char port_string[6];
  100. if (snprintf(port_string, SERVICE_NAME_MAX_SIZE, "%u", port) != 1) {
  101. perror("snprintf");
  102. return NULL;
  103. }
  104. hints.ai_family = AF_UNSPEC;
  105. struct addrinfo *result;
  106. int s = getaddrinfo(NULL, port_string, &hints, &result);
  107. if (s != 0) {
  108. gai_strerror(s);
  109. return NULL;
  110. }
  111. if (result -> ai_family != AF_INET && result -> ai_family != AF_INET6) {
  112. return NULL;
  113. }
  114. AdresseInternet *adresse = malloc(sizeof(AdresseInternet));
  115. if (adresse == NULL) {
  116. return NULL;
  117. }
  118. adresse -> nom[0] = '\0';
  119. adresse -> service[0] = '\0';
  120. memcpy(&(adresse -> sockAddr), result -> ai_addr, result ->ai_addrlen);
  121. freeaddrinfo(result);
  122. return NULL;
  123. }
  124.  
  125. void AdresseInternet_free(AdresseInternet *adresse) {
  126. if (adresse == NULL) {
  127. return NULL;
  128. }
  129. free(adresse -> nom);
  130. free(adresse -> service);
  131. free(&(adresse -> sockAddr));
  132. }
  133.  
  134. int AdresseInternet_getinfo(AdresseInternet *adresse,
  135. char *nomDNS, int tailleDNS, char *nomPort, int taillePort) {
  136. if (adresse == NULL || )
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement