aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xen/lib/xend/server/console.py
blob: ab8b22e41e5d322f870874f06726d1699db5cd5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from twisted.internet import reactor
from twisted.internet import protocol
from twisted.protocols import telnet

from xen.ext import xu

from xen.xend import EventServer
eserver = EventServer.instance()

import controller
from messages import *
from params import *

"""Telnet binary option."""
TRANSMIT_BINARY = '0'
WILL = chr(251)
IAC = chr(255)

class ConsoleProtocol(protocol.Protocol):
    """Asynchronous handler for a console TCP socket.
    """

    def __init__(self, controller, idx):
        self.controller = controller
        self.idx = idx
        self.addr = None
        self.binary = 0

    def connectionMade(self):
        peer = self.transport.getPeer()
        self.addr = (peer.host, peer.port)
        if self.controller.connect(self.addr, self):
            self.transport.write("Cannot connect to console %d on domain %d\n"
                                 % (self.idx, self.controller.dom))
            self.loseConnection()
            return
        else:
            # KAF: A nice quiet successful connect. Don't bother with telnet
            # control sequence -- telnet is not the appropriate protocol here. 
            #self.transport.write("Connected to console %d on domain %d\n"
            #                     % (self.idx, self.controller.dom))
            #self.setTelnetTransmitBinary()
            eserver.inject('xend.console.connect',
                           [self.idx, self.addr[0], self.addr[1]])

    def setTelnetTransmitBinary(self):
        """Send the sequence to set the telnet TRANSMIT-BINARY option.
        """
        self.write(IAC + WILL + TRANSMIT_BINARY)

    def dataReceived(self, data):
        if self.controller.handleInput(self, data):
            self.loseConnection()

    def write(self, data):
        #if not self.connected: return -1
        self.transport.write(data)
        return len(data)

    def connectionLost(self, reason=None):
        eserver.inject('xend.console.disconnect',
                       [self.idx, self.addr[0], self.addr[1]])
        self.controller.disconnect()

    def loseConnection(self):
        self.transport.loseConnection()

class ConsoleFactory(protocol.ServerFactory):
    """Asynchronous handler for a console server socket.
    """
    protocol = ConsoleProtocol
    
    def __init__(self, controller, idx):
        #protocol.ServerFactory.__init__(self)
        self.controller = controller
        self.idx = idx

    def buildProtocol(self, addr):
        proto = self.protocol(self.controller, self.idx)
        proto.factory = self
        return proto

class ConsoleControllerFactory(controller.ControllerFactory):
    """Factory for creating console controllers.
    """

    def createInstance(self, dom, console_port=None):
        if console_port is None:
            console_port = CONSOLE_PORT_BASE + dom
        console = ConsoleController(self, dom, console_port)
        self.addInstance(console)
        eserver.inject('xend.console.create',
                       [console.idx, console.dom, console.console_port])
        return console
        
    def consoleClosed(self, console):
        eserver.inject('xend.console.close', console.idx)
        self.delInstance(console)

class ConsoleController(controller.Controller):
    """Console controller for a domain.
    Does not poll for i/o itself, but relies on the notifier to post console
    output and the connected TCP sockets to post console input.
    """

    def __init__(self, factory, dom, console_port):
        #print 'ConsoleController> dom=', dom, type(dom)
        controller.Controller.__init__(self, factory, dom)
        self.majorTypes = [ CMSG_CONSOLE ]
        self.status = "new"
        self.addr = None
        self.conn = None
        self.rbuf = xu.buffer()
        self.wbuf = xu.buffer()
        self.console_port = console_port

        self.registerChannel()
        self.listener = None
        self.listen()
        #print 'ConsoleController<', 'dom=', self.dom, 'idx=', self.idx

    def sxpr(self):
        val =['console',
              ['status',       self.status ],
              ['id',           self.idx ],
              ['domain',       self.dom ],
              ['local_port',   self.channel.getLocalPort() ],
              ['remote_port',  self.channel.getRemotePort() ],
              ['console_port', self.console_port ] ]
        if self.addr:
            val.append(['connected', self.addr[0], self.addr[1]])
        return val

    def ready(self):
        return not (self.closed() or self.rbuf.empty())

    def closed(self):
        return self.status == 'closed'

    def connected(self):
        return self.status == 'connected'

    def close(self):
        try:
            #print 'ConsoleController> close dom=', self.dom
            self.status = "closed"
            if self.conn:
                self.conn.loseConnection()
            self.listener.stopListening()
            self.deregisterChannel()
            self.lostChannel()
        except Exception, ex:
            print 'ConsoleController>close>', ex
            raise

    def listen(self):
        """Listen for TCP connections to the console port..
        """
        if self.closed(): return
        self.status = "listening"
        if self.listener:
            #self.listener.startListening()
            pass
        else:
            f = ConsoleFactory(self, self.idx)
            self.listener = reactor.listenTCP(self.console_port, f)

    def connect(self, addr, conn):
        if self.closed(): return -1
        if self.connected(): return -1
        self.addr = addr
        self.conn = conn
        self.status = "connected"
        self.handleOutput()
        return 0

    def disconnect(self):
        if self.conn:
            self.conn.loseConnection()
        self.addr = None
        self.conn = None
        self.listen()

    def requestReceived(self, msg, type, subtype):
        #print '***Console', self.dom, msg.get_payload()
        self.rbuf.write(msg.get_payload())
        self.handleOutput()
        
    def responseReceived(self, msg, type, subtype):
        pass

    def produceRequests(self):
        # Send as much pending console data as there is room for.
        work = 0
        while not self.wbuf.empty() and self.channel.writeReady():
            msg = xu.message(CMSG_CONSOLE, 0, 0)
            msg.append_payload(self.wbuf.read(msg.MAX_PAYLOAD))
            work += self.channel.writeRequest(msg, notify=0)
        return work

    def handleInput(self, conn, data):
        """Handle some external input aimed at the console.
        Called from a TCP connection (conn).
        """
        if self.closed(): return -1
        if conn != self.conn: return 0
        self.wbuf.write(data)
        if self.produceRequests():
            self.channel.notify()
        return 0

    def handleOutput(self):
        """Handle buffered output from the console.
        Sends it to the connected console (if any).
        """
        if self.closed():
            #print 'Console>handleOutput> closed'
            return -1
        if not self.conn:
            #print 'Console>handleOutput> not connected'
            return 0
        while not self.rbuf.empty():
            try:
                #print 'Console>handleOutput> writing...'
                bytes = self.conn.write(self.rbuf.peek())
                if bytes > 0:
                    self.rbuf.discard(bytes)
            except socket.error, error:
                pass
        #print 'Console>handleOutput<'
        return 0