pssht  latest
SSH server library written in PHP
Connection.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
12 namespace fpoirotte\Pssht;
13 
17 
22 {
24  protected $channels;
25 
32  public function __construct(
33  \fpoirotte\Pssht\Transport $transport
34  ) {
35  $this->channels = array();
36 
37  $transport->setHandler(
38  // 90
39  \fpoirotte\Pssht\Messages\CHANNEL\OPEN::getMessageId(),
40  new \fpoirotte\Pssht\Handlers\CHANNEL\OPEN($this)
41  )->setHandler(
42  // 97
43  \fpoirotte\Pssht\Messages\CHANNEL\CLOSE::getMessageId(),
44  new \fpoirotte\Pssht\Handlers\CHANNEL\CLOSE($this)
45  )->setHandler(
46  // 98
47  \fpoirotte\Pssht\Messages\CHANNEL\REQUEST\Base::getMessageId(),
48  new \fpoirotte\Pssht\Handlers\CHANNEL\REQUEST($this)
49  );
50 
51  foreach (array_merge(range(91, 96), array(99, 100)) as $msgId) {
52  $transport->setHandler($msgId, $this);
53  }
54  }
55 
56  public function handle(
57  $msgType,
58  \fpoirotte\Pssht\Wire\Decoder $decoder,
59  \fpoirotte\Pssht\Transport $transport,
60  array &$context
61  ) {
62  $localChannel = $decoder->decodeUint32();
63  $encoder = new \fpoirotte\Pssht\Wire\Encoder();
64  $encoder->encodeUint32($localChannel);
65  $decoder->getBuffer()->unget($encoder->getBuffer()->get(0));
66 
67  if (isset($this->handlers[$localChannel][$msgType])) {
68  $handler = $this->handlers[$localChannel][$msgType];
69  $logging = \Plop\Plop::getInstance();
70  $logging->debug(
71  'Calling %(handler)s for channel #%(channel)d ' .
72  'with message type #%(msgType)d',
73  array(
74  'handler' => get_class($handler) . '::handle',
75  'channel' => $localChannel,
76  'msgType' => $msgType,
77  )
78  );
79  return $handler->handle($msgType, $decoder, $transport, $context);
80  }
81  return true;
82  }
83 
93  public function allocateChannel(\fpoirotte\Pssht\Messages\CHANNEL\OPEN $message)
94  {
95  for ($i = 0; isset($this->channels[$i]); ++$i) {
96  // Do nothing.
97  }
98  $this->channels[$i] = $message->getChannel();
99  $this->handlers[$i] = array();
100  return $i;
101  }
102 
112  public function freeChannel($id)
113  {
114  if (!is_int($id)) {
115  throw new \InvalidArgumentException();
116  }
117 
118  unset($this->channels[$id]);
119  unset($this->handlers[$id]);
120  return $this;
121  }
122 
132  public function getChannel($message)
133  {
134  if (is_int($message)) {
135  return $this->channels[$message];
136  }
137  return $this->channels[$message->getChannel()];
138  }
139 
152  public function setHandler(
153  $message,
154  $type,
155  \fpoirotte\Pssht\Handlers\HandlerInterface $handler
156  ) {
157  if (!is_int($type) || $type < 0 || $type > 255) {
158  throw new \InvalidArgumentException();
159  }
160 
161  if (!is_int($message)) {
162  if (!($message instanceof \fpoirotte\Pssht\Messages\CHANNEL\REQUEST\Base)) {
163  throw new \InvalidArgumentException();
164  }
165  $message = $message->getChannel();
166  }
167 
168  $this->handlers[$message][$type] = $handler;
169  return $this;
170  }
171 
184  public function unsetHandler(
185  $message,
186  $type,
187  \fpoirotte\Pssht\Handlers\HandlerInterface $handler
188  ) {
189  if (!is_int($type) || $type < 0 || $type > 255) {
190  throw new \InvalidArgumentException();
191  }
192 
193  if (!is_int($message)) {
194  if (!($message instanceof \fpoirotte\Pssht\Messages\CHANNEL\REQUEST\Base)) {
195  throw new \InvalidArgumentException();
196  }
197  $message = $message->getChannel();
198  }
199 
200  if (isset($this->handlers[$message][$type]) &&
201  $this->handlers[$message][$type] === $handler) {
202  unset($this->handlers[$message][$type]);
203  }
204  return $this;
205  }
206 }
__construct(\fpoirotte\Pssht\Transport $transport)
Definition: Connection.php:32
allocateChannel(\fpoirotte\Pssht\Messages\CHANNEL\OPEN $message)
Definition: Connection.php:93
setHandler($message, $type,\fpoirotte\Pssht\Handlers\HandlerInterface $handler)
Definition: Connection.php:152
handle($msgType,\fpoirotte\Pssht\Wire\Decoder $decoder,\fpoirotte\Pssht\Transport $transport, array &$context)
Definition: Connection.php:56
$channels
Opened SSH channels.
Definition: Connection.php:24
unsetHandler($message, $type,\fpoirotte\Pssht\Handlers\HandlerInterface $handler)
Definition: Connection.php:184