Debian Codegraph @

demo/kitchen-sink/docs/zeek.zeek

raw
1 # An example of the Zeek scripting language.
2
3 ##! A Zeekygen-style summmary comment.
4
5 # TODO: just an example of a todo-indicator
6
7 @load base/frameworks/notice
8
9 @if ( F )
10 @endif
11
12 module Example;
13
14 export {
15
16 type SimpleEnum: enum { ONE, TWO, THREE };
17
18 redef enum SimpleEnum += {
19
20 ## A Zeekygen-style comment.
21 FOUR,
22 FIVE, ##< A Zeekygen-style comment.
23 };
24
25 type SimpleRecord: record {
26 field1: count;
27 field2: bool;
28 } &redef;
29
30 redef record SimpleRecord += {
31
32 field3: string &optional;
33
34 field4: string &default="blah";
35 };
36
37 const init_option: bool = T;
38
39 option runtime_option: bool = F;
40
41 global test_opaque: opaque of md5;
42
43 global test_vector: vector of count;
44
45 global myfunction: function(msg: string, c: count &default=0): count;
46
47 global myhook: hook(tag: string);
48
49 global myevent: event(tag: string);
50 }
51
52 function myfunction(msg: string, c: count): count
53 {
54 print "in myfunction", msg, c;
55 return 0;
56 }
57
58 event myevent(msg: string) &priority=1
59 {
60 print "in myevent";
61 }
62
63 hook myhook(msg: string)
64 {
65 print "in myevent";
66 }
67
68 event zeek_init()
69 {
70 local b = T;
71 local s = "\xff\xaf\"and more after the escaped quote";
72 local p = /foo|bar\xbe\/and more after the escaped slash/;
73 local c = 10;
74
75 local sr = SimpleRecord($field1 = 0, $field2 = T, $field3 = "hi");
76
77 print sr?$field3, sr$field1;
78
79 local myset: set[string] = set("one", "two", "three");
80
81 add myset["four"];
82 delete myset["one"];
83
84 for ( ms in myset )
85 {
86 print ms is string, s as string;
87
88 print s[1:3];
89
90 local tern: count = s == "two" ? 2 : 0;
91
92 if ( s !in myset )
93 print fmt("error %4.2f: %s", 3.14159, "wtf?");
94 }
95
96 switch ( c ) {
97 case 1:
98 break;
99 case 2:
100 fallthrough;
101 default:
102 break;
103 }
104
105 if ( ! b )
106 print "here";
107 else
108 print "there";
109
110 while ( c != 0 )
111 {
112 if ( c >= 5 )
113 c += 0;
114 else if ( c == 8 )
115 c -= 0;
116
117 c = c / 1;
118 c = c / 1;
119 c = c - 1;
120 }
121
122 print |myset|;
123 print ~5;
124 print 1 & 0xff;
125 print 2 ^ 5;
126
127 myfunction("hello function");
128 hook myhook("hell hook");
129 event myevent("hello event");
130 schedule 1sec { myevent("hello scheduled event") };
131
132 print 0, 7;
133 print 0xff, 0xdeadbeef;
134
135 print 3.14159;
136 print 1234.0;
137 print 1234e0;
138 print .003E-23;
139 print .003E+23;
140
141 print 123/udp;
142 print 8000/tcp;
143 print 13/icmp;
144 print 42/unknown;
145
146 print google.com;
147 print 192.168.50.1;
148 print 255.255.255.255;
149 print 0.0.0.0;
150
151 print 10.0.0.0/16;
152
153 print [2001:0db8:85a3:0000:0000:8a2e:0370:7334];
154 # test for case insensitivity
155 print [2001:0DB8:85A3:0000:0000:8A2E:0370:7334];
156 # any case mixture is allowed
157 print [2001:0dB8:85a3:0000:0000:8A2E:0370:7334];
158 # leading zeroes of a 16-bit group may be omitted
159 print [2001:db8:85a3:0:0:8a2e:370:7334];
160 # a single occurrence of consecutive groups of zeroes may be replaced by ::
161 print [2001:db8:85a3::8a2e:370:7334];
162 # all zeroes should work
163 print [0:0:0:0:0:0:0:0];
164 # all zeroes condensed should work
165 print [::];
166 # hybrid ipv6-ipv4 address should work
167 print [2001:db8:0:0:0:FFFF:192.168.0.5];
168 # hybrid ipv6-ipv4 address with zero ommission should work
169 print [2001:db8::FFFF:192.168.0.5];
170
171 print [2001:0db8:85a3:0000:0000:8a2e:0370:7334]/64;
172
173 print 1day, 1days, 1.0day, 1.0days;
174 print 1hr, 1hrs, 1.0hr, 1.0hrs;
175 print 1min, 1mins, 1.0min, 1.0mins;
176 print 1sec, 1secs, 1.0sec, 1.0secs;
177 print 1msec, 1msecs, 1.0msec, 1.0msecs;
178 print 1usec, 1usecs, 1.0usec, 1.0usecs;
179 }