示例#1
0
//export Go_xxfi_connect
func Go_xxfi_connect(ctx *C.SMFICTX, hostname *C.char, hostaddr *C._SOCK_ADDR) C.sfsistat {
	ctxptr := ctx2int(ctx)
	var ip net.IP

	if hostaddr.sa_family == C.AF_INET {
		hostaddrin := (*sockaddr_in)(unsafe.Pointer(hostaddr))
		ip_addr := make([]byte, 4)
		binary.LittleEndian.PutUint32(ip_addr, hostaddrin.sin_addr)
		ip = net.IPv4(ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3])

	} else if hostaddr.sa_family == C.AF_INET6 {
		sa_in := (*C.struct_sockaddr_in6)(unsafe.Pointer(hostaddr))
		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() {
			LoggerPrintln("hostaddr.sa_family value not implemented")
		}
		ip = net.ParseIP("::")
	}

	m := milter.(checkForConnect)
	code := m.Connect(ctxptr, C.GoString(hostname), ip)
	if milter.GetDebug() {
		LoggerPrintf("Connect callback returned: %d\n", code)
	}
	return C.sfsistat(code)
}
func NewInterfaces() ([]InterfaceTotal, string) {
	var ifaces *C.struct_ifaddrs
	if getrc, _ := C.getifaddrs(&ifaces); getrc != 0 {
		return []InterfaceTotal{}, ""
	}
	defer C.freeifaddrs(ifaces)

	ifs := []InterfaceTotal{}
	IP := ""

	for fi := ifaces; fi != nil; fi = fi.ifa_next {
		if fi.ifa_addr == nil {
			continue
		}

		ifa_name := C.GoString(fi.ifa_name)
		if IP == "" &&
			fi.ifa_addr.sa_family == C.AF_INET &&
			ifa_name != "lo" &&
			!rx_lo.Match([]byte(ifa_name)) &&
			realInterfaceName(ifa_name) {

			sa_in := (*C.struct_sockaddr_in)(unsafe.Pointer(fi.ifa_addr))
			if C.inet_ntop(
				C.int(fi.ifa_addr.sa_family), // C.AF_INET,
				unsafe.Pointer(&sa_in.sin_addr),
				&C.ADDR[0],
				C.socklen_t(unsafe.Sizeof(C.ADDR))) != nil {

				IP = C.GoString((*C.char)(unsafe.Pointer(&C.ADDR)))
			}
		}

		if fi.ifa_addr.sa_family != C.AF_LINK {
			continue
		}

		data := fi.ifa_data
		if C.Ibytes(data) == 0 &&
			C.Obytes(data) == 0 {
			continue
		}
		ifs = append(ifs, InterfaceTotal{
			Name: ifa_name,
			In:   uint(C.Ibytes(data)),
			Out:  uint(C.Obytes(data)),
		})
	}
	return ifs, IP
}
示例#3
0
文件: getifaddrs.go 项目: vadv/ostent
func ntop(fi *C.struct_ifaddrs) (string, bool) {
	if fi.ifa_addr == nil {
		return "", false
	}
	if fi.ifa_addr.sa_family != C.AF_INET {
		return "", false
	}
	saIn := (*C.struct_sockaddr_in)(unsafe.Pointer(fi.ifa_addr))
	if nil == C.inet_ntop(
		C.int(fi.ifa_addr.sa_family), // C.AF_INET,
		unsafe.Pointer(&saIn.sin_addr),
		&C.ADDR[0],
		C.socklen_t(unsafe.Sizeof(C.ADDR))) {
		return "", false
	}
	return C.GoString((*C.char)(unsafe.Pointer(&C.ADDR))), true
}
func NewInterfaces(CH chan InterfacesInfo) {
	var ifaces *C.struct_ifaddrs
	if getrc, _ := C.getifaddrs(&ifaces); getrc != 0 {
		CH <- InterfacesInfo{}
		return
	}
	defer C.freeifaddrs(ifaces)

	ifs := []InterfaceInfo{}
	IP := ""

	for fi := ifaces; fi != nil; fi = fi.ifa_next {
		if fi.ifa_addr == nil {
			continue
		}

		ifa_name := C.GoString(fi.ifa_name)
		if IP == "" &&
			fi.ifa_addr.sa_family == C.AF_INET &&
			!rx_lo.Match([]byte(ifa_name)) &&
			realInterfaceName(ifa_name) {

			sa_in := (*C.struct_sockaddr_in)(unsafe.Pointer(fi.ifa_addr))
			if C.inet_ntop(
				C.int(fi.ifa_addr.sa_family), // C.AF_INET,
				unsafe.Pointer(&sa_in.sin_addr),
				&C.ADDR[0],
				C.socklen_t(unsafe.Sizeof(C.ADDR))) != nil {

				IP = C.GoString((*C.char)(unsafe.Pointer(&C.ADDR)))
			}
		}

		if fi.ifa_addr.sa_family != C.AF_LINK {
			continue
		}

		data := fi.ifa_data
		it := InterfaceInfo{
			Name:       ifa_name,
			InBytes:    uint(C.Ibytes(data)),
			OutBytes:   uint(C.Obytes(data)),
			InPackets:  uint(C.Ipackets(data)),
			OutPackets: uint(C.Opackets(data)),
			InErrors:   uint(C.Ierrors(data)),
			OutErrors:  uint(C.Oerrors(data)),
		}
		if it.InBytes == 0 &&
			it.OutBytes == 0 &&
			it.InPackets == 0 &&
			it.OutPackets == 0 &&
			it.InErrors == 0 &&
			it.OutErrors == 0 {
			continue
		}
		ifs = append(ifs, it)
	}
	CH <- InterfacesInfo{
		List: ifs,
		IP:   IP,
	}
}