messages.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "math/big"
  12. "reflect"
  13. "strconv"
  14. "strings"
  15. )
  16. // These are SSH message type numbers. They are scattered around several
  17. // documents but many were taken from [SSH-PARAMETERS].
  18. const (
  19. msgIgnore = 2
  20. msgUnimplemented = 3
  21. msgDebug = 4
  22. msgNewKeys = 21
  23. // Standard authentication messages
  24. msgUserAuthSuccess = 52
  25. msgUserAuthBanner = 53
  26. )
  27. // SSH messages:
  28. //
  29. // These structures mirror the wire format of the corresponding SSH messages.
  30. // They are marshaled using reflection with the marshal and unmarshal functions
  31. // in this file. The only wrinkle is that a final member of type []byte with a
  32. // ssh tag of "rest" receives the remainder of a packet when unmarshaling.
  33. // See RFC 4253, section 11.1.
  34. const msgDisconnect = 1
  35. // disconnectMsg is the message that signals a disconnect. It is also
  36. // the error type returned from mux.Wait()
  37. type disconnectMsg struct {
  38. Reason uint32 `sshtype:"1"`
  39. Message string
  40. Language string
  41. }
  42. func (d *disconnectMsg) Error() string {
  43. return fmt.Sprintf("ssh: disconnect, reason %d: %s", d.Reason, d.Message)
  44. }
  45. // See RFC 4253, section 7.1.
  46. const msgKexInit = 20
  47. type kexInitMsg struct {
  48. Cookie [16]byte `sshtype:"20"`
  49. KexAlgos []string
  50. ServerHostKeyAlgos []string
  51. CiphersClientServer []string
  52. CiphersServerClient []string
  53. MACsClientServer []string
  54. MACsServerClient []string
  55. CompressionClientServer []string
  56. CompressionServerClient []string
  57. LanguagesClientServer []string
  58. LanguagesServerClient []string
  59. FirstKexFollows bool
  60. Reserved uint32
  61. }
  62. // See RFC 4253, section 8.
  63. // Diffie-Helman
  64. const msgKexDHInit = 30
  65. type kexDHInitMsg struct {
  66. X *big.Int `sshtype:"30"`
  67. }
  68. const msgKexECDHInit = 30
  69. type kexECDHInitMsg struct {
  70. ClientPubKey []byte `sshtype:"30"`
  71. }
  72. const msgKexECDHReply = 31
  73. type kexECDHReplyMsg struct {
  74. HostKey []byte `sshtype:"31"`
  75. EphemeralPubKey []byte
  76. Signature []byte
  77. }
  78. const msgKexDHReply = 31
  79. type kexDHReplyMsg struct {
  80. HostKey []byte `sshtype:"31"`
  81. Y *big.Int
  82. Signature []byte
  83. }
  84. // See RFC 4253, section 10.
  85. const msgServiceRequest = 5
  86. type serviceRequestMsg struct {
  87. Service string `sshtype:"5"`
  88. }
  89. // See RFC 4253, section 10.
  90. const msgServiceAccept = 6
  91. type serviceAcceptMsg struct {
  92. Service string `sshtype:"6"`
  93. }
  94. // See RFC 4252, section 5.
  95. const msgUserAuthRequest = 50
  96. type userAuthRequestMsg struct {
  97. User string `sshtype:"50"`
  98. Service string
  99. Method string
  100. Payload []byte `ssh:"rest"`
  101. }
  102. // Used for debug printouts of packets.
  103. type userAuthSuccessMsg struct {
  104. }
  105. // See RFC 4252, section 5.1
  106. const msgUserAuthFailure = 51
  107. type userAuthFailureMsg struct {
  108. Methods []string `sshtype:"51"`
  109. PartialSuccess bool
  110. }
  111. // See RFC 4256, section 3.2
  112. const msgUserAuthInfoRequest = 60
  113. const msgUserAuthInfoResponse = 61
  114. type userAuthInfoRequestMsg struct {
  115. User string `sshtype:"60"`
  116. Instruction string
  117. DeprecatedLanguage string
  118. NumPrompts uint32
  119. Prompts []byte `ssh:"rest"`
  120. }
  121. // See RFC 4254, section 5.1.
  122. const msgChannelOpen = 90
  123. type channelOpenMsg struct {
  124. ChanType string `sshtype:"90"`
  125. PeersId uint32
  126. PeersWindow uint32
  127. MaxPacketSize uint32
  128. TypeSpecificData []byte `ssh:"rest"`
  129. }
  130. const msgChannelExtendedData = 95
  131. const msgChannelData = 94
  132. // Used for debug print outs of packets.
  133. type channelDataMsg struct {
  134. PeersId uint32 `sshtype:"94"`
  135. Length uint32
  136. Rest []byte `ssh:"rest"`
  137. }
  138. // See RFC 4254, section 5.1.
  139. const msgChannelOpenConfirm = 91
  140. type channelOpenConfirmMsg struct {
  141. PeersId uint32 `sshtype:"91"`
  142. MyId uint32
  143. MyWindow uint32
  144. MaxPacketSize uint32
  145. TypeSpecificData []byte `ssh:"rest"`
  146. }
  147. // See RFC 4254, section 5.1.
  148. const msgChannelOpenFailure = 92
  149. type channelOpenFailureMsg struct {
  150. PeersId uint32 `sshtype:"92"`
  151. Reason RejectionReason
  152. Message string
  153. Language string
  154. }
  155. const msgChannelRequest = 98
  156. type channelRequestMsg struct {
  157. PeersId uint32 `sshtype:"98"`
  158. Request string
  159. WantReply bool
  160. RequestSpecificData []byte `ssh:"rest"`
  161. }
  162. // See RFC 4254, section 5.4.
  163. const msgChannelSuccess = 99
  164. type channelRequestSuccessMsg struct {
  165. PeersId uint32 `sshtype:"99"`
  166. }
  167. // See RFC 4254, section 5.4.
  168. const msgChannelFailure = 100
  169. type channelRequestFailureMsg struct {
  170. PeersId uint32 `sshtype:"100"`
  171. }
  172. // See RFC 4254, section 5.3
  173. const msgChannelClose = 97
  174. type channelCloseMsg struct {
  175. PeersId uint32 `sshtype:"97"`
  176. }
  177. // See RFC 4254, section 5.3
  178. const msgChannelEOF = 96
  179. type channelEOFMsg struct {
  180. PeersId uint32 `sshtype:"96"`
  181. }
  182. // See RFC 4254, section 4
  183. const msgGlobalRequest = 80
  184. type globalRequestMsg struct {
  185. Type string `sshtype:"80"`
  186. WantReply bool
  187. Data []byte `ssh:"rest"`
  188. }
  189. // See RFC 4254, section 4
  190. const msgRequestSuccess = 81
  191. type globalRequestSuccessMsg struct {
  192. Data []byte `ssh:"rest" sshtype:"81"`
  193. }
  194. // See RFC 4254, section 4
  195. const msgRequestFailure = 82
  196. type globalRequestFailureMsg struct {
  197. Data []byte `ssh:"rest" sshtype:"82"`
  198. }
  199. // See RFC 4254, section 5.2
  200. const msgChannelWindowAdjust = 93
  201. type windowAdjustMsg struct {
  202. PeersId uint32 `sshtype:"93"`
  203. AdditionalBytes uint32
  204. }
  205. // See RFC 4252, section 7
  206. const msgUserAuthPubKeyOk = 60
  207. type userAuthPubKeyOkMsg struct {
  208. Algo string `sshtype:"60"`
  209. PubKey []byte
  210. }
  211. // typeTags returns the possible type bytes for the given reflect.Type, which
  212. // should be a struct. The possible values are separated by a '|' character.
  213. func typeTags(structType reflect.Type) (tags []byte) {
  214. tagStr := structType.Field(0).Tag.Get("sshtype")
  215. for _, tag := range strings.Split(tagStr, "|") {
  216. i, err := strconv.Atoi(tag)
  217. if err == nil {
  218. tags = append(tags, byte(i))
  219. }
  220. }
  221. return tags
  222. }
  223. func fieldError(t reflect.Type, field int, problem string) error {
  224. if problem != "" {
  225. problem = ": " + problem
  226. }
  227. return fmt.Errorf("ssh: unmarshal error for field %s of type %s%s", t.Field(field).Name, t.Name(), problem)
  228. }
  229. var errShortRead = errors.New("ssh: short read")
  230. // Unmarshal parses data in SSH wire format into a structure. The out
  231. // argument should be a pointer to struct. If the first member of the
  232. // struct has the "sshtype" tag set to a '|'-separated set of numbers
  233. // in decimal, the packet must start with one of those numbers. In
  234. // case of error, Unmarshal returns a ParseError or
  235. // UnexpectedMessageError.
  236. func Unmarshal(data []byte, out interface{}) error {
  237. v := reflect.ValueOf(out).Elem()
  238. structType := v.Type()
  239. expectedTypes := typeTags(structType)
  240. var expectedType byte
  241. if len(expectedTypes) > 0 {
  242. expectedType = expectedTypes[0]
  243. }
  244. if len(data) == 0 {
  245. return parseError(expectedType)
  246. }
  247. if len(expectedTypes) > 0 {
  248. goodType := false
  249. for _, e := range expectedTypes {
  250. if e > 0 && data[0] == e {
  251. goodType = true
  252. break
  253. }
  254. }
  255. if !goodType {
  256. return fmt.Errorf("ssh: unexpected message type %d (expected one of %v)", data[0], expectedTypes)
  257. }
  258. data = data[1:]
  259. }
  260. var ok bool
  261. for i := 0; i < v.NumField(); i++ {
  262. field := v.Field(i)
  263. t := field.Type()
  264. switch t.Kind() {
  265. case reflect.Bool:
  266. if len(data) < 1 {
  267. return errShortRead
  268. }
  269. field.SetBool(data[0] != 0)
  270. data = data[1:]
  271. case reflect.Array:
  272. if t.Elem().Kind() != reflect.Uint8 {
  273. return fieldError(structType, i, "array of unsupported type")
  274. }
  275. if len(data) < t.Len() {
  276. return errShortRead
  277. }
  278. for j, n := 0, t.Len(); j < n; j++ {
  279. field.Index(j).Set(reflect.ValueOf(data[j]))
  280. }
  281. data = data[t.Len():]
  282. case reflect.Uint64:
  283. var u64 uint64
  284. if u64, data, ok = parseUint64(data); !ok {
  285. return errShortRead
  286. }
  287. field.SetUint(u64)
  288. case reflect.Uint32:
  289. var u32 uint32
  290. if u32, data, ok = parseUint32(data); !ok {
  291. return errShortRead
  292. }
  293. field.SetUint(uint64(u32))
  294. case reflect.Uint8:
  295. if len(data) < 1 {
  296. return errShortRead
  297. }
  298. field.SetUint(uint64(data[0]))
  299. data = data[1:]
  300. case reflect.String:
  301. var s []byte
  302. if s, data, ok = parseString(data); !ok {
  303. return fieldError(structType, i, "")
  304. }
  305. field.SetString(string(s))
  306. case reflect.Slice:
  307. switch t.Elem().Kind() {
  308. case reflect.Uint8:
  309. if structType.Field(i).Tag.Get("ssh") == "rest" {
  310. field.Set(reflect.ValueOf(data))
  311. data = nil
  312. } else {
  313. var s []byte
  314. if s, data, ok = parseString(data); !ok {
  315. return errShortRead
  316. }
  317. field.Set(reflect.ValueOf(s))
  318. }
  319. case reflect.String:
  320. var nl []string
  321. if nl, data, ok = parseNameList(data); !ok {
  322. return errShortRead
  323. }
  324. field.Set(reflect.ValueOf(nl))
  325. default:
  326. return fieldError(structType, i, "slice of unsupported type")
  327. }
  328. case reflect.Ptr:
  329. if t == bigIntType {
  330. var n *big.Int
  331. if n, data, ok = parseInt(data); !ok {
  332. return errShortRead
  333. }
  334. field.Set(reflect.ValueOf(n))
  335. } else {
  336. return fieldError(structType, i, "pointer to unsupported type")
  337. }
  338. default:
  339. return fieldError(structType, i, fmt.Sprintf("unsupported type: %v", t))
  340. }
  341. }
  342. if len(data) != 0 {
  343. return parseError(expectedType)
  344. }
  345. return nil
  346. }
  347. // Marshal serializes the message in msg to SSH wire format. The msg
  348. // argument should be a struct or pointer to struct. If the first
  349. // member has the "sshtype" tag set to a number in decimal, that
  350. // number is prepended to the result. If the last of member has the
  351. // "ssh" tag set to "rest", its contents are appended to the output.
  352. func Marshal(msg interface{}) []byte {
  353. out := make([]byte, 0, 64)
  354. return marshalStruct(out, msg)
  355. }
  356. func marshalStruct(out []byte, msg interface{}) []byte {
  357. v := reflect.Indirect(reflect.ValueOf(msg))
  358. msgTypes := typeTags(v.Type())
  359. if len(msgTypes) > 0 {
  360. out = append(out, msgTypes[0])
  361. }
  362. for i, n := 0, v.NumField(); i < n; i++ {
  363. field := v.Field(i)
  364. switch t := field.Type(); t.Kind() {
  365. case reflect.Bool:
  366. var v uint8
  367. if field.Bool() {
  368. v = 1
  369. }
  370. out = append(out, v)
  371. case reflect.Array:
  372. if t.Elem().Kind() != reflect.Uint8 {
  373. panic(fmt.Sprintf("array of non-uint8 in field %d: %T", i, field.Interface()))
  374. }
  375. for j, l := 0, t.Len(); j < l; j++ {
  376. out = append(out, uint8(field.Index(j).Uint()))
  377. }
  378. case reflect.Uint32:
  379. out = appendU32(out, uint32(field.Uint()))
  380. case reflect.Uint64:
  381. out = appendU64(out, uint64(field.Uint()))
  382. case reflect.Uint8:
  383. out = append(out, uint8(field.Uint()))
  384. case reflect.String:
  385. s := field.String()
  386. out = appendInt(out, len(s))
  387. out = append(out, s...)
  388. case reflect.Slice:
  389. switch t.Elem().Kind() {
  390. case reflect.Uint8:
  391. if v.Type().Field(i).Tag.Get("ssh") != "rest" {
  392. out = appendInt(out, field.Len())
  393. }
  394. out = append(out, field.Bytes()...)
  395. case reflect.String:
  396. offset := len(out)
  397. out = appendU32(out, 0)
  398. if n := field.Len(); n > 0 {
  399. for j := 0; j < n; j++ {
  400. f := field.Index(j)
  401. if j != 0 {
  402. out = append(out, ',')
  403. }
  404. out = append(out, f.String()...)
  405. }
  406. // overwrite length value
  407. binary.BigEndian.PutUint32(out[offset:], uint32(len(out)-offset-4))
  408. }
  409. default:
  410. panic(fmt.Sprintf("slice of unknown type in field %d: %T", i, field.Interface()))
  411. }
  412. case reflect.Ptr:
  413. if t == bigIntType {
  414. var n *big.Int
  415. nValue := reflect.ValueOf(&n)
  416. nValue.Elem().Set(field)
  417. needed := intLength(n)
  418. oldLength := len(out)
  419. if cap(out)-len(out) < needed {
  420. newOut := make([]byte, len(out), 2*(len(out)+needed))
  421. copy(newOut, out)
  422. out = newOut
  423. }
  424. out = out[:oldLength+needed]
  425. marshalInt(out[oldLength:], n)
  426. } else {
  427. panic(fmt.Sprintf("pointer to unknown type in field %d: %T", i, field.Interface()))
  428. }
  429. }
  430. }
  431. return out
  432. }
  433. var bigOne = big.NewInt(1)
  434. func parseString(in []byte) (out, rest []byte, ok bool) {
  435. if len(in) < 4 {
  436. return
  437. }
  438. length := binary.BigEndian.Uint32(in)
  439. in = in[4:]
  440. if uint32(len(in)) < length {
  441. return
  442. }
  443. out = in[:length]
  444. rest = in[length:]
  445. ok = true
  446. return
  447. }
  448. var (
  449. comma = []byte{','}
  450. emptyNameList = []string{}
  451. )
  452. func parseNameList(in []byte) (out []string, rest []byte, ok bool) {
  453. contents, rest, ok := parseString(in)
  454. if !ok {
  455. return
  456. }
  457. if len(contents) == 0 {
  458. out = emptyNameList
  459. return
  460. }
  461. parts := bytes.Split(contents, comma)
  462. out = make([]string, len(parts))
  463. for i, part := range parts {
  464. out[i] = string(part)
  465. }
  466. return
  467. }
  468. func parseInt(in []byte) (out *big.Int, rest []byte, ok bool) {
  469. contents, rest, ok := parseString(in)
  470. if !ok {
  471. return
  472. }
  473. out = new(big.Int)
  474. if len(contents) > 0 && contents[0]&0x80 == 0x80 {
  475. // This is a negative number
  476. notBytes := make([]byte, len(contents))
  477. for i := range notBytes {
  478. notBytes[i] = ^contents[i]
  479. }
  480. out.SetBytes(notBytes)
  481. out.Add(out, bigOne)
  482. out.Neg(out)
  483. } else {
  484. // Positive number
  485. out.SetBytes(contents)
  486. }
  487. ok = true
  488. return
  489. }
  490. func parseUint32(in []byte) (uint32, []byte, bool) {
  491. if len(in) < 4 {
  492. return 0, nil, false
  493. }
  494. return binary.BigEndian.Uint32(in), in[4:], true
  495. }
  496. func parseUint64(in []byte) (uint64, []byte, bool) {
  497. if len(in) < 8 {
  498. return 0, nil, false
  499. }
  500. return binary.BigEndian.Uint64(in), in[8:], true
  501. }
  502. func intLength(n *big.Int) int {
  503. length := 4 /* length bytes */
  504. if n.Sign() < 0 {
  505. nMinus1 := new(big.Int).Neg(n)
  506. nMinus1.Sub(nMinus1, bigOne)
  507. bitLen := nMinus1.BitLen()
  508. if bitLen%8 == 0 {
  509. // The number will need 0xff padding
  510. length++
  511. }
  512. length += (bitLen + 7) / 8
  513. } else if n.Sign() == 0 {
  514. // A zero is the zero length string
  515. } else {
  516. bitLen := n.BitLen()
  517. if bitLen%8 == 0 {
  518. // The number will need 0x00 padding
  519. length++
  520. }
  521. length += (bitLen + 7) / 8
  522. }
  523. return length
  524. }
  525. func marshalUint32(to []byte, n uint32) []byte {
  526. binary.BigEndian.PutUint32(to, n)
  527. return to[4:]
  528. }
  529. func marshalUint64(to []byte, n uint64) []byte {
  530. binary.BigEndian.PutUint64(to, n)
  531. return to[8:]
  532. }
  533. func marshalInt(to []byte, n *big.Int) []byte {
  534. lengthBytes := to
  535. to = to[4:]
  536. length := 0
  537. if n.Sign() < 0 {
  538. // A negative number has to be converted to two's-complement
  539. // form. So we'll subtract 1 and invert. If the
  540. // most-significant-bit isn't set then we'll need to pad the
  541. // beginning with 0xff in order to keep the number negative.
  542. nMinus1 := new(big.Int).Neg(n)
  543. nMinus1.Sub(nMinus1, bigOne)
  544. bytes := nMinus1.Bytes()
  545. for i := range bytes {
  546. bytes[i] ^= 0xff
  547. }
  548. if len(bytes) == 0 || bytes[0]&0x80 == 0 {
  549. to[0] = 0xff
  550. to = to[1:]
  551. length++
  552. }
  553. nBytes := copy(to, bytes)
  554. to = to[nBytes:]
  555. length += nBytes
  556. } else if n.Sign() == 0 {
  557. // A zero is the zero length string
  558. } else {
  559. bytes := n.Bytes()
  560. if len(bytes) > 0 && bytes[0]&0x80 != 0 {
  561. // We'll have to pad this with a 0x00 in order to
  562. // stop it looking like a negative number.
  563. to[0] = 0
  564. to = to[1:]
  565. length++
  566. }
  567. nBytes := copy(to, bytes)
  568. to = to[nBytes:]
  569. length += nBytes
  570. }
  571. lengthBytes[0] = byte(length >> 24)
  572. lengthBytes[1] = byte(length >> 16)
  573. lengthBytes[2] = byte(length >> 8)
  574. lengthBytes[3] = byte(length)
  575. return to
  576. }
  577. func writeInt(w io.Writer, n *big.Int) {
  578. length := intLength(n)
  579. buf := make([]byte, length)
  580. marshalInt(buf, n)
  581. w.Write(buf)
  582. }
  583. func writeString(w io.Writer, s []byte) {
  584. var lengthBytes [4]byte
  585. lengthBytes[0] = byte(len(s) >> 24)
  586. lengthBytes[1] = byte(len(s) >> 16)
  587. lengthBytes[2] = byte(len(s) >> 8)
  588. lengthBytes[3] = byte(len(s))
  589. w.Write(lengthBytes[:])
  590. w.Write(s)
  591. }
  592. func stringLength(n int) int {
  593. return 4 + n
  594. }
  595. func marshalString(to []byte, s []byte) []byte {
  596. to[0] = byte(len(s) >> 24)
  597. to[1] = byte(len(s) >> 16)
  598. to[2] = byte(len(s) >> 8)
  599. to[3] = byte(len(s))
  600. to = to[4:]
  601. copy(to, s)
  602. return to[len(s):]
  603. }
  604. var bigIntType = reflect.TypeOf((*big.Int)(nil))
  605. // Decode a packet into its corresponding message.
  606. func decode(packet []byte) (interface{}, error) {
  607. var msg interface{}
  608. switch packet[0] {
  609. case msgDisconnect:
  610. msg = new(disconnectMsg)
  611. case msgServiceRequest:
  612. msg = new(serviceRequestMsg)
  613. case msgServiceAccept:
  614. msg = new(serviceAcceptMsg)
  615. case msgKexInit:
  616. msg = new(kexInitMsg)
  617. case msgKexDHInit:
  618. msg = new(kexDHInitMsg)
  619. case msgKexDHReply:
  620. msg = new(kexDHReplyMsg)
  621. case msgUserAuthRequest:
  622. msg = new(userAuthRequestMsg)
  623. case msgUserAuthSuccess:
  624. return new(userAuthSuccessMsg), nil
  625. case msgUserAuthFailure:
  626. msg = new(userAuthFailureMsg)
  627. case msgUserAuthPubKeyOk:
  628. msg = new(userAuthPubKeyOkMsg)
  629. case msgGlobalRequest:
  630. msg = new(globalRequestMsg)
  631. case msgRequestSuccess:
  632. msg = new(globalRequestSuccessMsg)
  633. case msgRequestFailure:
  634. msg = new(globalRequestFailureMsg)
  635. case msgChannelOpen:
  636. msg = new(channelOpenMsg)
  637. case msgChannelData:
  638. msg = new(channelDataMsg)
  639. case msgChannelOpenConfirm:
  640. msg = new(channelOpenConfirmMsg)
  641. case msgChannelOpenFailure:
  642. msg = new(channelOpenFailureMsg)
  643. case msgChannelWindowAdjust:
  644. msg = new(windowAdjustMsg)
  645. case msgChannelEOF:
  646. msg = new(channelEOFMsg)
  647. case msgChannelClose:
  648. msg = new(channelCloseMsg)
  649. case msgChannelRequest:
  650. msg = new(channelRequestMsg)
  651. case msgChannelSuccess:
  652. msg = new(channelRequestSuccessMsg)
  653. case msgChannelFailure:
  654. msg = new(channelRequestFailureMsg)
  655. default:
  656. return nil, unexpectedMessageError(0, packet[0])
  657. }
  658. if err := Unmarshal(packet, msg); err != nil {
  659. return nil, err
  660. }
  661. return msg, nil
  662. }