本文整理汇总了Golang中syscall.Sendto函数的典型用法代码示例。如果您正苦于以下问题:Golang Sendto函数的具体用法?Golang Sendto怎么用?Golang Sendto使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Sendto函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Sendto
func (k *PosixKernel) Sendto(fd co.Fd, buf co.Buf, size co.Len, flags int, sa syscall.Sockaddr, socklen co.Len) uint64 {
msg := make([]byte, size)
if err := buf.Unpack(msg); err != nil {
return UINT64_MAX // FIXME
}
return Errno(syscall.Sendto(int(fd), msg, flags, sa))
}
示例2: WriteTo
func (fd *netFD) WriteTo(p []byte, sa syscall.Sockaddr) (n int, err error) {
fd.wio.Lock()
defer fd.wio.Unlock()
if err := fd.incref(false); err != nil {
return 0, err
}
defer fd.decref()
for {
err = syscall.Sendto(fd.sysfd, p, 0, sa)
if err == syscall.EAGAIN {
err = errTimeout
if fd.wdeadline >= 0 {
if err = fd.pollServer.WaitWrite(fd); err == nil {
continue
}
}
}
break
}
if err == nil {
n = len(p)
} else {
err = &OpError{"write", fd.net, fd.raddr, err}
}
return
}
示例3: main
func main() {
log.SetPrefix("")
log.SetFlags(0)
if os.Geteuid() != 0 {
log.Fatal(errors.New("please run as root"))
}
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW)
check(err)
if len(os.Args) < 3 {
log.Fatal("usage: synflood <victimIP> <spoofedIP>")
}
raddr := net.ParseIP(os.Args[1])
addr := syscall.SockaddrInet4{
Port: 0,
Addr: to4Array(raddr),
}
p := packet(raddr)
switch runtime.GOOS {
case "darwin", "dragonfly", "freebsd", "netbsd":
// need to set explicitly
check(syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1))
// no need to receive anything
check(syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, 1))
case "linux":
// no need to receive anything
check(syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, 0))
}
for {
check(syscall.Sendto(fd, p, 0, &addr))
}
}
示例4: WriteTo
func (fd *netFD) WriteTo(p []byte, sa syscall.Sockaddr) (n int, err error) {
fd.wio.Lock()
defer fd.wio.Unlock()
if err := fd.incref(false); err != nil {
return 0, err
}
defer fd.decref()
if err := fd.pd.PrepareWrite(); err != nil {
return 0, &OpError{"write", fd.net, fd.raddr, err}
}
for {
err = syscall.Sendto(fd.sysfd, p, 0, sa)
if err == syscall.EAGAIN {
if err = fd.pd.WaitWrite(); err == nil {
continue
}
}
break
}
if err == nil {
n = len(p)
} else {
err = &OpError{"write", fd.net, fd.raddr, err}
}
return
}
示例5: writeTo
func (fd *netFD) writeTo(p []byte, sa syscall.Sockaddr) (n int, err error) {
if err := fd.writeLock(); err != nil {
return 0, err
}
defer fd.writeUnlock()
if err := fd.pd.prepareWrite(); err != nil {
return 0, err
}
for {
err = syscall.Sendto(fd.sysfd, p, 0, sa)
if err == syscall.EAGAIN {
if err = fd.pd.waitWrite(); err == nil {
continue
}
}
break
}
if err == nil {
n = len(p)
}
if _, ok := err.(syscall.Errno); ok {
err = os.NewSyscallError("sendto", err)
}
return
}
示例6: WriteTo
func (fd *netFD) WriteTo(p []byte, sa syscall.Sockaddr) (n int, err os.Error) {
if fd == nil || fd.sysfile == nil {
return 0, os.EINVAL
}
fd.wio.Lock()
defer fd.wio.Unlock()
fd.incref()
defer fd.decref()
if fd.wdeadline_delta > 0 {
fd.wdeadline = pollserver.Now() + fd.wdeadline_delta
} else {
fd.wdeadline = 0
}
var oserr os.Error
for {
errno := syscall.Sendto(fd.sysfd, p, 0, sa)
if errno == syscall.EAGAIN && fd.wdeadline >= 0 {
pollserver.WaitWrite(fd)
continue
}
if errno != 0 {
oserr = os.Errno(errno)
}
break
}
if oserr == nil {
n = len(p)
} else {
err = &OpError{"write", fd.net, fd.raddr, oserr}
}
return
}
示例7: Send
// Create a tcp socket and send data on it. This uses the sendto() system call
// instead of connect() - because connect() calls does not support sending
// data in the syn packet, but the sendto() system call does (as often used in
// connectionless protocols such as udp.
func (c *TFOClient) Send() (err error) {
c.fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
return
}
defer syscall.Close(c.fd)
sa := &syscall.SockaddrInet4{Addr: c.ServerAddr, Port: c.ServerPort}
// Data to appear, if an existing tcp fast open cookie is available, this
// data will appear in the SYN packet, if not, it will appear in the ACK.
data := []byte("Hello TCP Fast Open")
log.Printf("Client: Sending to server: %#v\n", string(data))
// Use the sendto() syscall, instead of connect()
err = syscall.Sendto(c.fd, data, syscall.MSG_FASTOPEN, sa)
if err != nil {
if err == syscall.EOPNOTSUPP {
err = errors.New("TCP Fast Open client support is unavailable (unsupported kernel or disabled, see /proc/sys/net/ipv4/tcp_fastopen).")
}
err = errors.New(fmt.Sprintf("Received error in sendTo():", err))
return
}
// Note, this exists before waiting for response and is meant to illustrate
// the use of the sendto() system call, not of a complete and proper socket
// setup and teardown processes.
return
}
示例8: main
func main() {
if len(os.Args) < 2 {
log.Fatal("Need interface name")
}
iface, err := net.InterfaceByName(os.Args[1])
if err != nil {
log.Fatal(err)
}
fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, 0)
if err != nil {
log.Fatal(err)
}
defer syscall.Close(fd)
buf := []byte{0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x54, 0x55}
err = syscall.Sendto(fd, buf, 0, &syscall.SockaddrLinklayer{Ifindex: iface.Index})
if err != nil {
log.Fatal("Cannot sendto:", err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
}
示例9: BenchmarkListenSock
func BenchmarkListenSock(b *testing.B) {
aConn := listenUDP(udpAddr)
bConn := listenUDP(udpAddr)
aFile := getFile(aConn)
bFile := getFile(bConn)
aSockaddr, err := syscall.Getsockname(int(aFile.Fd()))
if err != nil {
log.Fatal(err)
}
src := make([]byte, bytesNum)
io.ReadFull(rand.Reader, src)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = syscall.Sendto(int(bFile.Fd()), src, 0, aSockaddr)
if err != nil {
b.Fatal(err)
}
}
b.SetBytes(bytesNum)
}
示例10: traceOne
func traceOne(addr *syscall.SockaddrInet4, ttl int) *ReturnArgs {
cli, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
exitWithError(err)
}
srv, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP)
if err != nil {
exitWithError(err)
}
defer syscall.Close(cli)
defer syscall.Close(srv)
// set ttl, stolen from somewhere else...
// https://github.com/aeden/traceroute/blob/master/traceroute.go#L195
if err := syscall.SetsockoptInt(cli, syscall.SOL_IP, syscall.IP_TTL, ttl); err != nil {
exitWithError(err)
}
// set timeout, stolen from somewhere else...
// https://github.com/aeden/traceroute/blob/master/traceroute.go#L197
tv := syscall.NsecToTimeval(1e6 * TIMEOUT)
if err := syscall.SetsockoptTimeval(srv, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &tv); err != nil {
exitWithError(err)
}
if err := syscall.Bind(srv, toAddr(HOST, RECV_PORT)); err != nil {
exitWithError(err)
}
rr := &ReturnArgs{}
start := time.Now()
if err := syscall.Sendto(cli, makeICMP(), 0, addr); err != nil {
return rr
}
buf := make([]byte, 512)
_, from, err := syscall.Recvfrom(srv, buf, 0)
if err != nil {
return rr
}
rr.elapsed = float64(time.Since(start).Nanoseconds()) / 1e6
t, c := parseICMP(buf)
if t == 3 && c == 3 { // Destination port unreachable, type==3 && code==3
rr.done = true
} else if t != 11 { // Time Exceeded, type==11 && code in (0,1)
return rr
}
rr.ok = true
rr.ip = toStr(from)
addrs, err := net.LookupAddr(rr.ip)
if err != nil {
rr.addr = rr.ip
} else {
rr.addr = addrs[0]
}
return rr
}
示例11: Send
func (s *NetlinkSocket) Send(request *NetlinkRequest) error {
if s.fd < 0 {
return fmt.Errorf("Send called on a closed socket")
}
if err := syscall.Sendto(s.fd, request.Serialize(), 0, &s.lsa); err != nil {
return err
}
return nil
}
示例12: Traceroute
// Traceroute executes traceroute to given destination, using options from TracerouteOptions
// and sending updates to chan c
//
// Outbound packets are UDP packets and inbound packets are ICMP.
//
// Returns an error or nil if no error occurred
func Traceroute(dest *net.IPAddr, options *TracerouteOptions, c chan TraceUpdate) (err error) {
var destAddr [4]byte
copy(destAddr[:], dest.IP.To4())
socketAddr, err := getSocketAddr()
if err != nil {
return
}
timeoutMs := (int64)(options.TimeoutMs)
tv := syscall.NsecToTimeval(1000 * 1000 * timeoutMs)
ttl := 1
for {
// Set up receiving socket
recvSocket, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP)
if err != nil {
log.Fatal("Cannot setup receive socket, please run as root or with CAP_NET_RAW permissions")
return err
}
// Set up sending socket
sendSocket, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
log.Fatal("Cannot setup sending socket")
return err
}
start := time.Now()
syscall.SetsockoptInt(sendSocket, 0x0, syscall.IP_TTL, ttl)
syscall.SetsockoptTimeval(recvSocket, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &tv)
syscall.Bind(recvSocket, &syscall.SockaddrInet4{Port: options.Port, Addr: socketAddr})
syscall.Sendto(sendSocket, []byte{0x0}, 0, &syscall.SockaddrInet4{Port: options.Port, Addr: destAddr})
var p = make([]byte, options.PacketSize)
n, from, err := syscall.Recvfrom(recvSocket, p, 0)
elapsed := time.Since(start)
if err == nil {
currAddr := from.(*syscall.SockaddrInet4).Addr
hop := TraceUpdate{Success: true, Address: currAddr, N: n, ElapsedTime: elapsed, TTL: ttl}
currHost, err := net.LookupAddr(hop.addressString())
if err == nil {
hop.Host = currHost[0]
}
// Send update
c <- hop
ttl += 1
// We reached the destination
if ttl > options.MaxTTL || currAddr == destAddr {
ttl = 1
}
} else {
c <- TraceUpdate{Success: false, TTL: ttl}
ttl += 1
}
syscall.Close(recvSocket)
syscall.Close(sendSocket)
}
}
示例13: sendRSTPacket
func sendRSTPacket(Maker *TCPmaker, RemoteAddr *syscall.SockaddrInet4, socket int) {
buf := Maker.MakePacket(RST)
SendtoErr := syscall.Sendto(socket, buf, 0, RemoteAddr)
if SendtoErr != nil {
fmt.Fprintf(os.Stderr, "Sendto is failed : %s \n", SendtoErr)
return
} else {
fmt.Printf("Send Fin packet is ok \n")
}
}
示例14: sendPacket
func (w *Worker) sendPacket(buf []byte, RemoteAddr *syscall.SockaddrInet4, socket int) {
SendtoErr := syscall.Sendto(socket, buf, 0, RemoteAddr)
if SendtoErr != nil {
fmt.Fprintf(os.Stderr, "Sendto is failed : %s \n", SendtoErr)
return
} else {
//fmt.Printf("Sendto is ok \n")
}
}
示例15: send
func (s *NetlinkSocket) send(msg *NlMsgBuilder) (uint32, error) {
sa := syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
Pid: 0,
Groups: 0,
}
data, seq := msg.Finish()
return seq, syscall.Sendto(s.fd, data, 0, &sa)
}