char * host_name_to_cname(const char *src, char *dst, int dstlen) { struct hostent *hptr; unsigned char buf[HOSTENT_SIZE]; struct in_addr addr; assert(src != NULL); assert(dst != NULL); if (!(hptr = get_host_by_name(src, buf, sizeof(buf), NULL))) return(NULL); /* * If 'src' is an ip-addr string, it will simply be copied to h_name. * So, we need to perform a reverse query based on the in_addr * in order to obtain the canonical name of the host. * Besides, this additional query helps protect against DNS spoofing. */ memcpy(&addr, hptr->h_addr_list[0], hptr->h_length); if (!(hptr = get_host_by_addr((char *) &addr, 4, AF_INET, buf, sizeof(buf), NULL))) return(NULL); if (strlen(hptr->h_name) >= dstlen) { errno = ERANGE; return(NULL); } strcpy(dst, hptr->h_name); return(dst); }
int connect_to_server(struct client_params * params, int * stop_threads) { int thread_created; struct client_socket_params * socket_params; if (params == NULL) { return EXIT_FAILURE; } // Pointer to the value to stop all threads _client_global_stop_thread = stop_threads; // Create socket params->client->sock = recreate_socket(); socket_params = (struct client_socket_params *)params->client->custom_data; // Resolve hostname fprintf(stderr, "[*] Trying to connect to %s:%u\n", socket_params->host, socket_params->port); socket_params->hostent = get_host_by_name(socket_params->host); if (socket_params->hostent == NULL) { fprintf(stderr,"ERROR, no such host\n"); close_socket(¶ms->client->sock); free_client_params(¶ms); // it cleans up the struct too return EXIT_FAILURE; } // Init structures bzero((char *) &(socket_params->serv_addr), sizeof(socket_params->serv_addr)); socket_params->serv_addr.sin_family = AF_INET; bcopy((char *)(socket_params->hostent)->h_addr, (char *)&(socket_params->serv_addr).sin_addr.s_addr, socket_params->hostent->h_length); socket_params->serv_addr.sin_port = htons(socket_params->port); // Create thread to connect to it thread_created = pthread_create(&(params->client->thread), NULL, (void*)&client_connect_thread, params); if (thread_created != 0) { fprintf(stderr, "[*] Failed to create connection thread.\n"); close_socket(¶ms->client->sock); free_client_params(¶ms); // it cleans up the struct too return EXIT_FAILURE; } return EXIT_SUCCESS; }
int host_name_to_addr4(const char *name, struct in_addr *addr) { struct hostent *hptr; unsigned char buf[HOSTENT_SIZE]; assert(name != NULL); assert(addr != NULL); if (!(hptr = get_host_by_name(name, buf, sizeof(buf), NULL))) return(-1); if (hptr->h_length > 4) { errno = ERANGE; return(-1); } memcpy(addr, hptr->h_addr_list[0], hptr->h_length); return(0); }
netTaskErr getEpochTime(void) { uint8_t count; netTaskErr result = errOK; netStatus sntpStatus, dnsStatus; osEvent statusNtpWait, statusDnsWait; // getting IP of NTP server for (count = 0; count < DNS_WAIT_TRIES; count++) { osSignalClear(netTaskId, FLAG_DNS_RESOLVED); dnsStatus = get_host_by_name (ntpHostName, dnsCBack); if (dnsStatus == netOK) { statusDnsWait = osSignalWait(FLAG_DNS_RESOLVED, DNS_TIMEOUT_MS); if ((statusDnsWait.status == osEventSignal) && ((ntpHostIP[0] != 0) || (ntpHostIP[1] != 0) || (ntpHostIP[2] != 0) || (ntpHostIP[3] != 0)) ) { result = errOK; break; } else { osSignalClear(netTaskId, FLAG_DNS_RESOLVED); result = errDnsTOut; } } else { result = errDnsResolve; } osDelay(DNS_WAIT_DELAY_MS); } if (result != errOK) return result; // requesting Unix time from NTP server for (count = 0; count < SNTP_WAIT_TRIES; count++) { osSignalClear(netTaskId, FLAG_UDP_PACKET_RECV); sntpStatus = sntp_get_time (&ntpHostIP[0], sntpCBack); if (sntpStatus == netOK) { statusNtpWait = osSignalWait(FLAG_UDP_PACKET_RECV, UDP_TIMEOUT_MS); if ((statusNtpWait.status == osEventSignal) && (ntpEpochTime != 0)) { result = errOK; break; } else { osSignalClear(netTaskId, FLAG_UDP_PACKET_RECV); result = errNtpCBackTOut; } } else { result = errNtpNotReady; // SNTP not ready or bad parameters. } osDelay(SNTP_WAIT_DELAY_MS); } return result; }