Add support for ipv6

This commit is contained in:
Dolf Schimmel (Freeaqingme)
2015-06-01 22:16:02 +02:00
parent c0f4b47c5e
commit 24ed8dcbc3

View File

@@ -17,7 +17,7 @@ package gomilter
#cgo LDFLAGS: -lmilter #cgo LDFLAGS: -lmilter
#include <stdlib.h> #include <stdlib.h>
//#include <string.h> #include <arpa/inet.h>
#include "libmilter/mfapi.h" #include "libmilter/mfapi.h"
#include "filter.h" #include "filter.h"
@@ -28,6 +28,7 @@ import (
"encoding/binary" "encoding/binary"
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"net"
"os" "os"
"reflect" "reflect"
"strings" "strings"
@@ -101,7 +102,7 @@ func (m *MilterRaw) GetSocket() string {
// ********* Callback checking types ********* // ********* Callback checking types *********
type checkForConnect interface { type checkForConnect interface {
Connect(ctx uintptr, hostname, ip string) (sfsistat int8) Connect(ctx uintptr, hostname string, ip net.IP) (sfsistat int8)
} }
type checkForHelo interface { type checkForHelo interface {
@@ -200,29 +201,37 @@ func GobDecode(buf []byte, data interface{}) error {
//export Go_xxfi_connect //export Go_xxfi_connect
func Go_xxfi_connect(ctx *C.SMFICTX, hostname *C.char, hostaddr *C._SOCK_ADDR) C.sfsistat { func Go_xxfi_connect(ctx *C.SMFICTX, hostname *C.char, hostaddr *C._SOCK_ADDR) C.sfsistat {
ctxptr := ctx2int(ctx) ctxptr := ctx2int(ctx)
// Check if the host address is a regular ipv4 address var ip net.IP
if hostaddr.sa_family == C.AF_INET {
//LoggerPrintln(hostaddr.sa_data)
// hostaddrin is a parallel data structure of the C type hostaddr if hostaddr.sa_family == C.AF_INET {
//var hostaddrin *sockaddr_in
hostaddrin := (*sockaddr_in)(unsafe.Pointer(hostaddr)) hostaddrin := (*sockaddr_in)(unsafe.Pointer(hostaddr))
//LoggerPrintln(hostaddrin)
ip_addr := make([]byte, 4) ip_addr := make([]byte, 4)
binary.LittleEndian.PutUint32(ip_addr, hostaddrin.sin_addr) binary.LittleEndian.PutUint32(ip_addr, hostaddrin.sin_addr)
ip = net.IPv4(ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3])
// Call our application's callback } else if hostaddr.sa_family == C.AF_INET6 {
m := milter.(checkForConnect) sa_in := (*C.struct_sockaddr_in6)(unsafe.Pointer(hostaddr))
code := m.Connect(ctxptr, C.GoString(hostname), fmt.Sprintf("%d.%d.%d.%d", ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3])) var dst = make([]byte, 16)
C.inet_ntop(
C.int(hostaddr.sa_family),
unsafe.Pointer(&sa_in.sin6_addr),
(*C.char)(unsafe.Pointer(&dst)),
16)
ip = net.ParseIP(C.GoString((*C.char)(unsafe.Pointer(&dst))))
} else {
if milter.GetDebug() { if milter.GetDebug() {
LoggerPrintf("Connect callback returned: %d\n", code) LoggerPrintln("hostaddr.sa_family value not implemented")
} }
return C.sfsistat(code) ip = net.ParseIP("::")
} }
m := milter.(checkForConnect)
code := m.Connect(ctxptr, C.GoString(hostname), ip)
if milter.GetDebug() { if milter.GetDebug() {
LoggerPrintln("hostaddr.sa_family value not implemented") LoggerPrintf("Connect callback returned: %d\n", code)
} }
return C.SMFIS_CONTINUE return C.sfsistat(code)
} }
//export Go_xxfi_helo //export Go_xxfi_helo