Example #1
0
struct template_cache_entry *handle_template(struct template_hdr_v9 *hdr, struct packet_ptrs *pptrs, u_int16_t tpl_type, u_int32_t sid, u_int16_t *pens, u_int16_t len)
{
  struct template_cache_entry *tpl = NULL;
  u_int8_t version = 0;

  if (pens) *pens = FALSE;

  if (tpl_type == 0 || tpl_type == 1) version = 9;
  else if (tpl_type == 2 || tpl_type == 3) version = 10;

  /* 0 NetFlow v9, 2 IPFIX */
  if (tpl_type == 0 || tpl_type == 2) {
    if (tpl = find_template(hdr->template_id, pptrs, tpl_type, sid))
      tpl = refresh_template(hdr, tpl, pptrs, tpl_type, sid, pens, version, len);
    else tpl = insert_template(hdr, pptrs, tpl_type, sid, pens, version, len);
  }
  /* 1 NetFlow v9, 3 IPFIX */
  else if (tpl_type == 1 || tpl_type == 3) {
    if (tpl = find_template(hdr->template_id, pptrs, tpl_type, sid))
      tpl = refresh_opt_template(hdr, tpl, pptrs, tpl_type, sid, version, len);
    else tpl = insert_opt_template(hdr, pptrs, tpl_type, sid, version, len);
  }

  return tpl;
}
Example #2
0
//done
static void jmp(cpu *pcpu) {
	int temp;
	if ((sizeof_template(pcpu)==0)) {
		pcpu->ip = pcpu->ax;
	} else if ((temp = find_template(pcpu, BACKWARD))>-1) {
		pcpu->ip = temp;
	} else if ((temp = find_template(pcpu, FORWARD))>-1) {
		pcpu->ip = temp;
	} else {
		pcpu->fl = CPU_ERROR;
		pcpu->ip++;// = pcpu->ax;
	}
}
Example #3
0
static void adr(cpu *pcpu) {
	int temp;
	if ((temp = find_template(pcpu, FORWARD))>-1) {
		pcpu->ax = temp;
		pcpu->cx = sizeof_template(pcpu);
		pcpu->ip+=pcpu->cx;
	}
	else if ((temp = find_template(pcpu, BACKWARD))>-1) {
		pcpu->ax = temp;
		pcpu->cx = sizeof_template(pcpu);
		pcpu->ip+=pcpu->cx;
	}
	pcpu->ip++;
}
Example #4
0
//done
static void adrf(cpu *pcpu) {
	int temp;
	if ((temp = find_template(pcpu, FORWARD))>-1) {
		pcpu->ax = temp;
		pcpu->cx = sizeof_template(pcpu);
		pcpu->ip+=pcpu->cx;
	}// else
		//printf("Template not found\n");
	pcpu->ip++;
}
Example #5
0
//done
static void call(cpu *pcpu) {
	int temp;
	if (!pila_full(pcpu)) {
		pila_push(pcpu, (int)(pcpu->ip+1));
	}
	if ((temp = find_template(pcpu, FORWARD))>-1) {
		pcpu->ip = temp;
	} else {
		pcpu->fl = CPU_ERROR;
		pcpu->ip++;
	}
}	
void Item_factory::set_material_from_json(Item_tag new_id, catajson mat_list){
    //If the value isn't found, just return a group of null materials
    std::string material_list[2] = {"null", "null"};

    if (mat_list.is_array())
    {
        if (mat_list.has(2))
        {
            debugmsg("Too many materials provided for item %s", new_id.c_str());
        }
        material_list[0] = mat_list.get(0).as_string();
        material_list[1] = mat_list.get(1).as_string();
    }
    else
    {
        material_list[0] = mat_list.as_string();
    }

    itype* temp = find_template(new_id);
    temp->m1 = material_list[0];
    temp->m2 = material_list[1];
}
Example #7
0
apr_status_t http_send_custom_response(nproxy_connection_t * conn, http_resp_codes_t error_code) {
    size_t t;
    int clen;
    int e_code;
    char *tpl = NULL;
    char *e_msg = NULL;
    char *e_text = NULL;
    char *e_detail = NULL;
    char *payload = NULL;
    char found = 0;
    char timebuf[APR_RFC822_DATE_LEN];
    char *additional_headers = "";
    const char *tmp = NULL;
    nproxy_connection_side_t *side;
    apr_status_t rv;
    apr_size_t len;

    const char *fallback_error =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
        "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
        "<html>\n" "<head><title>%d %s</title></head>\n" "<body>\n" "<h1>%d %s</h1>\n" "<p>%s</p>\n" "<hr />\n" "<p><em>Generated by NProxy</em></p>\n" "</body>\n" "</html>\n";

    side = conn->browser;

    apr_rfc822_date(timebuf, apr_time_now());

    for (t = 0; t < sizeof(ec) / sizeof(ec[0]); t++) {
        if (error_code == ec[t].code) {
            e_code = ec[t].number;
            e_msg = ec[t].string;
            found = 1;
            break;
        }
    }

    if (!found) {
        e_msg = "Error code string not found.";
        e_code = error_code;
    }

    e_detail = "No details, sorry.";

    if ((tmp = nproxy_connection_get_variable(conn, "additional_headers"))) {
        if (!zstr(tmp)) {
            additional_headers = (char *) tmp;
        }
    }
    if ((tmp = nproxy_connection_get_variable(conn, "custom_cause"))) {
        if (!zstr(tmp)) {
            e_msg = (char *) tmp;
        }
    }
    if ((tmp = nproxy_connection_get_variable(conn, "custom_detail"))) {
        if (!zstr(tmp)) {
            e_detail = (char *) tmp;
        }
    }

    tmp = nproxy_connection_get_variable(conn, "custom_template");
    tpl = find_template(conn, e_code, tmp);

    if (tpl != NULL) {
        if (conn->requests != NULL) {
            nproxy_connection_set_variable(conn, "req_method", conn->requests->method);
            nproxy_connection_set_variable(conn, "req_host", conn->requests->host);
            nproxy_connection_set_variable(conn, "req_port", apr_psprintf(conn->pool, "%d", conn->requests->port));
            nproxy_connection_set_variable(conn, "req_url", conn->requests->url);
        }
        nproxy_connection_set_variable(conn, "resp_errno", apr_psprintf(conn->pool, "%d", e_code));
        nproxy_connection_set_variable(conn, "resp_cause", e_msg);
        nproxy_connection_set_variable(conn, "resp_detail", e_detail);
        nproxy_connection_set_variable(conn, "package", "NProxy");
        e_text = parse_template(conn, tpl);
    }

    if (zstr(e_text)) {
        e_text = apr_psprintf(conn->pool, fallback_error, e_code, e_msg, e_code, e_msg, e_detail);
    }

    clen = strlen(e_text);

    payload = apr_psprintf(conn->pool,
                           "%s %d %s\r\n"
                           "Content-Type: text/html\r\n"
                           "Connection: close\r\n"
                           "Proxy-Connection: close\r\n" "Content-Length: %d\r\n" "Date: %s\r\n" "%s" "\r\n" "%s", "HTTP/1.0", e_code, e_msg, clen, timebuf, additional_headers, e_text);

    len = strlen(payload);
    rv = apr_socket_send(side->sock, payload, &len);
    if (rv == APR_STATUS_SUCCESS) {
        if (conn->requests != NULL) {
            conn->requests->bytes_in += len;
            conn->requests->response_headers.resp_code = error_code;
        }
    } else {
        rv = APR_STATUS_ERROR;
    }

    return rv;
}
item Item_factory::create(Item_tag id, int created_at){
    return item(find_template(id), created_at);
}
//Returns a random template from those with the given group tag
itype* Item_factory::template_from(const Item_tag group_tag){
    return find_template( id_from(group_tag) );
}
// Load values from this data file into m_template_groups
void Item_factory::load_item_groups_from( game *g, const std::string file_name ) throw ( std::string ) {
    std::ifstream data_file;
    picojson::value input_value;

    data_file.open(file_name.c_str());

    if(! data_file.good()) {
        throw "Could not read " + file_name;
    }

    data_file >> input_value;
    data_file.close();

    if(! json_good()) {
        throw "The data in " + file_name + " is not an array";
    }
    if (! input_value.is<picojson::array>()) {
        throw file_name + "is not an array of item groups";
    }

    //Crawl through once and create an entry for every definition
    const picojson::array& all_items = input_value.get<picojson::array>();
    for (picojson::array::const_iterator entry = all_items.begin();
         entry != all_items.end(); ++entry) {
        // TODO: Make sure we have at least an item or group child, as otherwise
        //       later things will bug out.

        if( !(entry->is<picojson::object>()) ){
            debugmsg("Invalid group definition, entry not a JSON object");
            continue;
        }
        const picojson::value::object& entry_body = entry->get<picojson::object>();

        // The one element we absolutely require for an item definition is an id
        picojson::value::object::const_iterator key_pair = entry_body.find( "id" );
        if( key_pair == entry_body.end() || !(key_pair->second.is<std::string>()) ) {
            debugmsg("Group definition skipped, no id found or id was malformed.");
            continue;
        }

        Item_tag group_id = key_pair->second.get<std::string>();
        m_template_groups[group_id] = new Item_group(group_id);
    }
    //Once all the group definitions are set, fill them out
    for (picojson::array::const_iterator entry = all_items.begin();
         entry != all_items.end(); ++entry) {
        const picojson::value::object& entry_body = entry->get<picojson::object>();

        Item_tag group_id = entry_body.find("id")->second.get<std::string>();
        Item_group *current_group = m_template_groups.find(group_id)->second;

        //Add items
        picojson::value::object::const_iterator key_pair = entry_body.find("items");
        if( key_pair != entry_body.end() ) {
            if( !(key_pair->second.is<picojson::array>()) ) {
                debugmsg("Invalid item list for group definition '%s', list of items not an array.",
                         group_id.c_str());
                // We matched the find above, so we're NOT a group.
                continue;
            }
            //We have confirmed that we have a list of SOMETHING, now let's add them one at a time.
            const picojson::array& items_to_add = key_pair->second.get<picojson::array>();
            for (picojson::array::const_iterator item_pair = items_to_add.begin();
                 item_pair != items_to_add.end(); ++item_pair) {
                // Before adding, make sure this element is in the right format,
                // namely ["TAG_NAME", frequency number]
                if( !(item_pair->is<picojson::array>()) ) {
                    debugmsg("Invalid item list for group definition '%s', element is not an array.",
                             group_id.c_str());
                    continue;
                }
                if( item_pair->get<picojson::array>().size() != 2 ) {
                    debugmsg( "Invalid item list for group definition '%s', element does not have 2 values.",
                              group_id.c_str() );
                    continue;
                }
                picojson::array item_frequency_array = item_pair->get<picojson::array>();
                // Insure that the first value is a string, and the second is a number
                if( !item_frequency_array[0].is<std::string>() ||
                    !item_frequency_array[1].is<double>() ) {
                    debugmsg("Invalid item list for group definition '%s', element is not a valid tag/frequency pair.",
                             group_id.c_str());
                    continue;
                }
                if( m_missing_item == find_template( item_frequency_array[0].get<std::string>() ) &&
                    0 == g->itypes.count( item_frequency_array[0].get<std::string>() ) ) {
                    debugmsg( "Item '%s' listed in group '%s' does not exist.",
                              item_frequency_array[0].get<std::string>().c_str(), group_id.c_str() );
                    continue;
                }
                current_group->add_entry(item_frequency_array[0].get<std::string>(),
                                         (int)item_frequency_array[1].get<double>());
            }
        }

        //Add groups
        key_pair = entry_body.find("groups");
        if(key_pair != entry_body.end()){
            if( !(key_pair->second.is<picojson::array>()) ){
                debugmsg("Invalid group list for group definition '%s', list of items not an array.",
                         group_id.c_str());
                continue;
            }
            //We have confirmed that we have a list of SOMETHING, now let's add them one at a time.
            const picojson::array& items_to_add = key_pair->second.get<picojson::array>();
            for ( picojson::array::const_iterator item_pair = items_to_add.begin();
                  item_pair != items_to_add.end(); ++item_pair ) {
                //Before adding, make sure this element is in the right format, namely ["TAG_NAME", frequency number]
                if( !(item_pair->is<picojson::array>()) ) {
                    debugmsg("Invalid group list for group definition '%s', element is not an array.",
                             group_id.c_str());
                    continue;
                }
                if( item_pair->get<picojson::array>().size() != 2 ) {
                    debugmsg("Invalid group list for group definition '%s', element does not have 2 values.",
                             group_id.c_str());
                    continue;
                }
                picojson::array item_frequency_array = item_pair->get<picojson::array>();
                //Finally, insure that the first value is a string, and the second is a number
                if(!item_frequency_array[0].is<std::string>() ||
                   !item_frequency_array[1].is<double>() ){
                    debugmsg("Invalid group list for group definition '%s', element is not a valid tag/frequency pair.",
                             group_id.c_str());
                    continue;
                }
                Item_group* subgroup = m_template_groups.find(item_frequency_array[0].get<std::string>())->second;
                current_group->add_group( subgroup, (int)item_frequency_array[1].get<double>() );
            }
        }
    }
    if( !json_good() ) {
        throw "There was an error reading " + file_name;
    }
}