2 * Revision 1.2 1996/04/14 16:34:36 sde1000
4 * mpbin/mpstring functions moved from dh.c
6 * Revision 1.1 1996/03/14 17:05:03 sde1000
20 #include <sys/types.h>
25 #define MIN_BUFFER_SIZE 64
26 #define DEFAULT_BUFFER_SIZE 4096
27 #define MAX_BUFFER_SIZE 131072
29 static char *hexdigits="0123456789abcdef";
31 uint32_t message_level=M_WARNING|M_ERROR|M_FATAL;
32 uint32_t syslog_level=M_WARNING|M_ERROR|M_FATAL;
33 static uint32_t current_phase=0;
38 struct phase_hook *next;
41 static struct phase_hook *hooks[NR_PHASES]={NULL,};
43 static void vMessage(uint32_t class, char *message, va_list args)
46 if (class & message_level) {
47 if (class&M_FATAL || class&M_ERROR || class&M_WARNING) {
50 vfprintf(dest,message,args);
52 /* XXX do something about syslog output here */
54 /* Maybe send message to syslog */
55 vsprintf(buff, message, args);
56 /* XXX Send each line as a separate log entry */
57 log(syslog_prio[level], buff);
61 void Message(uint32_t class, char *message, ...)
67 vMessage(class,message,ap);
72 static void vfatal(int status, bool_t perror, char *message, va_list args)
78 enter_phase(PHASE_SHUTDOWN);
80 Message(M_FATAL, "secnet fatal error: ");
81 vMessage(M_FATAL, message, args);
82 Message(M_FATAL, ": %s\n",strerror(err));
85 Message(M_FATAL, "secnet fatal error: ");
86 vMessage(M_FATAL,message,args);
91 void fatal(char *message, ...)
94 va_start(args,message);
95 vfatal(current_phase,False,message,args);
99 void fatal_status(int status, char *message, ...)
102 va_start(args,message);
103 vfatal(status,False,message,args);
107 void fatal_perror(char *message, ...)
110 va_start(args,message);
111 vfatal(current_phase,True,message,args);
115 void fatal_perror_status(int status, char *message, ...)
118 va_start(args,message);
119 vfatal(status,True,message,args);
123 void cfgfatal(struct cloc loc, string_t facility, char *message, ...)
127 va_start(args,message);
129 enter_phase(PHASE_SHUTDOWN);
131 if (loc.file && loc.line) {
132 Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
134 } else if (!loc.file && loc.line) {
135 Message(M_FATAL, "config error (%s, line %d): ",facility,loc.line);
137 Message(M_FATAL, "config error (%s): ",facility);
140 vMessage(M_FATAL,message,args);
145 char *safe_strdup(char *s, char *message)
150 fatal_perror(message);
155 void *safe_malloc(size_t size, char *message)
160 fatal_perror(message);
165 /* Convert a buffer into its MP_INT representation */
166 void read_mpbin(MP_INT *a, uint8_t *bin, int binsize)
171 buff=safe_malloc(binsize*2 + 1,"read_mpbin");
173 for (i=0; i<binsize; i++) {
174 buff[i*2]=hexdigits[(bin[i] & 0xf0) >> 4];
175 buff[i*2+1]=hexdigits[(bin[i] & 0xf)];
179 mpz_set_str(a, buff, 16);
183 /* Convert a MP_INT into a hex string */
184 char *write_mpstring(MP_INT *a)
188 buff=safe_malloc(mpz_sizeinbase(a,16)+2,"write_mpstring");
189 mpz_get_str(buff, 16, a);
193 static uint8_t hexval(uint8_t c)
222 /* Convert a MP_INT into a buffer; return length; truncate if necessary */
223 uint32_t write_mpbin(MP_INT *a, uint8_t *buffer, uint32_t buflen)
228 if (buflen==0) return 0;
229 hb=write_mpstring(a);
234 /* The number starts with a half-byte */
235 buffer[i++]=hexval(hb[j++]);
237 for (; hb[j] && i<buflen; i++) {
238 buffer[i]=(hexval(hb[j])<<4)|hexval(hb[j+1]);
245 bool_t subnet_match(struct subnet_list *list, uint32_t address)
248 for (i=0; i<list->entries; i++) {
249 if (list->list[i].prefix == (address&list->list[i].mask)) return True;
254 /* The string buffer must be at least 16 bytes long */
255 string_t ipaddr_to_string(uint32_t addr)
260 s=safe_malloc(16,"ipaddr_to_string");
265 snprintf(s, 16, "%d.%d.%d.%d", a, b, c, d);
269 string_t subnet_to_string(struct subnet *sn)
271 uint32_t mask=sn->mask, addr=sn->prefix;
276 s=safe_malloc(19,"subnet_to_string");
281 for (i=0; mask; i++) {
284 snprintf(s, 19, "%d.%d.%d.%d/%d", a, b, c, d, i);
288 int sys_cmd(const char *path, char *arg, ...)
297 /* Parent -> wait for child */
302 /* Child -> exec command */
305 while ((args[i++]=va_arg(ap,char *)));
310 fatal_perror("sys_cmd(%s,%s,...)");
317 /* Take a list of log closures and merge them */
320 struct loglist *next;
323 static void log_vmulti(void *state, int priority, char *message, va_list args)
325 struct loglist *st=state, *i;
327 for (i=st; i; i=i->next) {
328 i->l->vlog(i->l->st,priority,message,args);
332 static void log_multi(void *st, int priority, char *message, ...)
336 va_start(ap,message);
338 log_vmulti(st,priority,message,ap);
343 struct log_if *init_log(list_t *ll)
348 struct loglist *l=NULL, *n;
351 while ((item=list_elem(ll,i++))) {
352 if (item->type!=t_closure) {
353 cfgfatal(item->loc,"init_log","item is not a closure");
355 cl=item->data.closure;
356 if (cl->type!=CL_LOG) {
357 cfgfatal(item->loc,"init_log","closure is not a logger");
359 n=safe_malloc(sizeof(*n),"init_log");
365 fatal("init_log: none of the items in the list are loggers");
367 r=safe_malloc(sizeof(*r), "init_log");
380 static void logfile_vlog(void *state, int priority, char *message,
383 struct logfile *st=state;
385 vfprintf(st->f,message,args);
389 static void logfile_log(void *state, int priority, char *message, ...)
393 va_start(ap,message);
394 logfile_vlog(state,priority,message,ap);
398 static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context,
403 st=safe_malloc(sizeof(*st),"logfile_apply");
404 st->cl.description="logfile";
407 st->cl.interface=&st->ops;
409 st->ops.log=logfile_log;
410 st->ops.vlog=logfile_vlog;
411 st->f=stderr; /* XXX ignore args */
413 return new_closure(&st->cl);
416 static char *phases[NR_PHASES]={
426 void enter_phase(uint32_t new_phase)
428 struct phase_hook *i;
430 Message(M_DEBUG_PHASE,"entering %s... ", phases[new_phase]);
431 current_phase=new_phase;
433 for (i=hooks[new_phase]; i; i=i->next)
434 i->fn(i->state, new_phase);
435 Message(M_DEBUG_PHASE,"now in %s\n",phases[new_phase]);
438 bool_t add_hook(uint32_t phase, hook_fn *fn, void *state)
440 struct phase_hook *h;
442 h=safe_malloc(sizeof(*h),"add_hook");
445 h->next=hooks[phase];
450 bool_t remove_hook(uint32_t phase, hook_fn *fn, void *state)
452 fatal("remove_hook: not implemented\n");
457 void log(struct log_if *lf, int priority, char *message, ...)
461 va_start(ap,message);
462 lf->vlog(lf->st,priority,message,ap);
468 struct buffer_if ops;
471 void buffer_assert_free(struct buffer_if *buffer, string_t file, uint32_t line)
474 fatal("BUF_ASSERT_FREE, %s line %d, owned by %s\n",
475 file,line,buffer->owner);
479 void buffer_assert_used(struct buffer_if *buffer, string_t file, uint32_t line)
482 fatal("BUF_ASSERT_USED, %s line %d, last owned by %s\n",
483 file,line,buffer->owner);
487 void buffer_init(struct buffer_if *buffer, uint32_t max_start_pad)
489 buffer->start=buffer->base+max_start_pad;
493 void *buf_append(struct buffer_if *buf, uint32_t amount) {
495 p=buf->start + buf->size;
500 void *buf_prepend(struct buffer_if *buf, uint32_t amount) {
502 return buf->start-=amount;
505 void *buf_unappend(struct buffer_if *buf, uint32_t amount) {
506 if (buf->size < amount) return 0;
507 return buf->start+(buf->size-=amount);
510 void *buf_unprepend(struct buffer_if *buf, uint32_t amount) {
518 /* Append a two-byte length and the string to the buffer. Length is in
519 network byte order. */
520 void buf_append_string(struct buffer_if *buf, string_t s)
525 *(uint16_t *)buf_append(buf,2)=htons(len);
526 memcpy(buf_append(buf,len),s,len);
529 void buffer_new(struct buffer_if *buf, uint32_t len)
539 buf->base=safe_malloc(len,"buffer_new");
542 static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context,
548 bool_t lockdown=False;
549 uint32_t len=DEFAULT_BUFFER_SIZE;
551 st=safe_malloc(sizeof(*st),"buffer_apply");
552 st->cl.description="buffer";
553 st->cl.type=CL_BUFFER;
555 st->cl.interface=&st->ops;
557 /* First argument, if present, is buffer length */
558 item=list_elem(args,0);
560 if (item->type!=t_number) {
561 cfgfatal(st->ops.loc,"buffer","first parameter must be a "
562 "number (buffer size)\n");
564 len=item->data.number;
565 if (len<MIN_BUFFER_SIZE) {
566 cfgfatal(st->ops.loc,"buffer","ludicrously small buffer size\n");
568 if (len>MAX_BUFFER_SIZE) {
569 cfgfatal(st->ops.loc,"buffer","ludicrously large buffer size\n");
572 /* Second argument, if present, is a dictionary */
573 item=list_elem(args,1);
575 if (item->type!=t_dict) {
576 cfgfatal(st->ops.loc,"buffer","second parameter must be a "
579 dict=item->data.dict;
580 lockdown=dict_read_bool(dict,"lockdown",False,"buffer",st->ops.loc,
584 buffer_new(&st->ops,len);
586 Message(M_WARNING,"buffer: XXX lockdown\n");
589 return new_closure(&st->cl);
592 init_module util_module;
593 void util_module(dict_t *dict)
595 add_closure(dict,"logfile",logfile_apply);
596 add_closure(dict,"sysbuffer",buffer_apply);