#include "kod_management.h"
#include "log.h"
#include "sntp-opts.h"
+#define DEBUG
int kod_init = 0, entryc = 0;
char *kod_db_file;
return resc;
}
+int
+kod_entry_exists (
+ char *search_str
+ )
+{
+ struct kod_entry **dummy;
+
+ return search_entry(search_str, dummy);
+}
+
void
add_entry (
char *hostname,
char msg[80];
snprintf(msg, 80, "Can't open KOD db file %s for writing!", kod_db_file);
- if(debug)
- debug_msg(msg);
+
+#ifdef DEBUG
+ debug_msg(msg);
+#endif
log_msg(msg, 2);
}
for(a=0; a<entryc && nptr != NULL; a++) {
- fprintf(db_s, "%s:%i%s\n", nptr->hostname, nptr->timestamp, nptr->type);
+ fprintf(db_s, "%s:%i:%s\n", nptr->hostname, nptr->timestamp, nptr->type);
nptr = nptr->next;
}
if(kod_init)
return;
+#ifdef DEBUG
+ printf("Initializing KOD DB...\n");
+#endif
+
register int a, b;
/* Max. of 255 characters for hostname, 10 for timestamp, 4 for kisscode, 2 for format : and 1 for \n */
- char *fbuf = (char *) malloc(sizeof(char) * 272);
+ char fbuf[272];
char *obuf = fbuf;
char error = 0;
if(db_file == NULL) {
char msg[80];
- snprintf(msg, 80, "Cannot open KOD db file %s", db_file);
+ snprintf(msg, 80, "kod_init_kod_db(): Cannot open KOD db file %s", db_file);
- if(debug)
- debug_msg(msg);
+#ifdef DEBUG
+ debug_msg(msg);
+ printf("%s\n", msg);
+#endif
log_msg(msg, 2);
}
+ printf("Starting to read KOD file %s...\n", db_file);
/* First let's see how many entries there are and check for right syntax */
- while(fgets(fbuf, 272, db_s) != 0) {
+
+ int scan_value = 0;
+ while(!feof(db_s)) {
+ printf("%s", fgets(fbuf, 272, db_s));
int sepc = 0;
for(a=0; a<strlen(fbuf); a++) {
if(fbuf[a] == ':')
char msg[80];
snprintf(msg, 80, "Syntax error in KOD db file %s in line %i (missing :)", db_file, (entryc + 1));
- if(debug)
- debug_msg(msg);
+#ifdef DEBUG
+ debug_msg(msg);
+ printf("%s\n", msg);
+#endif
log_msg(msg, 1);
- return;
+ return;
}
sepc = 0;
}
}
+ entryc--;
+
+#ifdef DEBUG
+ printf("KOD DB %s contains %i entries, reading...\n", db_file, entryc);
+#endif
+
rewind(db_s);
kod_db = (struct kod_entry *) malloc(sizeof(struct kod_entry) * entryc);
/* Read contents of file and make a linked list */
for(b=0; (!feof(db_s) || !ferror(db_s)) && b<entryc; b++) {
- if(!fscanf(db_s,
- "%s:%u:%s", kod_db[a].hostname,
- kod_db[a].timestamp,
- kod_db[a].type)) {
+ char *str_ptr = fgets(fbuf, 272, db_s);
+
+ int j = sscanf(fbuf, "%255[^:]", &(kod_db[b].hostname));
+ j += sscanf(fbuf + j, "%*[^:]:%i:%4s", &kod_db[b].timestamp, &(kod_db[b].type));
+
+ if(str_ptr == NULL) {
b = entryc;
error = 1;
}
else {
+#ifdef DEBUG
+ printf("KOD entry %i %i: %s at %i type %s\n", a, b, kod_db[b].hostname,
+ kod_db[b].timestamp, kod_db[b].type);
+#endif
if(b > 0)
kod_db[b-1].next = &kod_db[b];
}
-
}
+#ifdef DEBUG
+ for(a=0; a<entryc; a++)
+ printf("KOD entry %i: %s at %i type %s\n", a, kod_db[a].hostname,
+ kod_db[a].timestamp, kod_db[a].type);
+
+#endif
+
if(ferror(db_s) || error) {
char msg[80];
snprintf(msg, 80, "An error occured while parsing the KOD db file %s", db_file);
- if(debug)
- debug_msg(msg);
+#ifdef DEBUG
+ debug_msg(msg);
+#endif
log_msg(msg, 2);
/* Check for isc/binds logging facility, probably interesting, probably worth adapting */
#include "log.h"
-#include "sntp-opt.h"
+#include "sntp-opts.h"
-void log_msg(char *message, int type) {
- if(HAVE_OPT(FILELOG)) {
+int init = 0;
+int filelog = 0;
+
+FILE *log_file;
+
+
+void log_msg(char *message, char type) {
+ if(filelog) {
if(init) {
fprintf(log_file, message);
}
}
void debug_msg(char *message) {
- if(HAVE_OPT(FILELOG)) {
+ if(filelog) {
fprintf(stderr, message);
}
else {
void init_log(char *logfile) {
log_file = fopen(logfile, "a");
- init = 1;
- atexit(cleanup_log);
+
+ if(log_file == NULL) {
+ filelog = 0;
+
+ char error_msg[80];
+
+ snprintf(error_msg, 80, "init_log(): Cannot open logfile %s", logfile);
+
+ debug_msg(error_msg);
+
+ return;
+ }
+ else {
+ filelog = 1;
+ init = 1;
+ atexit(cleanup_log);
+ }
}
void cleanup_log(void) {
#endif
#include <ntp_stdlib.h>
+#include <ntp_unixtime.h>
+#include <l_stdlib.h>
#include <isc/result.h>
+#include <stdio.h>
#include <sntp-opts.h>
#include "networking.h"
-#include "header.h"
+/*#include "header.h"*/
#define NTP_PORT 123
-int ai_fam_templ;
+#define DEBUG
+int ai_fam_templ;
volatile int debug;
+/* tOptions sntpOptions; */
+
int
main (
- char **argv,
- int argc
+ int argc,
+ char **argv
)
{
- return sntp_main(argv, argc);
+ return sntp_main(argc, argv);
}
int
sntp_main (
- char **argv,
- int argc
+ int argc,
+ char **argv
)
{
+ char *kod_file, *log_file;
register int c;
+#ifdef DEBUG
+ printf("Starting %s with %i arguments...\n", argv[0], argc);
+#endif
+
if (isc_net_probeipv6() != ISC_R_SUCCESS) {
ai_fam_templ = AF_INET;
+#ifdef DEBUG
+ printf("No ipv6 support available, forcing ipv4\n");
+#endif
}
- int optct = optionProcess( &sntpOptions, argc, argv );
+#ifndef NO_DISK
+#endif
+
+ int optct = optionProcess(&sntpOptions, argc, argv);
argc -= optct;
- argv += optct;
+ argv += optct;
+
+ for(c=0; c<argc; c++)
+ printf("%s\n", argv[c]);
+
/* Parse config file if declared TODO */
init_log(OPT_ARG(FILELOG));
}
+ if(HAVE_OPT(KOD)) {
+ kod_init_kod_db(OPT_ARG(KOD));
+ }
+
+ kod_init_kod_db("test.kod");
+
+
+
/* Considering employing a variable that prevents functions of doing anything until
* everything is initialized properly
*/
- struct addrinfo **resh;
+ struct addrinfo **resh = (struct addrinfo **) malloc(sizeof(struct addrinfo **));
int resc = resolve_hosts(argv, argc, resh);
- if(resc < 0) {
+ if(resc < 1) {
/* Error! Network down? */
}
- resc = filter_reachable(resh, resc);
-
- if(resc < 0) {
- /* All hosts not reachable!? */
- }
-
- resc = filter_ntp(resh, resc);
-
- if(resc < 0) {
- /* No valid ntp responses */
- }
-
/* Select a certain ntp server according to simple criteria? For now
* let's just pay attention to previous KoDs.
*/
-
+ int sync_data_suc = 0;
for(c=0; c<resc && !sync_data_suc; c++) {
- if(!kod_entry_exists(resh[c])) {
- sync_data_suc = on_wire(resh[c]);
+ if(!kod_entry_exists(inet_ntoa(resh[c]->ai_addr))) {
+ int ow_ret = on_wire(resh[c]);
+ sync_data_suc = 1;
}
else {
- /* Debug/Log output */
+ struct kod_entry **reason;
+ int kodc = search_entry(resh[c], reason);
+
+ printf("KoD package exists for %s, stopping any further communication.", inet_ntoa(resh[c]->ai_addr));
+
}
}
return 0;
}
+
+int
+on_wire (
+ struct addrinfo *host
+ )
+{
+ SOCKET sock;
+
+ struct pkt *x_pkt = (struct pkt *) malloc(sizeof(struct pkt));
+ struct pkt *r_pkt = (struct pkt *) malloc(sizeof(struct pkt));
+
+ struct timeval tv_t1, tv_t2;
+ struct timezone tz;
+ l_fp t1, t4;
+
+ int error = GETTIMEOFDAY(&tv_t1, &tz);
+
+#ifdef DEBUG
+ printf("tv_t1 sec: %i msec: %i\n", tv_t1.tv_sec, tv_t1.tv_usec);
+#endif
+
+ TVTOTS(&tv_t1, &(x_pkt->org));
+
+/* Data from a valid timestamp */
+/* unsigned char *tptr = &(x_pkt->org);
+
+ tptr[0] = 0xcc;
+ tptr[1] = 0x44;
+ tptr[2] = 0x87;
+ tptr[3] = 0x45;*/
+
+ x_pkt->stratum = 15;
+ x_pkt->ppoll = 8;
+ x_pkt->li_vn_mode = 0x1b;
+
+
+
+ /*in_addr_t foo = inet_addr("127.0.0.1"); */
+ create_socket(&sock, host->ai_addr);
+
+ int rsock = sendpkt(sock, host->ai_addr, x_pkt, 48);
+
+ int rpktl = recvpkt(sock, r_pkt, x_pkt);
+
+ close_socket(sock);
+
+ NTOHL_FP(&(r_pkt->rec), &(r_pkt->rec));
+ TSTOTV(&(r_pkt->rec), &tv_t2);
+
+ double time = 0.0;
+
+ LFPTOD(&(r_pkt->rec), time);
+
+ printf("\nrec: %d\n", time);
+
+
+
+ return 0;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
#include "header.h"
#include "log.h"
+#ifdef DEBUG
+#define WS80 do { \
+ register int wsc; \
+ for(wsc=0; wsc<80; wsc++) \
+ printf("-"); \
+ printf("\n"); \
+} while(0);
+#endif
+
+
-int resolve_hosts (char **hosts, int hostc, struct addrinfo **res) {
+
+int
+resolve_hosts (
+ char **hosts,
+ int hostc,
+ struct addrinfo **res
+ )
+{
register unsigned int a, b;
unsigned int entryc = 0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
+ hints.ai_socktype = SOCK_DGRAM;
/* hints.ai_protocol = IPPROTO_UDP; */
- error = getaddrinfo(hosts[a], "ntp", &hints, tres[a]);
+ error = getaddrinfo(hosts[a], "123", &hints, tres[a]);
if(error) {
size_t msg_length = strlen(hosts[a]) + 21;
for(dres=*tres[a]; dres; dres=dres->ai_next) {
entryc++;
#ifdef DEBUG
- printf("--------------------------------------------------------------------------------\n");
+ WS80
printf("Resolv No.: %i Result of getaddrinfo for %s:\n", entryc, hosts[a]);
printf("socktype: %i ", dres->ai_socktype);
printf("protocol: %i ", dres->ai_protocol);
printf("Prefered socktype: %i IP: %s\n", dres->ai_socktype, inet_ntoa(dres->ai_addr));
- printf("--------------------------------------------------------------------------------\n\n");
+ WS80
+ printf("\n");
#endif
}
}
struct addrinfo **result = (struct addrinfo **) malloc(sizeof(struct addrinfo**) * entryc);
for(a=0, b=0; a<hostc; a++) {
-#ifdef DEBUG
- printf("%i %i: Copying address information...\n", a, b);
-#endif
-
struct addrinfo *cur = *tres[a];
if(cur->ai_next == NULL) {
return entryc;
}
+void
+create_socket (
+ SOCKET *rsock,
+ struct sockaddr *dest
+ )
+{
+ *rsock = socket(dest->sa_family, SOCK_DGRAM, 0);
+
+#ifdef DEBUG
+ if(*rsock == -1)
+ printf("Failed to create UDP socket with family %i\n", dest->sa_family);
+#endif
+
+
+/* int error = bind(*rsock, dest, SOCKLEN(dest));
+
+ if(error != 0) {
+ *rsock = -1;
+#ifdef DEBUG
+ printf("Failed to bind %i %i socket to address %s\n", error, errno, inet_ntoa(dest));
+#endif
+ } */
+}
+
+void
+close_socket (
+ SOCKET rsock
+ )
+{
+ close(rsock);
+}
+
/* Send a packet */
void
sendpkt (
- struct sockaddr_storage *dest,
+ SOCKET rsock,
+ struct sockaddr *dest,
struct pkt *pkt,
int len
)
{
- int sock;
- char *foo = (char *) malloc(len);
+#ifdef DEBUG
+ register int a;
- snprintf(foo, len, "%s", (char *)pkt);
+ unsigned char *cpy = (unsigned char *) malloc(sizeof(char) * len);
+
+ for(a=0; a<len; a++)
+ cpy[a] = ((unsigned char *) pkt)[a];
- printf("%s\n", foo);
+ for(a=0; a<len; a++) {
+ if(a > 0 && a%8 == 0)
+ printf("\n");
- return;
+ printf("%x: %x \t", a, cpy[a]);
+ }
- int cc = sendto(sock, (char *)pkt, len, 0, (struct sockaddr *)dest,
+
+ printf("\nSending packet to %s...\n", inet_ntoa(dest));
+#endif
+
+ int cc = sendto(rsock, (char *)pkt, len, 0, dest,
SOCKLEN(dest));
if (cc == SOCKET_ERROR) {
+#ifdef DEBUG
+ printf("Socket error: %i. Couldn't send packet!\n", cc);
+#endif
+
if (errno != EWOULDBLOCK && errno != ENOBUFS) {
}
}
-
+#ifdef DEBUG
+ else {
+ printf("Packet sent.\n");
+ }
+#endif
}
int
char *done
)
{
- socklen_t slen = SOCKLEN(rsock);
+ socklen_t slen = SOCKLEN(&rsock);
- int recvc = recvfrom(rsock, rdata, rdata_length, MSG_DONTWAIT,
+#ifdef DEBUG
+ printf("Trying to receive data from...\n");
+#endif
+
+ int recvc = recvfrom(rsock, rdata, rdata_length, 0,
sender, &slen);
+#ifdef DEBUG
+ printf("recvfrom returned...\n");
+#endif
+
+#ifdef DEBUG
+ register int a;
+
+ if(recvc > 0) {
+ printf("Received %i bytes from %s:\n", recvc, inet_ntoa(sender));
+ unsigned char *cpy = (unsigned char *) malloc(sizeof(char) * recvc);
+ for(a=0; a<recvc; a++)
+ cpy[a] = ((unsigned char *) rdata)[a];
+
+ for(a=0; a<recvc; a++) {
+ if(a > 0 && a%8 == 0)
+ printf("\n");
+
+ printf("%i: %x \t", a, cpy[a]);
+ }
+ }
+ else {
+ printf("Failure, recvc: %i\n", recvc);
+ }
+#endif
/* Remove this when found a reasonable max. size. For now
* notify when there's data left to fetch
*/
if ((PKT_MODE(rpkt->li_vn_mode) != MODE_SERVER
&& PKT_MODE(rpkt->li_vn_mode) != MODE_PASSIVE)
|| rpkt->stratum >= STRATUM_UNSPEC) {
- if (debug)
- fprintf(stderr, "receive: mode %d stratum %d\n",
+#ifdef DEBUG
+ printf("receive: mode %d stratum %d\n",
PKT_MODE(rpkt->li_vn_mode), rpkt->stratum);
+#endif
return -1;
}