[net][windows] Remove exported types and convert ALL_CAPS to mixedCaps

pull/549/head
pytimer 7 years ago
parent 1c2cebbbc4
commit 0ffcfc901f

@ -220,22 +220,22 @@ func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoC
func getTableUintptr(family uint32, buf []byte) uintptr { func getTableUintptr(family uint32, buf []byte) uintptr {
var ( var (
pmibTCPTable PMIB_TCPTABLE_OWNER_PID_ALL pmibTCPTable pmibTCPTableOwnerPidAll
pmibTCP6Table PMIB_TCP6TABLE_OWNER_PID_ALL pmibTCP6Table pmibTCP6TableOwnerPidAll
p uintptr p uintptr
) )
switch family { switch family {
case kindTCP4.family: case kindTCP4.family:
if len(buf) > 0 { if len(buf) > 0 {
pmibTCPTable = (*MIB_TCPTABLE_OWNER_PID)(unsafe.Pointer(&buf[0])) pmibTCPTable = (*mibTCPTableOwnerPid)(unsafe.Pointer(&buf[0]))
p = uintptr(unsafe.Pointer(pmibTCPTable)) p = uintptr(unsafe.Pointer(pmibTCPTable))
} else { } else {
p = uintptr(unsafe.Pointer(pmibTCPTable)) p = uintptr(unsafe.Pointer(pmibTCPTable))
} }
case kindTCP6.family: case kindTCP6.family:
if len(buf) > 0 { if len(buf) > 0 {
pmibTCP6Table = (*MIB_TCP6TABLE_OWNER_PID)(unsafe.Pointer(&buf[0])) pmibTCP6Table = (*mibTCP6TableOwnerPid)(unsafe.Pointer(&buf[0]))
p = uintptr(unsafe.Pointer(pmibTCP6Table)) p = uintptr(unsafe.Pointer(pmibTCP6Table))
} else { } else {
p = uintptr(unsafe.Pointer(pmibTCP6Table)) p = uintptr(unsafe.Pointer(pmibTCP6Table))
@ -247,21 +247,21 @@ func getTableUintptr(family uint32, buf []byte) uintptr {
func getTableInfo(filename string, table interface{}) (index, step, length int) { func getTableInfo(filename string, table interface{}) (index, step, length int) {
switch filename { switch filename {
case kindTCP4.filename: case kindTCP4.filename:
index = int(unsafe.Sizeof(table.(PMIB_TCPTABLE_OWNER_PID_ALL).DwNumEntries)) index = int(unsafe.Sizeof(table.(pmibTCPTableOwnerPidAll).DwNumEntries))
step = int(unsafe.Sizeof(table.(PMIB_TCPTABLE_OWNER_PID_ALL).Table)) step = int(unsafe.Sizeof(table.(pmibTCPTableOwnerPidAll).Table))
length = int(table.(PMIB_TCPTABLE_OWNER_PID_ALL).DwNumEntries) length = int(table.(pmibTCPTableOwnerPidAll).DwNumEntries)
case kindTCP6.filename: case kindTCP6.filename:
index = int(unsafe.Sizeof(table.(PMIB_TCP6TABLE_OWNER_PID_ALL).DwNumEntries)) index = int(unsafe.Sizeof(table.(pmibTCP6TableOwnerPidAll).DwNumEntries))
step = int(unsafe.Sizeof(table.(PMIB_TCP6TABLE_OWNER_PID_ALL).Table)) step = int(unsafe.Sizeof(table.(pmibTCP6TableOwnerPidAll).Table))
length = int(table.(PMIB_TCP6TABLE_OWNER_PID_ALL).DwNumEntries) length = int(table.(pmibTCP6TableOwnerPidAll).DwNumEntries)
case kindUDP4.filename: case kindUDP4.filename:
index = int(unsafe.Sizeof(table.(PMIB_UDPTABLE_OWNER_PID).DwNumEntries)) index = int(unsafe.Sizeof(table.(pmibUDPTableOwnerPid).DwNumEntries))
step = int(unsafe.Sizeof(table.(PMIB_UDPTABLE_OWNER_PID).Table)) step = int(unsafe.Sizeof(table.(pmibUDPTableOwnerPid).Table))
length = int(table.(PMIB_UDPTABLE_OWNER_PID).DwNumEntries) length = int(table.(pmibUDPTableOwnerPid).DwNumEntries)
case kindUDP6.filename: case kindUDP6.filename:
index = int(unsafe.Sizeof(table.(PMIB_UDP6TABLE_OWNER_PID).DwNumEntries)) index = int(unsafe.Sizeof(table.(pmibUDP6TableOwnerPid).DwNumEntries))
step = int(unsafe.Sizeof(table.(PMIB_UDP6TABLE_OWNER_PID).Table)) step = int(unsafe.Sizeof(table.(pmibUDP6TableOwnerPid).Table))
length = int(table.(PMIB_UDP6TABLE_OWNER_PID).DwNumEntries) length = int(table.(pmibUDP6TableOwnerPid).DwNumEntries)
} }
return return
@ -273,8 +273,8 @@ func getTCPConnections(family uint32) ([]ConnectionStat, error) {
buf []byte buf []byte
size uint32 size uint32
pmibTCPTable PMIB_TCPTABLE_OWNER_PID_ALL pmibTCPTable pmibTCPTableOwnerPidAll
pmibTCP6Table PMIB_TCP6TABLE_OWNER_PID_ALL pmibTCP6Table pmibTCP6TableOwnerPidAll
) )
if family == 0 { if family == 0 {
@ -285,14 +285,14 @@ func getTCPConnections(family uint32) ([]ConnectionStat, error) {
switch family { switch family {
case kindTCP4.family: case kindTCP4.family:
if len(buf) > 0 { if len(buf) > 0 {
pmibTCPTable = (*MIB_TCPTABLE_OWNER_PID)(unsafe.Pointer(&buf[0])) pmibTCPTable = (*mibTCPTableOwnerPid)(unsafe.Pointer(&buf[0]))
p = uintptr(unsafe.Pointer(pmibTCPTable)) p = uintptr(unsafe.Pointer(pmibTCPTable))
} else { } else {
p = uintptr(unsafe.Pointer(pmibTCPTable)) p = uintptr(unsafe.Pointer(pmibTCPTable))
} }
case kindTCP6.family: case kindTCP6.family:
if len(buf) > 0 { if len(buf) > 0 {
pmibTCP6Table = (*MIB_TCP6TABLE_OWNER_PID)(unsafe.Pointer(&buf[0])) pmibTCP6Table = (*mibTCP6TableOwnerPid)(unsafe.Pointer(&buf[0]))
p = uintptr(unsafe.Pointer(pmibTCP6Table)) p = uintptr(unsafe.Pointer(pmibTCP6Table))
} else { } else {
p = uintptr(unsafe.Pointer(pmibTCP6Table)) p = uintptr(unsafe.Pointer(pmibTCP6Table))
@ -303,7 +303,7 @@ func getTCPConnections(family uint32) ([]ConnectionStat, error) {
&size, &size,
true, true,
family, family,
TCP_TABLE_OWNER_PID_ALL, tcpTableOwnerPidAll,
0) 0)
if err == nil { if err == nil {
break break
@ -334,11 +334,11 @@ func getTCPConnections(family uint32) ([]ConnectionStat, error) {
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
switch family { switch family {
case kindTCP4.family: case kindTCP4.family:
mibs := (*MIB_TCPROW_OWNER_PID)(unsafe.Pointer(&buf[index])) mibs := (*mibTCPRowOwnerPid)(unsafe.Pointer(&buf[index]))
ns := mibs.convertToConnectionStat() ns := mibs.convertToConnectionStat()
stats = append(stats, ns) stats = append(stats, ns)
case kindTCP6.family: case kindTCP6.family:
mibs := (*MIB_TCP6ROW_OWNER_PID)(unsafe.Pointer(&buf[index])) mibs := (*mibTCP6RowOwnerPid)(unsafe.Pointer(&buf[index]))
ns := mibs.convertToConnectionStat() ns := mibs.convertToConnectionStat()
stats = append(stats, ns) stats = append(stats, ns)
} }
@ -354,8 +354,8 @@ func getUDPConnections(family uint32) ([]ConnectionStat, error) {
buf []byte buf []byte
size uint32 size uint32
pmibUDPTable PMIB_UDPTABLE_OWNER_PID pmibUDPTable pmibUDPTableOwnerPid
pmibUDP6Table PMIB_UDP6TABLE_OWNER_PID pmibUDP6Table pmibUDP6TableOwnerPid
) )
if family == 0 { if family == 0 {
@ -366,14 +366,14 @@ func getUDPConnections(family uint32) ([]ConnectionStat, error) {
switch family { switch family {
case kindUDP4.family: case kindUDP4.family:
if len(buf) > 0 { if len(buf) > 0 {
pmibUDPTable = (*MIB_UDPTABLE_OWNER_PID)(unsafe.Pointer(&buf[0])) pmibUDPTable = (*mibUDPTableOwnerPid)(unsafe.Pointer(&buf[0]))
p = uintptr(unsafe.Pointer(pmibUDPTable)) p = uintptr(unsafe.Pointer(pmibUDPTable))
} else { } else {
p = uintptr(unsafe.Pointer(pmibUDPTable)) p = uintptr(unsafe.Pointer(pmibUDPTable))
} }
case kindUDP6.family: case kindUDP6.family:
if len(buf) > 0 { if len(buf) > 0 {
pmibUDP6Table = (*MIB_UDP6TABLE_OWNER_PID)(unsafe.Pointer(&buf[0])) pmibUDP6Table = (*mibUDP6TableOwnerPid)(unsafe.Pointer(&buf[0]))
p = uintptr(unsafe.Pointer(pmibUDP6Table)) p = uintptr(unsafe.Pointer(pmibUDP6Table))
} else { } else {
p = uintptr(unsafe.Pointer(pmibUDP6Table)) p = uintptr(unsafe.Pointer(pmibUDP6Table))
@ -385,7 +385,7 @@ func getUDPConnections(family uint32) ([]ConnectionStat, error) {
&size, &size,
true, true,
family, family,
UDP_TABLE_OWNER_PID, udpTableOwnerPid,
0, 0,
) )
if err == nil { if err == nil {
@ -416,11 +416,11 @@ func getUDPConnections(family uint32) ([]ConnectionStat, error) {
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
switch family { switch family {
case kindUDP4.family: case kindUDP4.family:
mibs := (*MIB_UDPROW_OWNER_PID)(unsafe.Pointer(&buf[index])) mibs := (*mibUDPRowOwnerPid)(unsafe.Pointer(&buf[index]))
ns := mibs.convertToConnectionStat() ns := mibs.convertToConnectionStat()
stats = append(stats, ns) stats = append(stats, ns)
case kindUDP4.family: case kindUDP4.family:
mibs := (*MIB_UDP6ROW_OWNER_PID)(unsafe.Pointer(&buf[index])) mibs := (*mibUDP6RowOwnerPid)(unsafe.Pointer(&buf[index]))
ns := mibs.convertToConnectionStat() ns := mibs.convertToConnectionStat()
stats = append(stats, ns) stats = append(stats, ns)
} }
@ -431,7 +431,7 @@ func getUDPConnections(family uint32) ([]ConnectionStat, error) {
} }
// tcpStatuses https://msdn.microsoft.com/en-us/library/windows/desktop/bb485761(v=vs.85).aspx // tcpStatuses https://msdn.microsoft.com/en-us/library/windows/desktop/bb485761(v=vs.85).aspx
var tcpStatuses = map[MIB_TCP_STATE]string{ var tcpStatuses = map[mibTCPState]string{
1: "CLOSED", 1: "CLOSED",
2: "LISTEN", 2: "LISTEN",
3: "SYN_SENT", 3: "SYN_SENT",
@ -446,7 +446,7 @@ var tcpStatuses = map[MIB_TCP_STATE]string{
12: "DELETE", 12: "DELETE",
} }
func getExtendedTcpTable(pTcpTable uintptr, pdwSize *uint32, bOrder bool, ulAf uint32, tableClass TCP_TABLE_CLASS, reserved uint32) (errcode error) { func getExtendedTcpTable(pTcpTable uintptr, pdwSize *uint32, bOrder bool, ulAf uint32, tableClass tcpTableClass, reserved uint32) (errcode error) {
r1, _, _ := syscall.Syscall6(procGetExtendedTCPTable.Addr(), 6, pTcpTable, uintptr(unsafe.Pointer(pdwSize)), getUintptrFromBool(bOrder), uintptr(ulAf), uintptr(tableClass), uintptr(reserved)) r1, _, _ := syscall.Syscall6(procGetExtendedTCPTable.Addr(), 6, pTcpTable, uintptr(unsafe.Pointer(pdwSize)), getUintptrFromBool(bOrder), uintptr(ulAf), uintptr(tableClass), uintptr(reserved))
if r1 != 0 { if r1 != 0 {
errcode = syscall.Errno(r1) errcode = syscall.Errno(r1)
@ -454,7 +454,7 @@ func getExtendedTcpTable(pTcpTable uintptr, pdwSize *uint32, bOrder bool, ulAf u
return return
} }
func getExtendedUdpTable(pUdpTable uintptr, pdwSize *uint32, bOrder bool, ulAf uint32, tableClass UDP_TABLE_CLASS, reserved uint32) (errcode error) { func getExtendedUdpTable(pUdpTable uintptr, pdwSize *uint32, bOrder bool, ulAf uint32, tableClass udpTableClass, reserved uint32) (errcode error) {
r1, _, _ := syscall.Syscall6(procGetExtendedUDPTable.Addr(), 6, pUdpTable, uintptr(unsafe.Pointer(pdwSize)), getUintptrFromBool(bOrder), uintptr(ulAf), uintptr(tableClass), uintptr(reserved)) r1, _, _ := syscall.Syscall6(procGetExtendedUDPTable.Addr(), 6, pUdpTable, uintptr(unsafe.Pointer(pdwSize)), getUintptrFromBool(bOrder), uintptr(ulAf), uintptr(tableClass), uintptr(reserved))
if r1 != 0 { if r1 != 0 {
errcode = syscall.Errno(r1) errcode = syscall.Errno(r1)
@ -469,35 +469,36 @@ func getUintptrFromBool(b bool) uintptr {
return 0 return 0
} }
const ANY_SIZE = 1 const anySize = 1
type MIB_TCP_STATE int32 // type MIB_TCP_STATE int32
type mibTCPState int32
type TCP_TABLE_CLASS int32 type tcpTableClass int32
const ( const (
TCP_TABLE_BASIC_LISTENER TCP_TABLE_CLASS = iota tcpTableBasicListener tcpTableClass = iota
TCP_TABLE_BASIC_CONNECTIONS tcpTableBasicConnections
TCP_TABLE_BASIC_ALL tcpTableBasicAll
TCP_TABLE_OWNER_PID_LISTENER tcpTableOwnerPidListener
TCP_TABLE_OWNER_PID_CONNECTIONS tcpTableOwnerPidConnections
TCP_TABLE_OWNER_PID_ALL tcpTableOwnerPidAll
TCP_TABLE_OWNER_MODULE_LISTENER tcpTableOwnerModuleListener
TCP_TABLE_OWNER_MODULE_CONNECTIONS tcpTableOwnerModuleConnections
TCP_TABLE_OWNER_MODULE_ALL tcpTableOwnerModuleAll
) )
type UDP_TABLE_CLASS int32 type udpTableClass int32
const ( const (
UDP_TABLE_BASIC UDP_TABLE_CLASS = iota udpTableBasic udpTableClass = iota
UDP_TABLE_OWNER_PID udpTableOwnerPid
UDP_TABLE_OWNER_MODULE udpTableOwnerModule
) )
// TCP // TCP
type MIB_TCPROW_OWNER_PID struct { type mibTCPRowOwnerPid struct {
DwState uint32 DwState uint32
DwLocalAddr uint32 DwLocalAddr uint32
DwLocalPort uint32 DwLocalPort uint32
@ -506,7 +507,7 @@ type MIB_TCPROW_OWNER_PID struct {
DwOwningPid uint32 DwOwningPid uint32
} }
func (m *MIB_TCPROW_OWNER_PID) convertToConnectionStat() ConnectionStat { func (m *mibTCPRowOwnerPid) convertToConnectionStat() ConnectionStat {
ns := ConnectionStat{ ns := ConnectionStat{
Family: kindTCP4.family, Family: kindTCP4.family,
Type: kindTCP4.sockType, Type: kindTCP4.sockType,
@ -519,18 +520,18 @@ func (m *MIB_TCPROW_OWNER_PID) convertToConnectionStat() ConnectionStat {
Port: uint32(decodePort(m.DwRemotePort)), Port: uint32(decodePort(m.DwRemotePort)),
}, },
Pid: int32(m.DwOwningPid), Pid: int32(m.DwOwningPid),
Status: tcpStatuses[MIB_TCP_STATE(m.DwState)], Status: tcpStatuses[mibTCPState(m.DwState)],
} }
return ns return ns
} }
type MIB_TCPTABLE_OWNER_PID struct { type mibTCPTableOwnerPid struct {
DwNumEntries uint32 DwNumEntries uint32
Table [ANY_SIZE]MIB_TCPROW_OWNER_PID Table [anySize]mibTCPRowOwnerPid
} }
type MIB_TCP6ROW_OWNER_PID struct { type mibTCP6RowOwnerPid struct {
UcLocalAddr [16]byte UcLocalAddr [16]byte
DwLocalScopeId uint32 DwLocalScopeId uint32
DwLocalPort uint32 DwLocalPort uint32
@ -541,7 +542,7 @@ type MIB_TCP6ROW_OWNER_PID struct {
DwOwningPid uint32 DwOwningPid uint32
} }
func (m *MIB_TCP6ROW_OWNER_PID) convertToConnectionStat() ConnectionStat { func (m *mibTCP6RowOwnerPid) convertToConnectionStat() ConnectionStat {
ns := ConnectionStat{ ns := ConnectionStat{
Family: kindTCP6.family, Family: kindTCP6.family,
Type: kindTCP6.sockType, Type: kindTCP6.sockType,
@ -554,29 +555,29 @@ func (m *MIB_TCP6ROW_OWNER_PID) convertToConnectionStat() ConnectionStat {
Port: uint32(decodePort(m.DwRemotePort)), Port: uint32(decodePort(m.DwRemotePort)),
}, },
Pid: int32(m.DwOwningPid), Pid: int32(m.DwOwningPid),
Status: tcpStatuses[MIB_TCP_STATE(m.DwState)], Status: tcpStatuses[mibTCPState(m.DwState)],
} }
return ns return ns
} }
type MIB_TCP6TABLE_OWNER_PID struct { type mibTCP6TableOwnerPid struct {
DwNumEntries uint32 DwNumEntries uint32
Table [ANY_SIZE]MIB_TCP6ROW_OWNER_PID Table [anySize]mibTCP6RowOwnerPid
} }
type PMIB_TCPTABLE_OWNER_PID_ALL *MIB_TCPTABLE_OWNER_PID type pmibTCPTableOwnerPidAll *mibTCPTableOwnerPid
type PMIB_TCP6TABLE_OWNER_PID_ALL *MIB_TCP6TABLE_OWNER_PID type pmibTCP6TableOwnerPidAll *mibTCP6TableOwnerPid
// UDP // UDP
type MIB_UDPROW_OWNER_PID struct { type mibUDPRowOwnerPid struct {
DwLocalAddr uint32 DwLocalAddr uint32
DwLocalPort uint32 DwLocalPort uint32
DwOwningPid uint32 DwOwningPid uint32
} }
func (m *MIB_UDPROW_OWNER_PID) convertToConnectionStat() ConnectionStat { func (m *mibUDPRowOwnerPid) convertToConnectionStat() ConnectionStat {
ns := ConnectionStat{ ns := ConnectionStat{
Family: kindUDP4.family, Family: kindUDP4.family,
Type: kindUDP4.sockType, Type: kindUDP4.sockType,
@ -590,19 +591,19 @@ func (m *MIB_UDPROW_OWNER_PID) convertToConnectionStat() ConnectionStat {
return ns return ns
} }
type MIB_UDPTABLE_OWNER_PID struct { type mibUDPTableOwnerPid struct {
DwNumEntries uint32 DwNumEntries uint32
Table [ANY_SIZE]MIB_UDPROW_OWNER_PID Table [anySize]mibUDPRowOwnerPid
} }
type MIB_UDP6ROW_OWNER_PID struct { type mibUDP6RowOwnerPid struct {
UcLocalAddr [16]byte UcLocalAddr [16]byte
DwLocalScopeId uint32 DwLocalScopeId uint32
DwLocalPort uint32 DwLocalPort uint32
DwOwningPid uint32 DwOwningPid uint32
} }
func (m *MIB_UDP6ROW_OWNER_PID) convertToConnectionStat() ConnectionStat { func (m *mibUDP6RowOwnerPid) convertToConnectionStat() ConnectionStat {
ns := ConnectionStat{ ns := ConnectionStat{
Family: kindUDP6.family, Family: kindUDP6.family,
Type: kindUDP6.sockType, Type: kindUDP6.sockType,
@ -616,13 +617,13 @@ func (m *MIB_UDP6ROW_OWNER_PID) convertToConnectionStat() ConnectionStat {
return ns return ns
} }
type MIB_UDP6TABLE_OWNER_PID struct { type mibUDP6TableOwnerPid struct {
DwNumEntries uint32 DwNumEntries uint32
Table [ANY_SIZE]MIB_UDP6ROW_OWNER_PID Table [anySize]mibUDP6RowOwnerPid
} }
type PMIB_UDPTABLE_OWNER_PID *MIB_UDPTABLE_OWNER_PID type pmibUDPTableOwnerPid *mibUDPTableOwnerPid
type PMIB_UDP6TABLE_OWNER_PID *MIB_UDP6TABLE_OWNER_PID type pmibUDP6TableOwnerPid *mibUDP6TableOwnerPid
func decodePort(port uint32) uint16 { func decodePort(port uint32) uint16 {
return syscall.Ntohs(uint16(port)) return syscall.Ntohs(uint16(port))

Loading…
Cancel
Save