Beispiel #1
0
func (tunnel *Tunnel) HandleVOIPData(buff []byte, addr *net.UDPAddr, conn *net.UDPConn) {
	now := time.Now().Unix()

	_, receiver, _, err := tunnel.ReadVOIPData(buff)
	if err != nil {
		return
	}

	client := tunnel.FindClient(addr)
	if client == nil || client.appid == 0 {
		return
	}

	client.timestamp = now
	//转发消息
	other := tunnel.FindAppClient(client.appid, receiver)
	if other == nil {
		log.Infof("can't dispatch voip data sender:%d receiver:%d", client.uid, receiver)
		return
	}

	if other.has_header {
		buffer := new(bytes.Buffer)
		var h byte = VOIP_DATA
		buffer.WriteByte(h)
		buffer.Write(buff)
		data := buffer.Bytes()
		conn.WriteTo(data, other.addr)
	} else {
		data := buff
		conn.WriteTo(data, other.addr)
	}
}
Beispiel #2
0
func main() {

	go func() {
		var addr net.Addr
		var udp *net.UDPConn
		var err error
		if addr, err = net.ResolveUDPAddr("udp4", ":1234"); err != nil {
			panic(err)
		}
		if udp, err = net.ListenUDP("udp", addr.(*net.UDPAddr)); err != nil {
			panic(err)
		}
		var buf = make([]byte, 65535)
		n, addr, e := udp.ReadFrom(buf)
		if e != nil {
			panic(e)
		}
		udp.WriteTo(buf[:n], addr)
	}()
	runtime.Gosched()
	svr := NewPortalSvr()
	svr.basAddr = "127.0.0.1:1234"
	svr.basSecret = []byte("xxffe")
	if err := svr.Listen(":1288"); err != nil {
		panic(err)
	}
	svr.Run()
	err := svr.Auth("aas", "asas1", "255.255.255.255:1223")
	if err != nil {
		println(err.Error())
	}
}
Beispiel #3
0
func (tunnel *Tunnel) HandleAuth(buff []byte, addr *net.UDPAddr, conn *net.UDPConn) {
	now := time.Now().Unix()
	token, err := tunnel.ReadVOIPAuth(buff)
	if err != nil {
		return
	}
	client := tunnel.FindClient(addr)
	if client == nil {
		//首次收到认证消息
		client = &TunnelClient{appid: 0, uid: 0, addr: addr, timestamp: now, has_header: true, token: token}
		tunnel.AuthClient(client, token)
		return
	} else if client.token == token {
		//认证成功
		t := make([]byte, 2)
		t[0] = VOIP_AUTH_STATUS
		t[1] = 0
		conn.WriteTo(t, addr)
		client.timestamp = now
		log.Infof("tunnel auth appid:%d uid:%d", client.appid, client.uid)
	} else {
		//新的token
		tunnel.RemoveTunnelClient(client)
		client.token = token
		client.appid = 0
		client.uid = 0
		client.timestamp = now
		tunnel.AuthClient(client, token)
	}
}
Beispiel #4
0
func GetSend(conn *net.UDPConn, context *UdpContext) {
	file := context.file
	buffer := make([]byte, 1024)
	// Read into buffer
	len, err := file.Read(buffer)

	if len > 0 {
		// Write to socket
		_, err = conn.WriteTo(buffer[:len], context.dest)
		if err != nil {
			log.Println("Failed to write data to socket; abandoning transfer")
		}
	}

	// EOF?
	if err == io.EOF {
		log.Println("Transfer completed")
		return
	}

	// Some other error
	if err != nil {
		log.Println("Error reading file; abandoning transfer")
		return
	}
}
Beispiel #5
0
func (d *UDPClient) sendAnnouncement(remote net.Addr, conn *net.UDPConn) bool {
	if debug {
		l.Debugf("discover %s: broadcast: Sending self announcement to %v", d.url, remote)
	}

	ann := d.announcer.Announcement()
	pkt, err := ann.MarshalXDR()
	if err != nil {
		return false
	}

	myID := protocol.DeviceIDFromBytes(ann.This.ID)

	_, err = conn.WriteTo(pkt, remote)
	if err != nil {
		if debug {
			l.Debugf("discover %s: broadcast: Failed to send self announcement: %s", d.url, err)
		}
		return false
	}

	// Verify that the announce server responds positively for our device ID

	time.Sleep(1 * time.Second)

	ann, err = d.Lookup(myID)
	if err != nil && debug {
		l.Debugf("discover %s: broadcast: Self-lookup failed: %v", d.url, err)
	} else if debug {
		l.Debugf("discover %s: broadcast: Self-lookup returned: %v", d.url, ann.This.Addresses)
	}
	return len(ann.This.Addresses) > 0
}
func handleudp(conn *net.UDPConn, remaddr net.Addr, message []byte) {
	fmt.Printf("UDP packet from %s (to %s)... ", remaddr, conn.LocalAddr())
	n, error := conn.WriteTo(message, remaddr)
	checkError("Cannot write", error)
	if n != len(message) {
		panic("Cannot write")
	}
	fmt.Printf("Echoed %d bytes\n", n)
}
Beispiel #7
0
func HandleUdpGet(conn *net.UDPConn, context *UdpContext) {

	log.Printf("Request for file '%s' (UDP)", context.parameters)

	command := fmt.Sprintf("length: %d\n", context.length)
	conn.WriteTo([]byte(command), context.dest)

	GetSend(conn, context)
}
Beispiel #8
0
func TestHandlePing(t *testing.T) {
	m := GetMemberlist(t)
	m.config.EnableCompression = false
	defer m.Shutdown()

	var udp *net.UDPConn
	for port := 60000; port < 61000; port++ {
		udpAddr := fmt.Sprintf("127.0.0.1:%d", port)
		udpLn, err := net.ListenPacket("udp", udpAddr)
		if err == nil {
			udp = udpLn.(*net.UDPConn)
			break
		}
	}

	if udp == nil {
		t.Fatalf("no udp listener")
	}

	// Encode a ping
	ping := ping{SeqNo: 42}
	buf, err := encode(pingMsg, ping)
	if err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	// Send
	addr := &net.UDPAddr{IP: net.ParseIP(m.config.BindAddr), Port: m.config.BindPort}
	udp.WriteTo(buf.Bytes(), addr)

	// Wait for response
	go func() {
		time.Sleep(time.Second)
		panic("timeout")
	}()

	in := make([]byte, 1500)
	n, _, err := udp.ReadFrom(in)
	if err != nil {
		t.Fatalf("unexpected err %s", err)
	}
	in = in[0:n]

	msgType := messageType(in[0])
	if msgType != ackRespMsg {
		t.Fatalf("bad response %v", in)
	}

	var ack ackResp
	if err := decode(in[1:], &ack); err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	if ack.SeqNo != 42 {
		t.Fatalf("bad sequence no")
	}
}
Beispiel #9
0
func udpEchoHandler(c *net.UDPConn) {
	buf := make([]byte, 64)
	for {
		n, addr, err := c.ReadFrom(buf)
		if err != nil {
			return
		}
		if _, err = c.WriteTo(buf[0:n], addr); err != nil {
			return
		}
	}
}
Beispiel #10
0
// Listens for an incoming packet and replies to it.
func respond(t *testing.T, conn *net.UDPConn) {
	buff := make([]byte, 16)
	_, addr, err := conn.ReadFromUDP(buff)
	if err != nil {
		t.Fatalf("Cannot serve request: %s", err)
	}

	response := []byte("Some reply")
	if _, err = conn.WriteTo(response, addr); err != nil {
		t.Fatalf("Cannot write response: %s", err)
	}
}
Beispiel #11
0
/*
Send package
*/
func (pack *Package) Send(conn *net.UDPConn) (n int, err error) {
	pack.DataSize = uint16(len(pack.Data))
	buf := new(bytes.Buffer)
	err = binary.Write(buf, binary.LittleEndian, pack.SeqID)
	if err != nil {
		return
	}
	if pack.IsResponse {
		t := uint16(uint16(0X8000) | pack.MsgID)
		err = binary.Write(buf, binary.LittleEndian, t)
		if err != nil {
			return
		}
	} else {
		err = binary.Write(buf, binary.LittleEndian, pack.MsgID)
		if err != nil {
			return
		}
	}
	err = binary.Write(buf, binary.LittleEndian, pack.DataSize)
	if err != nil {
		return
	}
	if pack.DataSize > 0 {
		if pack.IsResponse {
			log.Printf(">>> Response (%s): {MsgID:%d, SeqID:%d, Data:%s}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID, string(pack.Data))
		} else {
			log.Printf(">>> Request  (%s): {MsgID:%d, SeqID:%d, Data:%s}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID, string(pack.Data))
		}
		n, err = buf.Write(pack.Data)
		if err != nil {
			return
		}
		if n != int(pack.DataSize) {
			return 0, errors.New("Write buffer error")
		}
	} else {
		if pack.IsResponse {
			log.Printf(">>> Response (%s): {MsgID:%d, SeqID:%d, Data:nil}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID)
		} else {
			log.Printf(">>> Request  (%s): {MsgID:%d, SeqID:%d, Data:nil}\r\n", pack.Addr.String(), pack.MsgID, pack.SeqID)
		}
	}
	n, err = conn.WriteTo(buf.Bytes(), pack.Addr)
	if err != nil {
		return
	}
	if n != int(pack.DataSize+PACKAGE_HEAD_LEN) {
		return n, errors.New("Send failed!")
	}
	return
}
Beispiel #12
0
// Transmit a message.
func Transmit(l *net.UDPConn, a *net.UDPAddr, m Message) error {
	d, err := m.encode()
	if err != nil {
		return err
	}

	if a == nil {
		_, err = l.Write(d)
	} else {
		_, err = l.WriteTo(d, a)
	}
	return err
}
Beispiel #13
0
func handleReq(n int, addr net.Addr, buf []byte, sCon *net.UDPConn){
	if n < 2 {
		log.Printf("%v Sent less than 2 bytes\n", addr)
		return
	}
	pId := binary.BigEndian.Uint16(buf[:2])
	switch {
	case pId == 0 && n == 2: //Public key request
		if _,err := sCon.WriteTo(encryption.RsaPub, addr); err != nil {
			log.Printf("Failed to reply to %v : %v", addr, err)
		}
	case pId == 0 && n > 2: //Registration
		ciphertext := buf[2:n]
		text, err := encryption.DecryptRSA(ciphertext)
		if err != nil{
			log.Printf("%v Incorrectly encrypted message (RSA)\n")
		}
		log.Println(text)
	case n >= 2*encryption.AES_LENGTH + 2 + encryption.HMAC_LENGTH: //Request
		pId-- //Switch to real pId
		if int(pId) >= len(data.Players) {
			print(pId)
			log.Printf("%v Sent an invalid player id\n", addr)
			return
		}
		key := data.Players[pId].Key
		text, err := encryption.Decrypt(buf[2:n], key)
		if err != nil {
			log.Printf("%v %s\n", addr, err)
			return
		}
		log.Printf("%v %d\n", addr, text)
		data.Players[pId].Addr = addr
		switch text[0] {
		case 1: //SET2
			if err := data.ReqSET2(text[1:], pId); err != nil {
				log.Printf("%v %s\n", addr, err)
			}
		default:
			log.Printf("%v Unknown request: %x", addr, text[0])
		}

		//Reply
		/*res := encryption.Encrypt([]byte{1,2,3,5,8},key)
		_, err = sCon.WriteTo(res, addr)
		checkWarn(err)*/

	default:
		log.Printf("%v Incorrectly encrypted message\n", addr)
	}
}
Beispiel #14
0
/*
 * Handle incoming DNS packet:
 * - Only handle requests with at least on query resource record and
 *   empty response resource records; skip all other packets.
 */
func process(conn *net.UDPConn, addr net.Addr, buf []byte, n int) {

	pos := 0
	id, pos := getShort(buf, pos)
	qtype, pos := getShort(buf, pos)
	if (qtype & 0x8000) != 0 {
		if verbose {
			fmt.Printf("[Tor-DNS] Request is not a standard query: %x\n", qtype)
		}
		return
	}
	mode := (qtype >> 11) & 0xF
	if mode > 1 {
		if verbose {
			fmt.Printf("[Tor-DNS] Request is not a standard type: %x\n", qtype)
		}
		return
	}

	qd_cnt, pos := getShort(buf, pos)
	an_cnt, pos := getShort(buf, pos)
	ns_cnt, pos := getShort(buf, pos)
	ar_cnt, pos := getShort(buf, pos)

	if an_cnt > 0 || ns_cnt > 0 || ar_cnt > 0 || qd_cnt != 1 {
		if verbose {
			fmt.Printf("[Tor-DNS] Request has invalid counts: (%d,%d,%d,%d)\n", qd_cnt, an_cnt, ns_cnt, ar_cnt)
		}
		return
	}

	var q query
	q.mode = mode
	name, num := read_name(buf[pos:])
	q.name = name
	pos += num
	q.typ, pos = getShort(buf, pos)
	q.class, pos = getShort(buf, pos)

	r, err := answer(id, q)
	if err != nil {
		fmt.Printf("[Tor-DNS] Can't answer query %x: %s\n", id, err.Error())
		return
	}
	if n = assemble(buf, id, r); n > 0 {
		conn.WriteTo(buf[:n], addr)
	}
}
Beispiel #15
0
func cliTunnelUDP(uconn *net.UDPConn, sconn net.Conn) {
	var raddr *net.UDPAddr

	go func() {
		b := lpool.Take()
		defer lpool.put(b)

		for {
			n, addr, err := uconn.ReadFromUDP(b)
			if err != nil {
				log.Println(err)
				return
			}
			raddr = addr
			r := bytes.NewBuffer(b[:n])
			udp, err := gosocks5.ReadUDPDatagram(r)
			if err != nil {
				return
			}
			udp.Header.Rsv = uint16(len(udp.Data))
			//log.Println("r", raddr.String(), udp.Header)

			if err := udp.Write(sconn); err != nil {
				log.Println(err)
				return
			}
		}
	}()

	for {
		b := lpool.Take()
		defer lpool.put(b)

		udp, err := gosocks5.ReadUDPDatagram(sconn)
		if err != nil {
			log.Println(err)
			return
		}
		//log.Println("w", udp.Header)
		udp.Header.Rsv = 0
		buf := bytes.NewBuffer(b[0:0])
		udp.Write(buf)
		if _, err := uconn.WriteTo(buf.Bytes(), raddr); err != nil {
			log.Println(err)
			return
		}
	}
}
Beispiel #16
0
func readAndWriteUDP(r io.Reader, w io.Writer, con *net.UDPConn, ra net.Addr, verbose bool) <-chan net.Addr {
	buf := make([]byte, BUFFER_LIMIT)
	cAddr := make(chan net.Addr)
	go func() {
		defer func() {
			con.Close()
			cAddr <- ra
		}()

		for {
			var bytesread int
			var errRead, errWrite error
			if con, ok := r.(*net.UDPConn); ok {
				var addr net.Addr
				bytesread, addr, errRead = con.ReadFrom(buf)
				if con.RemoteAddr() == nil && ra == nil {
					ra = addr
					cAddr <- ra
				}
			} else {
				bytesread, errRead = r.Read(buf)
			}
			if errRead != nil {
				if errRead != io.EOF {
					if verbose {
						log.Println("READ ERROR: ", errRead)
					}
				}
				break
			}
			if con, ok := w.(*net.UDPConn); ok && con.RemoteAddr() == nil {
				_, errWrite = con.WriteTo(buf[0:bytesread], ra)
			} else {
				_, errWrite = w.Write(buf[0:bytesread])
			}
			if errWrite != nil {
				if verbose {
					log.Println("WRITE ERROR: ", errWrite)
				}
				return
			}
		}
	}()
	return cAddr
}
Beispiel #17
0
func udphandle(conn *net.UDPConn, remaddr net.Addr, buf *bytes.Buffer) {
	var response types.DNSpacket
	if debug > 1 {
		debuglogger.Logf("%d bytes packet from %s\n", buf.Len(), remaddr)
	}
	response, noresponse := generichandle(buf, remaddr)
	if !noresponse {
		binaryresponse := serialize(response)
		_, error := conn.WriteTo(binaryresponse, remaddr)
		if error != nil {
			if debug > 2 {
				debuglogger.Logf("Error in Write: %s\n", error)
				return
			}
		}
	}
	// Else, ignore the incoming packet. May be we should reply REFUSED instead?
}
Beispiel #18
0
func dnsListen(conn net.UDPConn) {
	buf := make([]byte, 1024)
	n, addr, err := conn.ReadFrom(buf)
	log.Print("Addr", addr)

	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Data come in from: %s", addr)

	reply := dnsRequest(buf[0:n])
	_, err = conn.WriteTo(reply, addr)
	if err != nil {
		log.Fatal(err)
	} else {
		log.Print("=====EOF=====")
	}
}
Beispiel #19
0
func getReflexive(sock *net.UDPConn) (string, int, error) {
	serverAddr, err := net.ResolveUDPAddr("udp", *stunserver)
	if err != nil {

		return "", 0, errors.New("Couldn't resolve STUN server")
	}
	//println("connect stun server", *stunserver)

	var tid [12]byte
	if _, err = rand.Read(tid[:]); err != nil {
		return "", 0, err
	}

	request, err := stun.BindRequest(tid[:], nil, nil, true, false)
	if err != nil {
		return "", 0, err
	}

	n, err := sock.WriteTo(request, serverAddr)
	if err != nil {
		return "", 0, err
	}
	if n < len(request) {
		return "", 0, err
	}

	var buf [1024]byte
	n, _, err = sock.ReadFromUDP(buf[:])
	if err != nil {
		return "", 0, err
	}

	packet, err := stun.ParsePacket(buf[:n], nil)
	if err != nil {
		return "", 0, err
	}

	if packet.Class != stun.ClassSuccess || packet.Method != stun.MethodBinding || packet.Addr == nil || !bytes.Equal(tid[:], packet.Tid[:]) {
		return "", 0, errors.New("No address provided by STUN server")
	}

	return packet.Addr.IP.String(), packet.Addr.Port, nil
}
Beispiel #20
0
func sendPacketJob(c *net.UDPConn, txChan chan<- *mav.MavPacket) {
	raddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:14551")
	if err != nil {
		log.Fatalf("Error can not resolve UDP address:", err)
	}
	for {
		message := new(mav.Heartbeat)
		packet, err := mav.CreatePacket(1, 200, message)
		if err != nil {
			log.Fatalf("Error with the packet's creation:", err)
		}

		_, err = c.WriteTo(packet.Bytes(), raddr)
		if err != nil {
			log.Fatalf("Cannot write packet:", err)
		}
		txChan <- packet
		time.Sleep(1000 * time.Millisecond)
	}
}
Beispiel #21
0
func TestHandlePing_WrongNode(t *testing.T) {
	m := GetMemberlist(t)
	m.config.EnableCompression = false
	defer m.Shutdown()

	var udp *net.UDPConn
	for port := 60000; port < 61000; port++ {
		udpAddr := fmt.Sprintf("127.0.0.1:%d", port)
		udpLn, err := net.ListenPacket("udp", udpAddr)
		if err == nil {
			udp = udpLn.(*net.UDPConn)
			break
		}
	}

	if udp == nil {
		t.Fatalf("no udp listener")
	}

	// Encode a ping, wrong node!
	ping := ping{SeqNo: 42, Node: m.config.Name + "-bad"}
	buf, err := encode(pingMsg, ping)
	if err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	// Send
	addr := &net.UDPAddr{IP: net.ParseIP(m.config.BindAddr), Port: m.config.BindPort}
	udp.WriteTo(buf.Bytes(), addr)

	// Wait for response
	udp.SetDeadline(time.Now().Add(50 * time.Millisecond))
	in := make([]byte, 1500)
	_, _, err = udp.ReadFrom(in)

	// Should get an i/o timeout
	if err == nil {
		t.Fatalf("expected err %s", err)
	}
}
Beispiel #22
0
func receiveGoroutine(conn *net.UDPConn, ch chan<- int) {
	var count uint32 = 1

	for i := 0; i < 2; i++ {
		var data [1024]byte

		_, addr, err := conn.ReadFrom(data[:])
		if err != nil {
			log.Printf("Recv: %v", err)
		}

		buf := bytes.NewBuffer(data[:])
		err = binary.Read(buf, binary.BigEndian, &count)
		if err != nil {
			log.Printf("Buffer: %v\n", err)
			break
		}
		log.Printf("Receive From Client %d\n", count)

		count++

		bufW := new(bytes.Buffer)
		err = binary.Write(bufW, binary.LittleEndian, count)
		if err != nil {
			log.Printf("Buffer: %v\n", err)
		}
		_, err = conn.WriteTo(bufW.Bytes(), addr)
		if err != nil {
			log.Printf("Send: %v\n", err)
			break
		}

	}

	if err := conn.Close(); err != nil {
		log.Printf("Close: %v\n", err)
	}

	ch <- 1
}
Beispiel #23
0
func ChatWriter(channel chan string, conn *net.UDPConn) {

	//addrs, _ := ifi.MulticastAddrs()

	addr, err := net.ResolveUDPAddr("udp4", "192.168.1.255:4040")
	if err != nil {
		fmt.Println("Resolve Error: ", err)
		os.Exit(1)
	}

	for {
		select {
		case line := <-channel:

			_, err := conn.WriteTo([]byte(line), addr)
			if err != nil {
				fmt.Println("Write error: ", err)
				os.Exit(1)
			}

		}
	}
}
Beispiel #24
0
func Main(clusterName, self, buri, rwsk, rosk string, cl *doozer.Conn, udpConn *net.UDPConn, listener, webListener net.Listener, pulseInterval, fillDelay, kickTimeout int64, hi int64) {
	listenAddr := listener.Addr().String()

	canWrite := make(chan bool, 1)
	in := make(chan consensus.Packet, 50)
	out := make(chan consensus.Packet, 50)

	st := store.New()
	pr := &proposer{
		seqns: make(chan int64, alpha),
		props: make(chan *consensus.Prop),
		st:    st,
	}

	calSrv := func(start int64) {
		go gc.Pulse(self, st.Seqns, pr, pulseInterval)
		go gc.Clean(st, hi, time.Tick(1e9))
		var m consensus.Manager
		m.Self = self
		m.DefRev = start
		m.Alpha = alpha
		m.In = in
		m.Out = out
		m.Ops = st.Ops
		m.PSeqn = pr.seqns
		m.Props = pr.props
		m.TFill = fillDelay
		m.Store = st
		m.Ticker = time.Tick(10e6)
		go m.Run()
	}

	hostname, err := os.Hostname()
	if err != nil {
		hostname = "unknown"
	}

	if cl == nil { // we are the only node in a new cluster
		set(st, "/ctl/name", clusterName, store.Missing)
		set(st, "/ctl/node/"+self+"/addr", listenAddr, store.Missing)
		set(st, "/ctl/node/"+self+"/hostname", hostname, store.Missing)
		set(st, "/ctl/node/"+self+"/version", Version, store.Missing)
		set(st, "/ctl/cal/0", self, store.Missing)
		if buri == "" {
			set(st, "/ctl/ns/"+clusterName+"/"+self, listenAddr, store.Missing)
		}
		calSrv(<-st.Seqns)
		// Skip ahead alpha steps so that the registrar can provide a
		// meaningful cluster.
		for i := 0; i < alpha; i++ {
			st.Ops <- store.Op{1 + <-st.Seqns, store.Nop}
		}
		canWrite <- true
		go setReady(pr, self)
	} else {
		setC(cl, "/ctl/node/"+self+"/addr", listenAddr, store.Clobber)
		setC(cl, "/ctl/node/"+self+"/hostname", hostname, store.Clobber)
		setC(cl, "/ctl/node/"+self+"/version", Version, store.Clobber)

		rev, err := cl.Rev()
		if err != nil {
			panic(err)
		}

		stop := make(chan bool, 1)
		go follow(st, cl, rev+1, stop)

		errs := make(chan error)
		go func() {
			e, ok := <-errs
			if ok {
				panic(e)
			}
		}()
		doozer.Walk(cl, rev, "/", cloner{st.Ops, cl}, errs)
		close(errs)
		st.Flush()

		ch, err := st.Wait(store.Any, rev+1)
		if err == nil {
			<-ch
		}

		go func() {
			n := activate(st, self, cl)
			calSrv(n)
			advanceUntil(cl, st.Seqns, n+alpha)
			stop <- true
			canWrite <- true
			go setReady(pr, self)
			if buri != "" {
				b, err := doozer.DialUri(buri, "")
				if err != nil {
					panic(err)
				}
				setC(
					b,
					"/ctl/ns/"+clusterName+"/"+self,
					listenAddr,
					store.Missing,
				)
			}
		}()
	}

	shun := make(chan string, 3) // sufficient for a cluster of 7
	go member.Clean(shun, st, pr)
	go server.ListenAndServe(listener, canWrite, st, pr, rwsk, rosk)

	if rwsk == "" && rosk == "" && webListener != nil {
		web.Store = st
		web.ClusterName = clusterName
		go web.Serve(webListener)
	}

	go func() {
		for p := range out {
			n, err := udpConn.WriteTo(p.Data, p.Addr)
			if err != nil {
				log.Println(err)
				continue
			}
			if n != len(p.Data) {
				log.Println("packet len too long:", len(p.Data))
				continue
			}
		}
	}()

	selfAddr, ok := udpConn.LocalAddr().(*net.UDPAddr)
	if !ok {
		panic("no UDP addr")
	}
	lv := liveness{
		timeout: kickTimeout,
		ival:    kickTimeout / 2,
		self:    selfAddr,
		shun:    shun,
	}
	for {
		t := time.Now().UnixNano()

		buf := make([]byte, maxUDPLen)
		n, addr, err := udpConn.ReadFromUDP(buf)
		if err == syscall.EINVAL {
			return
		}
		if err != nil {
			log.Println(err)
			continue
		}

		buf = buf[:n]

		lv.mark(addr, t)
		lv.check(t)

		in <- consensus.Packet{addr, buf}
	}
}
Beispiel #25
0
func testWriteConn(t *testing.T, conn *net.UDPConn, addr *net.UDPAddr, dg datagram) {
	if _, err := conn.WriteTo(dg.bytes(), addr); err != nil {
		t.Fatal(err)
	}
}
Beispiel #26
0
func SendRRQTo(filename string, Conn *net.UDPConn, raddr *net.UDPAddr) {
	msg, err := makeRRQ(filename)
	checkError(err)
	_, err = Conn.WriteTo(msg, raddr)
	checkError(err)
}
Beispiel #27
0
func SendERRORTo(errCode []byte, errMsg string, Conn *net.UDPConn, raddr *net.UDPAddr) {
	msg, err := makeERROR(errCode, errMsg)
	checkError(err)
	_, err = Conn.WriteTo(msg, raddr)
	checkError(err)
}
Beispiel #28
0
func SendDATATo(block []byte, data []byte, Conn *net.UDPConn, raddr *net.UDPAddr) {
	msg, err := makeDATA(block, data)
	checkError(err)
	_, err = Conn.WriteTo(msg, raddr)
	checkError(err)
}
Beispiel #29
0
// writeToSessionUDP acts just like net.UDPConn.WritetTo(), but uses a *sessionUDP instead of a net.Addr.
func writeToSessionUDP(conn *net.UDPConn, b []byte, session *sessionUDP) (int, error) {
	n, err := conn.WriteTo(b, session.raddr)
	return n, err
}
Beispiel #30
0
func TestSendMsg_Piggyback(t *testing.T) {
	m := GetMemberlist(t)
	defer m.Shutdown()

	// Add a message to be broadcast
	a := alive{
		Incarnation: 10,
		Node:        "rand",
		Addr:        []byte{127, 0, 0, 255},
		Meta:        nil,
	}
	m.encodeAndBroadcast("rand", aliveMsg, &a)

	var udp *net.UDPConn
	for port := 60000; port < 61000; port++ {
		udpAddr := fmt.Sprintf("127.0.0.1:%d", port)
		udpLn, err := net.ListenPacket("udp", udpAddr)
		if err == nil {
			udp = udpLn.(*net.UDPConn)
			break
		}
	}

	// Encode a ping
	ping := ping{SeqNo: 42}
	buf, err := encode(pingMsg, ping)
	if err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	// Send
	addr := &net.UDPAddr{IP: net.ParseIP(m.config.BindAddr), Port: m.config.BindPort}
	udp.WriteTo(buf.Bytes(), addr)

	// Wait for response
	go func() {
		time.Sleep(time.Second)
		panic("timeout")
	}()

	in := make([]byte, 1500)
	n, _, err := udp.ReadFrom(in)
	if err != nil {
		t.Fatalf("unexpected err %s", err)
	}
	in = in[0:n]

	msgType := messageType(in[0])
	if msgType != compoundMsg {
		t.Fatalf("bad response %v", in)
	}

	// get the parts
	trunc, parts, err := decodeCompoundMessage(in[1:])
	if trunc != 0 {
		t.Fatalf("unexpected truncation")
	}
	if len(parts) != 2 {
		t.Fatalf("unexpected parts %v", parts)
	}
	if err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	var ack ackResp
	if err := decode(parts[0][1:], &ack); err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	if ack.SeqNo != 42 {
		t.Fatalf("bad sequence no")
	}

	var aliveout alive
	if err := decode(parts[1][1:], &aliveout); err != nil {
		t.Fatalf("unexpected err %s", err)
	}

	if aliveout.Node != "rand" || aliveout.Incarnation != 10 {
		t.Fatalf("bad mesg")
	}
}