static PyObject * pyunirec_ipaddr(PyObject *self, PyObject *args) { const char *bytes; int size; ip_addr_t *ip; if (!PyArg_ParseTuple(args, "s#", &bytes, &size)) return NULL; if (size == 16) { ip = (ip_addr_t *) bytes; if (ip_is4(ip)) { return Py_BuildValue("(I)", ip->ui32[2]); } else { return Py_BuildValue("(IIII)", ip->ui32[0], ip->ui32[1], ip->ui32[2], ip->ui32[3]); } } return NULL; }
int main(int argc, char **argv) { #ifdef UNIREC ur_template_t *tmplt = ur_create_template("SRC_IP,DST_IP,SRC_PORT,DST_PORT,PROTOCOL,PACKETS,BYTES,URL", NULL); if (tmplt == NULL) { fprintf(stderr, "Error when creating UniRec template.\n"); return 1; } char *rec = #else flow_rec_t *rec = (flow_rec_t*) #endif "asdfqwerASDFQWER" "asdfqwerASDFQWER" "bytesxyz" "pkts" "dp" "sp" "p" "\00\x00\x13\x00" "http://example.com/"; int x = 0; uint16_t y = 0; uint64_t z = 0; char tmp_str[20]; time_t start_time = time(NULL); for (int i = 0; i < 1000000000; i++) { #ifdef UNIREC if (ip_is4(ur_get_ptr(tmplt, rec, F_SRC_IP))) x += 1; if (ip_is4(ur_get_ptr(tmplt, rec, F_DST_IP))) x += 1; y += ur_get(tmplt, rec, F_SRC_PORT); y += ur_get(tmplt, rec, F_DST_PORT); y += ur_get(tmplt, rec, F_PROTOCOL); z += ur_get(tmplt, rec, F_PACKETS); z += ur_get(tmplt, rec, F_BYTES); memcpy(tmp_str, ur_get_ptr(tmplt, rec, F_URL), ur_get_var_len(tmplt, rec, F_URL)); #else if (ip_is4(&rec->src_ip)) x += 1; if (ip_is4(&rec->dst_ip)) x += 1; y += rec->src_port; y += rec->dst_port; y += rec->protocol; z += rec->packets; z += rec->bytes; memcpy(tmp_str, rec->url, rec->url_len); #endif } printf("%i %hu %lu\n", x, y, z); printf("Time: %fs\n", difftime(time(NULL), start_time)); #ifdef UNIREC ur_free_template(tmplt); #endif return 0; }