1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
%{
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package query
import (
"fmt"
"net"
"strconv"
"strings"
"time"
"unicode"
)
%}
%union {
num int
ip net.IP
str string
query Query
dur time.Duration
time time.Time
}
%type <query> top expr expr2
%type <time> timestamp
%token <str> HOST PORT PROTO AND OR NET MASK TCP UDP ICMP BEFORE AFTER IPP AGO VLAN MPLS
%token <ip> IP
%token <num> NUM
%token <dur> DURATION
%token <time> TIME
%%
top:
expr
{
parserlex.(*parserLex).out = $1
}
expr:
expr2
| expr AND expr2
{
$$ = intersectQuery{$1, $3}
}
| expr OR expr2
{
$$ = unionQuery{$1, $3}
}
expr2:
HOST IP
{
$$ = ipQuery{$2, $2}
}
| PORT NUM
{
if $2 < 0 || $2 >= 65536 {
parserlex.Error(fmt.Sprintf("invalid port %v", $2))
}
$$ = portQuery($2)
}
| VLAN NUM
{
if $2 < 0 || $2 >= 65536 {
parserlex.Error(fmt.Sprintf("invalid vlan %v", $2))
}
$$ = vlanQuery($2)
}
| MPLS NUM
{
if $2 < 0 || $2 >= (1 << 20) {
parserlex.Error(fmt.Sprintf("invalid mpls %v", $2))
}
$$ = mplsQuery($2)
}
| IPP PROTO NUM
{
if $3 < 0 || $3 >= 256 {
parserlex.Error(fmt.Sprintf("invalid proto %v", $3))
}
$$ = protocolQuery($3)
}
| NET IP '/' NUM
{
mask := net.CIDRMask($4, len($2) * 8)
if mask == nil {
parserlex.Error(fmt.Sprintf("bad cidr: %v/%v", $2, $4))
}
from, to, err := ipsFromNet($2, mask)
if err != nil {
parserlex.Error(err.Error())
}
$$ = ipQuery{from, to}
}
| NET IP MASK IP
{
from, to, err := ipsFromNet($2, net.IPMask($4))
if err != nil {
parserlex.Error(err.Error())
}
$$ = ipQuery{from, to}
}
| '(' expr ')'
{
$$ = $2
}
| TCP
{
$$ = protocolQuery(6)
}
| UDP
{
$$ = protocolQuery(17)
}
| ICMP
{
$$ = protocolQuery(1)
}
| BEFORE timestamp
{
var t timeQuery
t[1] = $2
$$ = t
}
| AFTER timestamp
{
var t timeQuery
t[0] = $2
$$ = t
}
timestamp:
TIME
{
$$ = $1
}
| DURATION AGO
{
$$ = parserlex.(*parserLex).now.Add(-$1)
}
%%
func ipsFromNet(ip net.IP, mask net.IPMask) (from, to net.IP, _ error) {
if len(ip) != len(mask) || (len(ip) != 4 && len(ip) != 16) {
return nil, nil, fmt.Errorf("bad IP or mask: %v %v", ip, mask)
}
from = make(net.IP, len(ip))
to = make(net.IP, len(ip))
for i := 0; i < len(ip); i++ {
from[i] = ip[i] & mask[i]
to[i] = ip[i] | ^mask[i]
}
return
}
// parserLex is used by the parser as a lexer.
// It must be named <prefix>Lex (where prefix is passed into go tool yacc with
// the -p flag).
type parserLex struct {
now time.Time // guarantees consistent time differences
in string
pos int
out Query
err error
}
// tokens provides a simple map for adding new keywords and mapping them
// to token types.
var tokens = map[string]int{
"after": AFTER,
"ago": AGO,
"&&": AND,
"and": AND,
"before": BEFORE,
"host": HOST,
"icmp": ICMP,
"ip": IPP,
"mask": MASK,
"net": NET,
"||": OR,
"or": OR,
"port": PORT,
"vlan": VLAN,
"mpls": MPLS,
"proto": PROTO,
"tcp": TCP,
"udp": UDP,
}
// Lex is called by the parser to get each new token. This implementation
// is currently quite simplistic, but it seems to work pretty well for our
// needs.
//
// The type of the input argument must be *<prefix>SymType.
func (x *parserLex) Lex(yylval *parserSymType) (ret int) {
for x.pos < len(x.in) && unicode.IsSpace(rune(x.in[x.pos])) {
x.pos++
}
for t, i := range tokens {
if strings.HasPrefix(x.in[x.pos:], t) {
x.pos += len(t)
return i
}
}
s := x.pos
var isIP, isDuration, isTime bool
L:
for x.pos < len(x.in) {
switch c := x.in[x.pos]; c {
case ':', '.':
isIP = true
x.pos++
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f':
x.pos++
case 'm', 'h':
x.pos++
isDuration = true
break L
case '-', 'T', '+', 'Z':
x.pos++
isTime = true
default:
break L
}
}
part := x.in[s:x.pos]
switch {
case isTime:
t, err := time.Parse(time.RFC3339, part)
if err != nil {
x.Error(fmt.Sprintf("bad time %q", part))
}
yylval.time = t
return TIME
case isIP:
yylval.ip = net.ParseIP(part)
if yylval.ip == nil {
x.Error(fmt.Sprintf("bad IP %q", part))
return -1
}
if ip4 := yylval.ip.To4(); ip4 != nil {
yylval.ip = ip4
}
return IP
case isDuration:
duration, err := time.ParseDuration(part)
if err != nil {
x.Error(fmt.Sprintf("bad duration %q", part))
}
yylval.dur = duration
return DURATION
case x.pos != s:
n, err := strconv.Atoi(part)
if err != nil { return -1 }
yylval.num = n
return NUM
case x.pos >= len(x.in):
return 0
}
switch c := x.in[x.pos]; c {
case ':', '.', '(', ')', '/':
x.pos++
return int(c)
}
return -1
}
// Error is called by the parser on a parse error.
func (x *parserLex) Error(s string) {
if x.err == nil {
x.err = fmt.Errorf("%v at character %v (%q HERE %q)", s, x.pos, x.in[:x.pos], x.in[x.pos:])
}
}
// parse parses an input string into a Query.
func parse(in string) (Query, error) {
lex := &parserLex{in: in, now: time.Now()}
parserParse(lex)
if lex.err != nil {
return nil, lex.err
}
return lex.out, nil
}
|