Exemple #1
0
// Closesocket wraps syscall.Closesocket.
func (sw *Switch) Closesocket(s syscall.Handle) (err error) {
	so := sw.sockso(s)
	if so == nil {
		return syscall.Closesocket(s)
	}
	sw.fmu.RLock()
	f, _ := sw.fltab[FilterClose]
	sw.fmu.RUnlock()

	af, err := f.apply(so)
	if err != nil {
		return err
	}
	so.Err = syscall.Closesocket(s)
	if err = af.apply(so); err != nil {
		return err
	}

	sw.smu.Lock()
	defer sw.smu.Unlock()
	if so.Err != nil {
		sw.stats.getLocked(so.Cookie).CloseFailed++
		return so.Err
	}
	delete(sw.sotab, s)
	sw.stats.getLocked(so.Cookie).Closed++
	return nil
}
Exemple #2
0
// Socket wraps syscall.Socket.
func (sw *Switch) Socket(family, sotype, proto int) (s syscall.Handle, err error) {
	so := &Status{Cookie: cookie(family, sotype, proto)}
	sw.fmu.RLock()
	f, _ := sw.fltab[FilterSocket]
	sw.fmu.RUnlock()

	af, err := f.apply(so)
	if err != nil {
		return syscall.InvalidHandle, err
	}
	s, so.Err = syscall.Socket(family, sotype, proto)
	if err = af.apply(so); err != nil {
		if so.Err == nil {
			syscall.Closesocket(s)
		}
		return syscall.InvalidHandle, err
	}

	sw.smu.Lock()
	defer sw.smu.Unlock()
	if so.Err != nil {
		sw.stats.getLocked(so.Cookie).OpenFailed++
		return syscall.InvalidHandle, so.Err
	}
	nso := sw.addLocked(s, family, sotype, proto)
	sw.stats.getLocked(nso.Cookie).Opened++
	return s, nil
}
Exemple #3
0
func getInterfaceList() ([]syscall.InterfaceInfo, os.Error) {
	s, e := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
	if e != 0 {
		return nil, os.NewSyscallError("Socket", e)
	}
	defer syscall.Closesocket(s)

	ii := [20]syscall.InterfaceInfo{}
	ret := uint32(0)
	size := uint32(unsafe.Sizeof(ii))
	e = syscall.WSAIoctl(s, syscall.SIO_GET_INTERFACE_LIST, nil, 0, (*byte)(unsafe.Pointer(&ii[0])), size, &ret, nil, 0)
	if e != 0 {
		return nil, os.NewSyscallError("WSAIoctl", e)
	}
	c := ret / uint32(unsafe.Sizeof(ii[0]))
	return ii[:c-1], nil
}
func getInterfaceInfos() ([]syscall.InterfaceInfo, error) {
	s, err := sysSocket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
	if err != nil {
		return nil, os.NewSyscallError("Socket", err)
	}
	defer syscall.Closesocket(s)

	iia := [20]syscall.InterfaceInfo{}
	ret := uint32(0)
	size := uint32(unsafe.Sizeof(iia))
	err = syscall.WSAIoctl(s, syscall.SIO_GET_INTERFACE_LIST, nil, 0, (*byte)(unsafe.Pointer(&iia[0])), size, &ret, nil, 0)
	if err != nil {
		return nil, os.NewSyscallError("WSAIoctl", err)
	}
	iilen := ret / uint32(unsafe.Sizeof(iia[0]))
	return iia[:iilen-1], nil
}
Exemple #5
0
func skipRawSocketTests() (skip bool, skipmsg string, err error) {
	// From http://msdn.microsoft.com/en-us/library/windows/desktop/ms740548.aspx:
	// Note: To use a socket of type SOCK_RAW requires administrative privileges.
	// Users running Winsock applications that use raw sockets must be a member of
	// the Administrators group on the local computer, otherwise raw socket calls
	// will fail with an error code of WSAEACCES. On Windows Vista and later, access
	// for raw sockets is enforced at socket creation. In earlier versions of Windows,
	// access for raw sockets is enforced during other socket operations.
	s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, 0)
	if err == syscall.WSAEACCES {
		return true, "skipping test; no access to raw socket allowed", nil
	}
	if err != nil {
		return true, "", err
	}
	defer syscall.Closesocket(s)
	return false, "", nil
}
func supportsRawIPSocket() (string, bool) {
	// From http://msdn.microsoft.com/en-us/library/windows/desktop/ms740548.aspx:
	// Note: To use a socket of type SOCK_RAW requires administrative privileges.
	// Users running Winsock applications that use raw sockets must be a member of
	// the Administrators group on the local computer, otherwise raw socket calls
	// will fail with an error code of WSAEACCES. On Windows Vista and later, access
	// for raw sockets is enforced at socket creation. In earlier versions of Windows,
	// access for raw sockets is enforced during other socket operations.
	s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, 0)
	if err == syscall.WSAEACCES {
		return fmt.Sprintf("no access to raw socket allowed on %s", runtime.GOOS), false
	}
	if err != nil {
		return err.Error(), false
	}
	syscall.Closesocket(s)
	return "", true
}
Exemple #7
0
func (s *Ssdp) createSocket() error {
	// create the socket
	var err error
	s.socket.socket, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
	if err != nil {
		return err
	}
	// make sure we can reuse it / share it
	if err := syscall.SetsockoptInt(s.socket.socket, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
		syscall.Closesocket(s.socket.socket)
		s.socket.socket = 0
		return err
	}
	// going to broadcast
	if err := syscall.SetsockoptInt(s.socket.socket, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1); err != nil {
		syscall.Closesocket(s.socket.socket)
		s.socket.socket = 0
		return err
	}
	// bind it to the ssdp port
	lsa := &syscall.SockaddrInet4{Port: 1900, Addr: [4]byte{0, 0, 0, 0}}
	err = syscall.Bind(s.socket.socket, lsa)
	if err != nil {
		syscall.Closesocket(s.socket.socket)
		s.socket.socket = 0
		return err
	}
	iter, err := net.Interfaces()
	if err != nil {
		syscall.Closesocket(s.socket.socket)
		s.socket.socket = 0
		return err
	}
	wasFound := false
	for i := range iter {
		if iter[i].Flags&net.FlagMulticast == 0 {
			continue
		}
		addrs, err := iter[i].Addrs()
		if err != nil {
			continue
		}
		for k := range addrs {
			as4 := addrs[k].(*net.IPAddr).IP.To4()
			// join the multicast group
			mreq := &syscall.IPMreq{Multiaddr: [4]byte{239, 255, 255, 250}, Interface: [4]byte{as4[0], as4[1], as4[2], as4[3]}}
			if err := syscall.SetsockoptIPMreq(s.socket.socket, syscall.IPPROTO_IP, syscall.IP_ADD_MEMBERSHIP, mreq); err != nil {
				syscall.Closesocket(s.socket.socket)
				s.socket.socket = 0
				return err
			}
			wasFound = true
		}
	}
	// if we couldn't join a group, fall back to just 0.0.0.0
	if !wasFound {
		mreq := &syscall.IPMreq{Multiaddr: [4]byte{239, 255, 255, 250}, Interface: [4]byte{0, 0, 0, 0}}
		if err := syscall.SetsockoptIPMreq(s.socket.socket, syscall.IPPROTO_IP, syscall.IP_ADD_MEMBERSHIP, mreq); err != nil {
			syscall.Closesocket(s.socket.socket)
			s.socket.socket = 0
			return err
		}
	}

	s.socket.readBytes = make([]byte, 2048)

	return nil
}
Exemple #8
0
func (s *Ssdp) closeSocket() {
	syscall.Closesocket(s.socket.socket)
	s.socket.socket = 0
}
Exemple #9
0
func closesocket(s syscall.Handle) error {
	return syscall.Closesocket(s)
}
func closesocket(s int) (errno int) {
	return syscall.Closesocket(int32(s))
}
Exemple #11
0
func closesocket(s syscall.Handle) (errno int) {
	return syscall.Closesocket(s)
}
Exemple #12
0
func closesocket(s syscall.Handle) (err error) {
	return syscall.Closesocket(s)
}