diff options
author | Peter Harris <git@peter.is-a-geek.org> | 2010-12-22 12:58:35 -0500 |
---|---|---|
committer | Peter Harris <git@peter.is-a-geek.org> | 2010-12-22 12:58:35 -0500 |
commit | 32b2f8870f0aeda7433df99db7317a8054a7f251 (patch) | |
tree | 2d8195707a3cb08cef1d4386b4f4bdfe1f413e13 | |
parent | 6cd2ce317d8907f820efc17a17124d33f6fcdcf5 (diff) |
Read entire connection block
-rw-r--r-- | xgob.go | 57 |
1 files changed, 44 insertions, 13 deletions
@@ -15,9 +15,14 @@ const X_TCP_PORT = 6000 type Error struct { } +type ConnectionBlock struct { + major, minor uint16 + todo []byte +} + type Connection struct { conn io.ReadWriteCloser - in *bufio.Reader + Setup ConnectionBlock } func parse_display (name string) (host string, protocol string, display int, screen int) { @@ -124,7 +129,7 @@ func Pad (i int) int { return (-i) & 3 } -func (c *Connection)write_setup(name string, data string) { +func (c *Connection) write_setup (name string, data string) { buf := make([]byte, 0, 100) buf = append(buf, 'B') // Big endian. @@ -160,21 +165,47 @@ func getUint16(in *bufio.Reader) uint16 { return rv } -func (c *Connection)read_setup() { - // TODO - status, _ := c.in.ReadByte() - for i := 0; i < 5; i++ { - _, _ = c.in.ReadByte() - } - len := getUint16(c.in) - println("Status: " + strconv.Itoa(int(status))) - println("Connection block size: " + strconv.Itoa(int(len))) +func (c *Connection) read_setup(in *bufio.Reader) bool { + status, _ := in.ReadByte() + reason_len, _ := in.ReadByte() + c.Setup.major = getUint16(in) + c.Setup.minor = getUint16(in) + len := getUint16(in) + + c.Setup.todo = make([]byte, len * 4) + var read int + for read < int(len) { + in, err := in.Read(c.Setup.todo[read:]) + if err != nil { + return false + } + read += in + } + + switch status { + case 0: + println("Server refused connection because") + println(bytes.NewBuffer(c.Setup.todo[:reason_len]).String()) + case 1: + /* Success */ + return true + case 2: + println("Server requires authentication") + println(bytes.NewBuffer(c.Setup.todo).String()) + default: + panic("Unexpected return value from server " + strconv.Uitoa(uint(status))) + } + return false } func (c *Connection)connect_auth(name string, data string) { c.write_setup(name, data) - c.in = bufio.NewReader(c.conn) - c.read_setup() + in := bufio.NewReader(c.conn) + if !c.read_setup(in) { + c.conn.Close() + c.conn = nil + return + } } func Connect (dpy string) (conn *Connection, screen int) { |