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
|
/* nbd client library in userspace: state machine
* Copyright Red Hat
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* State machine for parsing the oldstyle handshake. */
STATE_MACHINE {
OLDSTYLE.START:
/* We've already read the first 16 bytes of the handshake, we must
* now read the remainder.
*/
h->rbuf = &h->sbuf.old_handshake;
h->rlen = sizeof h->sbuf.old_handshake;
h->rbuf = (char *)h->rbuf + 16;
h->rlen -= 16;
SET_NEXT_STATE (%RECV_REMAINING);
return 0;
OLDSTYLE.RECV_REMAINING:
switch (recv_into_rbuf (h)) {
case -1: SET_NEXT_STATE (%.DEAD); return 0;
case 0: SET_NEXT_STATE (%CHECK);
}
return 0;
OLDSTYLE.CHECK:
uint64_t exportsize;
uint16_t gflags, eflags;
/* We already checked the magic and version in MAGIC.CHECK_MAGIC. */
exportsize = be64toh (h->sbuf.old_handshake.exportsize);
gflags = be16toh (h->sbuf.old_handshake.gflags);
eflags = be16toh (h->sbuf.old_handshake.eflags);
/* Server is unable to upgrade to TLS. If h->tls is not 'require' (2)
* then we can continue unencrypted.
*/
if (h->tls == LIBNBD_TLS_REQUIRE) {
SET_NEXT_STATE (%.DEAD);
set_error (ENOTSUP, "handshake: server is oldstyle, "
"but handle TLS setting is 'require' (2)");
return 0;
}
h->gflags = gflags;
debug (h, "gflags: 0x%" PRIx16, gflags);
if (gflags) {
set_error (0, "handshake: oldstyle server should not set gflags");
SET_NEXT_STATE (%.DEAD);
return 0;
}
if (nbd_internal_set_size_and_flags (h, exportsize, eflags) == -1) {
SET_NEXT_STATE (%.DEAD);
return 0;
}
nbd_internal_set_payload (h);
h->protocol = "oldstyle";
SET_NEXT_STATE (%.READY);
return 0;
} /* END STATE MACHINE */
|