Ejemplo n.º 1
0
Archivo: osc.go Proyecto: kward/go-osc
// ReceivePacket listens for incoming OSC packets and returns the packet and
// client address if one is received.
func (s *Server) ReceivePacket(ctx context.Context, c net.PacketConn) (Packet, net.Addr, error) {
	if deadline, ok := ctx.Deadline(); ok {
		if err := c.SetReadDeadline(deadline); err != nil {
			return nil, nil, err
		}
	}

	go func() {
		select {
		// case <-time.After(200 * time.Millisecond):
		// 	log.Println("Overslept.")
		case <-ctx.Done():
			log.Println(ctx.Err())
		}
	}()

	data := make([]byte, 65535)
	var n, start int
	n, addr, err := c.ReadFrom(data)
	if err != nil {
		return nil, nil, err
	}
	pkt, err := readPacket(bufio.NewReader(bytes.NewBuffer(data)), &start, n)
	return pkt, addr, err
}
Ejemplo n.º 2
0
// RFC 3489: Clients SHOULD retransmit the request starting with an interval
// of 100ms, doubling every retransmit until the interval reaches 1.6s.
// Retransmissions continue with intervals of 1.6s until a response is
// received, or a total of 9 requests have been sent.
func (v *packet) send(conn net.PacketConn, addr net.Addr) (*packet, error) {
	timeout := 100
	for i := 0; i < 9; i++ {
		length, err := conn.WriteTo(v.bytes(), addr)
		if err != nil {
			return nil, err
		}
		if length != len(v.bytes()) {
			return nil, errors.New("Error in sending data.")
		}
		conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Millisecond))
		if timeout < 1600 {
			timeout *= 2
		}
		packetBytes := make([]byte, 1024)
		length, _, err = conn.ReadFrom(packetBytes)
		if err == nil {
			return newPacketFromBytes(packetBytes[0:length])
		} else {
			if !err.(net.Error).Timeout() {
				return nil, err
			}
		}
	}
	return nil, nil
}
Ejemplo n.º 3
0
func runSyslog(c net.PacketConn, done chan<- string) {
	var buf [4096]byte
	var rcvd string
	for {
		c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		n, _, err := c.ReadFrom(buf[:])
		rcvd += string(buf[:n])
		if err != nil {
			break
		}
	}
	done <- rcvd
}
Ejemplo n.º 4
0
Archivo: osc.go Proyecto: WnP/go-osc
// readFromConnection retrieves OSC packets.
func (s *Server) readFromConnection(c net.PacketConn) (packet Packet, err error) {
	if s.ReadTimeout != 0 {
		err = c.SetReadDeadline(time.Now().Add(s.ReadTimeout))
		if err != nil {
			return nil, err
		}
	}
	data := make([]byte, 65535)
	var n, start int
	n, _, err = c.ReadFrom(data)
	if err != nil {
		return nil, err
	}
	return s.readPacket(bufio.NewReader(bytes.NewBuffer(data)), &start, n)
}
Ejemplo n.º 5
0
func read(conn net.PacketConn) (SwApi_FindSwAck, net.Addr, error) {
	var pkt SwApi_FindSwAck
	conn.SetReadDeadline(time.Now().Add(3 * time.Second))
	buf := make([]byte, 4096)
	len, peer, err := conn.ReadFrom(buf)
	if err != nil {
		if opErr, ok := err.(*net.OpError); ok {
			if opErr.Timeout() {
				return pkt, peer, err
			}
		}
		log.Fatal(err)
	}
	buffer := bytes.NewBuffer(buf[:len])
	err = binary.Read(buffer, _LE, &pkt)
	if err != nil {
		log.Fatal(err)
	}
	return pkt, peer, nil
}
Ejemplo n.º 6
0
func runPktSyslog(c net.PacketConn, done chan<- string) {
	var buf [4096]byte
	var rcvd string
	ct := 0
	for {
		var n int
		var err error

		c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		n, _, err = c.ReadFrom(buf[:])
		rcvd += string(buf[:n])
		if err != nil {
			if oe, ok := err.(*net.OpError); ok {
				if ct < 3 && oe.Temporary() {
					ct++
					continue
				}
			}
			break
		}
	}
	c.Close()
	done <- rcvd
}
Ejemplo n.º 7
0
func syslogWorker(die dieCh, conn net.PacketConn, cfg logplexc.Config, sr *serveRecord) {
	// Make world-writable so anything can connect and send logs.
	// This may be be worth locking down more, but as-is unless
	// pg_logplexcollector and the Postgres server share the same
	// running user common umasks will be useless.
	fi, err := os.Stat(sr.P)
	if err != nil {
		log.Fatalf(
			"exiting, cannot stat just created socket %q: %v",
			sr.P, err)
	}

	err = os.Chmod(sr.P, fi.Mode().Perm()|0222)
	if err != nil {
		log.Fatalf(
			"exiting, cannot make just created socket "+
				"world-writable %q: %v",
			sr.P, err)
	}

	cfg.Logplex = sr.u

	buf := make([]byte, 9*KB)
	target, err := logplexc.NewClient(&cfg)
	if err != nil {
		log.Fatalf("could not create auditing client: %v", err)
	}

	for {
		select {
		case <-die:
			return
		default:
			break
		}

		err := conn.SetReadDeadline(time.Now().Add(time.Duration(1 * time.Second)))
		if err != nil {
			log.Fatalf("could not set connection deadline: %v", err)
		}

		n, _, err := conn.ReadFrom(buf)
		if n > 0 {
			// Just send the message wholesale, which
			// leads to some weird syslog-in-syslog
			// framing, but perhaps it's good enough.
			target.BufferMessage(134, time.Now(),
				"audit", "-", append([]byte(
					"instance_type=shogun identity="+
						sr.I+" "), buf[:n]...))
		}

		if err != nil {
			if err, ok := err.(net.Error); ok {
				if err.Timeout() || err.Temporary() {
					continue
				}
			}

			log.Fatalf("got syslog datagram error %v", err)
		}
	}
}