diff --git a/backend-nbd.cpp b/backend-nbd.cpp index f5fbfc1..76b38fc 100644 --- a/backend-nbd.cpp +++ b/backend-nbd.cpp @@ -109,7 +109,7 @@ bool backend_nbd::connect(const bool retry) fd = -1; } - dev_size = NTOHLL(nbd_hello.size); + dev_size = my_NTOHLL(nbd_hello.size); } if (fd != -1 && memcmp(nbd_hello.magic1, "NBDMAGIC", 8) != 0) { @@ -157,7 +157,7 @@ bool backend_nbd::invoke_nbd(const uint32_t command, const uint64_t offset, cons nbd_request.magic = ntohl(0x25609513); nbd_request.type = htonl(command); - nbd_request.offset = HTONLL(offset); + nbd_request.offset = my_HTONLL(offset); nbd_request.length = htonl(n_bytes); if (WRITE(fd, reinterpret_cast(&nbd_request), sizeof nbd_request) != sizeof nbd_request) { diff --git a/iscsi-pdu.cpp b/iscsi-pdu.cpp index 2a98226..dee16ac 100644 --- a/iscsi-pdu.cpp +++ b/iscsi-pdu.cpp @@ -200,7 +200,7 @@ bool iscsi_pdu_ahs::set(const uint8_t *const in, const size_t n) if (n < 3) return false; - size_t expected_size = NTOHS(reinterpret_cast(in)->length); + size_t expected_size = my_NTOHS(reinterpret_cast(in)->length); if (expected_size + 3 != n) return false; @@ -212,7 +212,7 @@ bool iscsi_pdu_ahs::set(const uint8_t *const in, const size_t n) blob_t iscsi_pdu_ahs::get() { - uint16_t expected_size = NTOHS(reinterpret_cast(ahs)->length); + uint16_t expected_size = my_NTOHS(reinterpret_cast(ahs)->length); uint32_t out_size = sizeof(__ahs_header__) + expected_size; uint8_t *out = duplicate_new(ahs, out_size); @@ -388,9 +388,9 @@ bool iscsi_pdu_login_reply::set(const iscsi_pdu_login_request & reply_to) while(login_reply->TSIH == 0); } login_reply->Itasktag = reply_to.get_Itasktag(); - login_reply->StatSN = HTONL(discovery ? 0 : reply_to.get_ExpStatSN()); - login_reply->ExpCmdSN = HTONL(reply_to.get_CmdSN()); - login_reply->MaxCmdSN = HTONL(reply_to.get_CmdSN() + 1); + login_reply->StatSN = my_HTONL(discovery ? 0 : reply_to.get_ExpStatSN()); + login_reply->ExpCmdSN = my_HTONL(reply_to.get_CmdSN()); + login_reply->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + 1); return true; } @@ -552,10 +552,10 @@ bool iscsi_pdu_scsi_response::set(const iscsi_pdu_scsi_cmd & reply_to, const std pdu_response->datalenM = reply_data_plus_sense_header >> 8; pdu_response->datalenL = reply_data_plus_sense_header ; pdu_response->Itasktag = reply_to.get_Itasktag(); - pdu_response->StatSN = HTONL(reply_to.get_ExpStatSN()); - pdu_response->ExpCmdSN = HTONL(reply_to.get_CmdSN() + 1); - pdu_response->MaxCmdSN = HTONL(reply_to.get_CmdSN() + max_msg_depth); - pdu_response->ExpDataSN = HTONL(0); + pdu_response->StatSN = my_HTONL(reply_to.get_ExpStatSN()); + pdu_response->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1); + pdu_response->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth); + pdu_response->ExpDataSN = my_HTONL(0); if (ResidualCt.has_value()) { set_bits(&pdu_response->b2, 1, 1, true); // U (residual underflow) pdu_response->ResidualCt = ResidualCt.value(); @@ -638,11 +638,11 @@ std::vector iscsi_pdu_scsi_data_in::get() const set_bits(&pdu_data_in->b2, 7, 1, true); // F if (pdu_data_in_data.second < reply_to_copy->get_ExpDatLen()) { set_bits(&pdu_data_in->b2, 1, 1, true); // U - pdu_data_in->ResidualCt = HTONL(reply_to_copy->get_ExpDatLen() - pdu_data_in_data.second); + pdu_data_in->ResidualCt = my_HTONL(reply_to_copy->get_ExpDatLen() - pdu_data_in_data.second); } else if (pdu_data_in_data.second > reply_to_copy->get_ExpDatLen()) { set_bits(&pdu_data_in->b2, 2, 1, true); // O - pdu_data_in->ResidualCt = HTONL(pdu_data_in_data.second - reply_to_copy->get_ExpDatLen()); + pdu_data_in->ResidualCt = my_HTONL(pdu_data_in_data.second - reply_to_copy->get_ExpDatLen()); } set_bits(&pdu_data_in->b2, 0, 1, true); // S } @@ -653,12 +653,12 @@ std::vector iscsi_pdu_scsi_data_in::get() const pdu_data_in->datalenL = cur_len ; memcpy(pdu_data_in->LUN, reply_to_copy->get_LUN(), sizeof pdu_data_in->LUN); pdu_data_in->Itasktag = reply_to_copy->get_Itasktag(); - pdu_data_in->StatSN = HTONL(reply_to_copy->get_ExpStatSN()); - pdu_data_in->ExpCmdSN = HTONL(reply_to_copy->get_CmdSN() + 1); - pdu_data_in->MaxCmdSN = HTONL(reply_to_copy->get_CmdSN() + max_msg_depth); - pdu_data_in->DataSN = HTONL(ses->get_inc_datasn(reply_to_copy->get_Itasktag())); - pdu_data_in->bufferoff = HTONL(i); - pdu_data_in->ResidualCt = HTONL(use_pdu_data_size - i); + pdu_data_in->StatSN = my_HTONL(reply_to_copy->get_ExpStatSN()); + pdu_data_in->ExpCmdSN = my_HTONL(reply_to_copy->get_CmdSN() + 1); + pdu_data_in->MaxCmdSN = my_HTONL(reply_to_copy->get_CmdSN() + max_msg_depth); + pdu_data_in->DataSN = my_HTONL(ses->get_inc_datasn(reply_to_copy->get_Itasktag())); + pdu_data_in->bufferoff = my_HTONL(i); + pdu_data_in->ResidualCt = my_HTONL(use_pdu_data_size - i); auto temp_out = get_helper(pdu_data_in, pdu_data_in_data.first + i, cur_len); v_out.push_back(temp_out.at(0)); @@ -697,16 +697,16 @@ std::pair iscsi_pdu_scsi_data_in::gen_data_in_pdu(session *co pdu_data_in.datalenL = data_is_n_bytes ; memcpy(pdu_data_in.LUN, reply_to.get_LUN(), sizeof pdu_data_in.LUN); pdu_data_in.Itasktag = reply_to.get_Itasktag(); - pdu_data_in.StatSN = HTONL(reply_to.get_ExpStatSN()); - pdu_data_in.ExpCmdSN = HTONL(reply_to.get_CmdSN() + 1); - pdu_data_in.MaxCmdSN = HTONL(reply_to.get_CmdSN() + max_msg_depth); - pdu_data_in.DataSN = HTONL(ses->get_inc_datasn(reply_to.get_Itasktag())); - pdu_data_in.bufferoff = HTONL(offset_in_data); + pdu_data_in.StatSN = my_HTONL(reply_to.get_ExpStatSN()); + pdu_data_in.ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1); + pdu_data_in.MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth); + pdu_data_in.DataSN = my_HTONL(ses->get_inc_datasn(reply_to.get_Itasktag())); + pdu_data_in.bufferoff = my_HTONL(offset_in_data); if (reply_to.get_ExpDatLen() < offset_after_block) - pdu_data_in.ResidualCt = HTONL(offset_after_block - reply_to.get_ExpDatLen()); + pdu_data_in.ResidualCt = my_HTONL(offset_after_block - reply_to.get_ExpDatLen()); else - pdu_data_in.ResidualCt = HTONL(reply_to.get_ExpDatLen() - (offset_in_data + data_is_n_bytes)); + pdu_data_in.ResidualCt = my_HTONL(reply_to.get_ExpDatLen() - (offset_in_data + data_is_n_bytes)); size_t out_size = sizeof(pdu_data_in) + data_is_n_bytes; out_size = (out_size + 3) & ~3; @@ -815,9 +815,9 @@ bool iscsi_pdu_nop_in::set(const iscsi_pdu_nop_out & reply_to) memcpy(nop_in->LUN, reply_to.get_LUN(), sizeof nop_in->LUN); nop_in->Itasktag = reply_to.get_Itasktag(); nop_in->TTT = reply_to.get_TTT(); - nop_in->StatSN = HTONL(reply_to.get_ExpStatSN()); - nop_in->ExpCmdSN = HTONL(reply_to.get_CmdSN() + 1); - nop_in->MaxCmdSN = HTONL(reply_to.get_CmdSN() + max_msg_depth); + nop_in->StatSN = my_HTONL(reply_to.get_ExpStatSN()); + nop_in->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1); + nop_in->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth); return true; } @@ -847,11 +847,11 @@ bool iscsi_pdu_scsi_r2t::set(const iscsi_pdu_scsi_cmd & reply_to, const uint32_t memcpy(pdu_scsi_r2t->LUN, reply_to.get_LUN(), sizeof pdu_scsi_r2t->LUN); pdu_scsi_r2t->Itasktag = reply_to.get_Itasktag(); pdu_scsi_r2t->TTT = TTT; - pdu_scsi_r2t->StatSN = HTONL(reply_to.get_ExpStatSN()); - pdu_scsi_r2t->ExpCmdSN = HTONL(reply_to.get_CmdSN() + 1); - pdu_scsi_r2t->MaxCmdSN = HTONL(reply_to.get_CmdSN() + max_msg_depth); - pdu_scsi_r2t->bufferoff = HTONL(buffer_offset); - pdu_scsi_r2t->DDTF = HTONL(data_length); + pdu_scsi_r2t->StatSN = my_HTONL(reply_to.get_ExpStatSN()); + pdu_scsi_r2t->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1); + pdu_scsi_r2t->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth); + pdu_scsi_r2t->bufferoff = my_HTONL(buffer_offset); + pdu_scsi_r2t->DDTF = my_HTONL(data_length); return true; } @@ -953,9 +953,9 @@ bool iscsi_pdu_text_reply::set(const iscsi_pdu_text_request & reply_to, scsi *co memcpy(text_reply->LUN, reply_to.get_LUN(), sizeof text_reply->LUN); text_reply->TTT = reply_to.get_TTT(); text_reply->Itasktag = reply_to.get_Itasktag(); - text_reply->StatSN = HTONL(reply_to.get_ExpStatSN()); - text_reply->ExpCmdSN = HTONL(reply_to.get_CmdSN() + 1); - text_reply->MaxCmdSN = HTONL(reply_to.get_CmdSN() + max_msg_depth); + text_reply->StatSN = my_HTONL(reply_to.get_ExpStatSN()); + text_reply->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1); + text_reply->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth); return true; } @@ -1024,9 +1024,9 @@ bool iscsi_pdu_logout_reply::set(const iscsi_pdu_logout_request & reply_to) set_bits(&logout_reply->b1, 0, 6, o_logout_resp); // 0x26 set_bits(&logout_reply->b2, 7, 1, true); logout_reply->Itasktag = reply_to.get_Itasktag(); - logout_reply->StatSN = HTONL(reply_to.get_ExpStatSN()); - logout_reply->ExpCmdSN = HTONL(reply_to.get_CmdSN()); - logout_reply->MaxCmdSN = HTONL(reply_to.get_CmdSN()); + logout_reply->StatSN = my_HTONL(reply_to.get_ExpStatSN()); + logout_reply->ExpCmdSN = my_HTONL(reply_to.get_CmdSN()); + logout_reply->MaxCmdSN = my_HTONL(reply_to.get_CmdSN()); return true; } @@ -1094,9 +1094,9 @@ bool iscsi_pdu_taskman_reply::set(const iscsi_pdu_taskman_request & reply_to) set_bits(&taskman_reply->b1, 0, 6, o_scsi_taskmanr); // 0x22 set_bits(&taskman_reply->b2, 7, 1, true); taskman_reply->Itasktag = reply_to.get_Itasktag(); - taskman_reply->StatSN = HTONL(reply_to.get_ExpStatSN()); - taskman_reply->ExpCmdSN = HTONL(reply_to.get_CmdSN() + 1); - taskman_reply->MaxCmdSN = HTONL(reply_to.get_CmdSN() + max_msg_depth); + taskman_reply->StatSN = my_HTONL(reply_to.get_ExpStatSN()); + taskman_reply->ExpCmdSN = my_HTONL(reply_to.get_CmdSN() + 1); + taskman_reply->MaxCmdSN = my_HTONL(reply_to.get_CmdSN() + max_msg_depth); return true; } diff --git a/iscsi-pdu.h b/iscsi-pdu.h index 22e28fe..46c705d 100644 --- a/iscsi-pdu.h +++ b/iscsi-pdu.h @@ -187,7 +187,7 @@ class iscsi_pdu_login_request : public iscsi_pdu_bhs // login request 0x03 const uint8_t *get_ISID() const { return login_req->ISID; } uint16_t get_CID() const { return login_req->CID; } - uint32_t get_CmdSN() const { return NTOHL(login_req->CmdSN); } + uint32_t get_CmdSN() const { return my_NTOHL(login_req->CmdSN); } uint16_t get_TSIH() const { return login_req->TSIH; } bool get_T() const { return get_bits(login_req->b2, 7, 1); } bool get_C() const { return get_bits(login_req->b2, 6, 1); } @@ -195,7 +195,7 @@ class iscsi_pdu_login_request : public iscsi_pdu_bhs // login request 0x03 uint8_t get_NSG() const { return get_bits(login_req->b2, 0, 2); } uint8_t get_versionmin() const { return login_req->versionmin; } uint32_t get_Itasktag() const { return login_req->Itasktag; } - uint32_t get_ExpStatSN() const { return NTOHL(login_req->ExpStatSN); } + uint32_t get_ExpStatSN() const { return my_NTOHL(login_req->ExpStatSN); } std::optional get_initiator() const { return initiator; } virtual bool set_data(std::pair data_in) override; @@ -289,10 +289,10 @@ class iscsi_pdu_scsi_cmd : public iscsi_pdu_bhs // 0x01 const uint8_t * get_CDB() const { return cdb_pdu_req->CDB; } uint32_t get_Itasktag() const { return cdb_pdu_req->Itasktag; } - uint32_t get_ExpStatSN() const { return NTOHL(cdb_pdu_req->ExpStatSN); } - uint32_t get_CmdSN() const { return NTOHL(cdb_pdu_req->CmdSN); } + uint32_t get_ExpStatSN() const { return my_NTOHL(cdb_pdu_req->ExpStatSN); } + uint32_t get_CmdSN() const { return my_NTOHL(cdb_pdu_req->CmdSN); } const uint8_t * get_LUN() const { return cdb_pdu_req->LUN; } - uint32_t get_ExpDatLen() const { return NTOHL(cdb_pdu_req->expdatlen); } + uint32_t get_ExpDatLen() const { return my_NTOHL(cdb_pdu_req->expdatlen); } virtual std::optional get_response(scsi *const sd) override; // special case: response after one or more data-out PDUs @@ -392,7 +392,7 @@ class iscsi_pdu_scsi_data_out : public iscsi_pdu_bhs // 0x05 bool set(const iscsi_pdu_scsi_cmd & reply_to, const std::pair scsi_reply_data); std::vector get() const override; - uint32_t get_BufferOffset() const { return NTOHL(pdu_data_out->bufferoff); } + uint32_t get_BufferOffset() const { return my_NTOHL(pdu_data_out->bufferoff); } uint32_t get_TTT() const { return pdu_data_out->TTT; } bool get_F() const { return !!(pdu_data_out->b2 & 128); } uint32_t get_Itasktag() const { return pdu_data_out->Itasktag; } @@ -480,8 +480,8 @@ class iscsi_pdu_nop_out : public iscsi_pdu_bhs // NOP-Out 0x00 const uint8_t *get_LUN() const { return nop_out->LUN; } uint32_t get_Itasktag() const { return nop_out->Itasktag; } uint32_t get_TTT() const { return nop_out->TTT; } - uint32_t get_CmdSN() const { return NTOHL(nop_out->CmdSN); } - uint32_t get_ExpStatSN() const { return NTOHL(nop_out->ExpStatSN); } + uint32_t get_CmdSN() const { return my_NTOHL(nop_out->CmdSN); } + uint32_t get_ExpStatSN() const { return my_NTOHL(nop_out->ExpStatSN); } virtual std::optional get_response(scsi *const sd) override; }; @@ -599,9 +599,9 @@ class iscsi_pdu_text_request : public iscsi_pdu_bhs // text request 0x04 std::vector get() const override; const uint8_t * get_LUN() const { return text_req->LUN; } - uint32_t get_CmdSN() const { return NTOHL(text_req->CmdSN); } + uint32_t get_CmdSN() const { return my_NTOHL(text_req->CmdSN); } uint32_t get_Itasktag() const { return text_req->Itasktag; } - uint32_t get_ExpStatSN() const { return NTOHL(text_req->ExpStatSN); } + uint32_t get_ExpStatSN() const { return my_NTOHL(text_req->ExpStatSN); } uint32_t get_TTT() const { return text_req->TTT; } virtual std::optional get_response(scsi *const sd) override; @@ -679,9 +679,9 @@ class iscsi_pdu_logout_request : public iscsi_pdu_bhs // logout request 0x06 bool set(const uint8_t *const in, const size_t n) override; std::vector get() const override; - uint32_t get_CmdSN() const { return NTOHL(logout_req->CmdSN); } + uint32_t get_CmdSN() const { return my_NTOHL(logout_req->CmdSN); } uint32_t get_Itasktag() const { return logout_req->Itasktag; } - uint32_t get_ExpStatSN() const { return NTOHL(logout_req->ExpStatSN); } + uint32_t get_ExpStatSN() const { return my_NTOHL(logout_req->ExpStatSN); } virtual std::optional get_response(scsi *const sd) override; }; @@ -764,8 +764,8 @@ class iscsi_pdu_taskman_request : public iscsi_pdu_bhs // taskman request 0x02 std::vector get() const override; uint32_t get_Itasktag() const { return taskman_req->Itasktag; } - uint32_t get_ExpStatSN() const { return NTOHL(taskman_req->ExpStatSN); } - uint32_t get_CmdSN() const { return NTOHL(taskman_req->CmdSN); } + uint32_t get_ExpStatSN() const { return my_NTOHL(taskman_req->ExpStatSN); } + uint32_t get_CmdSN() const { return my_NTOHL(taskman_req->CmdSN); } virtual std::optional get_response(scsi *const sd) override; }; diff --git a/utils.cpp b/utils.cpp index 2fe36ea..e76cfbc 100644 --- a/utils.cpp +++ b/utils.cpp @@ -241,8 +241,7 @@ void encode_lun(uint8_t *const target, const uint64_t lun_nr) } } -#if !defined(__APPLE__) -uint16_t HTONS(const uint16_t x) +uint16_t my_HTONS(const uint16_t x) { constexpr const uint16_t e = 1; if (*reinterpret_cast(&e)) // system is little endian @@ -251,25 +250,24 @@ uint16_t HTONS(const uint16_t x) return x; } -uint32_t HTONL(const uint32_t x) +uint32_t my_HTONL(const uint32_t x) { constexpr const uint16_t e = 1; if (*reinterpret_cast(&e)) // system is little endian - return (HTONS(x) << 16) | HTONS(x >> 16); + return (my_HTONS(x) << 16) | my_HTONS(x >> 16); return x; } -uint16_t NTOHS(const uint16_t x) +uint16_t my_NTOHS(const uint16_t x) { - return HTONS(x); + return my_HTONS(x); } -uint32_t NTOHL(const uint32_t x) +uint32_t my_NTOHL(const uint32_t x) { - return HTONL(x); + return my_HTONL(x); } -#endif uint8_t * duplicate_new(const void *const in, const size_t n) { diff --git a/utils.h b/utils.h index f9ec834..f8ac54b 100644 --- a/utils.h +++ b/utils.h @@ -36,16 +36,15 @@ uint8_t * duplicate_new(const void *const in, const size_t n); std::string to_hex(const uint8_t *const in, const size_t n); -#if !defined(__APPLE__) -uint32_t NTOHL(const uint32_t x); -uint16_t NTOHS(const uint16_t x); -uint32_t HTONL(const uint32_t x); -uint16_t HTONS(const uint16_t x); -#endif +// Apple has NTOHL/etc macros that confuse the build +uint32_t my_NTOHL(const uint32_t x); +uint16_t my_NTOHS(const uint16_t x); +uint32_t my_HTONL(const uint32_t x); +uint16_t my_HTONS(const uint16_t x); void socket_set_nodelay(const int fd); -#define HTONLL(x) ((1==htonl(1)) ? (x) : (((uint64_t)htonl((x) & 0xFFFFFFFFUL)) << 32) | htonl((uint32_t)((x) >> 32))) -#define NTOHLL(x) ((1==ntohl(1)) ? (x) : (((uint64_t)ntohl((x) & 0xFFFFFFFFUL)) << 32) | ntohl((uint32_t)((x) >> 32))) +#define my_HTONLL(x) ((1==htonl(1)) ? (x) : (((uint64_t)htonl((x) & 0xFFFFFFFFUL)) << 32) | htonl((uint32_t)((x) >> 32))) +#define my_NTOHLL(x) ((1==ntohl(1)) ? (x) : (((uint64_t)ntohl((x) & 0xFFFFFFFFUL)) << 32) | ntohl((uint32_t)((x) >> 32))) extern uint64_t running_since;