func (conn *TCPConn) ReadIPPacket(b []byte) (n int, err error) { ivheader := make([]byte, cipher.IVSize+IPHeaderSize) if _, err = io.ReadFull(conn, ivheader); err != nil { glog.V(3).Infoln("fail to read iv and encrypted header", err) return } dec := cipher.NewDecrypter(conn.cipher, ivheader[:cipher.IVSize]) dec.Decrypt(b[:IPHeaderSize], ivheader[cipher.IVSize:]) n = int(binary.BigEndian.Uint16(b[2:4])) payload := make([]byte, n-IPHeaderSize) if _, err = io.ReadFull(conn, payload); err != nil { glog.V(3).Infoln("fail to read encrypted payload", err) return } dec.Decrypt(b[IPHeaderSize:n], payload) return }
func recieve(cph *cipher.Cipher, tcpConn net.Conn, data interface{}) (err error) { iv := make([]byte, cipher.IVSize) if _, err = io.ReadFull(tcpConn, iv); err != nil { glog.V(3).Infoln("fail to read iv", err) return } dec := cipher.NewDecrypter(cph, iv) encrypted := make([]byte, binary.Size(data)) if _, err = io.ReadFull(tcpConn, encrypted); err != nil { glog.V(3).Infoln("fail to read encrypted data", err) return } decrypted := make([]byte, len(encrypted)) dec.Decrypt(decrypted, encrypted) if err = binary.Read(bytes.NewBuffer(decrypted), binary.BigEndian, data); err != nil { glog.V(3).Infoln("fail to unmarshal data", err) return } glog.V(3).Infoln("data recieved", data) return }