Example #1
0
func MarshalPoint(pt abstract.Point) []byte {
	buf := new(bytes.Buffer)
	ptByte := make([]byte, SecretSize)
	pt.MarshalTo(buf)
	buf.Read(ptByte)
	return ptByte
}
Example #2
0
func (s Servent) MessageWithCallback(message uint8, buffer *bytes.Buffer, callback Callback) (err error) {
	conn, err := net.Dial("tcp", s.Addr)
	if err != nil {
		return err
	}

	defer conn.Close()

	binary.Write(conn, binary.LittleEndian, message)
	binary.Write(conn, binary.LittleEndian, uint64(buffer.Len()))

	bytes := make([]byte, 2048)
	for {
		_, err := buffer.Read(bytes)
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		binary.Write(conn, binary.LittleEndian, bytes)
	}

	if callback != nil {
		err = callback(conn)
		if err != nil {
			return err
		}
	}

	return nil
}
Example #3
0
func newSourceChannel(cs *SourceChannel) error {
	f, err := os.Open("/dev/urandom")
	if err != nil {
		return err
	}
	buf := new(bytes.Buffer)
	io.CopyN(buf, f, 4096)
	f.Close()
	go func() {
		for {
			if buf.Len() == 0 {
				return
			}
			e := make([]byte, 24)
			_, err := buf.Read(e)
			if err != nil {
				fmt.Fprintf(os.Stderr, "tunafish: error on buffer (%v)\n", err)
				return

			}
			if cs.In == nil {
				return
			}
			cs.In <- e
			<-time.After(30 * time.Millisecond)
		}
	}()
	return nil
}
Example #4
0
File: xml.go Project: nytlabs/mxj
// Get next XML doc from an io.Reader as a Map value.  Returns Map value and pointer to raw XML.
//	NOTE: Due to the implementation of xml.Decoder, the raw XML off the reader is buffered to *[]byte
//	      using a ByteReader. If the io.Reader is an os.File, there may be significant performance impact.
//	      See the examples - getmetrics1.go through getmetrics4.go - for comparative use cases on a large
//	      data set. If the io.Reader is wrapping a []byte value in-memory, however, such as http.Request.Body
//	      you CAN use it to efficiently unmarshal an XML and retrieve the raw XML in a single call.
func NewMapXmlReaderRaw(xmlReader io.Reader, cast ...bool) (Map, *[]byte, error) {
	var r bool
	if len(cast) == 1 {
		r = cast[0]
	}
	// create TeeReader so we can retrieve raw XML
	wb := new(bytes.Buffer)
	trdr := myTeeReader(xmlReader, wb) // see code at EOF

	// build the node tree
	n, err := xmlReaderToTree(trdr)

	// retrieve the raw XML that was decoded
	b := make([]byte, wb.Len())
	_, _ = wb.Read(b)

	if err != nil {
		return nil, &b, err
	}

	// create the Map value
	m := make(map[string]interface{})
	m[n.key] = n.treeToMap(r)

	return m, &b, nil
}
Example #5
0
File: action.go Project: jessta/ogo
func (a *OfpActionVendorPort) Read(b []byte) (n int, err error) {
	a.Length = a.Len()
	buf := new(bytes.Buffer)
	err = binary.Write(buf, binary.BigEndian, a)
	n, err = buf.Read(b)
	return
}
Example #6
0
// Writer implements io.Writer, this function encodes a message  using
// crypto.nacl.box.Seal, then prepends a random generated nonce to it,
// and writes it to s.w encoded using binary.Write
func (s SecureWriter) Write(message []byte) (n int, err error) {
	if len(message) > MAX_BYTE_LENGTH {
		return 0, errors.New("The data to be encoded is to big")
	}

	nonce := GenerateNonce()

	// Use box.Seal to encode the data
	var out []byte
	encText := box.Seal(out, message, &nonce, s.pub, s.priv)

	// The nonce will be preceding the binary data of the message
	binaryData := append(nonce[:], encText...)

	// Encode the nonce + encodedMsg using binary.Write
	buf := new(bytes.Buffer)
	err = binary.Write(buf, binary.LittleEndian, binaryData)

	// Get the size of encoded data and prepend it in format [2]byte
	size := uint16(buf.Len())
	dataSize := make([]byte, 2)
	binary.LittleEndian.PutUint16(dataSize, size)

	data := make([]byte, size)
	n, err = buf.Read(data)

	data = append(dataSize, data...)

	// Data will be:
	// |length [2]byte| binary encoded data [length]byte
	n, err = s.w.Write(data)
	return n, err
}
Example #7
0
func getFcgiOutput(br *bytes.Buffer) *bytes.Buffer {
	var output bytes.Buffer
	for {
		var h fcgiHeader
		err := binary.Read(br, binary.BigEndian, &h)
		if err == os.EOF {
			break
		}

		content := make([]byte, h.ContentLength)
		br.Read(content)

		//read padding
		if h.PaddingLength > 0 {
			padding := make([]byte, h.PaddingLength)
			br.Read(padding)
		}

		if h.Type == fcgiStdout {
			output.Write(content)
		}
	}

	return &output
}
Example #8
0
func (f *OFFlowMod) Read(b []byte) (n int, err error) {

	f.Header.Length = f.ComputeLength()
	buf := new(bytes.Buffer)
	buf.ReadFrom(f.Header)
	//	fmt.Println("FlowMod Read1: Header: ", buf.Bytes())
	buf.ReadFrom(f.OFMatch)
	//	fmt.Println("FlowMod Read2: Match: ", buf.Bytes())
	binary.Write(buf, binary.BigEndian, f.Cookie)
	//	fmt.Println("FlowMod Read3: Cookie: ", buf.Bytes())
	binary.Write(buf, binary.BigEndian, f.Command)
	binary.Write(buf, binary.BigEndian, f.IdleTimeout)
	binary.Write(buf, binary.BigEndian, f.HardTimeout)
	binary.Write(buf, binary.BigEndian, f.Priority)
	binary.Write(buf, binary.BigEndian, f.BufferID)
	binary.Write(buf, binary.BigEndian, f.OutPort)
	binary.Write(buf, binary.BigEndian, f.Flags)
	binary.Write(buf, binary.BigEndian, f.Action.Type)
	binary.Write(buf, binary.BigEndian, f.Action.Length)
	binary.Write(buf, binary.BigEndian, f.Action.Port)
	binary.Write(buf, binary.BigEndian, f.Action.MaxLen)

	//	fmt.Println("FlowMod Read4: all: ", buf.Bytes())

	n, err = buf.Read(b)
	if err != nil {
		return
	}
	return n, io.EOF
}
Example #9
0
func decodeAmf0Utf8(buffer *bytes.Buffer) (v string, err error) {
	// len
	var length int16
	if err = binary.Read(buffer, binary.BigEndian, &length); err != nil {
		err = Amf0StringLengthRead
		return
	}

	// empty string
	if length <= 0 {
		return
	}

	// data
	data := make([]byte, length)
	if _, err = buffer.Read(data); err != nil {
		err = Amf0StringDataRead
		return
	}

	// support utf8-1 only
	// 1.3.1 Strings and UTF-8
	// UTF8-1 = %x00-7F
	// TODO: support other utf-8 strings

	v = string(data)

	return
}
Example #10
0
func (hq *HashQuery) Parse() error {
	// Require HTTP POST
	if hq.Method != "POST" {
		return ErrorInvalidMethod(hq.Method)
	}
	hq.responseChan = make(ResponseChan)
	var body *bytes.Buffer
	{
		defer hq.Body.Close()
		buf, err := ioutil.ReadAll(hq.Body)
		if err != nil {
			return err
		}
		body = bytes.NewBuffer(buf)
	}
	// Parse hashquery POST data
	n, err := recon.ReadInt(body)
	if err != nil {
		return err
	}
	hq.Digests = make([]string, n)
	for i := 0; i < n; i++ {
		hashlen, err := recon.ReadInt(body)
		if err != nil {
			return err
		}
		hash := make([]byte, hashlen)
		_, err = body.Read(hash)
		if err != nil {
			return err
		}
		hq.Digests[i] = hex.EncodeToString(hash)
	}
	return nil
}
Example #11
0
func (a *ActionEnqueue) Read(b []byte) (n int, err error) {
	buf := new(bytes.Buffer)
	if err = binary.Write(buf, binary.BigEndian, a.Type); err != nil {
		return
	}
	n += 2
	if err = binary.Write(buf, binary.BigEndian, a.Length); err != nil {
		return
	}
	n += 2
	if err = binary.Write(buf, binary.BigEndian, a.Port); err != nil {
		return
	}
	n += 2
	if err = binary.Write(buf, binary.BigEndian, a.pad); err != nil {
		return
	}
	n += 6
	if err = binary.Write(buf, binary.BigEndian, a.QueueID); err != nil {
		return
	}
	n += 4
	if n, err = buf.Read(b); n == 0 {
		return
	}
	return n, io.EOF
}
Example #12
0
func (tags *EventHeaderTags) Decode(buffer *bytes.Buffer) bool {
	b := make([]byte, 2)
	realLen, err := buffer.Read(b)
	if err != nil || realLen != 2 {
		return false
	}
	tags.magic = binary.BigEndian.Uint16(b)
	if tags.magic != MAGIC_NUMBER {
		return false
	}
	if token, ok := DecodeStringValue(buffer); nil == ok {
		ss := strings.Split(token, _splitter)
		if len(ss) > 1 {
			tags.Token = ss[1]
			tags.UserToken = ss[0]
		} else if len(ss) == 1 {
			tags.Token = token
		} else {
			return false
		}
	} else {
		return false
	}
	return true
}
Example #13
0
File: match.go Project: jessta/ogo
func (m *OfpMatch) Read(b []byte) (n int, err error) {
	// Any non-zero value fields should not be wildcarded.
	if m.InPort != 0 {
		m.Wildcards = m.Wildcards ^ OFPFW_IN_PORT
	}
	if m.DLSrc.String() != "00:00:00:00:00:00" {
		m.Wildcards = m.Wildcards ^ OFPFW_DL_SRC
	}
	if m.DLDst.String() != "00:00:00:00:00:00" {
		m.Wildcards = m.Wildcards ^ OFPFW_DL_DST
	}
	if m.DLVLAN != 0 {
		m.Wildcards = m.Wildcards ^ OFPFW_DL_VLAN
	}
	if m.DLVLANPcp != 0 {
		m.Wildcards = m.Wildcards ^ OFPFW_DL_VLAN_PCP
	}
	if m.DLType != 0 {
		m.Wildcards = m.Wildcards ^ OFPFW_DL_TYPE
	}
	if m.NWTos != 0 {
		m.Wildcards = m.Wildcards ^ OFPFW_NW_TOS
	}
	if m.NWProto != 0 {
		m.Wildcards = m.Wildcards ^ OFPFW_NW_PROTO
	}
	if m.NWSrc.String() != "0.0.0.0" {
		m.Wildcards = m.Wildcards ^ OFPFW_NW_SRC_ALL
	}
	if m.NWDst.String() != "0.0.0.0" {
		m.Wildcards = m.Wildcards ^ OFPFW_NW_DST_ALL
	}
	if m.TPSrc != 0 {
		m.Wildcards = m.Wildcards ^ OFPFW_TP_SRC
	}
	if m.TPDst != 0 {
		m.Wildcards = m.Wildcards ^ OFPFW_TP_DST
	}
	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, m.Wildcards)
	binary.Write(buf, binary.BigEndian, m.InPort)
	binary.Write(buf, binary.BigEndian, m.DLSrc)
	binary.Write(buf, binary.BigEndian, m.DLDst)
	binary.Write(buf, binary.BigEndian, m.DLVLAN)
	binary.Write(buf, binary.BigEndian, m.DLVLANPcp)
	binary.Write(buf, binary.BigEndian, m.Pad)
	binary.Write(buf, binary.BigEndian, m.DLType)
	binary.Write(buf, binary.BigEndian, m.NWTos)
	binary.Write(buf, binary.BigEndian, m.NWProto)
	binary.Write(buf, binary.BigEndian, m.Pad1)
	binary.Write(buf, binary.BigEndian, m.NWSrc)
	binary.Write(buf, binary.BigEndian, m.NWDst)
	binary.Write(buf, binary.BigEndian, m.TPSrc)
	binary.Write(buf, binary.BigEndian, m.TPDst)
	n, err = buf.Read(b)
	if n == 0 {
		return
	}
	return n, io.EOF
}
Example #14
0
func TestExtOrPortSendTransport(t *testing.T) {
	tests := [...]struct {
		methodName string
		expected   []byte
	}{
		{"", []byte("\x00\x02\x00\x00")},
		{"a", []byte("\x00\x02\x00\x01a")},
		{"alpha", []byte("\x00\x02\x00\x05alpha")},
	}

	for _, test := range tests {
		var buf bytes.Buffer
		err := extOrPortSendTransport(&buf, test.methodName)
		if err != nil {
			t.Errorf("%q unexpectedly returned an error: %s", test.methodName, err)
		}
		p := make([]byte, 1024)
		n, err := buf.Read(p)
		if err != nil {
			t.Fatal(err)
		}
		output := p[:n]
		if !bytes.Equal(output, test.expected) {
			t.Errorf("%q → %s (expected %s)", test.methodName,
				fmtBytes(output), fmtBytes(test.expected))
		}
	}
}
Example #15
0
func (t *Connection) submitResp(seq uint32, buf *bytes.Buffer) (
	err error) {
	_, err = t.getContext(seq)
	if err != nil {
		glog.Error(err)
		return
	}
	// MsgID 10 octetString
	msgID := make([]byte, 10)
	_, err = buf.Read(msgID)
	if err != nil {
		return
	}
	// status
	var status uint32
	err = binary.Read(buf, binary.BigEndian, &status)
	if err != nil {
		return
	}
	if status != STATUS_OK {
		err = fmt.Errorf("Submit seq %d failed, status = %d", seq, status)
	}
	glog.Infof("Submit seq %d MsgID 0x%x", seq, msgID)
	return
}
Example #16
0
func (i *IPv4) Read(b []byte) (n int, err error) {
	buf := new(bytes.Buffer)
	var verIhl uint8 = (i.Version << 4) + i.IHL
	binary.Write(buf, binary.BigEndian, verIhl)
	var dscpEcn uint8 = (i.DSCP << 2) + i.ECN
	binary.Write(buf, binary.BigEndian, dscpEcn)
	binary.Write(buf, binary.BigEndian, i.Length)
	binary.Write(buf, binary.BigEndian, i.ID)
	var flagsFrag uint16 = (i.Flags << 13) + i.FragmentOffset
	binary.Write(buf, binary.BigEndian, flagsFrag)
	binary.Write(buf, binary.BigEndian, i.TTL)
	binary.Write(buf, binary.BigEndian, i.Protocol)
	binary.Write(buf, binary.BigEndian, i.Checksum)
	binary.Write(buf, binary.BigEndian, i.NWSrc)
	binary.Write(buf, binary.BigEndian, i.NWDst)
	binary.Write(buf, binary.BigEndian, i.Options)
	if i.Data != nil {
		if n, err := buf.ReadFrom(i.Data); n == 0 {
			return int(n), err
		}
	}
	if n, err = buf.Read(b); n == 0 {
		return
	}
	return n, io.EOF
}
Example #17
0
func describeSyntaxError(originalErr error, parsedBuffer bytes.Buffer) error {

	sErr, ok := originalErr.(*json.SyntaxError)
	if !ok {
		return originalErr
	}

	buffer := make([]byte, sErr.Offset)
	parsedBuffer.Read(buffer)

	lineOffset := strings.LastIndex(string(buffer[:len(buffer)-1]), "\n")
	if lineOffset == -1 {
		lineOffset = 0
	}

	lines := strings.Split(string(buffer[:len(buffer)-1]), "\n")
	lineNumber := len(lines)

	descriptiveErrorString := fmt.Sprintf("Syntax Error on line %v:\n%v\n%v^\n%v\n",
		lineNumber,
		string(buffer[lineOffset:]),
		strings.Repeat(" ", int(sErr.Offset)-2-lineOffset),
		originalErr,
	)

	return errors.New(descriptiveErrorString)
}
Example #18
0
func parse_tlv_fields(r *bytes.Buffer) (map[uint16]*TLVField, error) {
	tlvs := map[uint16]*TLVField{}

	for {
		p := make([]byte, 4)
		_, err := r.Read(p)

		if err == io.EOF {
			break
		} else if err != nil {
			return nil, err
		}

		// length
		l := unpackUi16(p[2:4])

		// Get Value
		v := make([]byte, l)

		_, err = r.Read(v)
		if err != nil {
			return nil, err
		}

		tlvs[unpackUi16(p[0:2])] = &TLVField{
			unpackUi16(p[0:2]),
			unpackUi16(p[2:4]),
			v,
		}
	}

	return tlvs, nil
}
Example #19
0
func readBytes(b *bytes.Buffer) []byte {
	length := readInt(b)
	data := make([]byte, length)
	b.Read(data)

	return data
}
Example #20
0
// parsePacket publishes event according to data parsed
func (n *Driver) parsePacket(buf *bytes.Buffer) {
	for buf.Len() > 0 {
		b, _ := buf.ReadByte()
		switch b {
		case CodeEx:
			n.Publish(n.Event("extended"), nil)
		case CodeSignalQuality:
			ret, _ := buf.ReadByte()
			n.Publish(n.Event("signal"), ret)
		case CodeAttention:
			ret, _ := buf.ReadByte()
			n.Publish(n.Event("attention"), ret)
		case CodeMeditation:
			ret, _ := buf.ReadByte()
			n.Publish(n.Event("meditation"), ret)
		case CodeBlink:
			ret, _ := buf.ReadByte()
			n.Publish(n.Event("blink"), ret)
		case CodeWave:
			buf.Next(1)
			var ret = make([]byte, 2)
			buf.Read(ret)
			n.Publish(n.Event("wave"), int16(ret[0])<<8|int16(ret[1]))
		case CodeAsicEEG:
			ret := make([]byte, 25)
			i, _ := buf.Read(ret)
			if i == 25 {
				n.Publish(n.Event("eeg"), n.parseEEG(ret))
			}
		}
	}
}
// relay sends and receives tunneled traffic (payload). An HTTP request is
// triggered when data is in the write queue or at a polling interval.
// There's a geometric increase, up to a maximum, in the polling interval when
// no data is exchanged. Only one HTTP request is in flight at a time.
func (meek *MeekConn) relay() {
	// Note: meek.Close() calls here in relay() are made asynchronously
	// (using goroutines) since Close() will wait on this WaitGroup.
	defer meek.relayWaitGroup.Done()
	interval := MIN_POLL_INTERVAL
	timeout := time.NewTimer(interval)
	sendPayload := make([]byte, MAX_SEND_PAYLOAD_LENGTH)
	for {
		timeout.Reset(interval)
		// Block until there is payload to send or it is time to poll
		var sendBuffer *bytes.Buffer
		select {
		case sendBuffer = <-meek.partialSendBuffer:
		case sendBuffer = <-meek.fullSendBuffer:
		case <-timeout.C:
			// In the polling case, send an empty payload
		case <-meek.broadcastClosed:
			// TODO: timeout case may be selected when broadcastClosed is set?
			return
		}
		sendPayloadSize := 0
		if sendBuffer != nil {
			var err error
			sendPayloadSize, err = sendBuffer.Read(sendPayload)
			meek.replaceSendBuffer(sendBuffer)
			if err != nil {
				NoticeAlert("%s", ContextError(err))
				go meek.Close()
				return
			}
		}
		receivedPayload, err := meek.roundTrip(sendPayload[:sendPayloadSize])
		if err != nil {
			NoticeAlert("%s", ContextError(err))
			go meek.Close()
			return
		}
		if receivedPayload == nil {
			// In this case, meek.roundTrip encountered broadcastClosed. Exit without error.
			return
		}
		receivedPayloadSize, err := meek.readPayload(receivedPayload)
		if err != nil {
			NoticeAlert("%s", ContextError(err))
			go meek.Close()
			return
		}
		if receivedPayloadSize > 0 || sendPayloadSize > 0 {
			interval = 0
		} else if interval == 0 {
			interval = MIN_POLL_INTERVAL
		} else {
			interval = time.Duration(float64(interval) * POLL_INTERNAL_MULTIPLIER)
			if interval >= MAX_POLL_INTERVAL {
				interval = MIN_POLL_INTERVAL
			}
		}
	}
}
Example #22
0
// read parses the compressed data frame
func (p *Parser) read() error {
	var s, f uint32
	var k, v []byte
	var err error

	r, err := zlib.NewReader(p.Conn)
	if err != nil {
		return err
	}
	defer r.Close()

	// Decompress
	buff := new(bytes.Buffer)
	io.Copy(buff, r)
	p.buffer = buff

	b := make([]byte, 2)
	for i := uint32(0); i < p.wlen; i++ {
		n, err := buff.Read(b)
		if err == io.EOF {
			return err
		}

		if n == 0 {
			continue
		}

		switch string(b) {
		case "1D": // window size
			binary.Read(buff, binary.BigEndian, &s)
			binary.Read(buff, binary.BigEndian, &f)

			var ev buffer.Event
			fields := make(map[string]string)
			fields["timestamp"] = time.Now().Format(time.RFC3339Nano)

			for j := uint32(0); j < f; j++ {
				if k, v, err = p.readKV(); err != nil {
					return err
				}
				fields[string(k)] = string(v)
			}

			ev.Source = fmt.Sprintf("lumberjack://%s%s", fields["host"], fields["file"])
			ev.Offset, _ = strconv.ParseInt(fields["offset"], 10, 64)
			ev.Line = uint64(s)
			t := fields["line"]
			ev.Text = &t
			ev.Fields = &fields

			// Send to the receiver which is a buffer. We block because...
			p.Recv.Send(&ev)
		default:
			return fmt.Errorf("unknown type")
		}
	}

	return nil
}
Example #23
0
func readBytes(buf *bytes.Buffer, length int) (b []byte, err error) {
	b = make([]byte, length)
	i, err := buf.Read(b)
	if length != i {
		err = errors.New(fmt.Sprintf("not enough bytes,%v/%v", buf.Len(), length))
	}
	return
}
Example #24
0
func (d *LinkDiscovery) Read(b []byte) (n int, err error) {
	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, d.SrcDPID)
	binary.Write(buf, binary.BigEndian, d.Nsec)
	binary.Write(buf, binary.BigEndian, d.Pad)
	n, err = buf.Read(b)
	return n, io.EOF
}
Example #25
0
func (c *Compressor) decode(buf *bytes.Buffer) []float64 {
	head, _ := buf.ReadByte()

	var pred int64
	if (head & 0x80) != 0 {
		pred = c.pred2.Prediction()
	} else {
		pred = c.pred1.Prediction()
	}

	nzb := (head & 0x70) >> 4
	if nzb > 3 {
		nzb++
	}

	dst := make([]byte, 8-nzb)

	// FIXME: errors
	buf.Read(dst)

	diff := c.ToLong(dst)
	actual := pred ^ diff

	c.pred1.Update(actual)
	c.pred2.Update(actual)

	var ret []float64
	ret = append(ret, math.Float64frombits(uint64(actual)))

	if (head & 0x08) != 0 {
		pred = c.pred2.Prediction()
	} else {
		pred = c.pred1.Prediction()
	}

	nzb = head & 0x07
	if nzb > 3 {
		nzb++
	}

	dst = make([]byte, 8-nzb)

	// FIXME: errors
	buf.Read(dst)

	diff = c.ToLong(dst)

	if nzb == 7 && diff == 0 {
		return ret
	}

	actual = pred ^ diff

	c.pred1.Update(actual)
	c.pred2.Update(actual)

	return append(ret, math.Float64frombits(uint64(actual)))
}
Example #26
0
func main() {
	// Listen on TCP port 2000 on all interfaces.
	l, err := net.Listen("tcp", ":7862")
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Server listening...")

	defer l.Close()
	for {
		// Wait for a connection.
		conn, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}
		// Handle the connection in a new goroutine.
		// The loop then returns to accepting, so that
		// multiple connections may be served concurrently.
		go func(c net.Conn) {
			// So this is new session

			// Create directory
			sessionDir := fmt.Sprintf("session_%d", sessionId)
			err := os.Mkdir(sessionDir, 0777)
			if isErrorPresent("Cannot create new directory", err) { return }

			// Create buffer and read all incoming data into it
			var buf bytes.Buffer
			readlen, err := io.Copy(&buf, c)
			if isErrorPresent("Cannot read incoming data", err) { return }

			// Check that this is valid packet (readlen % 30008 = 0)
			if readlen % 30008 != 0 {
				log.Printf("Data seems to be invalid package (length: %d)", readlen)
				return
			}

			// Iterate over all packages and save each into file
			var databuf []byte = make([]byte, 30008)
			for ;; {
				// Read buffer
				readln, err := buf.Read(databuf)
				if err == io.EOF || isErrorPresent("Cannot read chunk from packet", err) || readln != 30008 {
					break;
				}
				// Read position
				var x, y int32
				reader := bytes.NewReader(databuf)
				binary.Read(reader, binary.LittleEndian, &x)
				binary.Read(reader, binary.LittleEndian, &y)
				// Save to file
				ioutil.WriteFile(fmt.Sprintf("%s/%d_%d.mapdata", sessionDir, x, y), databuf, 0777)
			}
			c.Close()
		}(conn)
	}
}
Example #27
0
func (d *DHCP) Read(b []byte) (n int, err error) {
	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, d.Operation)
	n += 1
	binary.Write(buf, binary.BigEndian, d.HardwareType)
	n += 1
	binary.Write(buf, binary.BigEndian, d.HardwareLen)
	n += 1
	binary.Write(buf, binary.BigEndian, d.HardwareOpts)
	n += 1
	binary.Write(buf, binary.BigEndian, d.Xid)
	n += 4
	binary.Write(buf, binary.BigEndian, d.Secs)
	n += 2
	binary.Write(buf, binary.BigEndian, d.Flags)
	n += 2
	binary.Write(buf, binary.BigEndian, d.ClientIP)
	n += 4
	binary.Write(buf, binary.BigEndian, d.YourIP)
	n += 4
	binary.Write(buf, binary.BigEndian, d.ServerIP)
	n += 4
	binary.Write(buf, binary.BigEndian, d.GatewayIP)
	n += 4
	clientHWAddr := make([]byte, 16)
	copy(clientHWAddr[0:], d.ClientHWAddr)
	binary.Write(buf, binary.BigEndian, clientHWAddr)
	n += 16
	binary.Write(buf, binary.BigEndian, d.ServerName)
	n += 64
	binary.Write(buf, binary.BigEndian, d.File)
	n += 128
	binary.Write(buf, binary.BigEndian, dhcpMagic)
	n += 4

	optend := false
	for _, opt := range d.Options {
		m, err := DHCPWriteOption(buf, opt)
		n += m
		if err != nil {
			return n, err
		}
		if opt.OptionType() == DHCP_OPT_END {
			optend = true
		}
	}
	if !optend {
		m, err := DHCPWriteOption(buf, DHCPNewOption(DHCP_OPT_END, nil))
		n += m
		if err != nil {
			return n, err
		}
	}
	if n, err = buf.Read(b); n == 0 {
		return
	}
	return n, nil
}
Example #28
0
File: port.go Project: jessta/ogo
func (p *OfpPortMod) Read(b []byte) (n int, err error) {
	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, p)
	n, err = buf.Read(b)
	if err != nil {
		return
	}
	return n, io.EOF
}
Example #29
0
func (c *SwitchConfig) Read(b []byte) (n int, err error) {
	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, c)
	n, err = buf.Read(b)
	if n == 0 {
		return
	}
	return n, io.EOF
}
Example #30
0
func (v *VLAN) Read(b []byte) (n int, err error) {
	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, v.TPID)
	var tci uint16 = 0
	tci = (tci | uint16(v.PCP)<<13) + (tci | uint16(v.DEI)<<12) + (tci | uint16(v.VID))
	binary.Write(buf, binary.BigEndian, tci)
	n, err = buf.Read(b)
	return
}