void dhashclient::retrieve (bigint key, dhash_ctype ct, cb_cret cb, str& data, ptr<option_block> options) { ref<dhash_retrieve_res> res = New refcounted<dhash_retrieve_res> (DHASH_OK); dhash_retrieve_arg arg; arg.blockID = key; arg.ctype = ct; arg.options = 0; if (options) { arg.options = options->flags; if (options->flags & DHASHCLIENT_GUESS_SUPPLIED) { arg.guess = options->guess; //warn << "starting with guess: " << arg.guess << "\n"; //warn << "using guess\n"; } } // Praveen if (data.len()) { arg.data.setsize (data.len()); memcpy (arg.data.base (), data.cstr(), data.len()); } gwclnt->call (DHASHPROC_RETRIEVE, &arg, res, wrap (this, &dhashclient::retrievecb, cb, key, res)); }
str html_wss (str in) { if (!in) return in; mstr out (in.len ()); char *op = out.cstr (); const char *ip = in.cstr (); const char *ep = ip + in.len (); bool eat_ws_state = false; for ( ; ip < ep; ip++) { if (isspace (*ip)) { if (!eat_ws_state) { eat_ws_state = true; } } else { if (eat_ws_state) { *op++ = ' '; eat_ws_state = false; } *op++ = *ip; } } if (eat_ws_state) { *op++ = ' '; } out.setlen (op - out.cstr ()); return out; }
void dhblock_keyhash_srv::real_store (chordID key, str od, str nd, u_int32_t exp, cb_dhstat cb) { u_int32_t v1 = dhblock_keyhash::version (nd.cstr (), nd.len ()); if (od.len ()) { u_int32_t v0 = dhblock_keyhash::version (od.cstr (), od.len ()); if (v0 > v1) { chordID p = node->my_pred ()->id (); chordID m = node->my_ID (); if (betweenrightincl (p, m, key)) cb (DHASH_STALE); else cb (DHASH_RETRY); } else { info << "db delete: " << key << "\n"; db->remove (key, v0, wrap (this, &dhblock_keyhash_srv::delete_cb, key, nd, v1, exp, cb)); } } else { info << "db write: " << node->my_ID () << " N " << key << " " << nd.len () << "\n"; db_store (key, nd, v1, exp, cb); } }
inline str group_prefix (str s, str prefix) { if (s.len () <= prefix.len () || s[prefix.len ()] != '.' || memcmp (s.cstr (), prefix.cstr (), prefix.len ())) return NULL; return substr (s, prefix.len () + 1); }
bool siglog (const str &line) { if (!line) return false; int n = write (logfd, line.cstr (), line.len ()); if (n < int (line.len ())) return false; return true; }
void suio_print (suio *uio, const str &s) { if (s.len () <= suio::smallbufsize) uio->copy (s.cstr (), s.len ()); else { uio->print (s.cstr (), s.len ()); uio->iovcb (wrap (&s.b.Xplug, s.b.Xleak ())); } }
str my_tolower (const str &in) { if (!in) return in; mstr out (in.len ()); for (size_t i = 0; i < in.len (); i++) { out[i] = tolower (in[i]); } out.setlen (in.len ()); return out; }
str trunc_at_first_null (const str &s) { assert (s); const char *cp; size_t len = s.len (); size_t l; const char nullc = '\0'; // Figure out if a '\0' or the end-of-string comes first. for (cp = s.cstr (), l = 0; *cp != nullc && l < len; cp++, l++); str ret; if (*cp == nullc) { // The most likely outcome if (l == len) ret = s; // There was a '\0' inside the string else ret = s.cstr (); } else { // String is not null terminated! mstr m (len + 1); memcpy (m.cstr (), s.cstr (), len); m[len] = nullc; ret = m; } return ret; }
bool basename_dirname (const str &in, str *d, str *b) { size_t l = in.len (); if (l == 0) { *d = *b = NULL; return false; } bool ret = (in[0] == '/'); const char *bp = in.cstr (); const char *ep = bp + l - 1; const char *p; for ( p = ep; p >= bp && *p != '/'; p--) ; // Set the basename if possible if (p == ep) { *b = NULL; } else { *b = str (p + 1); } // if there were multiple slashes keep backing up for ( ; p >= bp && *p == '/' ; p-- ); if (p >= bp) { assert (*p != '/'); *d = str (bp, p - bp + 1); } else { *d = NULL; } return ret; }
bool is_safe (const str &d) { if (!d || d[0] == '.' || d[d.len () - 1] == '.' || safe_rxx.search (d)) return false; return true; }
sth_t mysql_t::prepare (const str &q, u_int l_opts) { if (l_opts & AMYSQL_DEFAULT) l_opts = opts; sth_t *rp = cache[q]; if (rp) return (*rp); sth_t r = NULL; if (l_opts & AMYSQL_PREPARED) { #if defined(HAVE_MYSQL_BINDFUNCS) && defined(HAVE_MYSQL_BIND) MYSQL_STMT *s = mysql_stmt_init (&mysql); if (!s) { err = strbuf ("MySQL ran out of memory on statment init: ") << mysql_error (&mysql); return NULL; } if (mysql_stmt_prepare (s, q, q.len ())) { err = strbuf ("could not prepare query (") << q << "): " << mysql_error (&mysql); return NULL; } r = sth_prepared_t::alloc (s, q, l_opts); #endif // HAVE_MYSQL_BINDFUNCS && HAVE_MYSQL_BIND } else { ptr<sth_parsed_t> r2 = sth_parsed_t::alloc (&mysql, q, l_opts); if (!r2->parse ()) return NULL; r = r2; } if (!(l_opts & AMYSQL_NOCACHE)) cache.insert (q, r); return r; }
//for looking up self-certifying hostnames in certprog interface int path2sch (str path, str *sch) { *sch = NULL; if (!path || !path.len ()) return ENOENT; if (sfs_parsepath (path)) { *sch = path; return 0; } str lookup; if (path[0] == '/') lookup = path; else lookup = strbuf () << sfsroot << "/" << path; str result = pathiterate (lookup, sch, 0, true); // warn << "RESULT: " << result << "\n"; if (errno == EINVAL) errno = 0; // if (*sch) // warn << "RETURNING: " << *sch << "\n"; // warn << "RETURNING ERROR CODE: " << strerror (errno) << "\n"; return errno; }
void outputter_t::output_str (str s) { if (_mode == OUTPUT_TREADMILL) { static rxx x ("\n"); vec<str> v; split (&v, x, s); for (u_int i = 0; i < v.size (); i++) { output_line_number (); _output_str (v[i], "\n"); } } else { // we might have set up a defered output_line_number from // within switch_to_mode; now is the time to do it. if (s.len () && _do_output_line_number) { output_line_number (); _do_output_line_number = false; } _output_str (s, NULL); if (_mode == OUTPUT_PASSTHROUGH) _lineno += count_newlines (s); } }
wide_str_t::wide_str_t (str utf8_in) { if (utf8_in) { _init (utf8_in.len ()); mbstate_t state; memset (&state, 0, sizeof (state)); const char *src = utf8_in.cstr (); setlocale (LC_CTYPE, "en_US.UTF-8"); ssize_t ret = mbstowcs (_buf->base (), src, _buf->size ()); if (ret < 0) { _err = true; _buf = NULL; _len = 0; } else { _len = ret; if (0 && src) { warn << "XX failed to completely convert string ('" << utf8_in << "'): " << "expected " << _len << " bytes, but only converted " << (src - utf8_in.cstr ()) << "\n"; _err = true; } else { _err = false; } } } }
void resolv_conf::reload_cb (ref<bool> d, bool failure, str newres) { if (*d) return; nbump = 0; reload_lock = false; last_reload = timenow; if (!newres) { warn ("resolv_conf::reload_cb: fork: %m\n"); setsock (true); return; } if (newres.len () != sizeof (_res)) { warn ("resolv_conf::reload_cb: short read\n"); setsock (true); return; } char oldnsaddr[sizeof (_res.nsaddr_list)]; memcpy (oldnsaddr, _res.nsaddr_list, sizeof (oldnsaddr)); memcpy (&_res, newres, sizeof (_res)); if (memcmp (oldnsaddr, _res.nsaddr_list, sizeof (oldnsaddr))) { warn ("reloaded DNS configuration (resolv.conf)\n"); ns_idx = _res.nscount ? _res.nscount - 1 : 0; //nbump = 0; last_reload = timenow; setsock (true); } else setsock (failure); }
int split (vec<str> *out, rxx pat, str expr, size_t lim, bool emptylast) { const char *p = expr; const char *const e = p + expr.len (); size_t n; if (out) out->clear (); // check p < e to see that we're not dealing with an empty // string (especially since x? matches ""). for (n = 0; p < e && n + 1 < lim; n++) { if (!pat._exec (p, e - p, 0)) { return 0; } if (!pat.success ()) break; if (out) out->push_back (str (p, pat.start (0))); p += max (pat.end (0), 1); } if (lim && (p < e || emptylast)) { n++; if (out) { out->push_back (str (p, e - p)); } } return n; }
bool repl_t::parse (str in) { const char *bp = in.cstr (); const char *ep = bp + in.len (); const char *last = bp; size_t inc = 1; for (const char *p = bp; p < ep; p += inc) { bool is_dig = false; inc = 1; if (p == ep - 1 || *p != '$') { /* noop */ } else if (p[1] == '$' || (is_dig = (isdigit (p[1])))) { add_str (bp, last, p, ep); inc = 2; if (is_dig) { add_capture (p[1] - '0'); last = p + 2; } else { last = p + 1; } } } add_str (bp, last, ep, ep); return true; }
void pub3::msgpack::outbuf_t::encode_str (str s) { assert (s); size_t l = s.len (); encode_raw_len (l, 0xa0, 0xda, 0xdb); put_str (s); }
void acl::fix_aclstr (str s, char *buf) { char sbuf[ACLSIZE]; int min = (ACLSIZE > s.len ()) ? s.len () : ACLSIZE; bzero (buf, ACLSIZE); bzero (sbuf, ACLSIZE); memcpy (sbuf, s.cstr (), min); str aclend (ENDACL "\n"); int tail = aclend.len (); char *c = strstr (sbuf, ENDACL); int p = c ? c - sbuf : 0; int total = p ? p + tail : s.len () + tail; if (!p) warn << "ACLEND not found in ACL \n"; if (total <= ACLSIZE) { memcpy (buf, s.cstr (), p); memcpy (buf + p, aclend.cstr (), aclend.len ()); return; } if (total > ACLSIZE) { if (p) warn << "ACLEND found at position " << p << "\n"; else warn << "ACLEND not found in ACL \n"; warn << "ACL does not fit in buffer. Truncating \n"; if ( p && p < (ACLSIZE - tail)) { warn << "Removing garbage after ACLEND \n"; memcpy (buf, s.cstr (), p); memcpy (buf + p, aclend.cstr (), aclend.len ()); return; } else { memcpy (buf, s.cstr (), ACLSIZE - tail); memcpy (buf + ACLSIZE - tail, aclend.cstr (), tail); return; } } }
str dir_standardize (const str &s) { const char *bp = s.cstr (); const char *ep = bp + s.len () - 1; for ( ; bp <= ep && *ep == '/'; ep--) ; ep++; return str (bp, ep - bp); }
void dhblock_keyhash_srv::delete_cb (chordID k, str d, u_int32_t v, u_int32_t exp, cb_dhstat cb, adb_status stat) { assert (stat == ADB_OK); info << "db write: " << node->my_ID () << " U " << k << " " << d.len () << "\n"; db_store (k, d, v, exp, cb); }
bool has_null_byte (const str &s) { bool ret = false; for (size_t i = 0; !ret && i < s.len (); i++) { if (!s[i]) { ret = true; } } return ret; }
static int start_log_to_file (const str &line, const str &logfile, int flags, mode_t m) { int fd; int n; if ((fd = open (logfile.cstr (), flags, m)) < 0) { warn ("%s: %m\n", logfile.cstr ()); fd = errfd; } else { warn << "Logging via logfile: " << logfile << "\n"; if (line) { n = write (fd, line.cstr (), line.len ()); if (n < (int (line.len ()))) warn << logfile << ": write to logfile failed\n"; } } return fd; }
sfs_core::select_policy_t sfs_core::select_policy_from_str (const str &s) { sfs_core::select_policy_t ret = SELECT_NONE; if (s && s.len () > 0) { char c = s[0]; ret = select_policy_from_char (c); } return ret; }
str trunc_after_null_byte (str s) { str ret; size_t l; if (!s) { ret = s; } else if ((l = strlen (s.cstr())) == s.len ()) { ret = s; } else { ret = str (s.cstr (), l); } return ret; }
static bool find_non_std_char (const str &s) { const u_int8_t *p = reinterpret_cast<const u_int8_t *> (s.cstr ()); const u_int8_t *ep = p + s.len (); for ( ; p < ep; p++) { if (*p <= 0x1f || *p > 0x7f) { return true; } } return false; }
u_int count_newlines (str s) { const char *end = s.cstr () + s.len (); u_int c = 0; for (const char *cp = s.cstr (); cp < end; cp++) { if (*cp == '\n') c++; } return c; }
void printtable (ptr<locationtable> locs, str header, str prefix) { if (header.len ()) warnx << header << "\n"; ptr<location> l = locs->first_loc (); while (l) { warnx << prefix << l->id () << " " << l->alive () << "\n"; l = locs->next_loc (l->id ()); } }
bool cicmp (const str &s1, const char *c2, u_int l) { if (s1.len () != l) return false; const char *c1 = s1.cstr (); for (u_int i = 0; i < l; i++) if (tolower (c1[i]) != tolower (c2[i])) return false; return true; }
dnsreq::dnsreq (resolver *rp, str n, u_int16_t t, bool search) : ntries (0), resp (rp), usetcp (false), constructed (false), error (0), type (t) { while (n.len () && n[n.len () - 1] == '.') { search = false; n = substr (n, 0, n.len () - 1); } if (!search) { srchno = -1; basename = NULL; name = n; } else { srchno = 0; basename = n; name = NULL; } start (false); constructed = true; }