// ========================================================================== // METHOD udpsocket::sendto // ========================================================================== bool udpsocket::sendto (ipaddress addr, int port, const string &data) { struct sockaddr_in6 remote_addr; memset(&remote_addr, 0, sizeof(remote_addr)); remote_addr.sin6_addr = addr; remote_addr.sin6_port = htons (port); remote_addr.sin6_family = AF_INET6; if (::sendto (sock, data.str (), data.strlen (), 0, (struct sockaddr *) &remote_addr, sizeof (remote_addr)) <0) { return false; } return true; }
bool sqlitehandle::query (const string &sql, value &into, const statstring &indexby) { sqlite3_stmt *qhandle; int qres; int rowcount=0; int colcount; int i; statstring curidx; bool done = false; into.clear (); if (sqlite3_prepare (hdl, sql.str(), sql.strlen(), &qhandle, 0) != SQLITE_OK) { errcode = 1; errstr = "Could not prepare: %s" %format (sqlite3_errmsg(hdl)); return false; } if (! (qres = sqlite3_step (qhandle))) { errcode = 1; errstr = "Error making first step: %s" %format (sqlite3_errmsg(hdl)); sqlite3_finalize (qhandle); return false; } colcount = sqlite3_column_count (qhandle); if (colcount == 0) { into("rowschanged") = sqlite3_changes (hdl); sqlite3_finalize (qhandle); return true; } statstring colnames[colcount]; value colidx; for (i=0; i<colcount; ++i) { colnames[i] = sqlite3_column_name (qhandle, i); colidx[colnames[i]] = i; } int indexfield = -1; if (colidx.exists (indexby)) indexfield = colidx[indexby]; if (! done) do { switch (qres) { case SQLITE_BUSY: sleep (1); sqlite3_reset(qhandle); continue; case SQLITE_MISUSE: // achtung, fallthrough case SQLITE_ERROR: errcode = 1; errstr = "Error in sqlite3_step: %s" %format (sqlite3_errmsg(hdl)); done = true; break; case SQLITE_DONE: done = true; break; case SQLITE_ROW: { if (indexfield>=0) { curidx = sqlite3_column_text (qhandle, indexfield); } value &myrow = (indexfield<0) ? into.newval() : into[curidx]; for (int i=0; i<colcount; i++) { int ctype = sqlite3_column_type (qhandle, i); statstring &curcol = colnames[i]; switch (ctype) { case SQLITE_INTEGER: myrow[curcol] = sqlite3_column_int (qhandle, i); break; case SQLITE_FLOAT: myrow[curcol] = sqlite3_column_double (qhandle, i); break; case SQLITE_BLOB: // FIXME: use sqlite3_column_blob case SQLITE_TEXT: myrow[curcol] = sqlite3_column_text (qhandle, i); break; default: break; } } } break; } rowcount++; } while ((qres = sqlite3_step (qhandle)) && !done); int finalize_result = sqlite3_finalize (qhandle); if (finalize_result != SQLITE_OK && finalize_result != SQLITE_SCHEMA) { errcode = 1; errstr = "Error finalizing: %s" %format (sqlite3_errmsg(hdl)); return false; } into ("insertid") = sqlite3_last_insert_rowid (hdl); into.type (t_dict); string tmp = into.tojson(); return true; }
$("4089", $("description","OpenPanel Interface") -> $("state", "permit") -> $("filter", "tcp") ) ) ) ) -> $("OpenCORE:Result", $("code", moderr::ok) -> $("message", "OK") ); string result; result = retval.toxml (false); fout.printf ("%u\n", result.strlen()); fout.puts (result); return true; } // ============================================= // METHOD: iptablesmodule::readconfiguration // ============================================= bool iptablesmodule::deleteconfiguration (const value &v) { // // No configuration can be deleted, this module // only uses updates //
bool operator<=(const char* str1,const string& str2){ return (str2.comparestr(str1,0,str2.datalen,0,str2.strlen(str1))<=0);}
bool operator>=(const string& str1,const char* str2){ return (str1.comparestr(str2,0,str1.datalen,0,str1.strlen(str2))>=0);}
// ========================================================================== // METHOD RPCRequestHandler::run // ========================================================================== int RPCRequestHandler::run (string &uri, string &postbody, value &inhdr, string &out, value &outhdr, value &env, tcpsocket &s) { try { DEBUG.storeFile ("RPCRequestHandler","postbody", postbody, "run"); CORE->log (log::debug, "RPC", "handle: %S %!" %format (uri, inhdr)); value indata; value res; string origin = "rpc"; uid_t uid = 0; RPCHandler hdl (sdb); indata.fromjson (postbody); if (inhdr.exists ("X-OpenCORE-Origin")) { origin = inhdr["X-OpenCORE-Origin"]; } CORE->log (log::debug, "RPC", "body: %!" %format (indata)); // Set up credentials if available s.getcredentials(); CORE->log (log::debug, "RPC", "credentials: %d %d %d", s.peer_uid, s.peer_gid, s.peer_pid); if (s.peer_pid == 0) { string peer_name = s.peer_name; if (peer_name == "127.0.0.1") { if (inhdr.exists ("X-Forwarded-For")) { peer_name = inhdr["X-Forwarded-For"]; } } if (origin.strchr ('/') >0) origin = origin.cutat ('/'); if (! origin) origin = "RPC"; origin.strcat ("/src=%s" %format (peer_name)); env["ip"] = s.peer_name = peer_name; } if (indata.exists ("header") && indata["header"].exists ("command")) { uri.strcat ("/%s" %format (indata["header"]["command"])); } res = hdl.handle (indata, s.peer_uid, origin); out = res.tojson (); if (inhdr.exists ("Accept-Encoding")) { string ae = inhdr["Accept-Encoding"]; if (ae.strstr ("deflate") >= 0) { unsigned long reslen = (out.strlen() * 1.05) + 12; char buf[reslen]; if (compress2 ((Bytef*) buf, &reslen, (const Bytef*) out.str(), out.strlen(), 4) == Z_OK) { outhdr["Content-Encoding"] = "deflate"; out.strcpy (buf+2, reslen-2); } else { log::write (log::warning, "RPC", "Compress error"); } } } outhdr["Content-type"] = "application/json"; } catch (...) { log::write (log::error, "RPC", "Exception caught"); } return HTTP_OK; }
bool value::loadcsv (const string &filename, bool withHeaders, const string &key) { file csvFile; if (! csvFile.openread (filename)) return false; value headerNames; int rowc, colc; int numColumns; bool withKey; int keyPos; value columnSplit; string row; clear(); if (key.strlen()) withKey = true; else withKey = false; keyPos = 0; if (csvFile.eof()) { csvFile.close(); return false; } numColumns = 0; if (withHeaders) { row = csvFile.gets(); __csv_breakme(); columnSplit = strutil::splitcsv(row); numColumns = columnSplit.count(); if (! numColumns) { csvFile.close(); return false; } for (colc=0; colc<numColumns; ++colc) { if (withKey && (columnSplit[colc].sval() == key)) { keyPos = colc; } headerNames.newval() = columnSplit[colc].sval(); } } try { while (! csvFile.eof()) { row = csvFile.gets(); if (row.strlen()) { columnSplit = strutil::splitcsv (row); if (withKey) { (*this)[columnSplit[keyPos].sval()].type ("row"); } else { newval("row"); } for (colc=0; colc<numColumns; ++colc) { if (withHeaders) { if (colc != keyPos) { (*this)[-1][headerNames[colc].sval()] = columnSplit[colc]; } } else { (*this)[-1].newval() = columnSplit[colc]; } } } } } catch (...) { } csvFile.close(); return true; }
// ======================================================================== // METHOD ::fromcsv // ---------------- // Converts a string containing CSV format data to a two-dimensional array // of values. // ======================================================================== bool value::fromcsv (const string &csvData, bool withHeaders, const string &key) { value headerNames; int rowc, colc; bool withKey; int keyPos; int numColumns, numRows; value rowSplit; value columnSplit; string row; if (key.strlen()) withKey = true; else withKey = false; keyPos = 0; clear(); rowSplit = strutil::splitlines (csvData); rowc = 0; numRows = rowSplit.count(); numColumns = 0; if (! numRows) return false; if (withHeaders) { row = rowSplit[0].sval(); columnSplit = strutil::splitcsv (row); numColumns = columnSplit.count(); if (! numColumns) return false; for (colc=0; colc<numColumns; ++colc) { headerNames.newval() = columnSplit[colc].sval(); if (headerNames[-1].sval() == key) { keyPos = colc; } } ++rowc; } for (; rowc<numRows; ++rowc) { row = rowSplit[rowc].sval(); if (row.strlen()) { columnSplit = strutil::splitquoted (row, ','); if (withKey) { (*this)[columnSplit[keyPos].sval()].type ("row"); } else { newval("row"); } for (colc=0; colc<numColumns; ++colc) { if (withHeaders) { (*this)[-1][headerNames[colc].sval()] = columnSplit[colc]; } else { (*this)[-1].newval() = columnSplit[colc]; } } } } return true; }