char* round_strreplace(const char* str, const char* orgToken, const char* repToken) { size_t strLen = round_strlen(str); if (strLen <= 0) { char* repStr = (char*)malloc(1); repStr[0] = '\0'; return repStr; } char* repStr = (char*)malloc(strLen + 1); size_t orgTokenLen = round_strlen(orgToken); if (orgTokenLen <= 0) { memcpy(repStr, str, (strLen + 1)); return repStr; } size_t repTokenLen = round_strlen(repToken); size_t tokenDiffLen = repTokenLen - orgTokenLen; ssize_t lastCopiedIdx = 0; ssize_t copiedLen = 0; size_t repStrLen = strLen; ssize_t orgTokenIdx = round_strstr(str, orgToken); while (0 <= orgTokenIdx) { if (tokenDiffLen != 0) { repStrLen += tokenDiffLen; repStr = (char*)realloc(repStr, repStrLen + 1); } size_t copyLen = (orgTokenIdx - lastCopiedIdx); if (0 < copyLen) { memcpy((repStr + copiedLen), (str + lastCopiedIdx), copyLen); } copiedLen += copyLen; repStr[copiedLen] = '\0'; memcpy((repStr + copiedLen), repToken, repTokenLen); copiedLen += repTokenLen; repStr[copiedLen] = '\0'; lastCopiedIdx = orgTokenIdx + orgTokenLen; orgTokenIdx = round_strstr((str + lastCopiedIdx), orgToken); if (0 < orgTokenIdx) { orgTokenIdx += lastCopiedIdx; } } size_t leftStrLen = (strLen - lastCopiedIdx); memcpy((repStr + copiedLen), (str + lastCopiedIdx), leftStrLen); copiedLen += leftStrLen; repStr[copiedLen] = '\0'; return repStr; }
static struct izo_upcall_hdr *upc_pack(__u32 opcode, int pathlen, char *path, char *fsetname, int reclen, char *rec, int *size) { struct izo_upcall_hdr *hdr; char *ptr; ENTRY; *size = sizeof(struct izo_upcall_hdr); if ( fsetname ) { *size += round_strlen(fsetname); } if ( path ) { *size += round_strlen(path); } if ( rec ) { *size += size_round(reclen); } PRESTO_ALLOC(hdr, *size); if (!hdr) { CERROR("intermezzo upcall: out of memory (opc %d)\n", opcode); EXIT; return NULL; } memset(hdr, 0, *size); ptr = (char *)hdr + sizeof(*hdr); /* XXX do we need fsuid ? */ hdr->u_len = *size; hdr->u_version = IZO_UPC_VERSION; hdr->u_opc = opcode; hdr->u_pid = current->pid; hdr->u_uid = current->fsuid; if (path) { /*XXX Robert: please review what len to pass in for NUL terminated strings */ hdr->u_pathlen = strlen(path); LOGL0(path, hdr->u_pathlen, ptr); } if (fsetname) { hdr->u_fsetlen = strlen(fsetname); LOGL0(fsetname, strlen(fsetname), ptr); } if (rec) { hdr->u_reclen = reclen; LOGL(rec, reclen, ptr); } EXIT; return hdr; }
const char* round_json_rpc_createrequeststring(const char* method, const char* params, char* buf, size_t bufSize) { snprintf( buf, bufSize, "{\"" ROUND_JSON_RPC_JSONRPC "\": \"" ROUND_JSON_RPC_VERSION "\"," "\"" ROUND_JSON_RPC_METHOD "\": \"%s\"," "\"" ROUND_JSON_RPC_PARAMS "\": %s, \"id\": 1}", method, ((0 < round_strlen(params)) ? params : "\"\"" )); return buf; }
ssize_t round_strrchr(const char* str, const char* chars, size_t nchars) { size_t strLen; ssize_t i, j; if (!str || chars == NULL) return -1; strLen = round_strlen(str); for (i = (strLen - 1); 0 <= i; i--) { for (j = 0; j < nchars; j++) { if (str[i] == chars[j]) return i; } } return -1; }
char* round_strltrim(char* str, char* delim, size_t ndelim) { size_t strLen; ssize_t i, j; strLen = round_strlen(str); for (i = 0; i < strLen; i++) { bool hasDelim = false; for (j = 0; j < ndelim; j++) { if (str[i] == delim[j]) { hasDelim = true; break; } } if (hasDelim == false) return (str + i); } return (str + strLen); }
char* round_strrtrim(char* str, char* delim, size_t ndelim) { size_t strLen; ssize_t i, j; strLen = round_strlen(str); for (i = (strLen - 1); 0 <= i; i--) { bool hasDelim = false; for (j = 0; j < ndelim; j++) { if (str[i] == delim[j]) { hasDelim = true; str[i] = '\0'; break; } } if (hasDelim == false) break; } return str; }
char* round_strtrimwhite(char* str) { size_t strLen; ssize_t i; strLen = round_strlen(str); if (strLen == 0) return str; for (i = (strLen - 1); 0 <= i; i--) { if (isspace(str[i])) { strLen--; } } for (i = 0; i < strLen; i++) { if (!isspace(str[i])) break; } if (i > 0) memmove(str, str + i, strLen - i); str[strLen] = 0; return str; }
char* round_strncat(char* str1, const char* str2, size_t cnt) { size_t str1Len; str1Len = round_strlen(str1); return round_strncpy((str1 + str1Len), str2, cnt); }
bool round_message_setstring(RoundMessage* msg, const char* str) { return round_message_setdata(msg, (byte*)str, (round_strlen(str) + 1)); }
bool round_remote_node_posthttpjsonrequest(RoundRemoteNode* node, const char* reqContent, RoundJSONObject** resObj, RoundError* err) { const char* remoteAddr = NULL; int remotePort = 0; if (!round_remote_node_getaddress(node, &remoteAddr) || !round_remote_node_getport(node, &remotePort)) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); return false; } mUpnpHttpRequest* httpReq = mupnp_http_request_new(); if (!httpReq) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); return false; } mupnp_http_request_setmethod(httpReq, MUPNP_HTTP_POST); mupnp_http_request_seturi(httpReq, ROUND_RPC_HTTP_ENDPOINT); mupnp_http_request_setcontent(httpReq, reqContent); mupnp_http_request_setcontentlength(httpReq, mupnp_strlen(reqContent)); mUpnpHttpResponse* httpRes = mupnp_http_request_post(httpReq, remoteAddr, remotePort); if (!httpRes) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_BAD_DESTINATION, err); mupnp_http_request_delete(httpReq); return false; } const char* resContent = mupnp_http_response_getcontent(httpRes); if (round_strlen(resContent) <= 0) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_BAD_DESTINATION, err); mupnp_http_request_delete(httpReq); return false; } // Set JSON Response RoundJSON* json = round_json_new(); if (!json) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); mupnp_http_request_delete(httpReq); } *resObj = NULL; if (round_json_parse(json, resContent, err)) { *resObj = round_json_poprootobject(json); } round_json_delete(json); // Set Error Response if (!mupnp_http_response_issuccessful(httpRes)) { if (*resObj) { RoundJSONObject* errObj; if (round_json_rpc_geterror(*resObj, &errObj)) { round_error_setjsonrpcerror(err, errObj); } } } return true; }
bool round_local_node_execrequest(RoundLocalNode* node, RoundJSONObject* reqObj, RoundJSONObject** resObj, RoundError* err) { // Set Response *resObj = round_json_rpc_response_new(); if (!resObj) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); return false; } // Set id and timestamp round_json_rpc_setrequestid(*resObj, reqObj); round_json_rpc_settimestamp(*resObj, round_local_node_getclock(node)); // Check node if (!node) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); round_json_rpc_seterror(*resObj, err); return false; } // Check request if (!round_json_object_ismap(reqObj)) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_REQUEST); round_json_rpc_seterror(*resObj, err); return false; } /* // Check dest if (!nodeReq->isDestValid()) { setError(RPC::JSON::ErrorCodeInvalidParams, error); return false; } bool isDestHash = nodeReq->isDestHash(); if (isDestHash) { std::string nodeHash; if (nodeReq->getDest(&nodeHash)) { NodeGraph *nodeGraph = getNodeGraph(); if (!nodeGraph->isHandleNode(this, nodeHash)) { setError(RPC::JSON::ErrorCodeMovedPermanently, error); return false; } } } // Exec Method (One node) bool isDestOne = nodeReq->isDestOne(); if (isDestOne) { return execMethod(nodeReq, nodeRes, error); } // Exec Method (Multi node) JSONArray *batchArray = new JSONArray(); nodeRes->setResult(batchArray); Error thisNodeError; NodeResponse *thisNodeRes = new NodeResponse(); execMethod(nodeReq, thisNodeRes, &thisNodeError); thisNodeRes->setDest(this); batchArray->add(thisNodeRes); NodeList otherNodes; if (nodeReq->isDestAll()) { getAllOtherNodes(&otherNodes); } else if (nodeReq->hasQuorum()) { size_t quorum; if (nodeReq->getQuorum(&quorum)) { getQuorumNodes(&otherNodes, quorum); } } for (NodeList::iterator node = otherNodes.begin(); node != otherNodes.end(); node++) { Error otherNodeError; NodeResponse *otherNodeRes = new NodeResponse(); (*node)->postMessage(nodeReq, otherNodeRes, &otherNodeError); otherNodeRes->setDest((*node)); batchArray->add(otherNodeRes); } */ // Exec Message const char* method = NULL; if (!round_json_rpc_getmethod(reqObj, &method) && (0 < round_strlen(method))) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_REQUEST); round_json_rpc_seterror(*resObj, err); return false; } const char* params = NULL; round_json_rpc_getparamsstring(reqObj, ¶ms); RoundJSONObject* resultObj = NULL; bool isMethodExecuted = round_method_manager_execmethod(node->methodMgr, method, params, &resultObj, err); if (isMethodExecuted) { if (resultObj) { round_json_rpc_setresult(*resObj, resultObj); round_json_object_delete(resultObj); } } else { round_json_rpc_seterror(*resObj, err); } /* bool isMethodExecuted = false; bool isMethodSuccess = false; if (isAliasMethod(name)) { isMethodExecuted = true; isMethodSuccess = execAliasMethod(nodeReq, nodeRes, error); } if (!isMethodExecuted) { setError(ROUND_RPC_ERROR_CODE_METHOD_NOT_FOUND, error); return false; } if (!isMethodSuccess) return false; if (!hasRoute(name)) { return true; } NodeResponse routeNodeRes; if (!execRoute(name, nodeRes, &routeNodeRes, error)) { return false; } nodeRes->set(&routeNodeRes); */ return isMethodExecuted; }