133 #include "../core/stdafx.hpp"
141 #include "../core/OTAssetContract.hpp"
142 #include "../core/util/OTDataCheck.hpp"
143 #include "../core/crypto/OTEnvelope.hpp"
144 #include "../core/OTLog.hpp"
145 #include "../core/OTMessage.hpp"
146 #include "../core/OTPayload.hpp"
147 #include "../core/OTPseudonym.hpp"
148 #include "../core/OTServerContract.hpp"
152 #include <winsock2.h>
153 #pragma comment(lib, "ws2_32.lib")
155 #include <netinet/in.h>
167 uint32_t lSize = thePayload.
GetSize();
171 theCMD.
fields.
type_id = (nTypeID > 0) ? static_cast<BYTE>(nTypeID) :
'\0';
179 int32_t nChecksum = byChecksum;
182 <<
" -- CMD TYPE: " << nTypeID <<
" -- CMD NUM: " << nCmdID
183 <<
" -- (followed by 2 bytes of filler)\n"
184 "PAYLOAD SIZE: " << lSize <<
" -- CHECKSUM: " << nChecksum
187 otLog5 <<
"First 9 bytes are:";
188 for (
int i = 0; i < 9; i++) {
194 bool OTServerConnection::s_bInitialized =
false;
196 void OTServerConnection::Initialize()
199 if (s_bInitialized) {
204 s_bInitialized =
true;
225 if (
nullptr == pCallback) {
226 otErr << __FUNCTION__ <<
": pCallback is nullptr";
252 m_pServerContract = &theServerContract;
253 m_pCallback = pCallback;
267 int64_t lNewRequestNumber)
const
269 if (m_pNym && m_pServerContract) {
270 otOut <<
"Received new request number from the server: "
271 << lNewRequestNumber <<
". Updating Nym records...\n";
278 otErr <<
"Expected m_pNym or m_pServerContract to be not null in "
279 "OTServerConnection::OnServerResponseToGetRequestNumber.\n";
285 if (m_pServerContract) {
302 m_pCallback =
nullptr;
305 m_pServerContract =
nullptr;
306 m_pWallet = &theWallet;
307 m_pClient = &theClient;
318 bool bSuccess =
false;
321 <<
"\n****************************************************************"
323 "===> Processing header from server response.\nFirst 9 bytes are: "
324 << theCMD.
buf[0] <<
" " << theCMD.
buf[1] <<
" " << theCMD.
buf[2] <<
" "
325 << theCMD.
buf[3] <<
" " << theCMD.
buf[4] <<
" " << theCMD.
buf[5] <<
" "
326 << theCMD.
buf[6] <<
" " << theCMD.
buf[7] <<
" " << theCMD.
buf[8]
330 otWarn <<
"Received a Type 1 Command...\n";
333 otWarn <<
"Checksum is valid! Processing payload.\n";
339 otErr <<
"Invalid checksum - Type 1 Command\n";
344 otErr <<
"Unknown command type: " << nCMDType <<
"\n";
350 if (bSuccess ==
false) {
351 int32_t err = 0, nread = 0;
358 if (0 == err || SOCKET_ERROR == err)
367 otErr <<
"Transmission error--therefore have flushed the pipe, "
368 "discarding " << nread <<
" bytes.\n";
392 for (nread = 0; nread < theCMD.
fields.
size; nread += err) {
395 if (0 == err || SOCKET_ERROR == 0)
406 otWarn <<
"Received TYPE 1 CMD 1, an OTMessage.\n";
409 otLog3 <<
"Received TYPE 1 CMD 2, an OTEnvelope containing an "
417 otErr <<
"(The payload was a 0 size.)\n";
421 otErr <<
"Number of bytes read (" << nread
422 <<
") did NOT match size in header (" << theCMD.
fields.
size
433 otLog4 <<
"Successfully retrieved payload message...\n";
436 otLog4 <<
"Successfully parsed payload message.\n";
439 (
nullptr != m_pServerContract)
443 if (m_pServerContract && pServerNym)
448 otOut <<
"VERIFIED -- this message was signed "
452 otOut <<
"Signature verification failed on "
453 "this message, proportedly from the "
459 otOut <<
"No server contract loaded, or could not "
460 "load public key from server contract.\n";
467 otErr <<
"Error parsing message.\n";
473 otErr <<
"Error retrieving message from payload.\n";
483 otWarn <<
"===> Received encrypted envelope. (Server "
484 "reply.) Decrypting...\n";
489 if (m_pNym && theEnvelope.
Open(*m_pNym, strEnvelopeContents)) {
495 strEnvelopeContents)) {
496 otLog4 <<
"Success decrypting the message out of "
497 "the envelope and parsing it.\n";
500 (
nullptr != m_pServerContract)
504 if (m_pServerContract && pServerNym)
509 otOut <<
"VERIFIED -- this message was "
510 "signed by the Server.\n";
513 otOut <<
"Signature verification failed on "
514 "this message, purportedly from "
520 otErr <<
"No server contract loaded, or could not "
521 "load public key from server contract.\n";
528 otErr <<
"Error loading message from envelope contents.\n";
533 otErr <<
"Unable to open envelope.\n";
538 otErr <<
"Error retrieving message from payload.\n";
543 otErr <<
"Error retrieving message from payload. Unknown type.\n";
550 if (m_pNym && m_pWallet && theMessage.
SignContract(*m_pNym) &&
576 if (m_pServerContract &&
583 theEnvelope.
Seal(*pServerNym, strEnvelopeContents);
607 <<
" message via ZMQ... Request number: "
610 (*m_pCallback)(*m_pServerContract, theEnvelope);
613 <<
" message (and hopefully receiving "
628 bool bSendCommand =
false;
629 bool bSendPayload =
false;
633 bool bHandledIt =
false;
663 "COMMAND to the server...)\n CHECKSUM: " << nChecksum <<
"\n";
666 else if (buf[0] ==
'2') {
675 otOut <<
"(User has instructed to send a size "
677 "command** to the server...)\n";
681 else if (buf[0] ==
'3') {
682 otOut <<
"(User has instructed to create a signed XML message "
683 "and send it to the server...)\n";
712 if (!bHandledIt && m_pNym && m_pServerContract) {
715 otOut <<
"(User has instructed to send a checkServerID "
716 "command to the server...)\n";
722 *m_pServerContract,
nullptr) >
729 otErr <<
"Error processing checkServerID command in "
730 "ProcessMessage: " << buf[0] <<
"\n";
734 else if (buf[0] ==
'r') {
735 otOut <<
"(User has instructed to send a createUserAccount "
736 "command to the server...)\n";
742 *m_pServerContract,
nullptr) >
749 otErr <<
"Error processing createUserAccount command in "
750 "ProcessMessage: " << buf[0] <<
"\n";
758 else if (buf[0] ==
'u') {
759 otOut <<
"(User has instructed to send a checkUser command "
760 "to the server...)\n";
765 *m_pNym, *m_pServerContract,
773 otErr <<
"Error processing checkUser command in "
774 "ProcessMessage: " << buf[0] <<
"\n";
778 else if (buf[0] ==
'a') {
779 otOut <<
"(User has instructed to send a createAccount "
780 "command to the server...)\n";
793 otErr <<
"Error processing createAccount command in "
794 "ProcessMessage: " << buf[0] <<
"\n";
798 else if (!strcmp(buf,
"issue\n")) {
799 otOut <<
"(User has instructed to send an issueAssetType "
800 "command to the server...)\n";
806 *m_pServerContract,
nullptr) >
813 otErr <<
"Error processing issueAssetType command in "
814 "ProcessMessage: " << buf[0] <<
"\n";
818 else if (!strcmp(buf,
"basket\n")) {
819 otOut <<
"(User has instructed to send an issueBasket "
820 "command to the server...)\n";
825 *m_pNym, *m_pServerContract,
833 otErr <<
"Error processing issueBasket command in "
834 "ProcessMessage: " << buf[0] <<
"\n";
838 else if (!strcmp(buf,
"exchange\n")) {
839 otOut <<
"(User has instructed to send an exchangeBasket "
840 "command to the server...)\n";
846 *m_pServerContract,
nullptr) >
853 otErr <<
"Error processing exchangeBasket command in "
854 "ProcessMessage: " << buf[0] <<
"\n";
858 else if (!strcmp(buf,
"offer\n")) {
859 otOut <<
"(User has instructed to send a marketOffer "
860 "command to the server...)\n";
865 *m_pNym, *m_pServerContract,
873 otErr <<
"Error processing marketOffer command in "
874 "ProcessMessage: " << buf[0] <<
"\n";
878 else if (!strcmp(buf,
"setassetname\n")) {
879 otOut <<
"(User has instructed to set an Asset Contract's "
880 "client-side name...)\n";
895 else if (!strcmp(buf,
"setservername\n")) {
896 otOut <<
"(User has instructed to set a Server Contract's "
897 "client-side name...)\n";
903 *m_pServerContract,
nullptr) >
912 else if (!strcmp(buf,
"setnymname\n")) {
914 <<
"(User has instructed to set a Nym's client-side name...)\n";
919 *m_pNym, *m_pServerContract,
929 else if (!strcmp(buf,
"setaccountname\n")) {
930 otOut <<
"(User wants to set an Asset Account's client-side "
937 *m_pServerContract,
nullptr) >
946 else if (buf[0] ==
's') {
947 otOut <<
"(User has instructed to send a sendUserMessage "
948 "command to the server...)\n";
954 *m_pServerContract,
nullptr) >
961 otErr <<
"Error processing sendUserMessage command in "
962 "ProcessMessage: " << buf[0] <<
"\n";
966 else if (buf[0] ==
'y') {
967 otOut <<
"(User has instructed to send a getNymbox command "
968 "to the server...)\n";
973 *m_pNym, *m_pServerContract,
981 otErr <<
"Error processing getNymbox command in "
982 "ProcessMessage: " << buf[0] <<
"\n";
986 else if (buf[0] ==
'i') {
987 otOut <<
"(User has instructed to send a getInbox command "
988 "to the server...)\n";
1000 otErr <<
"Error processing getInbox command in ProcessMessage: "
1005 else if (buf[0] ==
'o') {
1006 otOut <<
"(User has instructed to send a getOutbox command "
1007 "to the server...)\n";
1012 *m_pNym, *m_pServerContract,
1016 bSendCommand =
true;
1017 bSendPayload =
true;
1020 otErr <<
"Error processing getOutbox command in "
1021 "ProcessMessage: " << buf[0] <<
"\n";
1025 else if (buf[0] ==
'q') {
1026 otOut <<
"User has instructed to deposit a cheque...\n";
1031 theMessage, *m_pNym,
1032 *m_pServerContract,
nullptr) >
1035 bSendCommand =
true;
1036 bSendPayload =
true;
1039 otErr <<
"Error processing deposit cheque command in "
1040 "ProcessMessage: " << buf[0] <<
"\n";
1044 else if (buf[0] ==
'v') {
1045 otOut <<
"User has instructed to withdraw a voucher (like "
1046 "a cashier's cheque)...\n";
1051 theMessage, *m_pNym,
1052 *m_pServerContract,
nullptr) >
1055 bSendCommand =
true;
1056 bSendPayload =
true;
1059 otErr <<
"Error processing withdraw voucher command in "
1060 "ProcessMessage: " << buf[0] <<
"\n";
1064 else if (buf[0] ==
'w') {
1065 otOut <<
"(User has instructed to withdraw cash...)\n";
1070 theMessage, *m_pNym,
1071 *m_pServerContract,
nullptr) >
1074 bSendCommand =
true;
1075 bSendPayload =
true;
1078 otErr <<
"Error processing withdraw command in ProcessMessage: "
1083 else if (buf[0] ==
'd') {
1084 otOut <<
"(User has instructed to deposit cash tokens...)\n";
1089 theMessage, *m_pNym,
1090 *m_pServerContract,
nullptr) >
1093 bSendCommand =
true;
1094 bSendPayload =
true;
1097 otErr <<
"Error processing deposit command in ProcessMessage: "
1102 else if (!strcmp(buf,
"plan\n")) {
1103 otOut <<
"User has instructed to activate a payment plan...\n";
1108 *m_pNym, *m_pServerContract,
1112 bSendCommand =
true;
1113 bSendPayload =
true;
1116 otErr <<
"Error processing payment plan command in "
1117 "ProcessMessage: " << buf[0] <<
"\n";
1121 else if (buf[0] ==
'p') {
1122 otOut <<
"(User has instructed to deposit a purse "
1123 "containing cash...)\n";
1128 theMessage, *m_pNym,
1129 *m_pServerContract,
nullptr) >
1132 bSendCommand =
true;
1133 bSendPayload =
true;
1136 otErr <<
"Error processing deposit command in ProcessMessage: "
1141 else if (!strcmp(buf,
"test\n")) {
1142 otOut <<
"(User has instructed to perform a test...)\n";
1148 "Well well well, this is just a little bit of "
1149 "plaintext.\nNotice there are NO NEWLINES at the start.\n"
1150 "I'm just trying to make it as long as i can, so that\nI "
1151 "can test the envelope and armor functionality.\n");
1153 otOut <<
"MESSAGE:\n------>" << strMessage <<
"<--------\n";
1157 otOut <<
"ASCII ARMOR:\n------>" << ascMessage <<
"<--------\n";
1160 theEnvelope.
Seal(*m_pNym, strMessage);
1162 ascMessage.Release();
1166 otOut <<
"ENCRYPTED PLAIN TEXT AND THEN ASCII "
1167 "ARMOR:\n------>" << ascMessage <<
"<--------\n";
1169 strMessage.Release();
1172 the2Envelope.
Open(*m_pNym, strMessage);
1174 otOut <<
"DECRYPTED PLAIN TEXT:\n------>" << strMessage
1178 the3Envelope.
Seal(*m_pNym, strMessage.Get());
1180 ascMessage.Release();
1184 otOut <<
"RE-ENCRYPTED PLAIN TEXT AND THEN ASCII "
1185 "ARMOR:\n------>" << ascMessage <<
"<--------\n";
1187 strMessage.Release();
1190 the4Envelope.
Open(*m_pNym, strMessage);
1192 otOut <<
"RE-DECRYPTED PLAIN TEXT:\n------>" << strMessage
1196 the5Envelope.
Seal(*m_pNym, strMessage.Get());
1198 ascMessage.Release();
1202 otOut <<
"RE-RE-ENCRYPTED PLAIN TEXT AND THEN ASCII "
1203 "ARMOR:\n------>" << ascMessage <<
"<--------\n";
1205 strMessage.Release();
1208 the6Envelope.
Open(*m_pNym, strMessage);
1210 otOut <<
"RE-RE-DECRYPTED PLAIN TEXT:\n------>" << strMessage
1217 else if (!strcmp(buf,
"get\n")) {
1218 otOut <<
"(User has instructed to send a getAccount "
1219 "command to the server...)\n";
1224 *m_pNym, *m_pServerContract,
1228 bSendCommand =
true;
1229 bSendPayload =
true;
1232 otErr <<
"Error processing getAccount command in "
1233 "ProcessMessage: " << buf[0] <<
"\n";
1237 else if (!strcmp(buf,
"getcontract\n")) {
1238 otOut <<
"(User has instructed to send a getContract "
1239 "command to the server...)\n";
1244 *m_pNym, *m_pServerContract,
1248 bSendCommand =
true;
1249 bSendPayload =
true;
1252 otErr <<
"Error processing getContract command in "
1253 "ProcessMessage: " << buf[0] <<
"\n";
1257 else if (!strcmp(buf,
"signcontract\n")) {
1258 otOut <<
"Is the contract a server contract, or an asset "
1261 strContractType.
OTfgets(std::cin);
1263 char cContractType =
's';
1264 bool bIsAssetContract = strContractType.
At(0, cContractType);
1266 if (bIsAssetContract) {
1267 if (
'S' == cContractType ||
's' == cContractType)
1268 bIsAssetContract =
false;
1270 otOut <<
"Is the contract properly escaped already? (If "
1271 "escaped, all lines beginning with ----- will "
1272 "instead appear as - ----- ) [y\n]: ";
1282 bool bEscaped = strEscape.
At(0, cEscape);
1285 if (
'N' == cEscape ||
'n' == cEscape) bEscaped =
false;
1288 otOut <<
"Please enter an unsigned asset contract; "
1289 "terminate with ~ on a new line:\n> ";
1291 char decode_buffer[200];
1295 decode_buffer[0] = 0;
1298 fgets(decode_buffer,
sizeof(decode_buffer) - 1, stdin)) &&
1299 (decode_buffer[0] !=
'~')) {
1300 if (!bEscaped && decode_buffer[0] ==
'-') {
1310 }
while (decode_buffer[0] !=
'~');
1317 ?
dynamic_cast<OTContract*
>(&theAssetContract)
1318 : dynamic_cast<OTContract*>(&theServerContract);
1326 otOut <<
".\n..\n...\n....\n.....\n......\n.......\n........\n....."
1327 "....\n\nNEW CONTRACT:\n\n" << strContract <<
"\n";
1331 else if (!strcmp(buf,
"getmint\n")) {
1332 otOut <<
"(User has instructed to send a getMint command "
1333 "to the server...)\n";
1341 bSendCommand =
true;
1342 bSendPayload =
true;
1345 otErr <<
"Error processing getMint command in ProcessMessage: "
1350 else if (buf[0] ==
't') {
1351 otOut <<
"(User has instructed to send a Transfer command "
1352 "(Notarize Transactions) to the server...)\n";
1357 theMessage, *m_pNym,
1358 *m_pServerContract,
nullptr) >
1361 bSendCommand =
true;
1362 bSendPayload =
true;
1365 otErr <<
"Error processing notarizeTransactions command "
1366 "in ProcessMessage: " << buf[0] <<
"\n";
1370 else if (buf[0] ==
'g') {
1371 otOut <<
"(User has instructed to send a getRequest "
1372 "command to the server...)\n";
1377 *m_pNym, *m_pServerContract,
1381 bSendCommand =
true;
1382 bSendPayload =
true;
1385 otErr <<
"Error processing getRequest command in "
1386 "ProcessMessage: " << buf[0] <<
"\n";
1390 else if (buf[0] ==
'n') {
1407 int64_t lTransactionNumber =
1408 ((strlen(buf) > 2) ? atol(&(buf[2])) : 0);
1410 if (lTransactionNumber > 0) {
1418 otOut <<
"Transaction number " << lTransactionNumber
1419 <<
" added to both lists "
1420 "(on client side.)\n";
1423 otOut <<
"(User has instructed to send a "
1424 "getTransactionNum command to the "
1430 theMessage, *m_pNym,
1431 *m_pServerContract,
nullptr) >
1434 bSendCommand =
true;
1435 bSendPayload =
true;
1438 otErr <<
"Error processing getTransactionNum command "
1439 "in ProcessMessage: " << buf[0] <<
"\n";
1450 else if (!bHandledIt) {
1454 if (bSendCommand && bSendPayload) {
OTLOG_IMPORT OTLogStream otLog4
bool SetFocus(OTPseudonym &theNym, OTServerContract &theServerContract, TransportCallback *pCallback)
virtual EXPORT bool SignContract(const OTPseudonym &theNym, const OTPasswordData *pPWData=nullptr)
virtual EXPORT bool VerifySignature(const OTPseudonym &theNym, const OTPasswordData *pPWData=nullptr) const
EXPORT bool GetEnvelope(OTEnvelope &theEnvelope) const
EXPORT bool SaveContract()
EXPORT bool ParseRawFile()
EXPORT OT_BYTE CalcChecksum(OT_BYTE *buffer, uint32_t size)
EXPORT bool SetMessagePayload(const OTMessage &theMessage)
virtual EXPORT bool CreateContract(const OTString &strContract, const OTPseudonym &theSigner)
EXPORT void OTfgets(std::istream &ofs)
#define OT_CMD_HEADER_SIZE
OTLOG_IMPORT OTLogStream otOut
OTLOG_IMPORT OTLogStream otLog3
EXPORT bool SaveContractRaw(OTString &strOutput) const
EXPORT int32_t ProcessUserCommand(OT_CLIENT_CMD_TYPE requestedCommand, OTMessage &theMessage, OTPseudonym &theNym, const OTServerContract &theServer, const OTAccount *pAccount=nullptr, int64_t lTransactionAmount=0, OTAssetContract *pMyAssetContract=nullptr, const OTIdentifier *pHisNymID=nullptr, const OTIdentifier *pHisAcctID=nullptr)
EXPORT bool Open(const OTPseudonym &theRecipient, OTString &theOutput, const OTPasswordData *pPWData=nullptr)
bool SignAndSend(OTMessage &theMessage) const
EXPORT void Concatenate(const char *arg,...)
EXPORT bool Seal(const OTPseudonym &theRecipient, const OTString &theInput)
bool ProcessType1Cmd(u_header &theCMD, OTMessage &theServerReply) const
EXPORT bool SignContractWithFirstNymOnList(OTContract &theContract)
void SetupHeader(u_header &theCMD, int32_t nTypeID, int32_t nCmdID, OTPayload &thePayload)
EXPORT const OTPseudonym * GetContractPublicNym() const
EXPORT bool AddTransactionNum(OTPseudonym &SIGNER_NYM, const OTString &strServerID, int64_t lTransNum, bool bSave)
EXPORT bool GetAsciiArmoredData(OTASCIIArmor &theArmoredText, bool bLineBreaks=true) const
void ProcessMessageOut(const char *buf, const int32_t *pnExpectReply)
EXPORT OT_BOOL IsChecksumValid(OT_BYTE *buffer, uint32_t size)
EXPORT void SetPayloadSize(uint32_t lNewSize)
EXPORT bool GetMessagePayload(OTMessage &theMessage) const
virtual EXPORT void GetIdentifier(OTIdentifier &theIdentifier) const
EXPORT bool At(uint32_t index, char &c) const
bool GetServerID(OTIdentifier &theID) const
OTLOG_IMPORT OTLogStream otWarn
OTLOG_IMPORT OTLogStream otErr
void OnServerResponseToGetRequestNumber(int64_t lNewRequestNumber) const
EXPORT void OnUpdateRequestNum(OTPseudonym &SIGNER_NYM, const OTString &strServerID, int64_t lNewRequestNumber)
EXPORT bool LoadContractFromString(const OTString &theStr)
EXPORT bool SetEnvelope(const OTEnvelope &theEnvelope)
OTServerConnection(OTWallet &theWallet, OTClient &theClient)
virtual EXPORT void Release()
bool ProcessReply(u_header &theCMD, OTMessage &theServerReply)
OTLOG_IMPORT OTLogStream otLog5