Browse Source

Hide less interesting bits of the internal API

Wijnand 1 year ago
parent
commit
75ff870fa8
7 changed files with 235 additions and 224 deletions
  1. 2
    2
      conn.go
  2. 105
    102
      filter.go
  3. 4
    3
      filter_test.go
  4. 50
    50
      imsg.go
  5. 26
    26
      mproc.go
  6. 11
    0
      smtpd.go
  7. 37
    41
      table.go

+ 2
- 2
conn.go View File

@@ -5,8 +5,8 @@ import (
5 5
 	"os"
6 6
 )
7 7
 
8
-// NewConn wraps a file descriptor to a net.FileConn
9
-func NewConn(fd int) (net.Conn, error) {
8
+// newConn wraps a file descriptor to a net.FileConn
9
+func newConn(fd int) (net.Conn, error) {
10 10
 	f := os.NewFile(uintptr(fd), "")
11 11
 	return net.FileConn(f)
12 12
 }

+ 105
- 102
filter.go View File

@@ -11,23 +11,19 @@ import (
11 11
 )
12 12
 
13 13
 const (
14
-	FilterVersion = 51
15
-)
16
-
17
-const (
18
-	TypeFilterRegister uint32 = iota
19
-	TypeFilterEvent
20
-	TypeFilterQuery
21
-	TypeFilterPipe
22
-	TypeFilterResponse
14
+	typeFilterRegister uint32 = iota
15
+	typeFilterEvent
16
+	typeFilterquery
17
+	typeFilterPipe
18
+	typeFilterResponse
23 19
 )
24 20
 
25 21
 var filterTypeName = map[uint32]string{
26
-	TypeFilterRegister: "IMSG_FILTER_REGISTER",
27
-	TypeFilterEvent:    "IMSG_FILTER_EVENT",
28
-	TypeFilterQuery:    "IMSG_FILTER_QUERY",
29
-	TypeFilterPipe:     "IMSG_FILTER_PIPE",
30
-	TypeFilterResponse: "IMSG_FILTER_RESPONSE",
22
+	typeFilterRegister: "IMSG_FILTER_REGISTER",
23
+	typeFilterEvent:    "IMSG_FILTER_EVENT",
24
+	typeFilterquery:    "IMSG_FILTER_QUERY",
25
+	typeFilterPipe:     "IMSG_FILTER_PIPE",
26
+	typeFilterResponse: "IMSG_FILTER_RESPONSE",
31 27
 }
32 28
 
33 29
 func filterName(t uint32) string {
@@ -38,31 +34,31 @@ func filterName(t uint32) string {
38 34
 }
39 35
 
40 36
 const (
41
-	HookConnect = 1 << iota
42
-	HookHELO
43
-	HookMAIL
44
-	HookRCPT
45
-	HookDATA
46
-	HookEOM
47
-	HookReset
48
-	HookDisconnect
49
-	HookCommit
50
-	HookRollback
51
-	HookDataLine
37
+	hookConnect = 1 << iota
38
+	hookHELO
39
+	hookMAIL
40
+	hookRCPT
41
+	hookDATA
42
+	hookEOM
43
+	hookReset
44
+	hookDisconnect
45
+	hookCommit
46
+	hookRollback
47
+	hookDataLine
52 48
 )
53 49
 
54 50
 var hookTypeName = map[uint16]string{
55
-	HookConnect:    "HOOK_CONNECT",
56
-	HookHELO:       "HOOK_HELO",
57
-	HookMAIL:       "HOOK_MAIL",
58
-	HookRCPT:       "HOOK_RCPT",
59
-	HookDATA:       "HOOK_DATA",
60
-	HookEOM:        "HOOK_EOM",
61
-	HookReset:      "HOOK_RESET",
62
-	HookDisconnect: "HOOK_DISCONNECT",
63
-	HookCommit:     "HOOK_COMMIT",
64
-	HookRollback:   "HOOK_ROLLBACK",
65
-	HookDataLine:   "HOOK_DATALINE",
51
+	hookConnect:    "HOOK_CONNECT",
52
+	hookHELO:       "HOOK_HELO",
53
+	hookMAIL:       "HOOK_MAIL",
54
+	hookRCPT:       "HOOK_RCPT",
55
+	hookDATA:       "HOOK_DATA",
56
+	hookEOM:        "HOOK_EOM",
57
+	hookReset:      "HOOK_RESET",
58
+	hookDisconnect: "HOOK_DISCONNECT",
59
+	hookCommit:     "HOOK_COMMIT",
60
+	hookRollback:   "HOOK_ROLLBACK",
61
+	hookDataLine:   "HOOK_DATALINE",
66 62
 }
67 63
 
68 64
 func hookName(h uint16) string {
@@ -76,21 +72,21 @@ func hookName(h uint16) string {
76 72
 }
77 73
 
78 74
 const (
79
-	EventConnect = iota
80
-	EventReset
81
-	EventDisconnect
82
-	EventTXBegin
83
-	EventTXCommit
84
-	EventTXRollback
75
+	eventConnect = iota
76
+	eventReset
77
+	eventDisconnect
78
+	eventTXBegin
79
+	eventTXCommit
80
+	eventTXRollback
85 81
 )
86 82
 
87 83
 var eventTypeName = map[int]string{
88
-	EventConnect:    "EVENT_CONNECT",
89
-	EventReset:      "EVENT_RESET",
90
-	EventDisconnect: "EVENT_DISCONNECT",
91
-	EventTXBegin:    "EVENT_TX_BEGIN",
92
-	EventTXCommit:   "EVENT_TX_COMMIT",
93
-	EventTXRollback: "EVENT_TX_ROLLBACK",
84
+	eventConnect:    "EVENT_CONNECT",
85
+	eventReset:      "EVENT_RESET",
86
+	eventDisconnect: "EVENT_DISCONNECT",
87
+	eventTXBegin:    "EVENT_TX_BEGIN",
88
+	eventTXCommit:   "EVENT_TX_COMMIT",
89
+	eventTXRollback: "EVENT_TX_ROLLBACK",
94 90
 }
95 91
 
96 92
 func eventName(t int) string {
@@ -101,23 +97,23 @@ func eventName(t int) string {
101 97
 }
102 98
 
103 99
 const (
104
-	QueryConnect = iota
105
-	QueryHELO
106
-	QueryMAIL
107
-	QueryRCPT
108
-	QueryDATA
109
-	QueryEOM
110
-	QueryDataLine
100
+	queryConnect = iota
101
+	queryHELO
102
+	queryMAIL
103
+	queryRCPT
104
+	queryDATA
105
+	queryEOM
106
+	queryDataLine
111 107
 )
112 108
 
113 109
 var queryTypeName = map[int]string{
114
-	QueryConnect:  "QUERY_CONNECT",
115
-	QueryHELO:     "QUERY_HELO",
116
-	QueryMAIL:     "QUERY_MAIL",
117
-	QueryRCPT:     "QUERY_RCPT",
118
-	QueryDATA:     "QUERY_DATA",
119
-	QueryEOM:      "QUERY_EOM",
120
-	QueryDataLine: "QUERY_DATALINE",
110
+	queryConnect:  "QUERY_CONNECT",
111
+	queryHELO:     "QUERY_HELO",
112
+	queryMAIL:     "QUERY_MAIL",
113
+	queryRCPT:     "QUERY_RCPT",
114
+	queryDATA:     "QUERY_DATA",
115
+	queryEOM:      "QUERY_EOM",
116
+	queryDataLine: "QUERY_DATALINE",
121 117
 }
122 118
 
123 119
 func queryName(t int) string {
@@ -182,7 +178,7 @@ type Filter struct {
182 178
 	Version uint32
183 179
 
184 180
 	c net.Conn
185
-	m *Message
181
+	m *message
186 182
 
187 183
 	hooks   int
188 184
 	flags   int
@@ -192,42 +188,42 @@ type Filter struct {
192 188
 
193 189
 func (f *Filter) OnConnect(fn func(*Session, *ConnectQuery) error) {
194 190
 	f.Connect = fn
195
-	f.hooks |= HookConnect
191
+	f.hooks |= hookConnect
196 192
 }
197 193
 
198 194
 func (f *Filter) OnHELO(fn func(*Session, string) error) {
199 195
 	f.HELO = fn
200
-	f.hooks |= HookHELO
196
+	f.hooks |= hookHELO
201 197
 }
202 198
 
203 199
 func (f *Filter) OnMAIL(fn func(*Session, string, string) error) {
204 200
 	f.MAIL = fn
205
-	f.hooks |= HookMAIL
201
+	f.hooks |= hookMAIL
206 202
 }
207 203
 
208 204
 func (f *Filter) OnRCPT(fn func(*Session, string, string) error) {
209 205
 	f.RCPT = fn
210
-	f.hooks |= HookRCPT
206
+	f.hooks |= hookRCPT
211 207
 }
212 208
 
213 209
 func (f *Filter) OnDATA(fn func(*Session) error) {
214 210
 	f.DATA = fn
215
-	f.hooks |= HookDATA
211
+	f.hooks |= hookDATA
216 212
 }
217 213
 
218 214
 func (f *Filter) OnDataLine(fn func(*Session, string) error) {
219 215
 	f.DataLine = fn
220
-	f.hooks |= HookDataLine
216
+	f.hooks |= hookDataLine
221 217
 }
222 218
 
223 219
 // Register our filter with OpenSMTPD
224 220
 func (f *Filter) Register() error {
225 221
 	var err error
226 222
 	if f.m == nil {
227
-		f.m = new(Message)
223
+		f.m = new(message)
228 224
 	}
229 225
 	if f.c == nil {
230
-		if f.c, err = NewConn(0); err != nil {
226
+		if f.c, err = newConn(0); err != nil {
231 227
 			return err
232 228
 		}
233 229
 	}
@@ -237,31 +233,31 @@ func (f *Filter) Register() error {
237 233
 
238 234
 	// Fill hooks mask
239 235
 	if f.Connect != nil {
240
-		f.hooks |= HookConnect
236
+		f.hooks |= hookConnect
241 237
 	}
242 238
 	if f.HELO != nil {
243
-		f.hooks |= HookHELO
239
+		f.hooks |= hookHELO
244 240
 	}
245 241
 	if f.MAIL != nil {
246
-		f.hooks |= HookMAIL
242
+		f.hooks |= hookMAIL
247 243
 	}
248 244
 	if f.RCPT != nil {
249
-		f.hooks |= HookRCPT
245
+		f.hooks |= hookRCPT
250 246
 	}
251 247
 	if f.DATA != nil {
252
-		f.hooks |= HookDATA
248
+		f.hooks |= hookDATA
253 249
 	}
254 250
 	if f.DataLine != nil {
255
-		f.hooks |= HookDataLine
251
+		f.hooks |= hookDataLine
256 252
 	}
257 253
 	if f.EOM != nil {
258
-		f.hooks |= HookEOM
254
+		f.hooks |= hookEOM
259 255
 	}
260 256
 	if f.Disconnect != nil {
261
-		f.hooks |= HookDisconnect
257
+		f.hooks |= hookDisconnect
262 258
 	}
263 259
 	if f.Commit != nil {
264
-		f.hooks |= HookCommit
260
+		f.hooks |= hookCommit
265 261
 	}
266 262
 
267 263
 	if t, ok := filterTypeName[f.m.Type]; ok {
@@ -271,7 +267,7 @@ func (f *Filter) Register() error {
271 267
 	}
272 268
 
273 269
 	switch f.m.Type {
274
-	case TypeFilterRegister:
270
+	case typeFilterRegister:
275 271
 		var err error
276 272
 		if f.Version, err = f.m.GetTypeUint32(); err != nil {
277 273
 			return err
@@ -282,7 +278,7 @@ func (f *Filter) Register() error {
282 278
 		log.Printf("register version=%d,name=%q\n", f.Version, f.Name)
283 279
 
284 280
 		f.m.reset()
285
-		f.m.Type = TypeFilterRegister
281
+		f.m.Type = typeFilterRegister
286 282
 		f.m.PutTypeInt(f.hooks)
287 283
 		f.m.PutTypeInt(f.flags)
288 284
 		if err = f.m.WriteTo(f.c); err != nil {
@@ -292,6 +288,7 @@ func (f *Filter) Register() error {
292 288
 		return fmt.Errorf("filter: unexpected imsg type=%s\n", filterTypeName[f.m.Type])
293 289
 	}
294 290
 
291
+	f.ready = true
295 292
 	return nil
296 293
 }
297 294
 
@@ -299,8 +296,15 @@ func (f *Filter) Register() error {
299 296
 // parties closes stdin.
300 297
 func (f *Filter) Serve() error {
301 298
 	var err error
299
+
300
+	if !f.ready {
301
+		if err = f.Register(); err != nil {
302
+			return err
303
+		}
304
+	}
305
+
302 306
 	if f.m == nil {
303
-		f.m = new(Message)
307
+		f.m = new(message)
304 308
 	}
305 309
 	if f.session == nil {
306 310
 		if f.session, err = lru.New(1024); err != nil {
@@ -308,13 +312,12 @@ func (f *Filter) Serve() error {
308 312
 		}
309 313
 	}
310 314
 	if f.c == nil {
311
-		if f.c, err = NewConn(0); err != nil {
315
+		if f.c, err = newConn(0); err != nil {
312 316
 			return err
313 317
 		}
314 318
 	}
315 319
 
316 320
 	for {
317
-		//log.Printf("fdcount: %d [pid=%d]\n", fdCount(), os.Getpid())
318 321
 		if err := f.m.ReadFrom(f.c); err != nil {
319 322
 			if err.Error() != "resource temporarily unavailable" {
320 323
 				return err
@@ -334,13 +337,13 @@ func (f *Filter) handle() (err error) {
334 337
 	}
335 338
 
336 339
 	switch f.m.Type {
337
-	case TypeFilterEvent:
340
+	case typeFilterEvent:
338 341
 		if err = f.handleEvent(); err != nil {
339 342
 			return
340 343
 		}
341 344
 
342
-	case TypeFilterQuery:
343
-		if err = f.handleQuery(); err != nil {
345
+	case typeFilterquery:
346
+		if err = f.handlequery(); err != nil {
344 347
 			return
345 348
 		}
346 349
 	}
@@ -381,16 +384,16 @@ func (f *Filter) handleEvent() (err error) {
381 384
 	log.Printf("fdcount: %d [pid=%d]\n", fdCount(), os.Getpid())
382 385
 
383 386
 	switch t {
384
-	case EventConnect:
387
+	case eventConnect:
385 388
 		f.session.Add(id, NewSession(f, id))
386
-	case EventDisconnect:
389
+	case eventDisconnect:
387 390
 		f.session.Remove(id)
388 391
 	}
389 392
 
390 393
 	return
391 394
 }
392 395
 
393
-func (f *Filter) handleQuery() (err error) {
396
+func (f *Filter) handlequery() (err error) {
394 397
 	var (
395 398
 		id, qid uint64
396 399
 		t       int
@@ -421,7 +424,7 @@ func (f *Filter) handleQuery() (err error) {
421 424
 	s.qid = qid
422 425
 
423 426
 	switch t {
424
-	case QueryConnect:
427
+	case queryConnect:
425 428
 		var query ConnectQuery
426 429
 		if query.Local, err = f.m.GetTypeSockaddr(); err != nil {
427 430
 			return
@@ -440,7 +443,7 @@ func (f *Filter) handleQuery() (err error) {
440 443
 
441 444
 		log.Printf("filter: WARNING: no connect callback\n")
442 445
 
443
-	case QueryHELO:
446
+	case queryHELO:
444 447
 		var line string
445 448
 		if line, err = f.m.GetTypeString(); err != nil {
446 449
 			return
@@ -454,7 +457,7 @@ func (f *Filter) handleQuery() (err error) {
454 457
 		log.Printf("filter: WARNING: no HELO callback\n")
455 458
 		return f.respond(s, FilterOK, 0, "")
456 459
 
457
-	case QueryMAIL:
460
+	case queryMAIL:
458 461
 		var user, domain string
459 462
 		if user, domain, err = f.m.GetTypeMailaddr(); err != nil {
460 463
 			return
@@ -468,7 +471,7 @@ func (f *Filter) handleQuery() (err error) {
468 471
 		log.Printf("filter: WARNING: no MAIL callback\n")
469 472
 		return f.respond(s, FilterOK, 0, "")
470 473
 
471
-	case QueryRCPT:
474
+	case queryRCPT:
472 475
 		var user, domain string
473 476
 		if user, domain, err = f.m.GetTypeMailaddr(); err != nil {
474 477
 			return
@@ -482,7 +485,7 @@ func (f *Filter) handleQuery() (err error) {
482 485
 		log.Printf("filter: WARNING: no RCPT callback\n")
483 486
 		return f.respond(s, FilterOK, 0, "")
484 487
 
485
-	case QueryDATA:
488
+	case queryDATA:
486 489
 		if f.DATA != nil {
487 490
 			return f.DATA(s)
488 491
 		}
@@ -490,7 +493,7 @@ func (f *Filter) handleQuery() (err error) {
490 493
 		log.Printf("filter: WARNING: no DATA callback\n")
491 494
 		return f.respond(s, FilterOK, 0, "")
492 495
 
493
-	case QueryEOM:
496
+	case queryEOM:
494 497
 		var dataLen uint32
495 498
 		if dataLen, err = f.m.GetTypeUint32(); err != nil {
496 499
 			return
@@ -508,18 +511,18 @@ func (f *Filter) handleQuery() (err error) {
508 511
 }
509 512
 
510 513
 func (f *Filter) respond(s *Session, status, code int, line string) error {
511
-	log.Printf("filter: %s %s [code=%d,line=%q]\n", filterName(TypeFilterResponse), responseName(status), code, line)
514
+	log.Printf("filter: %s %s [code=%d,line=%q]\n", filterName(typeFilterResponse), responseName(status), code, line)
512 515
 
513
-	if s.qtype == QueryEOM {
516
+	if s.qtype == queryEOM {
514 517
 		// Not implemented
515 518
 		return nil
516 519
 	}
517 520
 
518
-	m := new(Message)
519
-	m.Type = TypeFilterResponse
521
+	m := new(message)
522
+	m.Type = typeFilterResponse
520 523
 	m.PutTypeID(s.qid)
521 524
 	m.PutTypeInt(s.qtype)
522
-	if s.qtype == QueryEOM {
525
+	if s.qtype == queryEOM {
523 526
 		// Not imlemented
524 527
 		return nil
525 528
 	}

+ 4
- 3
filter_test.go View File

@@ -14,14 +14,15 @@ func ExampleFilter() {
14 14
 	}
15 15
 
16 16
 	// Add another hook
17
-	filter.OnMAIL(func(session *Session, user, domain string) error {
17
+	filter.MAIL = func(session *Session, user, domain string) error {
18 18
 		if strings.ToLower(domain) == "example.org" {
19 19
 			return session.Reject()
20 20
 		}
21 21
 		return session.Accept()
22
-	})
22
+	}
23 23
 
24
-	// Register our filter with smtpd
24
+	// Register our filter with smtpd. This step is optional and will
25
+	// be performed by Serve() if omitted.
25 26
 	if err := filter.Register(); err != nil {
26 27
 		panic(err)
27 28
 	}

+ 50
- 50
imsg.go View File

@@ -20,8 +20,8 @@ const (
20 20
 	maxDomainPartSize = (255 + 1)
21 21
 )
22 22
 
23
-// MessageHeader is the header of an imsg frame (struct imsg_hdr)
24
-type MessageHeader struct {
23
+// messageHeader is the header of an imsg frame (struct imsg_hdr)
24
+type messageHeader struct {
25 25
 	Type   uint32
26 26
 	Len    uint16
27 27
 	Flags  uint16
@@ -29,9 +29,9 @@ type MessageHeader struct {
29 29
 	PID    uint32
30 30
 }
31 31
 
32
-// Message implements OpenBSD imsg
33
-type Message struct {
34
-	MessageHeader
32
+// message implements OpenBSD imsg
33
+type message struct {
34
+	messageHeader
35 35
 	Data []byte
36 36
 
37 37
 	// rpos is the read position in the current Data
@@ -41,7 +41,7 @@ type Message struct {
41 41
 	buf []byte
42 42
 }
43 43
 
44
-func (m *Message) reset() {
44
+func (m *message) reset() {
45 45
 	m.Type = 0
46 46
 	m.Len = 0
47 47
 	m.Flags = 0
@@ -54,7 +54,7 @@ func (m *Message) reset() {
54 54
 
55 55
 // ReadFrom reads a message from the specified net.Conn, parses the header and
56 56
 // reads the data payload.
57
-func (m *Message) ReadFrom(c net.Conn) error {
57
+func (m *message) ReadFrom(c net.Conn) error {
58 58
 	m.reset()
59 59
 
60 60
 	head := make([]byte, imsgHeaderSize)
@@ -63,12 +63,12 @@ func (m *Message) ReadFrom(c net.Conn) error {
63 63
 	}
64 64
 
65 65
 	r := bytes.NewBuffer(head)
66
-	if err := binary.Read(r, binary.LittleEndian, &m.MessageHeader); err != nil {
66
+	if err := binary.Read(r, binary.LittleEndian, &m.messageHeader); err != nil {
67 67
 		return err
68 68
 	}
69
-	debugf("imsg header: %+v\n", m.MessageHeader)
69
+	debugf("imsg header: %+v\n", m.messageHeader)
70 70
 
71
-	data := make([]byte, m.MessageHeader.Len-imsgHeaderSize)
71
+	data := make([]byte, m.messageHeader.Len-imsgHeaderSize)
72 72
 	if _, err := c.Read(data); err != nil {
73 73
 		return err
74 74
 	}
@@ -78,13 +78,13 @@ func (m *Message) ReadFrom(c net.Conn) error {
78 78
 	return nil
79 79
 }
80 80
 
81
-// WriteTo marshals the Message to wire format and sends it to the net.Conn
82
-func (m *Message) WriteTo(c net.Conn) error {
81
+// WriteTo marshals the message to wire format and sends it to the net.Conn
82
+func (m *message) WriteTo(c net.Conn) error {
83 83
 	m.Len = uint16(len(m.Data)) + imsgHeaderSize
84 84
 
85 85
 	buf := new(bytes.Buffer)
86
-	debugf("imsg header: %+v\n", m.MessageHeader)
87
-	if err := binary.Write(buf, binary.LittleEndian, &m.MessageHeader); err != nil {
86
+	debugf("imsg header: %+v\n", m.messageHeader)
87
+	if err := binary.Write(buf, binary.LittleEndian, &m.messageHeader); err != nil {
88 88
 		return err
89 89
 	}
90 90
 	buf.Write(m.Data)
@@ -94,7 +94,7 @@ func (m *Message) WriteTo(c net.Conn) error {
94 94
 	return err
95 95
 }
96 96
 
97
-func (m *Message) GetInt() (int, error) {
97
+func (m *message) GetInt() (int, error) {
98 98
 	if m.rpos+4 > len(m.Data) {
99 99
 		return 0, io.ErrShortBuffer
100 100
 	}
@@ -103,7 +103,7 @@ func (m *Message) GetInt() (int, error) {
103 103
 	return int(i), nil
104 104
 }
105 105
 
106
-func (m *Message) GetUint32() (uint32, error) {
106
+func (m *message) GetUint32() (uint32, error) {
107 107
 	if m.rpos+4 > len(m.Data) {
108 108
 		return 0, io.ErrShortBuffer
109 109
 	}
@@ -112,7 +112,7 @@ func (m *Message) GetUint32() (uint32, error) {
112 112
 	return u, nil
113 113
 }
114 114
 
115
-func (m *Message) GetSize() (uint64, error) {
115
+func (m *message) GetSize() (uint64, error) {
116 116
 	if m.rpos+8 > len(m.Data) {
117 117
 		return 0, io.ErrShortBuffer
118 118
 	}
@@ -121,7 +121,7 @@ func (m *Message) GetSize() (uint64, error) {
121 121
 	return u, nil
122 122
 }
123 123
 
124
-func (m *Message) GetString() (string, error) {
124
+func (m *message) GetString() (string, error) {
125 125
 	o := bytes.IndexByte(m.Data[m.rpos:], 0)
126 126
 	if o < 0 {
127 127
 		return "", errors.New("imsg: string not NULL-terminated")
@@ -132,7 +132,7 @@ func (m *Message) GetString() (string, error) {
132 132
 	return s, nil
133 133
 }
134 134
 
135
-func (m *Message) GetID() (uint64, error) {
135
+func (m *message) GetID() (uint64, error) {
136 136
 	if m.rpos+8 > len(m.Data) {
137 137
 		return 0, io.ErrShortBuffer
138 138
 	}
@@ -174,7 +174,7 @@ func (sa Sockaddr) String() string {
174 174
 	return fmt.Sprintf("%s:%d", sa.IP(), sa.Port())
175 175
 }
176 176
 
177
-func (m *Message) GetSockaddr() (net.Addr, error) {
177
+func (m *message) GetSockaddr() (net.Addr, error) {
178 178
 	s, err := m.GetSize()
179 179
 	if err != nil {
180 180
 		return nil, err
@@ -190,7 +190,7 @@ func (m *Message) GetSockaddr() (net.Addr, error) {
190 190
 	return a, nil
191 191
 }
192 192
 
193
-func (m *Message) GetMailaddr() (user, domain string, err error) {
193
+func (m *message) GetMailaddr() (user, domain string, err error) {
194 194
 	var buf [maxLocalPartSize + maxDomainPartSize]byte
195 195
 	if maxLocalPartSize+maxDomainPartSize > len(m.Data[m.rpos:]) {
196 196
 		return "", "", io.ErrShortBuffer
@@ -202,7 +202,7 @@ func (m *Message) GetMailaddr() (user, domain string, err error) {
202 202
 	return
203 203
 }
204 204
 
205
-func (m *Message) GetType(t uint8) error {
205
+func (m *message) GetType(t uint8) error {
206 206
 	if m.rpos >= len(m.Data) {
207 207
 		return io.ErrShortBuffer
208 208
 	}
@@ -210,107 +210,107 @@ func (m *Message) GetType(t uint8) error {
210 210
 	b := m.Data[m.rpos]
211 211
 	m.rpos++
212 212
 	if b != t {
213
-		return MProcTypeErr{t, b}
213
+		return mprocTypeErr{t, b}
214 214
 	}
215 215
 	return nil
216 216
 }
217 217
 
218
-func (m *Message) GetTypeInt() (int, error) {
219
-	if err := m.GetType(M_INT); err != nil {
218
+func (m *message) GetTypeInt() (int, error) {
219
+	if err := m.GetType(mINT); err != nil {
220 220
 		return 0, err
221 221
 	}
222 222
 	return m.GetInt()
223 223
 }
224 224
 
225
-func (m *Message) GetTypeUint32() (uint32, error) {
226
-	if err := m.GetType(M_UINT32); err != nil {
225
+func (m *message) GetTypeUint32() (uint32, error) {
226
+	if err := m.GetType(mUINT32); err != nil {
227 227
 		return 0, err
228 228
 	}
229 229
 	return m.GetUint32()
230 230
 }
231 231
 
232
-func (m *Message) GetTypeSize() (uint64, error) {
233
-	if err := m.GetType(M_SIZET); err != nil {
232
+func (m *message) GetTypeSize() (uint64, error) {
233
+	if err := m.GetType(mSIZET); err != nil {
234 234
 		return 0, err
235 235
 	}
236 236
 	return m.GetSize()
237 237
 }
238 238
 
239
-func (m *Message) GetTypeString() (string, error) {
240
-	if err := m.GetType(M_STRING); err != nil {
239
+func (m *message) GetTypeString() (string, error) {
240
+	if err := m.GetType(mSTRING); err != nil {
241 241
 		return "", err
242 242
 	}
243 243
 	return m.GetString()
244 244
 }
245 245
 
246
-func (m *Message) GetTypeID() (uint64, error) {
247
-	if err := m.GetType(M_ID); err != nil {
246
+func (m *message) GetTypeID() (uint64, error) {
247
+	if err := m.GetType(mID); err != nil {
248 248
 		return 0, err
249 249
 	}
250 250
 	return m.GetID()
251 251
 }
252 252
 
253
-func (m *Message) GetTypeSockaddr() (net.Addr, error) {
254
-	if err := m.GetType(M_SOCKADDR); err != nil {
253
+func (m *message) GetTypeSockaddr() (net.Addr, error) {
254
+	if err := m.GetType(mSOCKADDR); err != nil {
255 255
 		return nil, err
256 256
 	}
257 257
 	return m.GetSockaddr()
258 258
 }
259 259
 
260
-func (m *Message) GetTypeMailaddr() (user, domain string, err error) {
261
-	if err = m.GetType(M_MAILADDR); err != nil {
260
+func (m *message) GetTypeMailaddr() (user, domain string, err error) {
261
+	if err = m.GetType(mMAILADDR); err != nil {
262 262
 		return
263 263
 	}
264 264
 	return m.GetMailaddr()
265 265
 }
266 266
 
267
-func (m *Message) PutInt(v int) {
267
+func (m *message) PutInt(v int) {
268 268
 	var b [4]byte
269 269
 	binary.LittleEndian.PutUint32(b[:], uint32(v))
270 270
 	m.Data = append(m.Data, b[:]...)
271 271
 	m.Len += 4
272 272
 }
273 273
 
274
-func (m *Message) PutUint32(v uint32) {
274
+func (m *message) PutUint32(v uint32) {
275 275
 	var b [4]byte
276 276
 	binary.LittleEndian.PutUint32(b[:], v)
277 277
 	m.Data = append(m.Data, b[:]...)
278 278
 	m.Len += 4
279 279
 }
280 280
 
281
-func (m *Message) PutString(s string) {
281
+func (m *message) PutString(s string) {
282 282
 	m.Data = append(m.Data, append([]byte(s), 0)...)
283 283
 	m.Len += uint16(len(s)) + 1
284 284
 }
285 285
 
286
-func (m *Message) PutID(id uint64) {
286
+func (m *message) PutID(id uint64) {
287 287
 	var b [8]byte
288 288
 	binary.LittleEndian.PutUint64(b[:], id)
289 289
 	m.Data = append(m.Data, b[:]...)
290 290
 	m.Len += 8
291 291
 }
292 292
 
293
-func (m *Message) PutType(t uint8) {
293
+func (m *message) PutType(t uint8) {
294 294
 	m.Data = append(m.Data, t)
295 295
 	m.Len += 1
296 296
 }
297 297
 
298
-func (m *Message) PutTypeInt(v int) {
299
-	m.PutType(M_INT)
298
+func (m *message) PutTypeInt(v int) {
299
+	m.PutType(mINT)
300 300
 	m.PutInt(v)
301 301
 }
302 302
 
303
-func (m *Message) PutTypeUint32(v uint32) {
304
-	m.PutType(M_UINT32)
303
+func (m *message) PutTypeUint32(v uint32) {
304
+	m.PutType(mUINT32)
305 305
 	m.PutUint32(v)
306 306
 }
307 307
 
308
-func (m *Message) PutTypeString(s string) {
309
-	m.PutType(M_STRING)
308
+func (m *message) PutTypeString(s string) {
309
+	m.PutType(mSTRING)
310 310
 	m.PutString(s)
311 311
 }
312 312
 
313
-func (m *Message) PutTypeID(id uint64) {
314
-	m.PutType(M_ID)
313
+func (m *message) PutTypeID(id uint64) {
314
+	m.PutType(mID)
315 315
 	m.PutID(id)
316 316
 }

+ 26
- 26
mproc.go View File

@@ -5,33 +5,33 @@ import (
5 5
 )
6 6
 
7 7
 const (
8
-	M_INT = iota
9
-	M_UINT32
10
-	M_SIZET
11
-	M_TIME
12
-	M_STRING
13
-	M_DATA
14
-	M_ID
15
-	M_EVPID
16
-	M_MSGID
17
-	M_SOCKADDR
18
-	M_MAILADDR
19
-	M_ENVELOPE
8
+	mINT = iota
9
+	mUINT32
10
+	mSIZET
11
+	mTIME
12
+	mSTRING
13
+	mDATA
14
+	mID
15
+	mEVPID
16
+	mMSGID
17
+	mSOCKADDR
18
+	mMAILADDR
19
+	mENVELOPE
20 20
 )
21 21
 
22 22
 var mprocTypeName = map[uint8]string{
23
-	M_INT:      "M_INT",
24
-	M_UINT32:   "M_UINT32",
25
-	M_SIZET:    "M_SIZET",
26
-	M_TIME:     "M_TIME",
27
-	M_STRING:   "M_STRING",
28
-	M_DATA:     "M_DATA",
29
-	M_ID:       "M_ID",
30
-	M_EVPID:    "M_EVPID",
31
-	M_MSGID:    "M_MSGID",
32
-	M_SOCKADDR: "M_SOCKADDR",
33
-	M_MAILADDR: "M_MAILADDR",
34
-	M_ENVELOPE: "M_ENVELOPE",
23
+	mINT:      "M_INT",
24
+	mUINT32:   "M_UINT32",
25
+	mSIZET:    "M_SIZET",
26
+	mTIME:     "M_TIME",
27
+	mSTRING:   "M_STRING",
28
+	mDATA:     "M_DATA",
29
+	mID:       "M_ID",
30
+	mEVPID:    "M_EVPID",
31
+	mMSGID:    "M_MSGID",
32
+	mSOCKADDR: "M_SOCKADDR",
33
+	mMAILADDR: "M_MAILADDR",
34
+	mENVELOPE: "M_ENVELOPE",
35 35
 }
36 36
 
37 37
 func mprocType(t uint8) string {
@@ -41,11 +41,11 @@ func mprocType(t uint8) string {
41 41
 	return fmt.Sprintf("UNKNOWN %d", t)
42 42
 }
43 43
 
44
-type MProcTypeErr struct {
44
+type mprocTypeErr struct {
45 45
 	want, got uint8
46 46
 }
47 47
 
48
-func (err MProcTypeErr) Error() string {
48
+func (err mprocTypeErr) Error() string {
49 49
 	return fmt.Sprintf("mproc: expected type %s, got %s",
50 50
 		mprocType(err.want), mprocType(err.got))
51 51
 }

+ 11
- 0
smtpd.go View File

@@ -6,6 +6,17 @@ import (
6 6
 	"strings"
7 7
 )
8 8
 
9
+const (
10
+	// FilterVersion is the supported filter API version
11
+	FilterVersion = 51
12
+
13
+	// QueueVersion is the supported queue API version
14
+	QueueVersion = 1
15
+
16
+	// TableVersion is the supported table API version
17
+	TableVersion = 1
18
+)
19
+
9 20
 var (
10 21
 	// Debug flag
11 22
 	Debug bool

+ 37
- 41
table.go View File

@@ -11,29 +11,25 @@ import (
11 11
 )
12 12
 
13 13
 const (
14
-	TableAPIVersion = 1
15
-)
16
-
17
-const (
18
-	ProcTableOK = iota
19
-	ProcTableFail
20
-	ProcTableOpen
21
-	ProcTableClose
22
-	ProcTableUpdate
23
-	ProcTableCheck
24
-	ProcTableLookup
25
-	ProcTableFetch
14
+	procTableOK = iota
15
+	procTableFail
16
+	procTableOpen
17
+	procTableClose
18
+	procTableUpdate
19
+	procTableCheck
20
+	procTableLookup
21
+	procTableFetch
26 22
 )
27 23
 
28 24
 var procTableTypeName = map[uint32]string{
29
-	ProcTableOK:     "PROC_TABLE_OK",
30
-	ProcTableFail:   "PROC_TABLE_FAIL",
31
-	ProcTableOpen:   "PROC_TABLE_OPEN",
32
-	ProcTableClose:  "PROC_TABLE_CLOSE",
33
-	ProcTableUpdate: "PROC_TABLE_UPDATE",
34
-	ProcTableCheck:  "PROC_TABLE_CHECK",
35
-	ProcTableLookup: "PROC_TABLE_LOOKUP",
36
-	ProcTableFetch:  "PROC_TABLE_FETCH",
25
+	procTableOK:     "PROC_TABLE_OK",
26
+	procTableFail:   "PROC_TABLE_FAIL",
27
+	procTableOpen:   "PROC_TABLE_OPEN",
28
+	procTableClose:  "PROC_TABLE_CLOSE",
29
+	procTableUpdate: "PROC_TABLE_UPDATE",
30
+	procTableCheck:  "PROC_TABLE_CHECK",
31
+	procTableLookup: "PROC_TABLE_LOOKUP",
32
+	procTableFetch:  "PROC_TABLE_FETCH",
37 33
 }
38 34
 
39 35
 func procTableName(t uint32) string {
@@ -61,18 +57,18 @@ type Table struct {
61 57
 	Close func() error
62 58
 
63 59
 	c      net.Conn
64
-	m      *Message
60
+	m      *message
65 61
 	closed bool
66 62
 }
67 63
 
68 64
 func (t *Table) Serve() error {
69 65
 	var err error
70 66
 
71
-	if t.c, err = NewConn(0); err != nil {
67
+	if t.c, err = newConn(0); err != nil {
72 68
 		return err
73 69
 	}
74 70
 
75
-	t.m = new(Message)
71
+	t.m = new(message)
76 72
 
77 73
 	for !t.closed {
78 74
 		if err = t.m.ReadFrom(t.c); err != nil {
@@ -96,15 +92,15 @@ type tableOpenParams struct {
96 92
 
97 93
 func (t *Table) dispatch() (err error) {
98 94
 	switch t.m.Type {
99
-	case ProcTableOpen:
95
+	case procTableOpen:
100 96
 		/*
101 97
 			var op tableOpenParams
102 98
 			if err = t.getMessage(&op, maxLineSize+4); err != nil {
103 99
 				return
104 100
 			}
105 101
 
106
-			if op.Version != TableAPIVersion {
107
-				fatalf("table: bad API version %d (we support %d)", op.Version, TableAPIVersion)
102
+			if op.Version != TableVersion {
103
+				fatalf("table: bad API version %d (we support %d)", op.Version, TableVersion)
108 104
 			}
109 105
 			if bytes.IndexByte(op.Name[:], 0) <= 0 {
110 106
 				fatal("table: no name supplied")
@@ -113,8 +109,8 @@ func (t *Table) dispatch() (err error) {
113 109
 		var version uint32
114 110
 		if version, err = t.m.GetUint32(); err != nil {
115 111
 			return
116
-		} else if version != TableAPIVersion {
117
-			fatalf("table: expected API version %d, got %d", TableAPIVersion, version)
112
+		} else if version != TableVersion {
113
+			fatalf("table: expected API version %d, got %d", TableVersion, version)
118 114
 		}
119 115
 
120 116
 		var name string
@@ -126,15 +122,15 @@ func (t *Table) dispatch() (err error) {
126 122
 
127 123
 		debugf("table: version=%d,name=%q\n", version, name)
128 124
 
129
-		m := new(Message)
130
-		m.Type = ProcTableOK
125
+		m := new(message)
126
+		m.Type = procTableOK
131 127
 		m.Len = imsgHeaderSize
132 128
 		m.PID = uint32(os.Getpid())
133 129
 		if err = m.WriteTo(t.c); err != nil {
134 130
 			return
135 131
 		}
136 132
 
137
-	case ProcTableUpdate:
133
+	case procTableUpdate:
138 134
 		var r = 1
139 135
 
140 136
 		if t.Update != nil {
@@ -143,14 +139,14 @@ func (t *Table) dispatch() (err error) {
143 139
 			}
144 140
 		}
145 141
 
146
-		m := new(Message)
147
-		m.Type = ProcTableOK
142
+		m := new(message)
143
+		m.Type = procTableOK
148 144
 		m.PutInt(r)
149 145
 		if err = m.WriteTo(t.c); err != nil {
150 146
 			return
151 147
 		}
152 148
 
153
-	case ProcTableClose:
149
+	case procTableClose:
154 150
 		if t.Close != nil {
155 151
 			if err = t.Close(); err != nil {
156 152
 				return
@@ -160,7 +156,7 @@ func (t *Table) dispatch() (err error) {
160 156
 		t.closed = true
161 157
 		return
162 158
 
163
-	case ProcTableCheck:
159
+	case procTableCheck:
164 160
 		var service int
165 161
 		if service, err = t.m.GetInt(); err != nil {
166 162
 			return
@@ -188,7 +184,7 @@ func (t *Table) dispatch() (err error) {
188 184
 
189 185
 		log.Printf("table_check: result=%d\n", r)
190 186
 
191
-	case ProcTableLookup:
187
+	case procTableLookup:
192 188
 		var service int
193 189
 		if service, err = t.m.GetInt(); err != nil {
194 190
 			return
@@ -214,8 +210,8 @@ func (t *Table) dispatch() (err error) {
214 210
 			}
215 211
 		}
216 212
 
217
-		m := new(Message)
218
-		m.Type = ProcTableOK
213
+		m := new(message)
214
+		m.Type = procTableOK
219 215
 		m.PID = uint32(os.Getpid())
220 216
 		if val == "" {
221 217
 			m.PutInt(-1)
@@ -227,7 +223,7 @@ func (t *Table) dispatch() (err error) {
227 223
 			return
228 224
 		}
229 225
 
230
-	case ProcTableFetch:
226
+	case procTableFetch:
231 227
 		var service int
232 228
 		if service, err = t.m.GetInt(); err != nil {
233 229
 			return
@@ -248,8 +244,8 @@ func (t *Table) dispatch() (err error) {
248 244
 			}
249 245
 		}
250 246
 
251
-		m := new(Message)
252
-		m.Type = ProcTableOK
247
+		m := new(message)
248
+		m.Type = procTableOK
253 249
 		m.PID = uint32(os.Getpid())
254 250
 		if val == "" {
255 251
 			m.PutInt(-1)

Loading…
Cancel
Save