Пример #1
1
Файл: main.go Проект: qmsk/nlgo
func main() {
	sk := nlgo.NlSocketAlloc()
	if err := nlgo.GenlConnect(sk); err != nil {
		panic(err)
	} else if err := nlgo.GenlSendSimple(sk, nlgo.GENL_ID_CTRL, nlgo.CTRL_CMD_GETFAMILY, nlgo.CTRL_VERSION, syscall.NLM_F_DUMP); err != nil {
		panic(err)
	}
	nl80211 := nlgo.NlSocketAlloc()
	if err := nlgo.GenlConnect(nl80211); err != nil {
		panic(err)
	}

	data := make([]byte, syscall.Getpagesize())
	if n, _, _, _, err := syscall.Recvmsg(sk.Fd, data, nil, 0); err != nil {
		panic(err)
	} else if msgs, err := syscall.ParseNetlinkMessage(data[:n]); err != nil {
		log.Print("X", err)
	} else {
		for _, msg := range msgs {
			genl := *(*nlgo.GenlMsghdr)(unsafe.Pointer(&msg.Data[0]))
			if msg.Header.Type == nlgo.GENL_ID_CTRL && genl.Cmd == nlgo.CTRL_CMD_NEWFAMILY {
				if attr, err := nlgo.CtrlPolicy.Parse(msg.Data[nlgo.GENL_HDRLEN:]); err != nil {
					log.Print(err)
				} else if amap, ok := attr.(nlgo.AttrMap); !ok {
					log.Print(attr)
				} else if value := amap.Get(nlgo.CTRL_ATTR_FAMILY_NAME).(nlgo.NulString); string(value) == "nl80211" {
					log.Printf("%v", attr)
					for _, g := range amap.Get(nlgo.CTRL_ATTR_MCAST_GROUPS).(nlgo.AttrSlice).Slice() {
						group := g.Value.(nlgo.AttrMap)
						pid := group.Get(nlgo.CTRL_ATTR_MCAST_GRP_ID).(nlgo.U32)
						if err := nlgo.NlSocketAddMembership(nl80211, int(pid)); err != nil {
							log.Print(err)
						}
					}
				}
			} else {
				log.Print("UNKNOWN")
			}
		}
	}
	nlgo.NlSocketFree(sk)

	for {
		if n, _, _, _, err := syscall.Recvmsg(nl80211.Fd, data, nil, 0); err != nil {
			panic(err)
		} else if msgs, err := syscall.ParseNetlinkMessage(data[:n]); err != nil {
			log.Print("Y", err)
		} else {
			for _, msg := range msgs {
				genl := (*nlgo.GenlMsghdr)(unsafe.Pointer(&msg.Data[0]))
				if attr, err := nlgo.Nl80211Policy.Parse(msg.Data[nlgo.GENL_HDRLEN:]); err != nil {
					log.Print("Z", err)
				} else {
					log.Printf("NL80211_CMD_%s attrs=%s", nlgo.NL80211_CMD_itoa[genl.Cmd], attr)
				}
			}
		}
	}
}
Пример #2
0
func main() {
	nlsk := nlgo.NlSocketAlloc()
	nlgo.NlConnect(nlsk, syscall.NETLINK_GENERIC)
	familyID := uint16(22)
	fmt.Println(familyID)
	files, _ := ioutil.ReadDir("/proc/")
	var pids []int
	for _, f := range files {
		if f.Name()[0] >= '0' && f.Name()[0] <= '9' {
			pid, _ := strconv.ParseInt(f.Name(), 10, 32)
			pids = append(pids, int(pid))
		}
	}
	for i := 0; i < 50; i++ {
		time.Sleep(1000 * time.Millisecond)

		for _, p := range pids {
			t := netlink.GetTaskStats(nlsk, p)
			fmt.Printf("pid: %d ", p)
			if t != nil {
				if taskstatsMap[p] == nil {
					taskstatsMap[p] = t
				} else {
					fmt.Println(t.Read_bytes-taskstatsMap[p].Read_bytes,
						t.Write_bytes-taskstatsMap[p].Write_bytes)
					taskstatsMap[p] = t
				}

			}
		}
	}
}
Пример #3
0
func BenchmarkNetlink(b *testing.B) {
	nlsk := nlgo.NlSocketAlloc()
	nlgo.NlConnect(nlsk, syscall.NETLINK_GENERIC)
	for i := 0; i < b.N; i++ {
		for _, p := range pids {
			t := netlink.GetTaskStats(nlsk, p)
			if t != nil {
				if taskstatsMap[p] == nil {
					taskstatsMap[p] = t
				} else {
					taskstatsMap[p] = t
				}

			}
		}
	}
}
Пример #4
0
func main() {
	nlsk := nlgo.NlSocketAlloc()
	nlgo.NlConnect(nlsk, syscall.NETLINK_GENERIC)
	syscall.Bind(nlsk.Fd, &syscall.SockaddrNetlink{
		Family: syscall.AF_NETLINK,
		Pid:    uint32(os.Getpid()),
		Pad:    CN_IDX_PROC,
	})
	data := []byte{
		40,
		0,
		0,
		0,
		3,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		77,
		26,
		0,
		0,
		1,
		0,
		0,
		0,
		1,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		0,
		4,
		0,
		0,
		0,
		1,
		0,
		0,
		0,
	}

	p := (*ppp)(unsafe.Pointer(&data[12]))
	p.pid = uint32(os.Getpid())

	fmt.Println(data)

	err := syscall.Sendto(nlsk.Fd, data, 0, &nlsk.Peer)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("send success")
	}

	func() error {
		for {
			buf := make([]byte, 1024)
			if _, _, err := syscall.Recvfrom(nlsk.Fd, buf, 0); err != nil {
				return err
			}
			fmt.Println(buf)
		}
	}()

}