int __h3c_v8500_recv(pvp_uthttp put, char *utbuf, int *pack_len, int directon) { const static char FLG_RTSP_SETUP[] = "SETUP rtsp://"; char *ptr = NULL; if (directon == DO_REPLY) return 1; if (put->dport == 554) { puts("---- dport is 554 -------"); if (memcmp(utbuf, FLG_RTSP_SETUP, sizeof(FLG_RTSP_SETUP)-1) == 0) { oss_malloc(&ptr, *pack_len); memcpy(ptr, utbuf, *pack_len); jincheng_h3c_record_rtsp_request(put, &ptr, (u32*)pack_len); memcpy(utbuf, ptr, *pack_len); oss_free(&ptr); } } return 1; }
static int oss_pause (void) { if (state == OUTPUT_STATE_STOPPED) { return -1; } // set pause state oss_free(); state = OUTPUT_STATE_PAUSED; return 0; }
int main(int argc, char **argv) { const static char annotate = '#'; char *ptr_annotate = NULL; char path[128] = {0}; char line[128] = {0}; char *buf = NULL; char *ptr = NULL; size_t buf_sz = 0; pid_t pid = -1; // int len = 0; if ((argc == 0) && (strcmp(argv[1], "-d") != 0)) { if ((pid = create_daemon()) < 0) _exit(-1); else if (pid > 0) _exit(1); } openlog("vp-promon", LOG_CONS|LOG_PID|LOG_PERROR, LOG_USER); sprintf(path, "%s/promon.conf", PATH_CFG); if ( ! t_read_full_file(path, &buf, &buf_sz, 0)) { logdbg_fmt("Open configure file failed!(%s)\n", path); return -1; } ptr = buf; while ((ptr = loop_line_from_buf(ptr, line, sizeof(line))) != NULL) { if ((ptr_annotate = strchr(line, annotate)) != NULL) *ptr_annotate = '\0'; trimleft(line); /* if (line[strlen(line)-1] == '&') line[strlen(line)] = '\0'; */ if (line[0] == '\0') continue ; if ((pid = create_daemon()) < 0) break; else if (pid == 0) do_promon(line); } oss_free(&buf); return 0; }
int process_keda_protocol(char **ut_buf, u32 *pack_len) { char *ptr_one_proto = *ut_buf; u16 len_one_proto = 0; u16 len_msg = 0; char *ptr_keda_proto = NULL; u32 len_keda_proto = 0; puts("------------------ Source Data --------------------------------------"); t_disbuf(*ut_buf, *pack_len); puts("---------------------------------------------------------------------"); do { //ptr_one_proto = ptr_one_proto + len_last_proto; memcpy(&len_msg, ptr_one_proto + OFFSET_LEN_MSG, 2); len_msg = ntohs(len_msg); len_one_proto = len_msg + LEN_PROTO_HEAD; //printf("len_protocol: %d\n", len_one_proto); len_keda_proto = len_one_proto; oss_malloc(&ptr_keda_proto, len_keda_proto); memcpy(ptr_keda_proto, ptr_one_proto, len_keda_proto); //sleep (1); if (process_keda_one_protocol(&ptr_keda_proto, &len_keda_proto) < 0) return -1; //puts("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKkk"); //t_disbuf(ptr_keda_proto, len_keda_proto); //puts("--------------------------------------"); oss_free(&ptr_keda_proto); len_keda_proto = 0; ptr_one_proto += len_one_proto; } while (ptr_one_proto < *ut_buf + *pack_len); oss_free(ut_buf); *pack_len = 0; return 1; }
bool gl_set_data(const char *name, char *data, int len) { gldata *gld = NULL; char *tmp_data = NULL; if ((name == NULL) || (data == NULL) || (len < 0)) { logdbg_fmt("gl_set_data: arg error! nameptr:[%p], dataptr:[%p], len:[%d]\n", name,data,len); return false; } // data using this name, has already exist. if (gl_get_data(name) != NULL) { logdbg_fmt("gl_set_data: name:[%s] already been used!", name); return false; } //if ((gld = (gldata*)malloc(sizeof(gldata))) == NULL) if (oss_malloc(&gld, sizeof(gldata)) < 0) { logdbg_out("gl_set_data: malloc gld struct failed!"); return false; } if (oss_malloc(&tmp_data, len) < 0) { oss_free(&gld); logdbg_fmt("gl_set_data: malloc data buffer failed! size:[%d]", len); return false; } strncpy(gld->name, name, sizeof(gld->name)-1); memcpy(tmp_data, data, len); gld->data = tmp_data; gld->len = len; puts("1 >>>>>>>>>>>>>>>>>>>>>>"); t_disbuf(gld->data, gld->len); puts("2 >>>>>>>>>>>>>>>>>>>>>>"); pthread_mutex_lock(&s_gldata_mutex); gld->next = g_gl_data; g_gl_data = gld; pthread_mutex_unlock(&s_gldata_mutex); return true; }
bool tp_set_data(const char *name, const char *data, int len) { tprivate *tpriv = NULL; tdata *thread_data = NULL; char *tmp_data = NULL; if ((name == NULL) || (data == NULL) || (len < 0)) { logdbg_fmt("tp_set_data: arg error! nameptr:[%p], dataptr:[%p], len:[%d]\n", name,data,len); return false; } if ((tpriv = current_tprivate()) == NULL) { logdbg_out("tp_set_data: get current private data flg failed!"); return false; } // data using this name, has already exist. if (tp_getdata_from_priv(tpriv, name) != NULL) { logdbg_fmt("tp_set_data: name:[%s] already been used!", name); return false; } if (oss_malloc(&thread_data, sizeof(tdata)) < 0) return false; //if ((thread_data = (tdata*)malloc(sizeof(tdata))) == NULL) //return false; //if ((tmp_data = (char*)malloc(len)) == NULL) if (oss_malloc(&tmp_data, len) < 0) { oss_free(&thread_data); return false; } strncpy(thread_data->name, name, sizeof(thread_data->name)-1); memcpy(tmp_data, data, len); thread_data->data = tmp_data; thread_data->len = len; thread_data->next = tpriv->t_data; tpriv->t_data = thread_data; return true; }
int str2hex(char **str, u32 *len_str) { if ((str == NULL) || (*str == NULL) || (len_str == NULL)) { printf("str2hex: arg error! %p, %p, %p\n", str, *str, len_str); return -1; } if ((*len_str % 2) != 0) { printf("str2hex: length error!\n"); return -1; } u32 len_hex = *len_str/2; char *pHex = NULL; char hex = 0; u32 i = 0; char tmp[4] = {0}; char *ptr = NULL; oss_malloc(&pHex, len_hex); ptr = pHex; for (i = 0; i < len_hex; ++i) { memset(tmp, 0, sizeof(tmp)); memcpy(tmp, *str + i * 2, 2); hex = Hex2Int(tmp); memcpy(ptr, &hex, 1); ++ptr; } oss_free(str); *str = pHex; *len_str = len_hex; return 1; }
static int oss_stop (void) { state = OUTPUT_STATE_STOPPED; deadbeef->streamer_reset (1); return oss_free(); }
int do_sip_reply_invite(pvp_uthttp put, char **ut_buf, u32 *pack_len) { const static char FLG_INIP4[] = "IN IP4 "; const static char FLG_VPORT[] = "m=video "; char cli_ip[16] = {0}; // outer server ip char *pstr = NULL; char sport[8] = {0}; /*Source port*/ char r_src[64] = {0}; char r_dst[64] = {0}; char lip[16] = {0}; char dip[16] = {0}; char *ptr = NULL; u16 nlport = 0; char call_id[128] = {0}; puts("******** 1"); if ((pstr = strstr(*ut_buf, FLG_VPORT)) != NULL) { puts("******** 2"); puts(*ut_buf); pstr += sizeof(FLG_VPORT)-1; sscanf(pstr, "%[0-9]", sport); if ((nlport = pplist_getidle_port_x()) == 0) { logwar_out("get idle port failed!"); return -1; } puts("******** 3"); // replace ip if ((ptr = strnstr(*ut_buf, FLG_INIP4, *pack_len, true)) != NULL) { ptr += sizeof(FLG_INIP4)-1; sscanf(ptr, "%[0-9.]", dip); inet_ultoa(__gg.outer_addr, lip); sprintf(r_src, "%s%s", FLG_INIP4, dip); //sprintf(r_dst, "%s%s", FLG_INIP4, g_value[L_AUTHIP]); sprintf(r_dst, "%s%s", FLG_INIP4, lip); strreplace_pos(NULL, NULL, ut_buf, r_src, r_dst, -1, pack_len); } puts("******** 4"); // replace port sprintf(r_src, "%s%s", FLG_VPORT, sport); sprintf(r_dst, "%s%d", FLG_VPORT, nlport); strreplace_pos(NULL,NULL, ut_buf, r_src, r_dst, 1, pack_len); inet_ultoa(put->src_ip, cli_ip); // replace invite ip replace_cmd_ip_port(ut_buf, pack_len, cli_ip, put->src_port); puts("******** 5"); // start proxy char camera_id[32] = {0}; int ret = -1; clivlist *pcvn = NULL; if (oss_malloc(&pcvn, sizeof(clivlist)) < 0) return -1; get_virtual_cameraid(camera_id); strcpy(pcvn->visit_user, cli_ip); strcpy(pcvn->camera_id, camera_id); //if (is_tms()) //pcvn->lip = __gg.inner_addr; //else pcvn->lip = __gg.outer_addr; pcvn->dip = inet_atoul(dip); pcvn->lvport = nlport; pcvn->dvport = atoi(sport); pcvn->platform_id = a_get_pmid(); // pcvn->vstream_tout = put->session_tout; // // 信令超时时间需要很长,如果视频流使用相同超时时间,会有大量已经使用完的视频进程逗留在系统中 pcvn->vstream_tout = 60; pcvn->bind_video_port = nlport; puts("******** 6"); if (get_call_id(*ut_buf, *pack_len, call_id, sizeof(call_id)) == NULL) logdbg_out("获取Call id 失败!"); else if ( ! gl_set_data(call_id, (char*)&nlport, sizeof(nlport))) logdbg_out("记录Call id 失败!"); ret = __start_vs_udp_proxy(pcvn, true, __gg.ferry_port + 1); oss_free(&pcvn); //sip_replace_contact(ut_buf, pack_len, g_value[L_AUTHIP], 5060); //do_sip_reply_replace_by_key(put, SIP_FLAG_CONTACT, ut_buf, pack_len); } return 1; }
int memreplace_pos(char *pos_b, char *pos_e, char **content, u32 *len, int times, char *src, int nsrc, char *dst, int ndst) { find_mem fmem; char *pfind = NULL; char *pnewbuf = NULL; int newbuflen = 0; char *pos = NULL; char *pos_last = NULL; char *ptmp = NULL; char *ptail = NULL; if (content == NULL || *content == NULL || len == NULL) return -1; ptail = *content + *len; //puts("rep 1"); if (pos_b == NULL) pos_b = *content; if (pos_e == NULL) pos_e = ptail; if (pos_e < pos_b) return -1; if ( ((u32)(pos_b - *content) > *len) || ((pos_b - *content) < 0) ) return -1; if ( ((u32)(pos_e - *content) > *len) || ((pos_e - *content) < 0) ) return -1; //puts("rep 2"); init_find(&fmem); pos = pos_b; while (times != 0) { //puts("----- 1"); pfind = pos = (char*)memmem(pos, ptail - pos, src, nsrc); //puts("----- 2"); if (pfind != NULL) { if (pfind > pos_e) break; add_find(&fmem, pfind); } pos += nsrc; if ( (pos >= pos_e) || ((*content - pos) > nsrc) ) break; //puts("----- 3"); if (times > 0) --times; } //puts("----- 4"); if (fmem.nfind == 0) return 0; pos = NULL; newbuflen = *len + (ndst - nsrc) * fmem.nfind; //puts("rep 3"); #if 0 printf("new len:%d, nsrc:%d, ndst:%d\n", newbuflen, nsrc, ndst); #endif if (nsrc == ndst) { //puts("rep 4"); pnewbuf = *content; while ( (pos = loop_find(&fmem)) != NULL) memcpy(pos, dst, ndst); } else { //puts("rep 5"); if (oss_malloc(&pnewbuf, newbuflen + 1) < 0) { clr_find(&fmem); return -1; } ptmp = pnewbuf; pos_last = *content; while ( (pos = loop_find(&fmem)) != NULL) { memcpy(ptmp, pos_last, pos - pos_last); ptmp += pos - pos_last; pos_last += pos - pos_last; memcpy(ptmp, dst, ndst); ptmp += ndst; pos_last += nsrc; } memcpy(ptmp, pos_last, ptail - pos_last); pnewbuf[newbuflen] = 0; oss_free(content); *content = pnewbuf; *len = newbuflen; } //puts("rep 6"); clr_find(&fmem); return fmem.nfind; }
////////// // // Assembles the indicated file.vasm, creating a file.bxml // ////// void iAssembleFile(s8* tcPathname) { u32 lnI, lnMachineCodeBytes, lnErrorCount, lnWarningCount; s64 lnHandle, lnFileSize, lnNumread; s8* lcData; s8* lcBxmlPathname; SStartEnd prog; SProgram* lp; // program SVvmmcError* lve; // error SVvmmcWarning* lvw; // warning SAssembly* la; // assembly info SSourceFile* lsf; // top-level source file // Attempt to open the file lnHandle = oss_sharedAsciiOpenFile(tcPathname, false, true, false, false, false, false, false, false); if (lnHandle < 0) { // Open file error printf(mc_loadResourceAsciiText(IDS_UNABLE_TO_OPEN), tcPathname); ++gnErrors; return; } // If we get here, we're good // Get file size lnFileSize = oss_sharedAsciiFileSize(lnHandle); if (lnFileSize > 0xffffffff) { // Uhhh... what are they trying to do to us? :-) printf(mc_loadResourceAsciiText(IDS_FILE_IS_TOO_BIG), tcPathname); ++gnErrors; return; } if (lnFileSize == 0) { printf(mc_loadResourceAsciiText(IDS_FILE_IS_ZERO_BYTES), tcPathname); ++gnErrors; return; } // Allocate memory to load lcData = (s8*)oss_alloc((u32)lnFileSize, false); if (!lcData) { printf(mc_loadResourceAsciiText(IDS_ERROR_ALLOCATING), lnFileSize, tcPathname); ++gnErrors; return; } // If we get here, memory is allocated // Read into it lnNumread = oss_sharedAsciiReadFile(lnHandle, lcData, (u32)lnFileSize); if (lnNumread != lnFileSize) { printf(mc_loadResourceAsciiText(IDS_UNABLE_TO_READ_BYTES_FROM), (u32)lnFileSize, tcPathname, lnNumread); ++gnErrors; return; } // We're good oss_sharedAsciiCloseFile(lnHandle); // Initialize our variables memset(&prog, 0, sizeof(prog)); // Define this initial program to its default empty state lp = (SProgram*)vvm_SEChain_append(&prog, vvm_getNextUniqueId(), vvm_getNextUniqueId(), sizeof(SProgram), _COMMON_START_END_SMALL_BLOCK_SIZE, NULL); printf(mc_loadResourceAsciiText(IDS_ASSEMBLING), tcPathname); ////////// // Physically translate into machine code, and the output bxml file ////// lnMachineCodeBytes = mc_assembleSourceCode(tcPathname, lcData, (u32)lnFileSize, lp); // When we get here, we've assembled our source file, with warnings, errors, or whatever // Free up our processed memory oss_free(lcData); // See where we sit if (!lp->_assembly.root) { // A fatal, unrecoverable error occurred where we didn't even get the initial startup data allocated printf(mc_loadResourceAsciiText(IDS_UNRECOVERABLE_ERROR)); ++gnErrors; return; } // We're good. // Grab the top-level file info la = (SAssembly*)lp->_assembly.root->ptr; // SAssembly information lsf = (SSourceFile*)la->includeFiles.root->ptr; // Top-level source file (which contains the master list of all errors, warnings, and source file lines) // Report on warnings and errors if (!glQuiet) { // Report warnings if (!glIgnoreWarnings) { lnWarningCount = 0; for (lnI = 0; lnI < lsf->warnings.masterCount; lnI++) { if (lsf->warnings.master[lnI] && lsf->warnings.master[lnI]->used) { // Grab the warning data lvw = (SVvmmcWarning*)lsf->warnings.master[lnI]->ptr; printf("--- %s\n--- |W:%u L:%u C:%u - %s\n", lvw->pathName, lvw->code, lvw->lineNumber, lvw->column, lvw->text); ++lnWarningCount; } } } // Report errors lnErrorCount = 0; for (lnI = 0; lnI < lsf->errors.masterCount; lnI++) { if (lsf->errors.master[lnI] && lsf->errors.master[lnI]->used) { // Grab the warning data lve = (SVvmmcError*)lsf->errors.master[lnI]->ptr; printf("--- %s\n--- |E:%u L:%u C:%u - %s\n", lve->pathName, lve->code, lve->lineNumber, lve->column, lve->text); ++lnErrorCount; } } } // Write the output lcBxmlPathname = oss_changePathnameExtension(tcPathname, ".bxml"); mc_saveSnippetsToBxml(lcBxmlPathname, &prog, true); oss_free(lcBxmlPathname); // Indicate we've processed another file ++gnFilesProcessed; // Indicate our final status printf("---\n"); printf(mc_loadResourceAsciiText(IDS_ERRORS_WARNINGS_FOUND), lsf->errors.masterCount, lsf->warnings.masterCount, tcPathname); printf("---\n"); // Update global totals gnErrors += lnErrorCount; gnWarnings += lnWarningCount; }
int process_keda_one_protocol(char **pOneProto, u32 *len_one_proto) { puts("----------- ONE PROTOCOL ---------------"); t_disbuf(*pOneProto, *len_one_proto); puts("-- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); //const static char FLG_COMP[] = "\x02\x23"; u16 inet_len_msg_compress = 0; //u16 host_len_msg_compress = 0; u16 len_msg_uncompress = 0; unsigned long host_len_msg_compress_l = 0; unsigned long len_msg_uncompress_l = 0; unsigned char *pUncompress = NULL; unsigned char *ptr = NULL; //puts("process_keda_one_protocol: 1"); //if (memcmp(*pOneProto + 18, FLG_COMP, sizeof(FLG_COMP)-1) != 0) //return 1; //puts("process_keda_one_protocol: 2"); memcpy(&inet_len_msg_compress, *pOneProto + OFFSET_LEN_MSG, 2); host_len_msg_compress_l = ntohs(inet_len_msg_compress) - 4; //puts("process_keda_one_protocol: 3"); //inet_len_msg_compress = htons(ntohs(inet_len_msg_compress) - 4); //len_msg_compress_l = ntohs(inet_len_msg_compress) - 4; memcpy(&len_msg_uncompress, *pOneProto + 41, 2); len_msg_uncompress_l = len_msg_uncompress; //printf("len_msg_uncompress:%d\n", len_msg_uncompress); //puts("process_keda_one_protocol: 4"); if (oss_malloc(&pUncompress, len_msg_uncompress) < 0) { logerr_out("malloc uncompress memory failed!"); return -1; } //puts("process_keda_one_protocol: 5"); ptr = (unsigned char*)(*pOneProto + LEN_PROTO_HEAD + 4); //puts("--------- before uncompress -----------"); //t_disbuf(ptr, len_msg_compress_l); if (zdecompress(ptr,host_len_msg_compress_l, pUncompress, &len_msg_uncompress_l) < 0) { oss_free(&pUncompress); puts("-------------------------------------------------"); t_disbuf(ptr, host_len_msg_compress_l); logerr_out("uncompress failed!"); return 1; } //puts("process_keda_one_protocol: 6"); //printf("uncompress len:%ld\n", len_msg_uncompress_l); //puts("-------- uncompress ----------------------------------------"); t_disbuf(pUncompress, len_msg_uncompress_l); //if (process_keda_uncompress(put, &pUncompress, &len_msg_uncompress) < 0) //{ //oss_free(&pUncompress); //logerr_out("process keda uncompress failed!"); //return -1; //} //puts("process_keda_one_protocol: 7"); // ReCompress unsigned char *pNewCompress = NULL; unsigned long len_new_compress = len_msg_uncompress + 128; // sometimes compress data is larger than that uncompress. if (oss_malloc(&pNewCompress, len_new_compress) < 0) { logerr_fmt("malloc failed! size:%lu", len_new_compress); return -1; } if (zcompress(pUncompress,len_msg_uncompress_l, pNewCompress,&len_new_compress) < 0) { printf("pUn:%p, len_un:%lu, pNew:%p, len_new:%lu\n", pUncompress, len_msg_uncompress_l, pNewCompress,len_new_compress); oss_free(&pUncompress); oss_free(&pNewCompress); logerr_out("compress failed!"); exit(-1); return -1; } //puts("process_keda_one_protocol: 8"); //puts("------------ new compress ----------------------------"); //t_disbuf(pNewCompress, len_new_compress); // update data if ((ptr = (unsigned char*)realloc(*pOneProto, LEN_PROTO_HEAD + 4 + len_new_compress)) == NULL) { oss_free(&pUncompress); oss_free(&pNewCompress); logerr_out("realloc failed!"); return -1; } //puts("process_keda_one_protocol: 9"); *pOneProto= (char*)ptr; *len_one_proto = LEN_PROTO_HEAD + 4 + len_new_compress; memcpy(*pOneProto + LEN_PROTO_HEAD + 4, pNewCompress, len_new_compress); len_new_compress += 4; u16 inet_len_new_compress = htons(len_new_compress); memcpy(*pOneProto + 20, &inet_len_new_compress, 2); //puts("process_keda_one_protocol: 10"); //puts("-------- new data -----------"); //t_disbuf((unsigned char*)*ut_buf, *pack_len); oss_free(&pUncompress); oss_free(&pNewCompress); //puts("process_keda_one_protocol: 11"); return 1; }
////////// // #02 - Save the processed bxml file to disk (should match cgc_Test_Bxml_2_1 in Bxml_test2.h) ////// bool iivvmt_testBxml_2(u64 lnHandleLog, SBxml* bxml) { u64 lnBytesWritten, lnBytesRead, lnErrorOffset, lnErrorCode, lnSha1As64Bit; u32 lnSha1As32Bit; u8 sha20Bytes[20]; s8* lcFileOut; SBxml* bxml2; // Tell which test we're running vvm_resourcePrintf(IDS_VVM_TEST_BXML_2); // Save the current state to see how she fared lcFileOut = oss_sharedAsciiGetTempFilename(); if (!lcFileOut) { // Unable to allocate the temporary filename vvm_resourcePrintf(IDS_VVM_TEST_BXML_UNABLE_TO_GET_TEMP_FILE); vvm_resourcePrintf(IDS_VVM_TEST_FAIL); return(false); } // Save the file to disk vvm_bxmlSave(bxml, lcFileOut, sizeof(lcFileOut) - 1, true, true, &lnBytesWritten); // Try to read it back in, and compare the memory bxml2 = vvm_bxmlLoad(lcFileOut, strlen(lcFileOut), &lnBytesRead, &lnErrorOffset, &lnErrorCode); // Clean house oss_sharedAsciiDeleteFile(lcFileOut); oss_free(lcFileOut); // Did we read it back in correctly? if (!bxml2 || lnErrorCode != 0 || lnBytesRead != lnBytesWritten) { // Unable to load the temporary file back in correctly vvm_resourcePrintf(IDS_VVM_TEST_BXML_UNABLE_TO_RELOAD_TEMP_FILE); vvm_resourcePrintf(IDS_VVM_TEST_FAIL); return(false); } // If we get here, file was loaded properly // Grab the SHA-1 value from the loaded file lnSha1As64Bit = vvm_bxmlNodeSha1(bxml2, sha20Bytes); lnSha1As32Bit = vvm_sha1Compute32BitFromSha1(sha20Bytes); if (lnSha1As64Bit != cgnTestBxmlDotHNodeSha1As64Bit || lnSha1As32Bit != cgnTestBxmlDotHNodeSha1As32Bit) { // The file does not match the expected SHA-1 value vvm_resourcePrintf(IDS_VVM_TEST_BXML_RELOAD_DOES_NOT_MATCH); vvm_resourcePrintf(IDS_VVM_TEST_FAIL); return(false); } // If we get here, we're good, the re-loaded file matched the form with which it was // saved, and the SHA-1 validation upon reload indicated they were identical. // In short, it's time to do a little dance! Woo hoo! :-) // Delete the loaded node vvm_bxmlNodeDelete(bxml2, true); bxml2 = NULL; // If we get here, we're good vvm_resourcePrintf(IDS_VVM_TEST_PASS); return(true); }
static void * __start_local_tcpclient(void * arg) { SAI ser_addr; SAI cli_addr; int cli_sock = -1; struct timeval tout; char buf[32] = {0}; vp_local_client lc; char *pSend = NULL; char *pRecv = NULL; int nSend = 0; int nRecvBuf = 0; int nRecv = 0; if (arg == NULL) return NULL; memcpy(&lc, arg, sizeof(lc)); oss_free(&arg); puts("-- 1"); if ((cli_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { logdbg_out("Start local tcp client: create socket failed!"); return NULL; } puts("-- 2"); if (Setsockopt(cli_sock, SOL_SOCKET, SO_REUSEADDR) < 0) { logdbg_out("Start local tcp client: set socket reuse failed!"); close(cli_sock); return NULL; } // set server address init_sockaddr(&ser_addr, lc.args.dip, lc.args.dport); puts("-- 3"); // set client address if ((lc.args.lip != 0) || (lc.args.lport != 0)) { init_sockaddr(&cli_addr, lc.args.lip, lc.args.lport); if (Bind(cli_sock, cli_addr, sizeof(cli_addr)) < 0) { logdbg_fmt("Start local tcp client: bind socket to %s:%d failed!", inet_ultoa(lc.args.lip, buf), lc.args.lport); close(cli_sock); return NULL; } } puts("-- 4"); tout.tv_sec = lc.args.session_tout; // Seconds Timeout tout.tv_usec = 0; if (setsockopt(cli_sock, SOL_SOCKET, SO_RCVTIMEO, &tout,sizeof(struct timeval)) < 0) { logdbg_out("Start local tcp client: set connect timeout failed!"); close(cli_sock); return NULL; } puts("-- 5"); if (Connect(cli_sock, (struct sockaddr*)&ser_addr, sizeof(ser_addr), 5) < 0) { logdbg_fmt("Start local tcp client: connect %s:%d failed!", inet_ultoa(lc.args.lip, buf), lc.args.lport); close(cli_sock); return NULL; } puts("-- 6"); if (lc.do_connect != NULL) { if (lc.do_connect(&(lc.args), &pSend, &nSend) <= 0) { close(cli_sock); return NULL; } if (Send(cli_sock, pSend, nSend, MSG_NOSIGNAL) <= 0) { close(cli_sock); return NULL; } } puts("-- 7"); // int (* do_process_recv)(pvp_local_client_args *pclient_args, char **ppreply, int *preply_len); nRecvBuf = BUF_SIZE; //if (pRecv = (char*)malloc(BUF_SIZE); if (oss_malloc(&pRecv, nRecvBuf) < 0) { logdbg_out("Start local tcp client: malloc receive buffer failed!"); close(cli_sock); return NULL; } char *pTmp = NULL; while (true) { if (nRecvBuf < BUF_SIZE) { if ((pTmp = (char*)realloc(pRecv, BUF_SIZE)) == NULL) { logdbg_out("Start local tcp client: realloc buffer failed!"); break; } pRecv = pTmp; nRecvBuf = BUF_SIZE; } if ((nRecv = Recv(cli_sock, pRecv, nRecvBuf, 0)) <= 0) { if (nRecv < 0) logdbg_out("Start local tcp client: receive failed!"); break; } if (lc.do_process_recv != NULL) { if (lc.do_process_recv(&lc.args, &pRecv, &nRecv, &nRecvBuf) < 0) break; } } close_sock(&cli_sock); return NULL; }