//Copy from golang source func anyToSockaddr(rsa *syscall.RawSockaddrAny) (syscall.Sockaddr, error) { switch rsa.Addr.Family { case syscall.AF_INET: pp := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa)) sa := new(syscall.SockaddrInet4) p := (*[2]byte)(unsafe.Pointer(&pp.Port)) sa.Port = int(p[0])<<8 + int(p[1]) for i := 0; i < len(sa.Addr); i++ { sa.Addr[i] = pp.Addr[i] } return sa, nil case syscall.AF_INET6: pp := (*syscall.RawSockaddrInet6)(unsafe.Pointer(rsa)) sa := new(syscall.SockaddrInet6) p := (*[2]byte)(unsafe.Pointer(&pp.Port)) sa.Port = int(p[0])<<8 + int(p[1]) sa.ZoneId = pp.Scope_id for i := 0; i < len(sa.Addr); i++ { sa.Addr[i] = pp.Addr[i] } return sa, nil } return nil, syscall.EAFNOSUPPORT }
func getSockaddr(network, addr string) (sa syscall.Sockaddr, soType int, err error) { // TODO: add support for tcp networks. if network != "tcp4" && network != "tcp6" { return nil, -1, errors.New("only tcp4 and tcp6 network is supported") } tcpAddr, err := net.ResolveTCPAddr(network, addr) if err != nil { return nil, -1, err } switch network { case "tcp4": var sa4 syscall.SockaddrInet4 sa4.Port = tcpAddr.Port copy(sa4.Addr[:], tcpAddr.IP.To4()) return &sa4, syscall.AF_INET, nil case "tcp6": var sa6 syscall.SockaddrInet6 sa6.Port = tcpAddr.Port copy(sa6.Addr[:], tcpAddr.IP.To16()) if tcpAddr.Zone != "" { ifi, err := net.InterfaceByName(tcpAddr.Zone) if err != nil { return nil, -1, err } sa6.ZoneId = uint32(ifi.Index) } return &sa6, syscall.AF_INET6, nil default: return nil, -1, errors.New("Unknown network type " + network) } }
func createEncapListener(ip net.IP, port int) (int, error) { const ( UDP_ENCAP = 100 UDP_ENCAP_ESPINUDP = 2 ) s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0) if err != nil { return 0, err } err = syscall.SetsockoptInt(s, syscall.IPPROTO_UDP, UDP_ENCAP, UDP_ENCAP_ESPINUDP) if err != nil { return 0, err } var family int if len(ip) <= net.IPv4len { family = syscall.AF_INET } else if ip.To4() != nil { family = syscall.AF_INET } else { family = syscall.AF_INET6 } var bindaddr syscall.Sockaddr switch family { case syscall.AF_INET: if len(ip) == 0 { ip = net.IPv4zero } sa := new(syscall.SockaddrInet4) for i := 0; i < net.IPv4len; i++ { sa.Addr[i] = ip[i] } sa.Port = port bindaddr = sa case syscall.AF_INET6: sa := new(syscall.SockaddrInet6) for i := 0; i < net.IPv6len; i++ { sa.Addr[i] = ip[i] } sa.Port = port // TODO: optionally allow zone for ipv6 // sa.ZoneId = uint32(zoneToInt(zone)) bindaddr = sa } err = syscall.Bind(s, bindaddr) if err != nil { return 0, err } return s, nil }
func getSockaddr(network, addr string) (sa syscall.Sockaddr, soType int, err error) { // TODO: add support for tcp and tcp6 networks. if network != "tcp4" { return nil, -1, errors.New("only tcp4 network is supported") } tcpAddr, err := net.ResolveTCPAddr(network, addr) if err != nil { return nil, -1, err } var sa4 syscall.SockaddrInet4 sa4.Port = tcpAddr.Port copy(sa4.Addr[:], tcpAddr.IP.To4()) return &sa4, syscall.AF_INET, nil }
func doSendThread(conn int) { for data := range chanSend { dstIP := data.addr.IP.To4() if dstIP == nil { log.Println("send fail: no dest ip") continue } addr := syscall.SockaddrInet4{} addr.Port = 0 addr.Addr[0], addr.Addr[1], addr.Addr[2], addr.Addr[3] = dstIP[0], dstIP[1], dstIP[2], dstIP[3] err := syscall.Sendto(conn, data.data, 0, &addr) if err != nil { log.Println("Send Thread ", err, data.addr.String()) continue } } }
func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) { switch family { case syscall.AF_INET: if len(ip) == 0 { ip = IPv4zero } if ip = ip.To4(); ip == nil { return nil, InvalidAddrError("non-IPv4 address") } sa := new(syscall.SockaddrInet4) for i := 0; i < IPv4len; i++ { sa.Addr[i] = ip[i] } sa.Port = port return sa, nil case syscall.AF_INET6: if len(ip) == 0 { ip = IPv6zero } // IPv4 callers use 0.0.0.0 to mean "announce on any available address". // In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0", // which it refuses to do. Rewrite to the IPv6 unspecified address. if ip.Equal(IPv4zero) { ip = IPv6zero } if ip = ip.To16(); ip == nil { return nil, InvalidAddrError("non-IPv6 address") } sa := new(syscall.SockaddrInet6) for i := 0; i < IPv6len; i++ { sa.Addr[i] = ip[i] } sa.Port = port sa.ZoneId = uint32(zoneToInt(zone)) return sa, nil } return nil, InvalidAddrError("unexpected socket family") }
func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, os.Error) { switch family { case syscall.AF_INET: if len(ip) == 0 { ip = IPv4zero } if ip = ip.To4(); ip == nil { return nil, os.EINVAL } s := new(syscall.SockaddrInet4) for i := 0; i < IPv4len; i++ { s.Addr[i] = ip[i] } s.Port = port return s, nil case syscall.AF_INET6: if len(ip) == 0 { ip = IPzero } // IPv4 callers use 0.0.0.0 to mean "announce on any available address". // In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0", // which it refuses to do. Rewrite to the IPv6 all zeros. if p4 := ip.To4(); p4 != nil && p4[0] == 0 && p4[1] == 0 && p4[2] == 0 && p4[3] == 0 { ip = IPzero } if ip = ip.To16(); ip == nil { return nil, os.EINVAL } s := new(syscall.SockaddrInet6) for i := 0; i < IPv6len; i++ { s.Addr[i] = ip[i] } s.Port = port return s, nil } return nil, os.EINVAL }
func (w *Worker) run() { for i := 0; ; i++ { /* Make tcp Packet */ tcpMaker := new(TCPmaker) tcpMaker.DestAddr = w.nowDestAddr tcpMaker.DestPort = w.nowDestPort tcpMaker.SourceAddr = w.SourceAddr if w.IsRandomSrcPort { tcpMaker.SourcePort = uint16(getFreePort()) } else { tcpMaker.SourcePort = w.SourcePort } PacketLen := tcpMaker.GetPacketLen() Packet := make([]byte, PacketLen) buf := tcpMaker.MakePacket(SYN) copy(Packet, buf) RemoteAddr := new(syscall.SockaddrInet4) InetIPAddr := tcpMaker.GetInetDestIPAddr() destAddr := unsafe.Pointer(&InetIPAddr) byteAddr := (*[4]byte)(destAddr) RemoteAddr.Addr[0] = byteAddr[0] RemoteAddr.Addr[1] = byteAddr[1] RemoteAddr.Addr[2] = byteAddr[2] RemoteAddr.Addr[3] = byteAddr[3] RemoteAddr.Port = int(tcpMaker.GetlittleEndianDestPort()) socket := w.createRawSocket() //fmt.Printf("%d sendpacket,port :%d \n", i, w.nowDestPort) w.sendPacket(Packet, RemoteAddr, socket) RecvBuf, RecvLen := w.recvPacket(socket) if RecvLen == 44 && w.check(RecvBuf) { Openned_Port := parseSrcPortFromRecvBuf(RecvBuf) Openned_Addr := parseSrcAddrFromRecvBuf(RecvBuf) DstAddr := parseDstAddrFromRecvBuf(RecvBuf) if DstAddr == w.SourceAddr { fmt.Printf("IP :%s \t\t Port :%s \t\t is open \n", Openned_Addr, Openned_Port) //sendRSTPacket(tcpMaker, RemoteAddr, socket) } syscall.Close(socket) } else { //fmt.Printf("IP :%s Port :%d \t is not open \n", w.nowDestAddr, w.nowDestPort) syscall.Close(socket) } NextIP, NextPort := w.nextTask() //fmt.Printf("NextIP %s NextPort %d\n", NextIP, NextPort) if NextIP == "" && 0 == NextPort { //w.notify <- 1 break } else { w.setNowIPAddr(NextIP) w.setNowPort(NextPort) } } }