7 #include "kod_management.h"
10 #include "ntp_stdlib.h"
13 int kod_init
= 0, kod_db_cnt
= 0;
14 const char *kod_db_file
;
15 struct kod_entry
**kod_db
; /* array of pointers to kod_entry */
19 * Search for a KOD entry
24 struct kod_entry
**dst
27 register int a
, b
, resc
= 0;
29 for (a
= 0; a
< kod_db_cnt
; a
++)
30 if (!strcmp(kod_db
[a
]->hostname
, hostname
))
38 *dst
= emalloc(resc
* sizeof(**dst
));
41 for (a
= 0; a
< kod_db_cnt
; a
++)
42 if (!strcmp(kod_db
[a
]->hostname
, hostname
)) {
43 (*dst
)[b
] = *kod_db
[a
];
54 char *type
/* 4 bytes not \0 terminated */
58 struct kod_entry
*pke
;
60 pke
= emalloc(sizeof(*pke
));
61 pke
->timestamp
= time(NULL
);
62 memcpy(pke
->type
, type
, 4);
63 pke
->type
[sizeof(pke
->type
) - 1] = '\0';
64 strncpy(pke
->hostname
, hostname
,
65 sizeof(pke
->hostname
));
66 pke
->hostname
[sizeof(pke
->hostname
) - 1] = '\0';
69 * insert in address ("hostname") order to find duplicates
71 for (n
= 0; n
< kod_db_cnt
; n
++)
72 if (strcmp(kod_db
[n
]->hostname
, pke
->hostname
) >= 0)
76 0 == strcmp(kod_db
[n
]->hostname
, pke
->hostname
)) {
77 kod_db
[n
]->timestamp
= pke
->timestamp
;
82 kod_db
= erealloc(kod_db
, kod_db_cnt
* sizeof(kod_db
[0]));
83 if (n
!= kod_db_cnt
- 1)
84 memmove(&kod_db
[n
+ 1], &kod_db
[n
],
85 sizeof(kod_db
[0]) * ((kod_db_cnt
- 1) - n
));
98 for (a
= 0; a
< kod_db_cnt
; a
++)
99 if (!strcmp(kod_db
[a
]->hostname
, hostname
)
100 && !strcmp(kod_db
[a
]->type
, type
))
110 memmove(&kod_db
[a
], &kod_db
[a
+ 1],
111 (kod_db_cnt
- a
) * sizeof(kod_db
[0]));
123 db_s
= fopen(kod_db_file
, "w");
126 * If opening fails, blindly attempt to create each directory
127 * in the path first, then retry the open.
129 if (NULL
== db_s
&& strlen(kod_db_file
)) {
130 dirmode
= S_IRUSR
| S_IWUSR
| S_IXUSR
133 pch
= strchr(kod_db_file
+ 1, DIR_SEP
);
134 while (NULL
!= pch
) {
136 mkdir(kod_db_file
, dirmode
);
138 pch
= strchr(pch
+ 1, DIR_SEP
);
140 db_s
= fopen(kod_db_file
, "w");
146 snprintf(msg
, sizeof(msg
),
147 "Can't open KOD db file %s for writing!",
157 for (a
= 0; a
< kod_db_cnt
; a
++) {
158 fprintf(db_s
, "%16.16llx %s %s\n", (unsigned long long)
159 kod_db
[a
]->timestamp
, kod_db
[a
]->type
,
160 kod_db
[a
]->hostname
);
174 * Max. of 254 characters for hostname, 10 for timestamp, 4 for
175 * kisscode, 2 for spaces, 1 for \n, and 1 for \0
177 char fbuf
[254+10+4+2+1+1];
180 unsigned long long ull
;
184 atexit(write_kod_db
);
187 printf("Initializing KOD DB...\n");
190 kod_db_file
= estrdup(db_file
);
193 db_s
= fopen(db_file
, "r");
198 snprintf(msg
, sizeof(msg
), "kod_init_kod_db(): Cannot open KoD db file %s", db_file
);
208 if (ENABLED_OPT(NORMALVERBOSE
))
209 printf("Starting to read KoD file %s...\n", db_file
);
210 /* First let's see how many entries there are and check for right syntax */
212 while (!feof(db_s
) && NULL
!= fgets(fbuf
, sizeof(fbuf
), db_s
)) {
214 /* ignore blank lines */
220 for (a
= 0; a
< len
; a
++) {
224 if ('\n' == fbuf
[a
]) {
226 if (strcmp(db_file
, "/dev/null")) {
228 snprintf(msg
, sizeof(msg
),
229 "Syntax error in KoD db file %s in line %i (missing space)",
230 db_file
, kod_db_cnt
+ 1);
246 if (0 == kod_db_cnt
) {
248 printf("KoD DB %s empty.\n", db_file
);
255 printf("KoD DB %s contains %d entries, reading...\n", db_file
, kod_db_cnt
);
260 kod_db
= emalloc(sizeof(kod_db
[0]) * kod_db_cnt
);
262 /* Read contents of file */
264 !feof(db_s
) && !ferror(db_s
) && b
< kod_db_cnt
;
267 str_ptr
= fgets(fbuf
, sizeof(fbuf
), db_s
);
268 if (NULL
== str_ptr
) {
273 /* ignore blank lines */
274 if ('\n' == fbuf
[0]) {
279 kod_db
[b
] = emalloc(sizeof(*kod_db
[b
]));
281 if (3 != sscanf(fbuf
, "%llx %4s %254s", &ull
,
282 kod_db
[b
]->type
, kod_db
[b
]->hostname
)) {
290 kod_db
[b
]->timestamp
= (time_t)ull
;
293 if (ferror(db_s
) || error
) {
297 snprintf(msg
, sizeof(msg
), "An error occured while parsing the KoD db file %s", db_file
);
309 for (a
= 0; a
< kod_db_cnt
; a
++)
310 printf("KoD entry %d: %s at %llx type %s\n", a
,
312 (unsigned long long)kod_db
[a
]->timestamp
,