Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #include <stdlib.h>
- // #include <stdio.h>
- // #include <sys/types.h>
- // #include <sys/socket.h>
- // // #include <netdb.h>
- // #include "/usr/include/rpc/netdb.h"
- // #include <arpa/inet.h>
- #define _XOPEN_SOURCE 600
- #define _POSIX_C_SOURCE 200112L
- #include "AdresseInternet.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <string.h>
- #include <unistd.h>
- #define DNS_MAX_SIZE 256
- #define SERVICE_NAME_MAX_SIZE 20
- AdresseInternet * AdresseInternet_new(const char* nom, uint16_t port) {
- struct addrinfo hints;
- if (memset(&hints, 0, sizeof(hints)) == NULL) {
- perror("memset");
- return NULL;
- }
- char port_string[6];
- if (snprintf(port_string, SERVICE_NAME_MAX_SIZE, "%u", port) != 1) {
- perror("snprintf");
- return NULL;
- }
- hints.ai_flags = AI_NUMERICSERV;
- hints.ai_family = AF_UNSPEC;
- struct addrinfo *result;
- int s = getaddrinfo(nom, port_string, &hints, &result);
- if (s != 0) {
- gai_strerror(s);
- return NULL;
- }
- if (result -> ai_family != AF_INET && result -> ai_family != AF_INET6) {
- return NULL;
- }
- AdresseInternet *adresse = malloc(sizeof(AdresseInternet));
- if (adresse == NULL) {
- return NULL;
- }
- adresse -> nom[0] = '\0';
- adresse -> service[0] = '\0';
- memcpy(&(adresse -> sockAddr), result -> ai_addr, result ->ai_addrlen);
- return NULL;
- }
- AdresseInternet *AdresseInternet_any (uint16_t port) {
- struct addrinfo hints;
- if (memset(&hints, 0, sizeof(hints)) == NULL) {
- perror("memset");
- return NULL;
- }
- char port_string[6];
- if (snprintf(port_string, SERVICE_NAME_MAX_SIZE, "%u", port) != 1) {
- perror("snprintf");
- return NULL;
- }
- hints.ai_flags = AI_PASSIVE;
- hints.ai_family = AF_UNSPEC;
- struct addrinfo *result;
- int s = getaddrinfo(NULL, port_string, &hints, &result);
- if (s != 0) {
- gai_strerror(s);
- return NULL;
- }
- if (result -> ai_family != AF_INET && result -> ai_family != AF_INET6) {
- return NULL;
- }
- AdresseInternet *adresse = malloc(sizeof(AdresseInternet));
- if (adresse == NULL) {
- return NULL;
- }
- adresse -> nom[0] = '\0';
- adresse -> service[0] = '\0';
- memcpy(&(adresse -> sockAddr), result -> ai_addr, result ->ai_addrlen);
- freeaddrinfo(result);
- return NULL;
- }
- AdresseInternet *AdresseInternet_loopback(uint16_t port) {
- struct addrinfo hints;
- if (memset(&hints, 0, sizeof(hints)) == NULL) {
- perror("memset");
- return NULL;
- }
- char port_string[6];
- if (snprintf(port_string, SERVICE_NAME_MAX_SIZE, "%u", port) != 1) {
- perror("snprintf");
- return NULL;
- }
- hints.ai_family = AF_UNSPEC;
- struct addrinfo *result;
- int s = getaddrinfo(NULL, port_string, &hints, &result);
- if (s != 0) {
- gai_strerror(s);
- return NULL;
- }
- if (result -> ai_family != AF_INET && result -> ai_family != AF_INET6) {
- return NULL;
- }
- AdresseInternet *adresse = malloc(sizeof(AdresseInternet));
- if (adresse == NULL) {
- return NULL;
- }
- adresse -> nom[0] = '\0';
- adresse -> service[0] = '\0';
- memcpy(&(adresse -> sockAddr), result -> ai_addr, result ->ai_addrlen);
- freeaddrinfo(result);
- return NULL;
- }
- void AdresseInternet_free(AdresseInternet *adresse) {
- if (adresse == NULL) {
- return NULL;
- }
- free(adresse -> nom);
- free(adresse -> service);
- free(&(adresse -> sockAddr));
- }
- int AdresseInternet_getinfo(AdresseInternet *adresse,
- char *nomDNS, int tailleDNS, char *nomPort, int taillePort) {
- if (adresse == NULL || )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement