func (t *Interface) Write(ch chan []byte) (err error) { tx := syscall.Overlapped{} var hevent windows.Handle hevent, err = windows.CreateEvent(nil, 0, 0, nil) if err != nil { return } tx.HEvent = syscall.Handle(hevent) for { select { case data := <-ch: var l uint32 syscall.WriteFile(t.file, data, &l, &tx) syscall.WaitForSingleObject(tx.HEvent, syscall.INFINITE) tx.Offset += uint32(len(data)) } } }
func (l *winEventLog) Open(recordNumber uint64) error { bookmark, err := sys.CreateBookmark(l.channelName, recordNumber) if err != nil { return err } defer sys.Close(bookmark) // Using a pull subscription to receive events. See: // https://msdn.microsoft.com/en-us/library/windows/desktop/aa385771(v=vs.85).aspx#pull signalEvent, err := windows.CreateEvent(nil, 0, 0, nil) if err != nil { return nil } subscriptionHandle, err := sys.Subscribe( 0, // null session (used for connecting to remote event logs) signalEvent, l.channelName, "", // Query - nil means all events bookmark, // Bookmark - for resuming from a specific event sys.EvtSubscribeStartAfterBookmark) if err != nil { return err } l.subscription = subscriptionHandle return nil }
func (l *winEventLog) Open(recordNumber uint64) error { bookmark, err := win.CreateBookmark(l.channelName, recordNumber) if err != nil { return err } defer win.Close(bookmark) // Using a pull subscription to receive events. See: // https://msdn.microsoft.com/en-us/library/windows/desktop/aa385771(v=vs.85).aspx#pull signalEvent, err := windows.CreateEvent(nil, 0, 0, nil) if err != nil { return nil } debugf("%s using subscription query=%s", l.logPrefix, l.query) subscriptionHandle, err := win.Subscribe( 0, // Session - nil for localhost signalEvent, "", // Channel - empty b/c channel is in the query l.query, // Query - nil means all events bookmark, // Bookmark - for resuming from a specific event win.EvtSubscribeStartAfterBookmark) if err != nil { return err } l.subscription = subscriptionHandle return nil }
func newEvent() (*event, error) { h, err := windows.CreateEvent(nil, 0, 0, nil) if err != nil { return nil, err } return &event{h: h}, nil }
func (t *tun) Read(ch chan []byte) (err error) { overlappedRx := syscall.Overlapped{} var hevent windows.Handle hevent, err = windows.CreateEvent(nil, 0, 0, nil) if err != nil { return } overlappedRx.HEvent = syscall.Handle(hevent) buf := make([]byte, t.mtu) var l uint32 for { if err := syscall.ReadFile(t.fd, buf, &l, &overlappedRx); err != nil { } if _, err := syscall.WaitForSingleObject(overlappedRx.HEvent, syscall.INFINITE); err != nil { fmt.Println(err) } overlappedRx.Offset += l totalLen := 0 switch buf[0] & 0xf0 { case 0x40: totalLen = 256*int(buf[2]) + int(buf[3]) case 0x60: continue totalLen = 256*int(buf[4]) + int(buf[5]) + IPv6_HEADER_LENGTH } fmt.Println("read data", buf[:totalLen]) send := make([]byte, totalLen) copy(send, buf) ch <- send } }
func newWindowsEvent(name string) (*windowsEvent, error) { str, err := windows.UTF16PtrFromString(name) if nil != err { return nil, err } handle, err := windows.CreateEvent(nil, 0, 0, str) if nil != err { return nil, err } return &windowsEvent{handle: handle}, nil }
func (t *Interface) Read(ch chan []byte) (err error) { rx := syscall.Overlapped{} var hevent windows.Handle hevent, err = windows.CreateEvent(nil, 0, 0, nil) if err != nil { return } rx.HEvent = syscall.Handle(hevent) buf := make([]byte, 1500) var l uint32 for { if err := syscall.ReadFile(t.file, buf, &l, &rx); err != nil { } if _, err := syscall.WaitForSingleObject(rx.HEvent, syscall.INFINITE); err != nil { Log(Error, "Failed to read from TUN/TAP: %v", err) } rx.Offset += l ch <- buf } }