113 #include <sys/socket.h>
127 #define IP_MATCH_CODE_OPEN '['
128 #define IP_MATCH_CODE_CLOSE ']'
129 #define IP_MATCH_CODE_OVAL 'N'
130 #define IP_MATCH_CODE_RANGE 'R'
131 #define IP_MATCH_CODE_EOF '\0'
132 #define IP_MATCH_CODE_ERR 256
137 #define STR vstring_str
138 #define LEN VSTRING_LEN
147 return (memcpy(dst,
STR(byte_codes),
LEN(byte_codes)));
154 const char *myname =
"ip_match_dump";
155 const unsigned char *bp;
162 if (*byte_codes != AF_INET)
163 msg_panic(
"%s: malformed byte-code header", myname);
171 bp = (
const unsigned char *) byte_codes + 1;
204 msg_panic(
"%s: unexpected byte code (decimal %d) "
205 "after \"%s\"", myname, ch,
STR(printable));
218 msg_panic(
"%s: unexpected byte code (decimal %d) after \"%s\"",
219 myname, ch,
STR(printable));
225 if (++octet_count == 4) {
227 msg_panic(
"%s: unexpected byte code (decimal %d) after \"%s\"",
228 myname, ch,
STR(printable));
229 return (
STR(printable));
232 msg_panic(
"%s: truncated byte code after \"%s\"",
233 myname,
STR(printable));
244 static char *ip_match_print_code_prefix(
const char *byte_codes,
size_t len)
263 fmt = (*byte_codes == AF_INET ?
"%d " :
"%02x ");
264 for (bp = byte_codes; bp < byte_codes + len; bp++)
267 return (
STR(printable));
274 const char *myname =
"ip_match_execute";
275 const unsigned char *bp;
276 const unsigned char *ap;
284 if (*byte_codes != AF_INET)
285 msg_panic(
"%s: malformed byte-code header (decimal %d)",
286 myname, *(
const unsigned char *) byte_codes);
292 bp = (
const unsigned char *) byte_codes + 1;
293 ap = (
const unsigned char *) addr_bytes;
295 for (octet_count = 0; octet_count < 4; octet_count++, ap++) {
316 matched = (*ap >= bp[0] && *ap <= bp[1]);
322 matched = (*ap == *bp);
331 size_t len = (
const char *) bp - byte_codes - 1;
333 msg_panic(
"%s: unexpected byte code (decimal %d) "
334 "after \"%s\"", myname, ch,
335 ip_match_print_code_prefix(byte_codes, len));
346 size_t len = (
const char *) bp - byte_codes - 1;
348 msg_panic(
"%s: unexpected byte code (decimal %d) after \"%s\"",
349 myname, ch, ip_match_print_code_prefix(byte_codes, len));
357 static int ip_match_next_token(
char **pstart,
char **psaved_start,
int *poval)
367 #define IP_MATCH_RETURN_TOK(next, type) \
368 do { *pstart = (char *) (next); return (type); } while (0)
373 #define IP_MATCH_RETURN_TOK_VAL(next, type, oval) do { \
374 *poval = (oval); IP_MATCH_RETURN_TOK((next), type); \
381 *psaved_start = *pstart;
382 cp = (
unsigned char *) *pstart;
386 for (cp += 1;
ISDIGIT(*cp); cp++) {
424 start_width = here - start;
425 here_width = next - here;
427 start_width, start_width == 0 ?
"" : start,
428 here_width, here_width == 0 ?
"" : here, next);
447 #define FIND_TERMINATOR(start, cp) do { \
449 for (cp = (start) ; *cp; cp++) { \
450 if (*cp == '[') _level++; \
451 if (*cp != ']') continue; \
452 if (--_level == 0) break; \
459 if (*pattern ==
'[') {
463 return (
STR(byte_codes));
476 return (
STR(byte_codes));
495 switch (token_type = ip_match_next_token(&cp, &saved_cp, &oval)) {
512 token_type = ip_match_next_token(&cp, &saved_cp, &oval);
515 look_ahead = ip_match_next_token(&cp, &saved_cp, &oval);
517 if (look_ahead ==
'.') {
519 if (ip_match_next_token(&cp, &saved_cp, &oval) ==
'.'
520 && ip_match_next_token(&cp, &saved_cp, &oval)
522 && saved_oval <= oval) {
527 ip_match_next_token(&cp, &saved_cp, &oval);
529 ipmatch_print_parse_error(byte_codes, pattern,
531 "numeric range error");
532 return (
STR(byte_codes));
541 token_type = look_ahead;
542 if (token_type ==
';') {
547 ipmatch_print_parse_error(byte_codes, pattern,
549 "need \";\" or \"%c\"",
551 return (
STR(byte_codes));
554 ipmatch_print_parse_error(byte_codes, pattern, saved_cp, cp,
555 "need decimal number 0..255");
556 return (
STR(byte_codes));
566 ipmatch_print_parse_error(byte_codes, pattern, saved_cp, cp,
567 "need decimal number 0..255 or \"%c\"",
569 return (
STR(byte_codes));
576 if (octet_count == 4) {
578 (void) ip_match_next_token(&cp, &saved_cp, &oval);
579 ipmatch_print_parse_error(byte_codes, pattern, saved_cp, cp,
580 "garbage after pattern");
581 return (
STR(byte_codes));
590 if (ip_match_next_token(&cp, &saved_cp, &oval) !=
'.') {
591 ipmatch_print_parse_error(byte_codes, pattern, saved_cp, cp,
593 return (
STR(byte_codes));
603 #include <sys/socket.h>
604 #include <netinet/in.h>
605 #include <arpa/inet.h>
612 int main(
int argc,
char **argv)
620 int echo_input = !isatty(0);
627 bufp =
STR(line_buf);
634 if ((user_pattern =
mystrtok(&bufp,
" \t")) == 0)
651 while ((user_address =
mystrtok(&bufp,
" \t")) != 0) {
652 struct in_addr netw_addr;
654 switch (
inet_pton(AF_INET, user_address, &netw_addr)) {
658 (
char *) &netw_addr.s_addr) ?
#define vstring_fgets_nonl(s, p)
#define IP_MATCH_CODE_ERR
NORETURN msg_panic(const char *fmt,...)
#define IP_MATCH_CODE_RANGE
int main(int argc, char **argv)
int ip_match_execute(const char *byte_codes, const char *addr_bytes)
char * mystrtok(char **src, const char *sep)
#define IP_MATCH_CODE_OPEN
char * ip_match_save(const VSTRING *byte_codes)
#define VSTRING_ADDCH(vp, ch)
VSTRING * vstring_sprintf_append(VSTRING *vp, const char *format,...)
VSTRING * vstring_vsprintf(VSTRING *vp, const char *format, va_list ap)
VSTREAM * vstream_printf(const char *fmt,...)
#define VSTRING_RESET(vp)
VSTRING * vstring_alloc(ssize_t len)
int inet_pton(int af, const char *src, void *dst)
#define FIND_TERMINATOR(start, cp)
VSTRING * vstring_sprintf(VSTRING *vp, const char *format,...)
int vstream_fflush(VSTREAM *stream)
#define IP_MATCH_RETURN_TOK_VAL(next, type, oval)
char * ip_match_parse(VSTRING *byte_codes, char *pattern)
char * ip_match_dump(VSTRING *printable, const char *byte_codes)
VSTRING * vstring_free(VSTRING *vp)
#define IP_MATCH_CODE_CLOSE
char * printable(char *string, int replacement)
#define IP_MATCH_RETURN_TOK(next, type)
#define IP_MATCH_CODE_OVAL
void * mymalloc(ssize_t len)