void sys_errpack(int pri, char *fn, int ln, int en, struct sockaddr_in *peer, void *pack, unsigned len, char *fmt, ...) { bstring bt = bfromcstr(""); bstring bt2 = bfromcstr(""); int sz; int n; bvformata(sz, bt, fmt, fmt); if (sz == BSTR_OK) { bassignformat(bt2, ". Packet from %s:%u, length: %d, content:", inet_ntoa(peer->sin_addr), ntohs(peer->sin_port), len); bconcat(bt, bt2); for(n=0; n < len; n++) { bassignformat(bt, " %02hhx", ((unsigned char*)pack)[n]); bconcat(bt, bt2); } if (1) { fprintf(stderr, "%s: %d: %d (%s) %s", fn, ln, en, strerror(en), bt->data); } else { if (en) syslog(pri, "%s: %d: %d (%s) %s", fn, ln, en, strerror(en), bt->data); else syslog(pri, "%s: %d: %s", fn, ln, bt->data); } } bdestroy(bt); bdestroy(bt2); }
static int chilli_sessions(bstring b) { struct dhcp_conn_t *conn = dhcp->firstusedconn; char *state = "<font color=green>Authorized</font>"; bstring s = bfromcstr(""); bcatcstr(b, "{\"service\":[{ \""); bcatcstr(b, getenv("CAP_table")); bcatcstr(b, "\":[ "); while (conn) { struct app_conn_t *appconn = (struct app_conn_t *)conn->peer; if (appconn && appconn->s_state.authenticated) { } else { state = "<font color=red>Redirect</font>"; } bassignformat(s, "{" "\"state\":\"%s\"," "\"macAddress\":\"%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\"," "\"ipAddress\":\"%s\",", state, conn->hismac[0], conn->hismac[1], conn->hismac[2], conn->hismac[3], conn->hismac[4], conn->hismac[5], inet_ntoa(conn->hisip) ); conn = conn->next; if (appconn) { session_json_params(&appconn->s_state, &appconn->s_params, s, 0); bcatcstr(s, ","); session_json_acct(&appconn->s_state, &appconn->s_params, s, 0); } bcatcstr(s, "},"); bconcat(b, s); } bcatcstr(b, "]} ]}"); return 0; }
// Generates the LuaJIT header given a Lua script and a property file. The // header file is generated based on the property usage of the 'event' // variable in the script. // // source - The source code of the Lua script. // property_file - The property file used to lookup properties. // event_decl - A pointer to where the struct def should be returned. // init_descriptor_func - A pointer to where the descriptor init function should be returned. // // Returns 0 if successful, otherwise returns -1. int sky_lua_generate_event_info(bstring source, sky_property_file *property_file, bstring *event_decl, bstring *init_descriptor_func) { int rc; bstring identifier = NULL; assert(source != NULL); assert(property_file != NULL); assert(event_decl != NULL); assert(init_descriptor_func != NULL); // Initialize returned value. *event_decl = bfromcstr( " int64_t timestamp;\n" " uint16_t action_id;\n" ); check_mem(*event_decl); *init_descriptor_func = bfromcstr( " ffi.C.sky_data_descriptor_set_timestamp_offset(descriptor, ffi.offsetof(\"sky_lua_event_t\", \"timestamp\"));\n" " ffi.C.sky_data_descriptor_set_action_id_offset(descriptor, ffi.offsetof(\"sky_lua_event_t\", \"action_id\"));\n" ); check_mem(*init_descriptor_func); // Setup a lookup of properties. bool lookup[SKY_PROPERTY_ID_COUNT+1]; memset(lookup, 0, sizeof(lookup)); // Loop over every mention of an "event." property. int pos = 0; struct tagbstring EVENT_DOT_STR = bsStatic("event."); while((pos = binstr(source, pos, &EVENT_DOT_STR)) != BSTR_ERR) { // Make sure that this is not part of another identifier. bool skip = false; if(pos > 0 && (isalnum(bchar(source, pos-1)) || bchar(source, pos-1) == '_')) { skip = true; } // Move past the "event." string. pos += blength(&EVENT_DOT_STR); if(!skip) { // Read in identifier. int i; for(i=pos+1; i<blength(source); i++) { char ch = bchar(source, i); if(!(isalnum(ch) || ch == '_')) { break; } } identifier = bmidstr(source, pos, i-pos); check_mem(identifier); if(blength(identifier)) { sky_property *property = NULL; rc = sky_property_file_find_by_name(property_file, identifier, &property); check(rc == 0, "Unable to find property by name: %s", bdata(identifier)); check(property != NULL, "Property not found: %s", bdata(identifier)); if(!lookup[property->id-SKY_PROPERTY_ID_MIN]) { // Append property definition to event decl and function. switch(property->data_type) { case SKY_DATA_TYPE_STRING: { bformata(*event_decl, " char %s[];\n", bdata(property->name)); break; } case SKY_DATA_TYPE_INT: { bformata(*event_decl, " int64_t %s;\n", bdata(property->name)); break; } case SKY_DATA_TYPE_DOUBLE: { bformata(*event_decl, " double %s;\n", bdata(property->name)); break; } case SKY_DATA_TYPE_BOOLEAN: { bformata(*event_decl, " bool %s;\n", bdata(property->name)); break; } default:{ sentinel("Invalid sky lua type: %d", property->data_type); } } check_mem(*event_decl); bformata(*init_descriptor_func, " ffi.C.sky_data_descriptor_set_property(descriptor, %d, ffi.offsetof(\"sky_lua_event_t\", \"%s\"), %d);\n", property->id, bdata(property->name), property->data_type); check_mem(*init_descriptor_func); // Flag the property as already processed. lookup[property->id - SKY_PROPERTY_ID_MIN] = true; } } bdestroy(identifier); identifier = NULL; } } // Wrap properties in a struct. bassignformat(*event_decl, "typedef struct {\n%s} sky_lua_event_t;", bdata(*event_decl)); check_mem(*event_decl); // Wrap info function. bassignformat(*init_descriptor_func, "function sky_init_descriptor(_descriptor)\n" " descriptor = ffi.cast(\"sky_data_descriptor_t*\", _descriptor)\n" "%s" "end\n", bdata(*init_descriptor_func) ); check_mem(*init_descriptor_func); return 0; error: bdestroy(identifier); bdestroy(*event_decl); *event_decl = NULL; bdestroy(*init_descriptor_func); *init_descriptor_func = NULL; return -1; }