4 Commits

Author SHA1 Message Date
88a3f1f3a5 „gomilter.go“ ändern 2018-12-29 13:21:33 +00:00
Dolf Schimmel
ec6c4f3113 Merge branch 'master' of https://github.com/davrux/gomilter into HEAD 2015-12-31 23:08:11 +01:00
Dolf Schimmel (Freeaqingme)
c60841ef06 Revert "Make gobencode() optional in SetPriv(), return errors instead of integers"
This reverts commit 6a57e8c3fc.
2015-11-25 20:42:41 +01:00
Meik Kreyenkoetter
f724fd7bcb Linker path for Debian/Ubuntu.
Libmilter is installed in /usr/lib/libmilter on Debian and Ubuntu
systems. Added additional Linker path for cgo.
2015-10-02 11:51:58 +02:00

View File

@@ -10,11 +10,13 @@ gomilter.go
*/
//+build cgo
package gomilter
/*
#cgo LDFLAGS: -lmilter
#cgo LDFLAGS: -L/usr/lib/libmilter -lmilter
#include <stdlib.h>
#include <netinet/in.h>
@@ -28,7 +30,6 @@ import (
"bytes"
"encoding/binary"
"encoding/gob"
"errors"
"fmt"
"log"
"net"
@@ -358,24 +359,14 @@ func GetSymVal(ctx uintptr, symname string) string {
return C.GoString(cval)
}
// Beware that if your struct is too large it will be discarded
// without storing. You may want to simply provide a reference
// to something you keep in a map or similar structure yourself.
func SetPriv(ctx uintptr, privatedata interface{}) error {
// See also: http://bit.ly/1HVWA9I
func SetPriv(ctx uintptr, privatedata interface{}) int {
// privatedata seems to work for any data type
// Structs must have exported fields
// Serialize Go privatedata into a byte slice
bytedata, err := GobEncode(privatedata)
if err != nil {
return err
}
bytedata, _ := GobEncode(privatedata)
return SetPrivBytes(ctx, bytedata)
}
// See also: http://bit.ly/1HVWA9I
func SetPrivBytes(ctx uintptr, bytedata []byte) error {
// length and size
// length is a uint32 (usually 4 bytes)
// the length will be stored in front of the byte sequence
@@ -384,7 +375,7 @@ func SetPrivBytes(ctx uintptr, bytedata []byte) error {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, length)
if err != nil {
return errors.New("Could not write binary data into buffer: " + err.Error())
return -1
}
lengthbytes := buf.Bytes()
@@ -410,29 +401,10 @@ func SetPrivBytes(ctx uintptr, bytedata []byte) error {
// Call libmilter smfi_setpriv
type CtxPtr *C.struct_smfi_str
res := int(C.smfi_setpriv(int2ctx(ctx), unsafe.Pointer(lenStart)))
if res != int(C.MI_SUCCESS) {
return errors.New("smfi_setpriv() returned a failure")
}
return nil
return int(C.smfi_setpriv(int2ctx(ctx), unsafe.Pointer(lenStart)))
}
func GetPriv(ctx uintptr, privatedata interface{}) error {
databytes, err := GetPrivBytes(ctx)
if err != nil {
return err
}
err = GobDecode(databytes, privatedata)
if err != nil {
return err
}
return nil
}
func GetPrivBytes(ctx uintptr) ([]byte, error) {
func GetPriv(ctx uintptr, privatedata interface{}) int {
/* Retrieve the private data stored by the milter
Retrieving the data will release the memory allocated for it
Don't try to retrieve it again unless you call SetPriv first
@@ -443,7 +415,7 @@ func GetPrivBytes(ctx uintptr) ([]byte, error) {
// Make sure data has been set with a previous call to SetPriv
if CArray == nil {
return nil, errors.New("smfi_getpriv() call failed")
return -1
}
// Read uint32 size bytes from the start of the pointer
@@ -463,7 +435,7 @@ func GetPrivBytes(ctx uintptr) ([]byte, error) {
buf := bytes.NewBuffer(lengthbytes)
err := binary.Read(buf, binary.BigEndian, &length)
if err != nil {
return nil, errors.New("Could not parse binary data")
return -1
}
// Read byte sequence of data
@@ -478,7 +450,12 @@ func GetPrivBytes(ctx uintptr) ([]byte, error) {
C.smfi_setpriv(int2ctx(ctx), nil)
// Unserialize the data bytes back into a data structure
return databytes, nil
err = GobDecode(databytes, privatedata)
if err != nil {
return -1
}
return 0
}
func SetReply(ctx uintptr, rcode, xcode, message string) int {