Exemplo n.º 1
0
func main() {
	m1 := &test.Member{
		Name: proto.String("Linus"),
		Age:  proto.Int32(99),
	}
	m1.Skills = []test.Skill{test.Skill_ASM, test.Skill_C}
	m1_data, err := proto.Marshal(m1)
	if err != nil {
		log.Fatal("marshaling error: ", err)
		os.Exit(1)
	}
	fmt.Println("m1:          ", m1)
	fmt.Println("m1.Skills:   ", m1.Skills)
	fmt.Println("actual data: ", m1_data)
	println("=== unmarshalling m1 into m2 ===")

	m2 := &test.Member{}
	proto.Unmarshal(m1_data, m2)
	fmt.Println("m2:          ", m2)

	port := 9090
	fmt.Println("=== END EXAMPLES / SERVER STARTING at %d ===", port)
	service := fmt.Sprintf(":%d", port)
	tcpAddr, err := net.ResolveTCPAddr("ip4", service)
	checkError(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	for {
		conn, err := listener.Accept()
		checkError(err)

		input := make([]byte, 1024)
		n, err := conn.Read(input)
		input = input[:n]
		fmt.Println("input: ", input)
		club := &test.Club{}
		err = proto.Unmarshal(input, club)
		checkError(err)

		sum := int32(0)
		fmt.Println("len: ", len(club.Member))
		for _, m := range club.Member {
			sum += *m.Age
		}
		avg := float64(sum) / float64(len(club.Member))
		fmt.Printf("avg age: %f   (sum: %d)\n", avg, sum)
		reply := &test.Reply{Average: proto.Float64(avg)}
		out_data, err := proto.Marshal(reply)
		conn.Write(out_data) // don't care about return value
		conn.Close()         // we're finished with this client
	}

}
Exemplo n.º 2
0
func (this *InitProtoHandler) Main() {
	defer this.Cleanup()
	fmt.Printf("InitProtoHandler·Main\n")
	header, data := this.Proxy.ReadMsg(0)
	if header == nil || *header.Type != Client_JOIN {
		fmt.Printf("Invalid data!\n")
		return
	}
	//Player joins
	joinmsg := NewClientJoin()
	player := NewPlayer()
	proto.Unmarshal(data, joinmsg)
	player.Name = *joinmsg.Playername
	worldhandler := GlobalRoutines["worldhandler"].(*WorldHandler)
	coord := worldhandler.World.GetCoord(50, 50)
	fmt.Printf("%d %d\n", coord.X, coord.Y)

	pusher := NewPushProtoHandler(this.Proxy)
	go pusher.Main()
	player.ProtoHandlers[PORT_PUSH] = pusher

	worldhandler.PlacePlayer(player, coord)
	this.Acceptbool()
	//Pass on to another handler
	session := make(Session)
	session["player"] = player

	inworld := NewInWorldProtoHandler(this.Proxy)
	player.ProtoHandlers[PORT_MAIN] = inworld
	inworld.Main(&session)

}
Exemplo n.º 3
0
func (server *Server) handleCryptSetup(client *Client, msg *Message) {
	cs := &mumbleproto.CryptSetup{}
	err := proto.Unmarshal(msg.buf, cs)
	if err != nil {
		client.Panic(err.String())
		return
	}

	// No client nonce. This means the client
	// is requesting that we re-sync our nonces.
	if len(cs.ClientNonce) == 0 {
		log.Printf("Requested crypt-nonce resync")
		cs.ClientNonce = make([]byte, cryptstate.AESBlockSize)
		if copy(cs.ClientNonce, client.crypt.EncryptIV[0:]) != cryptstate.AESBlockSize {
			return
		}
		client.sendProtoMessage(MessageCryptSetup, cs)
	} else {
		log.Printf("Received client nonce")
		if len(cs.ClientNonce) != cryptstate.AESBlockSize {
			return
		}

		client.crypt.Resync += 1
		if copy(client.crypt.DecryptIV[0:], cs.ClientNonce) != cryptstate.AESBlockSize {
			return
		}
		log.Printf("Crypt re-sync successful")
	}
}
Exemplo n.º 4
0
func TestMarshalUnmarshalBase64(t *testing.T) {
	var encbuf []byte
	var decbuf []byte
	t.Logf("start with buf %s\n", proto.CompactTextString(&cr))
	t.Log("marshalling protobuf")
	buf, err := proto.Marshal(&cr)
	if err != nil {
		t.Error("marshal error: ", err)
	}
	t.Log("marshalled")
	t.Log("urlencoding")
	t.Logf("need %d size buffer\n", base64.URLEncoding.EncodedLen(len(buf)-1))
	t.Log(buf)
	t.Logf("%v %s\n", buf, buf)
	encbuf = make([]byte, base64.URLEncoding.EncodedLen(len(buf)), base64.URLEncoding.EncodedLen(len(buf)))
	base64.URLEncoding.Encode(encbuf, buf)
	t.Log("urlencoded")
	t.Log("urldecoding")
	t.Logf("need %d size buffer\n", base64.URLEncoding.DecodedLen(len(encbuf)))
	t.Logf("%v %s\n", encbuf, encbuf)
	decbuf = make([]byte, base64.URLEncoding.DecodedLen(len(encbuf)), base64.URLEncoding.DecodedLen(len(encbuf)))
	n, err := base64.URLEncoding.Decode(decbuf, encbuf)
	t.Logf("wrote %d bytes from encbuf to decbuf. len(encbuf)=%d, len(buf)=%d\n", n, len(encbuf), len(buf))
	if err != nil {
		t.Error("urldecode error: ", err)
	}
	t.Log("urldecoded")

	t.Log(buf, decbuf)
	rcr := &CheckResult{}
	t.Log("unmarshalling")
	err = proto.Unmarshal(decbuf, rcr)
	t.Logf("%s\n", proto.CompactTextString(rcr))
}
Exemplo n.º 5
0
func TestRunBroadcastThree(t *testing.T) {
	c := make(chan Packet, 100)
	sentinel := Packet{Addr: "sentinel"}
	var r run
	r.seqn = 1
	r.out = c
	r.addrs = map[string]bool{
		"x": true,
		"y": true,
		"z": true,
	}

	r.broadcast(newInvite(1))
	c <- sentinel

	exp := msg{
		Seqn: proto.Int64(1),
		Cmd:  invite,
		Crnd: proto.Int64(1),
	}

	addrs := map[string]bool{}
	for i := 0; i < len(r.addrs); i++ {
		p := <-c
		addrs[p.Addr] = true
		var got msg
		err := proto.Unmarshal(p.Data, &got)
		assert.Equal(t, nil, err)
		assert.Equal(t, exp, got)
	}

	assert.Equal(t, sentinel, <-c)
	assert.Equal(t, r.addrs, addrs)
}
Exemplo n.º 6
0
func TestRunBroadcastFive(t *testing.T) {
	c := make(chan Packet, 100)
	var r run
	r.seqn = 1
	r.out = c
	r.addr = []*net.UDPAddr{
		&net.UDPAddr{net.IP{1, 2, 3, 4}, 5},
		&net.UDPAddr{net.IP{2, 3, 4, 5}, 6},
		&net.UDPAddr{net.IP{3, 4, 5, 6}, 7},
		&net.UDPAddr{net.IP{4, 5, 6, 7}, 8},
		&net.UDPAddr{net.IP{5, 6, 7, 8}, 9},
	}

	r.broadcast(newInvite(1))
	c <- Packet{}

	exp := msg{
		Seqn: proto.Int64(1),
		Cmd:  invite,
		Crnd: proto.Int64(1),
	}

	addr := make([]*net.UDPAddr, len(r.addr))
	for i := 0; i < len(r.addr); i++ {
		p := <-c
		addr[i] = p.Addr
		var got msg
		err := proto.Unmarshal(p.Data, &got)
		assert.Equal(t, nil, err)
		assert.Equal(t, exp, got)
	}

	assert.Equal(t, Packet{}, <-c)
	assert.Equal(t, r.addr, addr)
}
Exemplo n.º 7
0
func readMessage(r io.Reader) (msg *protocol.Message, err os.Error) {
start:
	// Read length
	length, err := readLength(r)
	if err != nil {
		if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
			// Socket timed out on read, read again
			goto start
		} else {
			return nil, err
		}
	}

	// Read the message bytes
	bs := make([]byte, length)
	if n, err := io.ReadFull(r, bs); err != nil {
		log.Println("Error reading message bytes:", err)
	} else if n != len(bs) {
		return nil, os.NewError(fmt.Sprintf("Read only %d bytes out of expected %d bytes!", n, length))
	}

	// Unmarshal
	msg = new(protocol.Message)
	if err := proto.Unmarshal(bs, msg); err != nil {
		return nil, err
	}
	return msg, nil
}
Exemplo n.º 8
0
func TestRunBroadcastFive(t *testing.T) {
	c := make(chan Packet, 100)
	sentinel := Packet{Addr: "sentinel"}
	var r run
	r.seqn = 1
	r.out = c
	r.addr = []string{"v", "w", "x", "y", "z"}

	r.broadcast(newInvite(1))
	c <- sentinel

	exp := msg{
		Seqn: proto.Int64(1),
		Cmd:  invite,
		Crnd: proto.Int64(1),
	}

	addrs := make([]string, len(r.addr))
	for i := 0; i < len(r.addr); i++ {
		p := <-c
		addrs[i] = p.Addr
		var got msg
		err := proto.Unmarshal(p.Data, &got)
		assert.Equal(t, nil, err)
		assert.Equal(t, exp, got)
	}

	assert.Equal(t, sentinel, <-c)
	assert.Equal(t, r.addr, addrs)
}
Exemplo n.º 9
0
// Broadcast text messages
func (server *Server) handleTextMessage(client *Client, msg *Message) {
	txtmsg := &mumbleproto.TextMessage{}
	err := proto.Unmarshal(msg.buf, txtmsg)
	if err != nil {
		client.Panic(err.String())
		return
	}

	// fixme(mkrautz): Check text message length.
	// fixme(mkrautz): Sanitize text as well.

	clients := make(map[uint32]*Client)

	// Tree
	for _, chanid := range txtmsg.TreeId {
		if channel, ok := server.Channels[int(chanid)]; ok {
			if !server.HasPermission(client, channel, TextMessagePermission) {
				client.sendPermissionDenied(client, channel, TextMessagePermission)
			}
			for _, target := range channel.clients {
				clients[target.Session] = target
			}
		}
	}

	// Direct-to-channel
	for _, chanid := range txtmsg.ChannelId {
		if channel, ok := server.Channels[int(chanid)]; ok {
			if !server.HasPermission(client, channel, TextMessagePermission) {
				client.sendPermissionDenied(client, channel, TextMessagePermission)
				return
			}
			for _, target := range channel.clients {
				clients[target.Session] = target
			}
		}
	}

	// Direct-to-clients
	for _, session := range txtmsg.Session {
		if target, ok := server.clients[session]; ok {
			if !server.HasPermission(client, target.Channel, TextMessagePermission) {
				client.sendPermissionDenied(client, target.Channel, TextMessagePermission)
				return
			}
			clients[session] = target
		}
	}

	// Remove ourselves
	clients[client.Session] = nil, false

	for _, target := range clients {
		target.sendProtoMessage(MessageTextMessage, &mumbleproto.TextMessage{
			Actor:   proto.Uint32(client.Session),
			Message: txtmsg.Message,
		})
	}
}
Exemplo n.º 10
0
func mustUnmarshal(b []byte) (r *response) {
	r = new(response)
	err := proto.Unmarshal(b, r)
	if err != nil {
		panic(err)
	}
	return
}
Exemplo n.º 11
0
func mustUnmarshal(b []byte) (r *R) {
	r = new(R)
	err := proto.Unmarshal(b, r)
	if err != nil {
		panic(err)
	}
	return
}
Exemplo n.º 12
0
func (ds *PBSDataStore) Get(key string, value interface{}) (err os.Error) {
	if ds.keys[key] != nil {
		data, err := ds.datastore.Get(key)
		if err == nil {
			err = proto.Unmarshal(data, value)
		}
	}
	return
}
Exemplo n.º 13
0
// User stats message. Shown in the Mumble client when a
// user right clicks a user and selects 'User Information'.
func (server *Server) handleUserStatsMessage(client *Client, msg *Message) {
	stats := &mumbleproto.UserStats{}
	err := proto.Unmarshal(msg.buf, stats)
	if err != nil {
		client.Panic(err.String())
	}

	log.Printf("UserStats")
}
Exemplo n.º 14
0
Arquivo: ncd.go Projeto: pjjw/ncd
func root(w http.ResponseWriter, r *http.Request) {
	// check header
	if *flagPassword != "" && *flagUsername != "" {
		auth, ok := r.Header["Authorization"]
		if ok && strings.HasPrefix(auth[0], "Basic ") {
			str := strings.TrimLeft(auth[0], "Basic ")
			decode, err := base64.StdEncoding.DecodeString(str)
			if err != nil {
				log.Print("cannot decode auth string: ", err)
				return
			}
			user, pass, err := url.UnescapeUserinfo(string(decode))
			if err != nil {
				log.Print("auth: couldn't decode user/pass: "******"auth: wrong user/pass: "******"/"+pass, *r)
				return
			}
			/* log.Printf("auth: %#v, user: %s, pass: %s", auth, user, pass)*/
		} else {
			log.Print("auth: no authorization")
			return
		}
	}

	checkpb := new(CheckResultSet)
	if r.Method == "POST" {
		cout := new(bytes.Buffer)
		if _, err := cout.ReadFrom(r.Body); err != nil {
			log.Print("error! ", err)
			return
		}
		switch r.Header["Content-Type"][0] {
		case "application/x-protobuf":
			err := proto.Unmarshal(cout.Bytes(), checkpb)
			if err != nil {
				log.Printf("unmarshalling error: ", err)
			}
		case "application/json", "text/plain", "application/x-www-form-urlencoded", "multipart/form-data":
			err := json.Unmarshal(cout.Bytes(), checkpb)
			if err != nil {
				log.Printf("unmarshalling error: ", err)
			}
		}
		logger.Printf("check returned! %s", proto.CompactTextString(checkpb))
		for _, v := range checkpb.Results {
			_, err := WriteCheck(v, *flagSpoolDir)
			if err != nil {
				logger.Print("writecheck failed: ", err)
			}
		}
	} else {
		/* logger.Printf("NOT POST!! %s", r.Method)*/
	}
	templ.Execute(w, nil)
}
Exemplo n.º 15
0
func (c *context) Call(service, method string, in, out interface{}) os.Error {
	data, err := proto.Marshal(in)
	if err != nil {
		return err
	}
	res, err := call(service, method, data)
	if err != nil {
		return err
	}
	return proto.Unmarshal(res, out)
}
Exemplo n.º 16
0
func TestMarshalUnmarshal(t *testing.T) {
	t.Log("marshalling protobuf")
	buf, err := proto.Marshal(&cr)
	if err != nil {
		log.Fatal("marshal error: ", err)
	}
	t.Log("marshalled")
	rcr := new(CheckResult)
	t.Log("unmarshalling")
	err = proto.Unmarshal(buf, rcr)
	t.Log(rcr)
}
Exemplo n.º 17
0
func (this *ProtoHandler) IsAccepted(port int) bool {
	_, data := this.Proxy.ReadMsg(port)
	a := NewAcceptBool()
	err := proto.Unmarshal(data, a)
	if err != nil {
		return false
	}
	if *a.Accept == true {
		return true
	}
	return false
}
Exemplo n.º 18
0
func ReceiveProtoBuf(reader io.Reader, pb interface{}) os.Error {
	fmt.Println("ReceiveRequest...")
	var size uint64
	if err := binary.Read(reader, binary.LittleEndian, &size); err != nil {
		return err
	}
	fmt.Println("size =", size)
	buf := make([]byte, size)
	if _, err := io.ReadFull(reader, buf); err != nil {
		return err
	}
	return proto.Unmarshal(buf, pb)
}
Exemplo n.º 19
0
// Permission query
func (server *Server) handlePermissionQuery(client *Client, msg *Message) {
	query := &mumbleproto.PermissionQuery{}
	err := proto.Unmarshal(msg.buf, query)
	if err != nil {
		client.Panic(err.String())
	}

	if query.ChannelId == nil {
		return
	}

	channel := server.Channels[int(*query.ChannelId)]
	server.sendClientPermissions(client, channel)
}
Exemplo n.º 20
0
func (c *conn) read(r *request) error {
	var size int32
	err := binary.Read(c.c, binary.BigEndian, &size)
	if err != nil {
		return err
	}

	buf := make([]byte, size)
	_, err = io.ReadFull(c.c, buf)
	if err != nil {
		return err
	}

	return proto.Unmarshal(buf, r)
}
Exemplo n.º 21
0
Arquivo: manager.go Projeto: kr/doozer
func recvPacket(q heap.Interface, P Packet) {
	var p packet
	p.Addr = P.Addr

	err := proto.Unmarshal(P.Data, &p.M)
	if err != nil {
		return
	}

	if p.M.Seqn == nil {
		return
	}

	heap.Push(q, p)
}
Exemplo n.º 22
0
// listenLoop() receives pongs from the needle server and cargo packets from peers
func (p *Peer) listenLoop() {
	for {
		buf := make([]byte, MaxPacketSize)
		n, addr, err := p.conn.ReadFromUDP(buf)
		if err != nil {
			continue
		}
		Logf("Packet from %s/%d\n", addr.String(), n)

		payload := &proto.PeerBound{}
		err = pb.Unmarshal(buf[0:n], payload)
		if err != nil {
			continue
		}

		switch {
		case payload.Pong != nil:
			for _, m := range payload.Pong.Punches {
				// Resolve remote peer
				addr, err := net.ResolveUDPAddr(*m.Address)
				if err != nil {
					break
				}

				// Prepare cargo packet
				payload := &proto.PeerBound{}
				payload.Cargo = &proto.Cargo{}
				payload.Cargo.OriginId = &p.id
				packet, err := pb.Marshal(payload)
				if err != nil {
					break
				}
				// Start sending empty cargos
				go func() {
					for {
						p.conn.WriteToUDP(packet, addr)
						time.Sleep(1e9)
					}
				}()
			}
		case payload.Cargo != nil:
			fmt.Printf("  cargo from %s\n", *payload.Cargo.OriginId)
		}

	}
}
Exemplo n.º 23
0
func recvPacket(q heap.Interface, P Packet) {
	var p packet
	p.Addr = P.Addr

	err := proto.Unmarshal(P.Data, &p.msg)
	if err != nil {
		log.Println(err)
		return
	}

	if p.msg.Seqn == nil || p.msg.Cmd == nil {
		log.Printf("discarding %#v", p)
		return
	}

	log.Println("recv", p.String())
	heap.Push(q, p)
}
Exemplo n.º 24
0
func recvPacket(q heap.Interface, P Packet) (p *packet) {
	p = new(packet)
	p.Addr = P.Addr

	err := proto.Unmarshal(P.Data, &p.msg)
	if err != nil {
		log.Println(err)
		return nil
	}

	if p.msg.Seqn == nil || p.msg.Cmd == nil {
		log.Printf("discarding %#v", p)
		return nil
	}

	heap.Push(q, p)
	return p
}
Exemplo n.º 25
0
func (server *Server) handlePingMessage(client *Client, msg *Message) {
	ping := &mumbleproto.Ping{}
	err := proto.Unmarshal(msg.buf, ping)
	if err != nil {
		client.Panic(err.String())
		return
	}

	// Phony response for ping messages. We don't keep stats
	// for this yet.
	client.sendProtoMessage(MessagePing, &mumbleproto.Ping{
		Timestamp: ping.Timestamp,
		Good:      proto.Uint32(uint32(client.crypt.Good)),
		Late:      proto.Uint32(uint32(client.crypt.Late)),
		Lost:      proto.Uint32(uint32(client.crypt.Lost)),
		Resync:    proto.Uint32(uint32(client.crypt.Resync)),
	})
}
Exemplo n.º 26
0
// DecodeKey decodes a key from the opaque representation returned by Encode.
func DecodeKey(encoded string) (*Key, error) {
	// Re-add padding.
	if m := len(encoded) % 4; m != 0 {
		encoded += strings.Repeat("=", 4-m)
	}

	b, err := base64.URLEncoding.DecodeString(encoded)
	if err != nil {
		return nil, err
	}

	ref := new(pb.Reference)
	if err := proto.Unmarshal(b, ref); err != nil {
		return nil, err
	}

	return protoToKey(ref)
}
Exemplo n.º 27
0
// Handle a user remove packet. This can either be a client disconnecting, or a
// user kicking or kick-banning another player.
func (server *Server) handleUserRemoveMessage(client *Client, msg *Message) {
	userremove := &mumbleproto.UserRemove{}
	err := proto.Unmarshal(msg.buf, userremove)
	if err != nil {
		client.Panic(err.String())
	}

	// Get the client to be removed.
	removeClient, ok := server.clients[*userremove.Session]
	if !ok {
		client.Panic("Invalid session in UserRemove message")
		return
	}

	ban := false
	if userremove.Ban != nil {
		ban = *userremove.Ban
	}

	// Check client's permissions
	perm := Permission(KickPermission)
	if ban {
		perm = Permission(BanPermission)
	}

	if removeClient.IsSuperUser() || !server.HasPermission(client, server.root, perm) {
		client.sendPermissionDenied(client, server.root, perm)
		return
	}

	if ban {
		// fixme(mkrautz): Implement banning.
		log.Printf("handleUserRemove: Banning is not yet implemented.")
	}

	userremove.Actor = proto.Uint32(uint32(client.Session))
	if err = server.broadcastProtoMessage(MessageUserRemove, userremove); err != nil {
		log.Panic("Unable to broadcast UserRemove message")
		return
	}

	removeClient.ForceDisconnect()
}
Exemplo n.º 28
0
// read reads a protocol buffer from the socketAPI socket.
func read(r *bufio.Reader, pb interface{}) os.Error {
	b, err := r.ReadSlice('\n')
	if err != nil {
		return err
	}
	n, err := strconv.Atoi(string(b[:len(b)-1]))
	if err != nil {
		return err
	}
	if n < 0 {
		return os.NewError("appengine: negative message length")
	}
	b = make([]byte, n)
	_, err = io.ReadFull(r, b)
	if err != nil {
		return err
	}
	return proto.Unmarshal(b, pb)
}
Exemplo n.º 29
0
func (this *ProtoProxy) readMsg() (*Header, []byte, os.Error) {
	//Read header first
	var hdrlen uint32
	var datalen uint32
	data, err := this.Read(this.Buffer[0:8])
	if err != nil {
		return nil, nil, err
	}
	//fmt.Printf("len(data) = %d\n", len(data))
	err = binary.Read(data[0:4], binary.BigEndian, &hdrlen)
	if err != nil {
		return nil, nil, err
	}
	err = binary.Read(data[4:8], binary.BigEndian, &datalen)
	if err != nil {
		return nil, nil, err
	}
	//fmt.Printf("hdrlen=%d, datalen=%d\n", hdrlen, datalen)
	if !(hdrlen < 96 && datalen < 16000) {
		return nil, nil, os.ENOMEM
	}
	hdrdata := make(Buf, hdrlen)
	newdata := make(Buf, datalen)
	tmp, err := this.Read(this.Buffer[0:hdrlen])
	if err != nil {
		//this.Conn.Close()
		return nil, nil, err
	}
	copy(hdrdata, tmp)
	header := NewHeader()
	err = proto.Unmarshal(hdrdata, header)
	if err != nil {
		return nil, nil, err
	}
	tmp, err = this.Read(this.Buffer[0:datalen])
	if err != nil {
		return nil, nil, err
	}
	copy(newdata, tmp)

	return header, newdata, nil
}
Exemplo n.º 30
0
Arquivo: server.go Projeto: kr/doozerd
func (c *conn) readBuf() (*T, os.Error) {
	var size int32
	err := binary.Read(c.c, binary.BigEndian, &size)
	if err != nil {
		return nil, err
	}

	buf := make([]byte, size)
	_, err = io.ReadFull(c.c, buf)
	if err != nil {
		return nil, err
	}

	var t T
	err = pb.Unmarshal(buf, &t)
	if err != nil {
		return nil, err
	}
	return &t, nil
}