static void ProcessOption( char ***argv ) /***************************************/ { char * item; char option; NewOption = TRUE; (**argv)++; // skip the switch character. item = **argv; option = tolower( *item ); switch( option ) { case 'c': // class list. ProcList( ProcClass, argv ); break; case 's': ProcList( ProcSeg, argv ); break; case 'x': ProcList( ProcExclude, argv ); break; case 'b': MakeBackup = FALSE; (*argv)++; break; default: put( "option not recognized: " ); putlen( item, 1 ); put( "\n" ); (*argv)++; } }
void put_code(OCODE *cd) /* * outputFile a generic instruction. */ { int op = cd->opcode,len=0,len2=0; AMODE *aps = cd->oper1,*apd = cd->oper2, *ap3 = cd->oper3; if (!prm_asmfile) return; if (op == op_line) { if (!prm_lines) return; fprintf(outputFile,";\n; Line %d:\t%s\n;\n",(int)apd,(char *)aps); return; } if (aps) len = aps->length; if (apd) len2 = apd->length; needpointer = (len != len2) || ((!aps || aps->mode !=am_dreg) && (!apd || apd->mode !=am_dreg)); putop(op); if (prm_nasm && op >=op_ja && op <= op_jns) fprintf(outputFile,"\tNEAR"); switch (op) { case op_rep: case op_repz: case op_repe: case op_repnz: case op_repne: case op_lock: return; } putlen(len); if( aps != 0 ) { fprintf(outputFile,"\t"); if (op == op_dd) nosize = TRUE; putamode(aps); nosize = FALSE; if( apd != 0 ) { fprintf(outputFile,","); putamode(apd); } if( ap3 != 0 ) { fprintf(outputFile,","); putamode(ap3); } } fprintf(outputFile,"\n"); }
void pl_print(message_t *head) { int i = 0; while(head != NULL){ char buf[100]; sprintf(buf, " %d MESSAGE:\t", i); printf("%15s",buf); putlen(head->len); puthbuf(head->buf, head->len); printf("\n"); head = head->next; i++; } }
void maccmd_print(message_t *head) { int i = 0; while(head != NULL){ char buf[100]; sprintf(buf, " %d MACCMD:\t", i); printf("%15s",buf); putlen(head->len-1); printf("[%02X] ", head->buf[0]); puthbuf(head->buf+1, head->len-1); printf("\n"); head = head->next; i++; } }
/* * output a generic instruction. */ PRIVATE void put_code P1 (const CODE *, ip) { putop (ip->opcode); putlen (ip->length); if (ip->oper1 != NIL_ADDRESS) { oprintf ("\t"); putamode (ip->oper1, ip->length); if (ip->oper2 != NIL_ADDRESS) { if (ip->opcode == op_line) { oprintf ("%s%s>>>>\t", newline, comment); } else { oprintf (","); } putamode (ip->oper2, ip->length); } } oprintf ("%s", newline); }
extern void PrintLine( char *buff, unsigned len ) /***********************************************/ { putlen( buff, len ); put( "\n" ); }
int config_parse(const char *file, config_t *config) { JSON_Value *jvroot; JSON_Object *joroot; JSON_Object *jomaccmd; JSON_Array *jarray; JSON_Value_Type jtype; const char *string; int ret; int i; if(file == NULL){ return -1; } /** Clear all flags */ config_free(config); printf("Start parsing configuration file....\n\n"); /* parsing json and validating output */ jvroot = json_parse_file_with_comments(file); jtype = json_value_get_type(jvroot); if (jtype != JSONObject) { return -1; } joroot = json_value_get_object(jvroot); string = json_object_get_string(joroot, "band"); if(string == NULL){ config->band = LW_BAND_EU868; }else{ for(i=0; i<LW_BAND_MAX_NUM; i++){ if(0 == strcmp(string, config_band_tab[i])){ config->band = (lw_band_t)i; break; } } if(i==LW_BAND_MAX_NUM){ config->band = LW_BAND_EU868; } } string = json_object_dotget_string(joroot, "key.nwkskey"); if(string != NULL){ if(str2hex(string, config->nwkskey, 16) == 16){ config->flag |= CFLAG_NWKSKEY; } } string = json_object_dotget_string(joroot, "key.appskey"); if(string != NULL){ if(str2hex(string, config->appskey, 16) == 16){ config->flag |= CFLAG_APPSKEY; } } string = json_object_dotget_string(joroot, "key.appkey"); if(string != NULL){ if(str2hex(string, config->appkey, 16) == 16){ config->flag |= CFLAG_APPKEY; } } ret = json_object_dotget_boolean(joroot, "join.key"); if(ret==0){ //printf("Join key false\n"); config->joinkey = false; }else if(ret==1){ //printf("Join key true\n"); config->joinkey = true; }else{ //printf("Unknown join key value\n"); config->joinkey = false; } string = json_object_dotget_string(joroot, "join.request"); if(string != NULL){ uint8_t tmp[255]; int len; len = str2hex(string, tmp, 255); if(len>0){ config->flag |= CFLAG_JOINR; config->joinr = malloc(len); if(config->joinr == NULL){ return -2; } config->joinr_size = len; memcpy(config->joinr, tmp, config->joinr_size); } } string = json_object_dotget_string(joroot, "join.accept"); if(string != NULL){ uint8_t tmp[255]; int len; len = str2hex(string, tmp, 255); if(len>0){ config->flag |= CFLAG_JOINA; config->joina = malloc(len); if(config->joina == NULL){ return -3; } config->joina_size = len; memcpy(config->joina, tmp, config->joina_size); } } jarray = json_object_get_array(joroot, "messages"); if(jarray != NULL){ uint8_t tmp[255]; for (i = 0; i < json_array_get_count(jarray); i++) { string = json_array_get_string(jarray, i); if(string!=NULL){ int len = str2hex(string, tmp, 255); if(len>0){ message_t *pl = malloc(sizeof(message_t)); memset(pl, 0, sizeof(message_t)); if(pl == NULL){ return -3; } pl->buf = malloc(len); if(pl->buf == NULL){ return -3; } pl->len = len; memcpy(pl->buf, tmp, pl->len); pl_insert(&config->message, pl); }else{ printf("Messages[%d] \"%s\" is not hex string\n", i, string); } }else{ printf("Messages item %d is not string\n", i); } } }else{ printf("Can't get payload array\n"); } jarray = json_object_get_array(joroot, "maccommands"); if(jarray != NULL){ uint8_t mhdr; int len; uint8_t tmp[255]; for (i = 0; i < json_array_get_count(jarray); i++) { jomaccmd = json_array_get_object(jarray, i); string = json_object_get_string(jomaccmd, "MHDR"); if(string != NULL){ len = str2hex(string, &mhdr, 1); if(len != 1){ printf("\"maccommands\"[%d].MHDR \"%s\" must be 1 byte hex string\n", i, string); continue; } }else{ string = json_object_get_string(jomaccmd, "direction"); if(string != NULL){ int j; len = strlen(string); if(len>200){ printf("\"maccommands\"[%d].direction \"%s\" too long\n", i, string); continue; } for(j=0; j<len; j++){ tmp[j] = tolower(string[j]); } tmp[j] = '\0'; if(0==strcmp((char *)tmp, "up")){ mhdr = 0x80; }else if(0==strcmp((char *)tmp, "down")){ mhdr = 0xA0; }else{ printf("\"maccommands\"[%d].MHDR \"%s\" must be 1 byte hex string\n", i, string); continue; } }else{ printf("Can't recognize maccommand direction\n"); continue; } } string = json_object_get_string(jomaccmd, "command"); if(string != NULL){ len = str2hex(string, tmp, 255); if(len <= 0){ printf("\"maccommands\"[%d].command \"%s\" is not hex string\n", i, string); continue; } }else{ printf("c\"maccommands\"[%d].command is not string\n", i); continue; } message_t *pl = malloc(sizeof(message_t)); memset(pl, 0, sizeof(message_t)); if(pl == NULL){ return -3; } pl->buf = malloc(len+1); if(pl->buf == NULL){ return -3; } pl->len = len+1; pl->buf[0] = mhdr; pl->next = 0; memcpy(pl->buf+1, tmp, pl->len-1); pl_insert(&config->maccmd, pl); } } print_spliter(); printf("%15s %s\n","BAND:\t", config_band_tab[LW_BAND_EU868]); printf("%15s","NWKSKEY:\t"); putlen(16); puthbuf(config->nwkskey, 16); printf("\n"); printf("%15s","APPSKEY:\t"); putlen(16); puthbuf(config->appskey, 16); printf("\n"); printf("%15s","APPKEY:\t"); putlen(16); puthbuf(config->appkey, 16); printf("\n"); printf("%15s","JOINR:\t"); putlen(config->joinr_size); puthbuf(config->joinr, config->joinr_size ); printf("\n"); printf("%15s","JOINA:\t"); putlen(config->joina_size); puthbuf(config->joina, config->joina_size ); printf("\n"); pl_print(config->message); maccmd_print(config->maccmd); json_value_free(jvroot); return 0; }
void snmp_input(u_char *pkt, int len){ u_char *snmppacket; int snmplen; SNMPObject so, ver, comm, pdu; SNMPObject reqid, errst, errin, vl, vb, oid, vo; SNMPObject result; u_char *ebuf; char *strp; int rlen, vlen, i, j=0, k; int errorstat=0, errorindex=0; u_long val; ebuf = resbuf + sizeof(resbuf) - 1; snmppacket = pkt + sizeof(struct ip) + sizeof(struct udphdr); snmplen = len - sizeof(struct ip) - sizeof(struct udphdr); DEBUG3( "snmp snmppkt = %lx, len = %lu, snmplen = %lu\n", snmppacket, len, snmplen); so = get_next_object(snmppacket, snmplen); if( so.type != (TAG_SEQ | TYPE_COMPOUND)){ DEBUG1( "so.type = %x\n", so.type); goto error_discard; } ver = get_next_object( so.p, so.len ); if( ver.type != TAG_INTEGER || ver.i != 0 ){ DEBUG1( "ver.type = %x\n", ver.type); goto error_discard; } comm = get_next_object( ver.next, so.len - ver.len ); if( comm.type != TAG_OCTETSTRING ){ DEBUG1( "comm.type = %x\n", comm.type); goto error_discard; } /* verify .... */ pdu = get_next_object( comm.next, so.len - ver.len - comm.len ); switch (pdu.type) { case PDU_GETREQUEST: case PDU_GETNEXT: case PDU_SETREQUEST: break; default: DEBUG1( "pdu.type = %x\n", pdu.type); goto error_discard; } rlen = pdu.len; reqid = get_next_object(pdu.p, rlen); if( reqid.type != TAG_INTEGER ){ DEBUG1( "reqid.type = %x\n", reqid.type); goto error_discard; } rlen -= reqid.tlen; errst = get_next_object( reqid.next, rlen ); if( errst.type != TAG_INTEGER ){ DEBUG1( "errst.type = %x\n", errst.type); goto error_discard; } rlen -= errst.tlen; errin = get_next_object( errst.next, rlen ); if( errin.type != TAG_INTEGER ){ DEBUG1( "errin.type = %x\n", errin.type); goto error_discard; } rlen -= errin.tlen; vl = get_next_object( errin.next, rlen ); if( vl.type != (TAG_SEQ | TYPE_COMPOUND)){ DEBUG1( "vl.type = %x\n", vl.type); goto error_discard; } vb = get_next_object( vl.p, vl.len ); if( vb.type != (TAG_SEQ | TYPE_COMPOUND)){ DEBUG1( "vb.type = %x\n", vb.type); goto error_discard; } do { oid = get_next_object( vb.p, vb.len ); if( oid.type != TAG_OID ){ DEBUG1( "oid.type = %x\n", oid.type); goto error_discard; } vo = get_next_object( oid.next, vb.len - oid.len ); if( vo.type == TAG_ERROR ){ DEBUG1( "vo.type = %x\n", vo.type); goto error_discard; } /* look for oid in our table */ for(i=0; mib[i].len; i++){ if( oid.len != mib[i].len ) continue; for(k=0; k<oid.len; k++){ if( oid.p[k] != mib[i].oid[k] ) break; } if( k == oid.len ) break; } if( !mib[i].len ){ /* not found report the error */ errorstat = ERRSTAT_NOSUCHNAME; }else{ if( pdu.type == PDU_GETNEXT ){ /* go to the next item */ i++; if( !mib[i].len ) /* there is nothing next - report an error */ errorstat = ERRSTAT_NOSUCHNAME; } } if( pdu.type == PDU_SETREQUEST ){ if( !( mib[i].tag & WRITABLE) ){ /* read-only variable */ errorstat = ERRSTAT_READONLY; } /* XXX set it */ }else{ /* get request */ if( !( mib[i].tag & READABLE) ){ /* write-only variable */ errorstat = ERRSTAT_GENERR; } /* get and encode our result */ /* note: we fill our buffer from the right and work back */ vlen = 0; switch (mib[i].tag & ~WRITABLE ) { case IT_STRING|READABLE: strp = (char*)mib[i].val; vlen = strlen( strp ); goto str_comn; case IT_OID|READABLE: /* val is ptr to {len, encoded_oid} */ strp = (char*)mib[i].val; vlen = *strp++; str_comn: /* copy data */ j += putstr(ebuf-j, strp, vlen, mib[i].otag, sizeof(resbuf)-j); break; case IT_CONST|READABLE: val = *(u_long*)&mib[i].val; goto int_comn; case IT_INT|READABLE: val = *(u_short*)(mib[i].val); goto int_comn; case IT_INT4|READABLE: val = *(u_long*)(mib[i].val); goto int_comn; case IT_INT1|READABLE: val = *(u_char*)(mib[i].val); int_comn: j += putint(ebuf-j, val, mib[i].otag, sizeof(resbuf)-j); break; default: /* if we have an error of some sort */ j += putint(ebuf-j, 0, TAG_INTEGER, sizeof(resbuf)-j); break; } } /* copy oid */ j += putstr(ebuf-j, (char*)mib[i].oid, mib[i].len, TAG_OID, sizeof(resbuf)-j); /* output the vb seq */ j += putlen( ebuf-j, j, sizeof(resbuf)-j); ebuf[-j++] = TYPE_COMPOUND | TAG_SEQ; /* handle multi-item requests */ /* get the next request */ rlen -= vb.tlen; vb = get_next_object( vb.next, rlen ); }while( rlen > 0 && vb.type == (TAG_SEQ | TYPE_COMPOUND)); /* output the vl seq */ j += putlen( ebuf-j, j, sizeof(resbuf)-j); ebuf[-j++] = TYPE_COMPOUND | TAG_SEQ; /* errstat, errindex, reqid */ j += putint(ebuf-j, errorindex, TAG_INTEGER, sizeof(resbuf)-j); j += putint(ebuf-j, errorstat, TAG_INTEGER, sizeof(resbuf)-j); j += putint(ebuf-j, reqid.i, TAG_INTEGER, sizeof(resbuf)-j); /* length and pdu_response */ j += putlen( ebuf-j, j, sizeof(resbuf)-j); ebuf[-j++] = PDU_GETRESPONSE; /* community */ bcopy((char*)ver.next, (char*)(ebuf-j-comm.tlen+1), comm.tlen); j += comm.tlen; /* version */ j += putint( ebuf-j, 0, TAG_INTEGER, sizeof(resbuf)-j); /* seq */ j += putlen( ebuf-j, j, sizeof(resbuf)-j); ebuf[-j++] = TYPE_COMPOUND | TAG_SEQ; /* whewwww!!! */ snmp_output(pkt, ebuf, j); return; error_discard: /* the SNMP packet was corrupt/invalid/wrong version/wrong community/... */ /* as per RFC1157, 4.1, we discard and perform no further action */ return; }