void* ntp_connect( const char* server, int server_port, void (*ntp_func)(uint32_t, uint32_t, uint32_t, uint32_t)) { ntp_handle* handle; if((handle = (ntp_handle*)malloc(sizeof(ntp_handle))) == NULL) return NULL; handle->receive_func = ntp_func; handle->last_sec_sent = 0; handle->last_nsec_sent = 0; if((handle->sock = network_connect_udp( server, server_port, NULL, NULL)) == INVALID_SOCKET) { free(handle); return NULL; } //printf("Unable to reach '%s:%d'\n", server, server_port); if((handle->listener = network_listen_udp_from( handle->sock, ntp_listen, handle->receive_packet, NTP_PACKET_SIZE, (void*)handle)) == NULL) { network_disconnect(handle->sock); free(handle); return NULL; } return (void*)handle; }
int main(int argc, char *argv[]) { dnsPacket *dns, *dnsresponse; int iResult, iReceived; char *qdomain, *qserver, *response; unsigned short qtype = 1, qclass = 1; qdomain = ((qdomain = command_arg_get(0, '-', argc, argv)) != NULL) ? qdomain : DNS_DEFAULT_LOOKUP; qserver = ((qserver = command_arg_get(1, '-', argc, argv)) != NULL) ? qserver : DNS_LOOKUP_SERVER; // create our empty DNS packet dns = dns_create(); // set the required header fields dns->base->id = DNS_IDENTIFIER; // use recursive query? if (command_opt_set("-r", argc, argv)) { dns->base->flags |= DNS_FLAGS_RD; printf("+ Using recursive querying\n"); } // use a different query type? if (command_opt_set("-cname", argc, argv)) { qtype = DNS_TYPE_CNAME; printf("+ Using CNAME query type\n"); } else if (command_opt_set("-ns", argc, argv)) { qtype = DNS_TYPE_NS; printf("+ Using NS query type\n"); } else if (command_opt_set("-mx", argc, argv)) { qtype = DNS_TYPE_MX; printf("+ Using MX query type\n"); } else if (command_opt_set("-ptr", argc, argv)) { qtype = DNS_TYPE_PTR; printf("+ Using PTR query type\n"); } else if (command_opt_set("-aaaa", argc, argv)) { qtype = DNS_TYPE_AAAA; printf("+ Using AAAA query type\n"); } // add our question section dns_add_question(dns, qdomain, strlen(qdomain), qtype, qclass); dns_hton(dns); // initialize the network network_init(qserver, DNS_PORT); network_connect_udp(); // send the packet out printf("Looking up %s using %s...\n", qdomain, qserver); iResult = network_send((char *)dns->base, dns->size); assert(iResult > 0); // block until a response is ready response = network_receive(&iReceived); assert(iReceived > 0); // load up the response packet dnsresponse = dns_parse(response, iReceived); assert(dnsresponse->base->flags & DNS_FLAGS_RESPONSE && dnsresponse->base->id == DNS_IDENTIFIER); output_print_response(dnsresponse); output_print_sections(dnsresponse); }