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 "unaligned.h"
22 #define MIN_BUFFER_SIZE 64
23 #define DEFAULT_BUFFER_SIZE 4096
24 #define MAX_BUFFER_SIZE 131072
26 static char *hexdigits
="0123456789abcdef";
28 uint32_t message_level
=M_WARNING
|M_ERROR
|M_FATAL
;
29 uint32_t syslog_level
=M_WARNING
|M_ERROR
|M_FATAL
;
30 static uint32_t current_phase
=0;
35 struct phase_hook
*next
;
38 static struct phase_hook
*hooks
[NR_PHASES
]={NULL
,};
40 static void vMessage(uint32_t class, char *message
, va_list args
)
43 if (class & message_level
) {
44 if (class&M_FATAL
|| class&M_ERROR
|| class&M_WARNING
) {
47 vfprintf(dest
,message
,args
);
49 /* XXX do something about syslog output here */
51 /* Maybe send message to syslog */
52 vsprintf(buff
, message
, args
);
53 /* XXX Send each line as a separate log entry */
54 log(syslog_prio
[level
], buff
);
58 void Message(uint32_t class, char *message
, ...)
64 vMessage(class,message
,ap
);
69 static void vfatal(int status
, bool_t perror
, char *message
, va_list args
)
75 enter_phase(PHASE_SHUTDOWN
);
77 Message(M_FATAL
, "secnet fatal error: ");
78 vMessage(M_FATAL
, message
, args
);
79 Message(M_FATAL
, ": %s\n",strerror(err
));
82 Message(M_FATAL
, "secnet fatal error: ");
83 vMessage(M_FATAL
,message
,args
);
88 void fatal(char *message
, ...)
91 va_start(args
,message
);
92 vfatal(current_phase
,False
,message
,args
);
96 void fatal_status(int status
, char *message
, ...)
99 va_start(args
,message
);
100 vfatal(status
,False
,message
,args
);
104 void fatal_perror(char *message
, ...)
107 va_start(args
,message
);
108 vfatal(current_phase
,True
,message
,args
);
112 void fatal_perror_status(int status
, char *message
, ...)
115 va_start(args
,message
);
116 vfatal(status
,True
,message
,args
);
120 void cfgfatal(struct cloc loc
, string_t facility
, char *message
, ...)
124 va_start(args
,message
);
126 enter_phase(PHASE_SHUTDOWN
);
128 if (loc
.file
&& loc
.line
) {
129 Message(M_FATAL
, "config error (%s, %s:%d): ",facility
,loc
.file
,
131 } else if (!loc
.file
&& loc
.line
) {
132 Message(M_FATAL
, "config error (%s, line %d): ",facility
,loc
.line
);
134 Message(M_FATAL
, "config error (%s): ",facility
);
137 vMessage(M_FATAL
,message
,args
);
142 char *safe_strdup(char *s
, char *message
)
147 fatal_perror(message
);
152 void *safe_malloc(size_t size
, char *message
)
157 fatal_perror(message
);
162 /* Convert a buffer into its MP_INT representation */
163 void read_mpbin(MP_INT
*a
, uint8_t *bin
, int binsize
)
168 buff
=safe_malloc(binsize
*2 + 1,"read_mpbin");
170 for (i
=0; i
<binsize
; i
++) {
171 buff
[i
*2]=hexdigits
[(bin
[i
] & 0xf0) >> 4];
172 buff
[i
*2+1]=hexdigits
[(bin
[i
] & 0xf)];
176 mpz_set_str(a
, buff
, 16);
180 /* Convert a MP_INT into a hex string */
181 char *write_mpstring(MP_INT
*a
)
185 buff
=safe_malloc(mpz_sizeinbase(a
,16)+2,"write_mpstring");
186 mpz_get_str(buff
, 16, a
);
190 static uint8_t hexval(uint8_t c
)
219 /* Convert a MP_INT into a buffer; return length; truncate if necessary */
220 uint32_t write_mpbin(MP_INT
*a
, uint8_t *buffer
, uint32_t buflen
)
225 if (buflen
==0) return 0;
226 hb
=write_mpstring(a
);
231 /* The number starts with a half-byte */
232 buffer
[i
++]=hexval(hb
[j
++]);
234 for (; hb
[j
] && i
<buflen
; i
++) {
235 buffer
[i
]=(hexval(hb
[j
])<<4)|hexval(hb
[j
+1]);
242 bool_t
subnet_match(struct subnet
*s
, uint32_t address
)
244 return (s
->prefix
==(address
&s
->mask
));
247 bool_t
subnet_matches_list(struct subnet_list
*list
, uint32_t address
)
250 for (i
=0; i
<list
->entries
; i
++) {
251 if (list
->list
[i
].prefix
== (address
&list
->list
[i
].mask
)) return True
;
256 bool_t
subnets_intersect(struct subnet a
, struct subnet b
)
258 uint32_t mask
=a
.mask
&b
.mask
;
259 return ((a
.prefix
&mask
)==(b
.prefix
&mask
));
262 bool_t
subnet_intersects_with_list(struct subnet a
, struct subnet_list
*b
)
266 for (i
=0; i
<b
->entries
; i
++) {
267 if (subnets_intersect(a
,b
->list
[i
])) return True
;
272 bool_t
subnet_lists_intersect(struct subnet_list
*a
, struct subnet_list
*b
)
275 for (i
=0; i
<a
->entries
; i
++) {
276 if (subnet_intersects_with_list(a
->list
[i
],b
)) return True
;
281 /* The string buffer must be at least 16 bytes long */
282 string_t
ipaddr_to_string(uint32_t addr
)
287 s
=safe_malloc(16,"ipaddr_to_string");
292 snprintf(s
, 16, "%d.%d.%d.%d", a
, b
, c
, d
);
296 string_t
subnet_to_string(struct subnet
*sn
)
298 uint32_t mask
=sn
->mask
, addr
=sn
->prefix
;
303 s
=safe_malloc(19,"subnet_to_string");
308 for (i
=0; mask
; i
++) {
312 fatal("subnet_to_string: invalid subnet structure!\n");
314 snprintf(s
, 19, "%d.%d.%d.%d/%d", a
, b
, c
, d
, sn
->len
);
318 int sys_cmd(const char *path
, char *arg
, ...)
327 /* Parent -> wait for child */
332 /* Child -> exec command */
335 while ((args
[i
++]=va_arg(ap
,char *)));
340 fatal_perror("sys_cmd(%s,%s,...)");
347 /* Take a list of log closures and merge them */
350 struct loglist
*next
;
353 static void log_vmulti(void *state
, int priority
, char *message
, va_list args
)
355 struct loglist
*st
=state
, *i
;
357 for (i
=st
; i
; i
=i
->next
) {
358 i
->l
->vlog(i
->l
->st
,priority
,message
,args
);
362 static void log_multi(void *st
, int priority
, char *message
, ...)
366 va_start(ap
,message
);
368 log_vmulti(st
,priority
,message
,ap
);
373 struct log_if
*init_log(list_t
*ll
)
378 struct loglist
*l
=NULL
, *n
;
381 while ((item
=list_elem(ll
,i
++))) {
382 if (item
->type
!=t_closure
) {
383 cfgfatal(item
->loc
,"init_log","item is not a closure");
385 cl
=item
->data
.closure
;
386 if (cl
->type
!=CL_LOG
) {
387 cfgfatal(item
->loc
,"init_log","closure is not a logger");
389 n
=safe_malloc(sizeof(*n
),"init_log");
395 fatal("init_log: none of the items in the list are loggers");
397 r
=safe_malloc(sizeof(*r
), "init_log");
410 static void logfile_vlog(void *state
, int priority
, char *message
,
413 struct logfile
*st
=state
;
415 vfprintf(st
->f
,message
,args
);
419 static void logfile_log(void *state
, int priority
, char *message
, ...)
423 va_start(ap
,message
);
424 logfile_vlog(state
,priority
,message
,ap
);
428 static list_t
*logfile_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
433 st
=safe_malloc(sizeof(*st
),"logfile_apply");
434 st
->cl
.description
="logfile";
437 st
->cl
.interface
=&st
->ops
;
439 st
->ops
.log
=logfile_log
;
440 st
->ops
.vlog
=logfile_vlog
;
441 st
->f
=stderr
; /* XXX ignore args */
443 return new_closure(&st
->cl
);
446 static char *phases
[NR_PHASES
]={
451 "PHASE_GETRESOURCES",
457 void enter_phase(uint32_t new_phase
)
459 struct phase_hook
*i
;
461 if (hooks
[new_phase
])
462 Message(M_DEBUG_PHASE
,"Running hooks for %s...\n", phases
[new_phase
]);
463 current_phase
=new_phase
;
465 for (i
=hooks
[new_phase
]; i
; i
=i
->next
)
466 i
->fn(i
->state
, new_phase
);
467 Message(M_DEBUG_PHASE
,"Now in %s\n",phases
[new_phase
]);
470 bool_t
add_hook(uint32_t phase
, hook_fn
*fn
, void *state
)
472 struct phase_hook
*h
;
474 h
=safe_malloc(sizeof(*h
),"add_hook");
477 h
->next
=hooks
[phase
];
482 bool_t
remove_hook(uint32_t phase
, hook_fn
*fn
, void *state
)
484 fatal("remove_hook: not implemented\n");
489 void log(struct log_if
*lf
, int priority
, char *message
, ...)
493 va_start(ap
,message
);
494 lf
->vlog(lf
->st
,priority
,message
,ap
);
500 struct buffer_if ops
;
503 void buffer_assert_free(struct buffer_if
*buffer
, string_t file
, uint32_t line
)
506 fatal("BUF_ASSERT_FREE, %s line %d, owned by %s\n",
507 file
,line
,buffer
->owner
);
511 void buffer_assert_used(struct buffer_if
*buffer
, string_t file
, uint32_t line
)
514 fatal("BUF_ASSERT_USED, %s line %d, last owned by %s\n",
515 file
,line
,buffer
->owner
);
519 void buffer_init(struct buffer_if
*buffer
, uint32_t max_start_pad
)
521 buffer
->start
=buffer
->base
+max_start_pad
;
525 void *buf_append(struct buffer_if
*buf
, uint32_t amount
) {
527 p
=buf
->start
+ buf
->size
;
532 void *buf_prepend(struct buffer_if
*buf
, uint32_t amount
) {
534 return buf
->start
-=amount
;
537 void *buf_unappend(struct buffer_if
*buf
, uint32_t amount
) {
538 if (buf
->size
< amount
) return 0;
539 return buf
->start
+(buf
->size
-=amount
);
542 void *buf_unprepend(struct buffer_if
*buf
, uint32_t amount
) {
550 /* Append a two-byte length and the string to the buffer. Length is in
551 network byte order. */
552 void buf_append_string(struct buffer_if
*buf
, string_t s
)
557 buf_append_uint16(buf
,len
);
558 memcpy(buf_append(buf
,len
),s
,len
);
561 void buffer_new(struct buffer_if
*buf
, uint32_t len
)
571 buf
->base
=safe_malloc(len
,"buffer_new");
574 static list_t
*buffer_apply(closure_t
*self
, struct cloc loc
, dict_t
*context
,
580 bool_t lockdown
=False
;
581 uint32_t len
=DEFAULT_BUFFER_SIZE
;
583 st
=safe_malloc(sizeof(*st
),"buffer_apply");
584 st
->cl
.description
="buffer";
585 st
->cl
.type
=CL_BUFFER
;
587 st
->cl
.interface
=&st
->ops
;
589 /* First argument, if present, is buffer length */
590 item
=list_elem(args
,0);
592 if (item
->type
!=t_number
) {
593 cfgfatal(st
->ops
.loc
,"buffer","first parameter must be a "
594 "number (buffer size)\n");
596 len
=item
->data
.number
;
597 if (len
<MIN_BUFFER_SIZE
) {
598 cfgfatal(st
->ops
.loc
,"buffer","ludicrously small buffer size\n");
600 if (len
>MAX_BUFFER_SIZE
) {
601 cfgfatal(st
->ops
.loc
,"buffer","ludicrously large buffer size\n");
604 /* Second argument, if present, is a dictionary */
605 item
=list_elem(args
,1);
607 if (item
->type
!=t_dict
) {
608 cfgfatal(st
->ops
.loc
,"buffer","second parameter must be a "
611 dict
=item
->data
.dict
;
612 lockdown
=dict_read_bool(dict
,"lockdown",False
,"buffer",st
->ops
.loc
,
616 buffer_new(&st
->ops
,len
);
618 /* XXX mlock the buffer if possible */
621 return new_closure(&st
->cl
);
624 init_module util_module
;
625 void util_module(dict_t
*dict
)
627 add_closure(dict
,"logfile",logfile_apply
);
628 add_closure(dict
,"sysbuffer",buffer_apply
);