static int encode_decode_tests(void) { char buf[100]; ademco_contactid_receiver_state_t *receiver; ademco_contactid_sender_state_t *sender; logging_state_t *logging; ademco_contactid_report_t result; int i; printf("Encode and decode tests\n"); if ((receiver = ademco_contactid_receiver_init(NULL, NULL, NULL)) == NULL) return 2; logging = ademco_contactid_receiver_get_logging_state(receiver); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW); span_log_set_tag(logging, "Ademco-rx"); if ((sender = ademco_contactid_sender_init(NULL, NULL, NULL)) == NULL) return 2; logging = ademco_contactid_sender_get_logging_state(sender); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW); span_log_set_tag(logging, "Ademco-tx"); for (i = 0; i < 5; i++) { if (encode_msg(buf, &reports[i]) < 0) { printf("Bad encode message\n"); return -1; } printf("'%s'\n", buf); if (decode_msg(&result, buf)) { printf("Bad decode message\n"); return -1; } ademco_contactid_receiver_log_msg(receiver, &result); printf("\n"); if (memcmp(&reports[i], &result, sizeof(result))) { printf("Received message does not match the one sent\n"); return -1; } } if (encode_msg(buf, &reports[5]) >= 0) { printf("Incorrectly good message\n"); return -1; } printf("'%s'\n", buf); printf("\n"); printf(" Passed\n"); return 0; }
int print_msg_info(int fd,struct sip_msg* msg) { char *payload=0; char *prefix=0; int retval=-1; if((prefix=pkg_malloc(500))==0){ printf("OUT OF MEMORY !!!\n"); return -1; } memset(prefix,0,500); strcpy(prefix," "); if(parse_headers(msg,HDR_EOH_F,0)<0) goto error; if(!(payload=pkg_malloc(MAX_ENCODED_MSG + MAX_MESSAGE_LEN))) goto error; if(encode_msg(msg,payload,MAX_ENCODED_MSG + MAX_MESSAGE_LEN)<0){ printf("Unable to encode msg\n"); goto error; } if(print_encoded_msg(fd,payload,prefix)<0){ printf("Unable to print encoded msg\n"); pkg_free(payload); goto error; } pkg_free(payload); retval =0; error: if(prefix) pkg_free(prefix); return retval; }
/** * creates an as_event in shared memory and returns its address or NULL if error. * event_length(4) UNSIGNED INT includes the length 4 bytes itself * type(1), * processor_id(4), * flags(4), * transport(1). * src_ip_len(1), * src_ip(4 or 16), * dst_ip_len(1), * dst_ip(4 or 16), * src_port(2), * dst_port(2), * */ char * create_as_event_sl(struct sip_msg *msg,char processor_id,int *evt_len,int flags) { unsigned int i; unsigned short int port; unsigned int k,len; char *buffer=NULL; if(!(buffer=shm_malloc(ENCODED_MSG_SIZE))){ LM_ERR("create_as_event_t Out Of Memory !!\n"); return 0; } *evt_len=0; /*leave 4 bytes for event length*/ k=4; /*type*/ buffer[k++]=(unsigned char)SL_REQ_IN; /*processor_id*/ buffer[k++]=(unsigned char)processor_id; /*flags*/ flags=htonl(flags); memcpy(buffer+k,&flags,4); k+=4; /*protocol should be UDP,TCP,TLS or whatever*/ buffer[k++]=(unsigned char)msg->rcv.proto; /*src ip len + src ip*/ len=msg->rcv.src_ip.len; buffer[k++]=(unsigned char)len; memcpy(buffer+k,&(msg->rcv.src_ip.u),len); k+=len; /*dst ip len + dst ip*/ len=msg->rcv.dst_ip.len; buffer[k++]=(unsigned char)len; memcpy(buffer+k,&(msg->rcv.dst_ip.u),len); k+=len; /*src port */ port=htons(msg->rcv.src_port); memcpy(buffer+k,&port,2); k+=2; /*dst port */ port=htons(msg->rcv.dst_port); memcpy(buffer+k,&port,2); k+=2; /*length of event (hdr+payload-4), copied at the beginning*/ if(encode_msg(msg,buffer+k,ENCODED_MSG_SIZE-k)<0){ LM_ERR("Unable to encode msg\n"); goto error; } i = GET_PAY_SIZE(buffer+k); k+=i; *evt_len=k; k=htonl(k); memcpy(buffer,&k,4); return buffer; error: if(buffer) shm_free(buffer); return 0; }
SPAN_DECLARE(int) ademco_contactid_sender_put(ademco_contactid_sender_state_t *s, const ademco_contactid_report_t *report) { if (s->busy) return -1; if ((s->tx_digits_len = encode_msg(s->tx_digits, report)) < 0) return -1; s->busy = true; return dtmf_tx_put(&s->dtmf, s->tx_digits, s->tx_digits_len); }
/** * @addr * @msg */ int pv_signal_threads_recv(void * addr, unsigned char msg) { int t; int err = 0; uint32_t encoded_msg = encode_msg(addr, msg); for (t = 0; t < numThreads_g; t++) { err = pv_in_mbox_write(thread_env_g[t].ctx, &encoded_msg, 1, 1); } return err; }
void JsonParser::parse_msg(string msg, int sock){ Json::Value root; Json::Features f = Json::Features::strictMode(); Json::Reader reader(f); bool parsedOk = false; string s_response = ""; string json_msg; int status = 0; parsedOk=reader.parse(msg,root,false); if(!parsedOk){ printf("parse error: %s",STR2CHAR(reader.getFormatedErrorMessages())); return; } m_iMsgType = root["msgType"].asInt(); switch(m_iMsgType) { case SIGN_IN:{ printf("******************sign_in******************\n"); string userId = root["srcId"].asString(); string passwd = root["password"].asString(); bool is_correct = 0; m_pMysqlExec->verify_pw(is_correct, userId, passwd); if(is_correct){ printf("password correct!\n"); } online_map.insert(map<string, int>::value_type(userId, sock)); break; } case REGISTER: { printf("******************register******************\n"); string userId = root["srcId"].asString(); string passwd = root["password"].asString(); if(m_pMysqlExec->reg_user(userId, passwd)){ online_map.insert(map<string, int>::value_type(userId, sock)); } break; } case SEND_MSG: { printf("******************send_msg******************\n"); string srcId = root["srcId"].asString(); string dstId = root["dstId"].asString(); string msg = root["msg"].asString(); bool is_buddy = 0; m_pMysqlExec->is_buddy(is_buddy, srcId, dstId); if(!is_buddy){ printf("%s and %s are not friends\n", srcId.c_str(), dstId.c_str()); break; } map<string, int>::iterator res = online_map.find(dstId); if(res != online_map.end()){ printf("find %s's sock\n", dstId.c_str()); json_msg = encode_msg(srcId, msg); send_msg(res->second, STR2CHAR(json_msg), json_msg.length()); } break; } case FIND_PERSON:{ printf("******************find_person******************\n"); string dstId = root["dstId"].asString(); bool is_exist = 0; if(!m_pMysqlExec->find_user(is_exist, dstId)){ printf("can not find user %s\n", dstId.c_str()); } if(is_exist){ json_msg = encode_response(FIND_PERSON_RES, OK, dstId); }else{ json_msg = encode_response(FIND_PERSON_RES, FAIL, dstId); } send_msg(sock, STR2CHAR(json_msg), json_msg.length()); break; } case ADD_BUDDY:{ printf("******************add_buddy******************\n"); string srcId = root["srcId"].asString(); string dstId = root["dstId"].asString(); bool is_buddy = 0; if(!m_pMysqlExec->is_buddy(is_buddy, srcId, dstId)){ json_msg = encode_response(ADD_BUDDY_RES, FAIL, dstId); send_msg(sock, STR2CHAR(json_msg), json_msg.length()); break; } if(!is_buddy){ if(!m_pMysqlExec->add_buddy(srcId, dstId)){ status = FAIL; } } status = OK; json_msg = encode_response(ADD_BUDDY_RES, status, dstId); send_msg(sock, STR2CHAR(json_msg), json_msg.length()); break; } case DEL_BUDDY:{ string srcId = root["srcId"].asString(); string dstId = root["dstId"].asString(); if(!m_pMysqlExec->del_buddy(srcId, dstId)){ json_msg = encode_response(ADD_BUDDY_RES, FAIL, dstId); send_msg(sock, STR2CHAR(json_msg), json_msg.length()); break; } string json_msg; json_msg = encode_response(DEL_BUDDY_RES, OK, dstId); send_msg(sock, STR2CHAR(json_msg), json_msg.length()); break; } default:{ printf("unknown type\n"); break; } } }
/** * creates an as_event in shared memory and returns its address or NULL if error. * event_length(4) UNSIGNED INT includes the length 4 bytes itself * type(1), * flags(4), * transport(1). * src_ip_len(1), * src_ip(4 or 16), * dst_ip_len(1), * dst_ip(4 or 16), * src_port(2), * dst_port(2), * hash index(4), * label(4), * [cancelled hash_index,label] * */ char * create_as_event_t(struct cell *t,struct sip_msg *msg,char processor_id,int *evt_len,int flags) { unsigned int i,hash_index,label; unsigned short int port; unsigned int k,len; char *buffer=NULL; struct cell *originalT; originalT=0; if(!(buffer=shm_malloc(ENCODED_MSG_SIZE))){ LM_ERR("Out Of Memory !!\n"); return 0; } *evt_len=0; if(t){ hash_index=t->hash_index; label=t->label; }else{ /**seas_f.tmb.t_get_trans_ident(msg,&hash_index,&label); this is bad, because it ref-counts !!!*/ LM_ERR("no transaction provided...\n"); goto error; } k=4; /*type*/ buffer[k++]=(unsigned char)T_REQ_IN; /*processor_id*/ buffer[k++]=(unsigned char)processor_id; /*flags*/ if(is_e2e_ack(t,msg)){ flags|=E2E_ACK; }else if(msg->REQ_METHOD==METHOD_CANCEL){ LM_DBG("new CANCEL\n"); originalT=seas_f.tmb.t_lookup_original_t(msg); if(!originalT || originalT==T_UNDEFINED){ /** we dont even pass the unknown CANCEL to JAIN*/ LM_WARN("CANCEL does not match any existing transaction!!\n"); goto error; }else{ flags|=CANCEL_FOUND; //seas_f.tmb.unref_cell(originalT); } LM_DBG("Cancelling transaction !!\n"); } flags=htonl(flags); memcpy(buffer+k,&flags,4); k+=4; /*protocol should be UDP,TCP,TLS or whatever*/ buffer[k++]=(unsigned char)msg->rcv.proto; /*src ip len + src ip*/ len=msg->rcv.src_ip.len; buffer[k++]=(unsigned char)len; memcpy(buffer+k,&(msg->rcv.src_ip.u),len); k+=len; /*dst ip len + dst ip*/ len=msg->rcv.dst_ip.len; buffer[k++]=(unsigned char)len; memcpy(buffer+k,&(msg->rcv.dst_ip.u),len); k+=len; /*src port */ port=htons(msg->rcv.src_port); memcpy(buffer+k,&port,2); k+=2; /*dst port */ port=htons(msg->rcv.dst_port); memcpy(buffer+k,&port,2); k+=2; /*hash_index*/ i=htonl(hash_index); memcpy(buffer+k,&i,4); k+=4; /*label (is the collision slot in the hash-table)*/ i=htonl(label); memcpy(buffer+k,&i,4); k+=4; if(msg->REQ_METHOD==METHOD_CANCEL && originalT){ LM_DBG("Cancelled transaction: Hash_Index=%d, Label=%d\n",originalT->hash_index,originalT->label); /*hash_index*/ i=htonl(originalT->hash_index); memcpy(buffer+k,&i,4); k+=4; /*label (is the collision slot in the hash-table)*/ i=htonl(originalT->label); memcpy(buffer+k,&i,4); k+=4; } /*length of event (hdr+payload-4), copied at the beginning*/ if(encode_msg(msg,buffer+k,ENCODED_MSG_SIZE-k)<0){ LM_ERR("Unable to encode msg\n"); goto error; } i = GET_PAY_SIZE(buffer+k); k+=i; *evt_len=k; k=htonl(k); memcpy(buffer,&k,4); return buffer; error: if(buffer) shm_free(buffer); return 0; }
char* create_as_action_reply(struct cell *c,struct tmcb_params *params,int uac_id,char processor_id,int *evt_len) { int i; unsigned int code,flags; unsigned short int port; unsigned int k,len; char *buffer; struct sip_msg *msg; if(!(buffer=shm_malloc(ENCODED_MSG_SIZE))){ LM_ERR("create_as_action_reply Out Of Memory !!\n"); return 0; } msg=0; *evt_len=0; flags=0; if(params->rpl==FAKED_REPLY) flags=FAKED_REPLY_FLAG; /*length*/ k=4; /*type*/ buffer[k++]=(unsigned char)RES_IN; /*processor id*/ buffer[k++]=processor_id; /*flags (by now, not used)*/ flags=htonl(flags); memcpy(buffer+k,&flags,4); k+=4; /*recv info*/ if(!(params->rpl == FAKED_REPLY)) { msg=params->rpl; /*protocol should be UDP,TCP,TLS or whatever*/ buffer[k++]=(unsigned char)msg->rcv.proto; /*src ip len + src ip*/ len=msg->rcv.src_ip.len; buffer[k++]=(unsigned char)len; memcpy(buffer+k,&(msg->rcv.src_ip.u),len); k+=len; /*dst ip len + dst ip*/ len=msg->rcv.dst_ip.len; buffer[k++]=(unsigned char)len; memcpy(buffer+k,&(msg->rcv.dst_ip.u),len); k+=len; /*src port */ port=htons(msg->rcv.src_port); memcpy(buffer+k,&port,2); k+=2; /*dst port */ port=htons(msg->rcv.dst_port); memcpy(buffer+k,&port,2); k+=2; }else{ /*protocol*/ buffer[k++]=0; /*src ip len*/ buffer[k++]=0; /*dst ip len*/ buffer[k++]=0; /*skip src port and dst port*/ buffer[k++]=0; buffer[k++]=0; buffer[k++]=0; buffer[k++]=0; } /*hash_index*/ i=htonl(c->hash_index); memcpy(buffer+k,&i,4); k+=4; /*label*/ i=(!strncmp(c->method.s,"CANCEL",6)) ? \ htonl(((struct as_uac_param*)*params->param)->label) : \ htonl(c->label); memcpy(buffer+k,&i,4); k+=4; /*uac_id*/ uac_id=htonl(uac_id); memcpy(buffer+k,&uac_id,4); k+=4; /*code*/ code=htonl(params->code); memcpy(buffer+k,&code,4); k+=4; /*length of event (hdr+payload-4), copied at the beginning*/ if(params->rpl != FAKED_REPLY) { if((i=encode_msg(msg,buffer+k,ENCODED_MSG_SIZE-k))<0){ LM_ERR("failed to encode msg\n"); goto error; } k+=i; } *evt_len=k; k=htonl(k); memcpy(buffer,&k,4); return buffer; error: return 0; }
int main(int argc, char *argv[]) { int sockfd,portnum,n,i,flag=0; struct sockaddr_in serv_addr; struct hostent *servname; struct msg_header mess_hdr; struct attr_header atri_hdr; struct attr_header atri_hdr2; char *buff; char recv_buff[1024]; fd_set readfiledesc; char username_buff[16]; char message_buff[512]; FD_ZERO (&readfiledesc); if(argc<4) { fprintf(stderr,"usage : client username hostname portno\n" ); exit(0); } portnum=atoi(argv[3]); sockfd = socket(AF_INET, SOCK_STREAM,0); if(sockfd==-1) { perror("error opening socket!"); exit(0); } servname=gethostbyname(argv[2]); if(servname==NULL) { fprintf(stderr,"cannot find host"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)servname->h_addr,(char *)&serv_addr.sin_addr.s_addr,servname->h_length); serv_addr.sin_port = htons(portnum); if(connect(sockfd,&serv_addr,sizeof(serv_addr))<0) { perror("Error during connection."); exit(0); } bzero(username_buff,16); strcpy(username_buff,argv[1]); mess_hdr.vrsn=3; mess_hdr.type=2; atri_hdr.type=2; atri_hdr.length=(strlen(username_buff)+1); buff = encode_msg(&mess_hdr, &atri_hdr, NULL, username_buff, NULL); n = write(sockfd,buff,mess_hdr.length); free(buff); //SEND USERNAME AS JOIN FUNCTION //n = write(sockfd,username_buff,strlen(username_buff)); if(n==-1) { perror("Error writing username to server\n"); exit(0); } FD_SET(0, &readfiledesc); FD_SET(sockfd, &readfiledesc); while(1) { if (select(sockfd+1, &readfiledesc, NULL, NULL, NULL) == -1) { perror("Error in select function."); exit(0); } for(i = 0; i <= sockfd; i++) { if (FD_ISSET(i, &readfiledesc)) { if (i == 0) { //THIS IS FOR READING FROM KEYBOARD fgets(message_buff, sizeof(message_buff), stdin); mess_hdr.vrsn=3; mess_hdr.type=4; atri_hdr.type=4; atri_hdr.length=(strlen(message_buff)+1); buff = encode_msg(&mess_hdr, &atri_hdr, NULL, message_buff, NULL); if (buff != NULL){ n = write(sockfd,buff,mess_hdr.length); free(buff); message_buff[0]='\0'; } else { n=-1; } //SEND MESSAGE TO SERVER //n = write(sockfd,message_buff,strlen(message_buff)); if (n == -1) { perror("Error sending the message"); exit(0); } flag=1; } if ((i == sockfd)&&(flag==0)) { //THIS IS FOR READING FROM THE SERVER n = recv(sockfd, recv_buff, 4, 0); if (n == -1) { perror("Error during receive"); exit(0); } decode_msg_header(recv_buff,&mess_hdr); bzero(recv_buff, 1024); n = recv(sockfd, recv_buff,mess_hdr.length-4,0); int type = (int) mess_hdr.type; decode_msg(recv_buff, &type, &atri_hdr, &atri_hdr2, username_buff, message_buff); if(mess_hdr.type==3) { printf("%s: ",username_buff); printf("%s\n",message_buff); } if(mess_hdr.type==8) { printf("%s is ONLINE!\n",username_buff); } if(mess_hdr.type==6) { printf("%s is OFFLINE\n",username_buff); } //buff[n] = '\0'; //printf("%s\n",buff); } FD_SET(0, &readfiledesc); FD_SET(sockfd, &readfiledesc); } } flag=0; } close(sockfd); return 0; }