Commits

Liam Staskawicz committed 00fe426

RtpHeader: simplify collection of CsrcList and check that it's valid

Comments (0)

Files changed (2)

 
 import (
 	"encoding/binary"
+	"errors"
 	"log"
 	"net"
 )
 }
 
 // Extract the fixed size header from the beginning of this packet
-func (p *RtpPacket) Header() *RtpHeader {
+func (p *RtpPacket) Header() (*RtpHeader, error) {
 	h := &RtpHeader{}
 
+	if len(p.buf) < 12 {
+		return nil, errors.New("RtpPacket: len(buf) < size of header")
+	}
+
 	b0 := p.buf[0]
 	h.Version = (b0 & versionMask) >> versionShift
 	h.Padding = (b0 & paddingMask) != 0
 	h.Timestamp = be.Uint32(p.buf[4:])
 	h.SyncSrc = be.Uint32(p.buf[8:])
 
-	if h.CsrcCount > 0 {
-		h.CsrcList = make([]uint32, h.CsrcCount)
-		for i := uint8(0); i < h.CsrcCount; i++ {
-			h.CsrcList[i] = be.Uint32(p.buf[12+i*4:])
-		}
+	// XXX: ensure buf len is safe for given Count
+	for i := uint8(0); i < h.CsrcCount; i++ {
+		h.CsrcList = append(h.CsrcList, be.Uint32(p.buf[12+i*4:]))
+	}
+
+	if !h.IsValid() {
+		return nil, errors.New("RtpHeader: invalid")
+	}
+
+	return h, nil
+}
+
+func (h *RtpHeader) IsValid() bool {
+	// according to appendix A.1
+	if h.Version != supportedProtocolVersion {
+		return false
+	}
+
+	if _, ok := PayloadTypes[h.PayloadType]; !ok {
+		return false
 	}
 
-	return h
+	// other suggested checks not implemented for now
+	return true
 }
 // a new RTP packet has arrived from our transport
 func (s *Session) HandleRtp(p *RtpPacket) {
 	// XXX: check if packet is valid
+	hdr, err := p.Header()
+	if err != nil {
+		log.Println(err)
+		return
+	}
 
 	// forward packet to application
 	if s.RtpHandler != nil {