static int push_code_block(lang_c_ctx_t *ctx, parser_t *p, const char *buf, size_t sz) { code_block_t *node; dbg_return_if (p == NULL, ~0); dbg_return_if (ctx == NULL, ~0); node = (code_block_t*)u_zalloc(sizeof(code_block_t)); dbg_err_if(node == NULL); node->sz = sz; node->buf = (char*)u_malloc(sz); dbg_err_if(node->buf == NULL); node->code_line = p->code_line; node->file_in = ctx->ti->file_in; memcpy(node->buf, buf, sz); TAILQ_INSERT_TAIL(&ctx->code_blocks, node, np); return 0; err: if(node) free_code_block(node); return ~0; }
/** * \brief Fill a buffer object with the content of a file * * Open \p filename and copy its whole content into the given buffer \p ubuf * * \param ubuf an already allocated ::u_buf_t object * \param filename path of the source file * * \retval 0 on success * \retval ~0 on failure */ int u_buf_load (u_buf_t *ubuf, const char *filename) { struct stat st; FILE *fp = NULL; dbg_return_if (ubuf == NULL, ~0); dbg_return_if (filename == NULL, ~0); dbg_err_sif (stat(filename, &st) == -1); /* clear the current data */ dbg_err_if (u_buf_clear(ubuf)); /* be sure to have a big enough buffer */ dbg_err_if (u_buf_reserve(ubuf, st.st_size)); dbg_err_sifm ((fp = fopen(filename, "r")) == NULL, "%s", filename); /* fill the buffer with the whole file content */ dbg_err_if (fread(ubuf->data, st.st_size, 1, fp) != 1); ubuf->len = st.st_size; (void) fclose(fp); return 0; err: U_FCLOSE(fp); return ~0; }
/** * \brief Convert an asctime(3) string to \c time_t * * Convert the asctime(3) string \p str to its \c time_t representation \p tp. * * \param str the string to be converted * \param tp the \c time_t conversion of \p str as a value-result * argument * \return * - \c 0 successful * - \c ~0 failure */ int u_asctime_to_tt(const char *str, time_t *tp) { enum { BUFSZ = 64 }; char wday[BUFSZ], mon[BUFSZ]; unsigned int day, year, hour, min, sec; struct tm tm; int i; dbg_return_if (str == NULL, ~0); dbg_return_if (tp == NULL, ~0); dbg_return_if (strlen(str) >= BUFSZ, ~0); dbg_err_if((i = sscanf(str, "%s %s %u %u:%u:%u %u", wday, mon, &day, &hour, &min, &sec, &year)) != 7); memset(&tm, 0, sizeof(struct tm)); /* time */ tm.tm_sec = sec; tm.tm_min = min; tm.tm_hour = hour; /* date */ tm.tm_mday = day; tm.tm_mon = month_idx(mon); tm.tm_year = year - 1900; dbg_err_if(tm.tm_mon < 0); *tp = timegm(&tm); return 0; err: return ~0; }
/* remove a child process whose pid is 'pid' to children list */ static int server_reap_child(server_t *s, pid_t pid) { child_t *child; backend_t *be; dbg_err_if (s == NULL); /* get the child object */ dbg_err_if(children_get_by_pid(s->children, pid, &child)); /* remove the child from the list */ dbg_err_if(children_del(s->children, child)); be = child->be; /* check that the minimum number of process are active */ be->nchild--; if(be->nchild < be->start_child) be->fork_child = be->start_child - be->nchild; U_FREE(child); return 0; err: return ~0; }
/** * \brief Convert a \c time_t value to a rfc822 time string * * Convert the \c time_t value \p ts to a rfc822 time string * * \param dst placeholder for the rfc822 time string. The buffer, * of at least RFC822_DATE_BUFSZ bytes, must be preallocated * by the caller. * \param ts the \c time_t value to be converted * * \return * - \c 0 successful * - \c ~0 failure */ int u_tt_to_rfc822(char dst[RFC822_DATE_BUFSZ], time_t ts) { char buf[RFC822_DATE_BUFSZ]; struct tm tm; dbg_return_if (dst == NULL, ~0); #ifdef OS_WIN memcpy(&tm, gmtime(&ts), sizeof(tm)); #else dbg_err_if(gmtime_r(&ts, &tm) == NULL); #endif dbg_err_if(tm.tm_wday > 6 || tm.tm_wday < 0); dbg_err_if(tm.tm_mon > 11 || tm.tm_mon < 0); dbg_err_if(u_snprintf(buf, sizeof buf, "%s, %02u %s %02u %02u:%02u:%02u GMT", days3[tm.tm_wday], tm.tm_mday, months[tm.tm_mon], tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec)); /* copy out */ u_strlcpy(dst, buf, RFC822_DATE_BUFSZ); return 0; err: return ~0; }
/** * \brief Parse an URI string and create the corresponding ::u_uri_t object * * Parse the NUL-terminated string \p uri and create an ::u_uri_t object at * \p *pu * * \param uri the NUL-terminated string that must be parsed * \param opts bitmask of or'ed ::u_uri_opts_t values * \param pu the newly created ::u_uri_t object containing the b * * \retval 0 on success * \retval ~0 on error */ int u_uri_crumble (const char *uri, u_uri_opts_t opts, u_uri_t **pu) { u_uri_t *u = NULL; int rc = 0; char es[1024]; regex_t re; regmatch_t pmatch[10]; dbg_return_if (uri == NULL, ~0); dbg_return_if (pu == NULL, ~0); dbg_err_if ((rc = regcomp(&re, uri_pat, REG_EXTENDED))); dbg_err_if ((rc = regexec(&re, uri, 10, pmatch, 0))); dbg_err_if (u_uri_new(opts, &u)); dbg_err_if (u_uri_fill(u, uri, pmatch)); regfree(&re); *pu = u; return 0; err: if (rc) { regerror(rc, &re, es, sizeof es); u_dbg("%s: %s", uri, es); } regfree(&re); if (u) u_uri_free(u); return ~0; }
static int so_atom_delete_oldest(session_opt_t *so) { atom_t *atom, *oldest; size_t count, i; dbg_err_if (so == NULL); count = atoms_count(so->atoms); nop_err_if(count == 0); dbg_err_if(atoms_getn(so->atoms, 0, &oldest)); for(i = 1; i < count; ++i) { dbg_err_if(atoms_getn(so->atoms, i, &atom)); /* save if older */ if(atom->arg <= oldest->arg) oldest = atom; } dbg_err_if(atoms_remove(so->atoms, oldest)); return 0; err: return ~0; }
/** * \ingroup vars * \brief Set binary value of a variable * * Set binary value of variable \p v. * * \param v variable object * \param data value data * \param size value size * * \return \c 0 if successful, non-zero on error */ int var_set_bin_value(var_t *v, const unsigned char *data, size_t size) { dbg_err_if (v == NULL); dbg_err_if (data == NULL); U_FREE(v->data); if(data && size) { v->size = size; v->data = u_malloc(size+1); dbg_err_if(v->data == NULL); memcpy(v->data, data, size); v->data[size] = 0; /* zero-term v->data so it can be used as a string */ } else { v->size = 0; v->data = NULL; } if(v->svalue) dbg_err_if(u_string_set(v->svalue, v->data, v->size)); return 0; err: return ~0; }
/* uninstall this service from the system */ int RemoveService(void) { SC_HANDLE hSCM, hService; int rc; hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); dbg_err_if(hSCM == NULL); /* Open this service for DELETE access */ hService = OpenService(hSCM, ss_name, DELETE); dbg_err_if(hService == NULL); /* Remove this service from the SCM's database */ rc = DeleteService(hService); dbg_err_if(rc == 0); /* success */ MessageBox(NULL, "Uninstall succeeded", ss_name, MB_OK); return 0; err: /* common error handling */ warn_strerror(GetLastError()); MessageBox(NULL, "Uninstall failed", ss_name, MB_OK); return ~0; }
int io_mem_create(char *buf, size_t size, int flags, io_t **pio) { io_mem_t *im = NULL; dbg_err_if (buf == NULL); dbg_err_if (pio == NULL); dbg_err_if(io_create(io_mem_t, (io_t**)&im)); im->io.type = IO_TYPE_MEM; im->buf = buf; im->size = size; im->flags = flags; im->off = 0; im->io.read = (io_read_op) io_mem_read; im->io.write = (io_write_op) io_mem_write; im->io.seek = (io_seek_op) io_mem_seek; im->io.tell = (io_tell_op) io_mem_tell; im->io.close = (io_close_op) io_mem_close; im->io.free = (io_free_op) io_mem_free; *pio = (io_t*)im; return 0; err: if(im) io_free((io_t *)im); return ~0; }
ec_pdu_t *kache_get_evcoap_response(kache_evcoap_t *ke, ec_client_t *cli, char *uri) { kache_obj_t *obj; dbg_err_if((obj = kache_get(ke->kache,uri))==NULL); ec_pdu_t *pdu; dbg_err_if((pdu = malloc(sizeof(ec_pdu_t))) == NULL); ec_opts_init(&pdu->opts); ec_pdu_t *req = &cli->req; ev_uint16_t ct; ec_opts_get_content_type( &req->opts, &ct); kache_content_type_t *mt; dbg_err_if((mt = malloc(sizeof(kache_content_type_t)))==NULL); kache_ct_from_ecct(mt,&ct); kache_rep_t *rep; rep = kache_get_rep_by_media_type(obj,mt); //Translation dbg_err_if(kache_rep_to_evcoap_pdu_(rep, pdu)); return pdu; err: return NULL; }
/** * \brief create a \c klog_t object reading configuration parameters from * a configuration "log" record * * \param ls a log configuration record * \param pkl the corresponding \c klog_args_t object as a value-result * argument * \return * - \c 0 success * - \c ~0 on failure (\p pka MUST not be referenced) */ int klog_open_from_config(u_config_t *ls, klog_t **pkl) { klog_t *kl = NULL; klog_args_t *kargs = NULL; /* parse config parameters */ dbg_err_if(klog_args(ls, &kargs)); /* create a klog object */ dbg_err_if(klog_open(kargs, &kl)); klog_args_free(kargs); kargs = NULL; /* stick it */ *pkl = kl; return 0; err: if(kargs) klog_args_free(kargs); if(kl) klog_close(kl); return ~0; }
static int session_mem_remove(session_t *ss) { ppc_t *ppc; dbg_err_if (ss == NULL); if(ctx->pipc) { /* children context */ ppc = server_get_ppc(ctx->server); dbg_err_if(ppc == NULL); dbg_err_if(ppc_write(ppc, ctx->pipc, PPC_CMD_MSES_REMOVE, ss->filename, strlen(ss->filename) + 1) < 0); /* remove the session-atom from the (local copy) atom list */ dbg_err_if(so_atom_remove(ss->so, ss->filename)); } else { /* parent context */ dbg_err_if(so_atom_remove(ss->so, ss->filename)); } return 0; err: return ~0; }
/* this function will be called once by the server during startup */ int session_client_module_init(u_config_t *config, session_opt_t *so) { u_config_t *c; const char *v; /* config may be NULL */ dbg_err_if (so == NULL); /* default */ so->hash = EVP_sha1(); /* always enable encryption for client-side sessions */ if(!so->encrypt) warn("encryption is required for client side session"); so->encrypt = 1; if(config && !u_config_get_subkey(config, "client", &c)) { if((v = u_config_get_subkey_value(c, "hash_function")) != NULL) { if(!strcasecmp(v, "md5")) so->hash = EVP_md5(); else if(!strcasecmp(v, "sha1")) so->hash = EVP_sha1(); #ifdef SSL_OPENSSL else if(!strcasecmp(v, "ripemd160")) so->hash = EVP_ripemd160(); #endif else warn_err("config error: bad hash_function"); } } #ifdef SSL_OPENSSL /* initialize OpenSSL HMAC stuff */ HMAC_CTX_init(&so->hmac_ctx); /* gen HMAC key */ dbg_err_if(!RAND_bytes(so->hmac_key, HMAC_KEY_LEN)); /* init HMAC with our key and chose hash algorithm */ HMAC_Init_ex(&so->hmac_ctx, so->hmac_key, HMAC_KEY_LEN, so->hash, NULL); #endif #ifdef SSL_CYASSL /* gen HMAC key */ dbg_err_if(!RAND_bytes(so->hmac_key, HMAC_KEY_LEN)); if(strcmp(so->hash, "MD5") == 0) HmacSetKey(&so->hmac_ctx, MD5, so->hmac_key, HMAC_KEY_LEN); else if(strcmp(so->hash, "SHA") == 0) HmacSetKey(&so->hmac_ctx, SHA, so->hmac_key, HMAC_KEY_LEN); #endif return 0; err: return ~0; }
/** * \brief Removes bytes from the tail of the buffer * * Set the new length of the buffer; \p newlen must be less or equal to the * current buffer length. * * \param ubuf a previously allocated buffer object * \param newlen new length of the buffer * * \return * \retval 0 on success * \retval ~0 on error */ int u_buf_shrink(u_buf_t *ubuf, size_t newlen) { dbg_err_if (ubuf == NULL); dbg_err_if (newlen > ubuf->len); ubuf->len = newlen; return 0; err: return ~0; }
char *io_mem_get_buf(io_t *io) { io_mem_t *im = (io_mem_t*)io; dbg_err_if(io == NULL); dbg_err_if(im->io.type != IO_TYPE_MEM); return im->buf; err: return NULL; }
/** * \ingroup vars * \brief Set the name of a variable * * Set the name of variable \p v * * \param v variable object * \param name variable name (null-terminated) * * \return \c 0 if successful, non-zero on error */ int var_set_name(var_t *v, const char *name) { dbg_err_if (v == NULL); dbg_err_if (name == NULL); dbg_err_if(u_string_set(v->sname, name, strlen(name))); return 0; err: return ~0; }
size_t io_mem_get_bufsz(io_t *io) { io_mem_t *im = (io_mem_t*)io; dbg_err_if(io == NULL); dbg_err_if(im->io.type != IO_TYPE_MEM); return im->size; err: return 0; }
/** * \ingroup vars * \brief Set the name and value of a variable * * Set variable \p var to \p name and \p value. * * \param var variable object * \param name string name (null-terminated) * \param value string value (null-terminated) * * \return \c 0 if successful, non-zero on error */ int var_set(var_t *var, const char *name, const char *value) { dbg_err_if (var == NULL); dbg_err_if (name == NULL); dbg_err_if (value == NULL); dbg_err_if(var_set_name(var, name)); dbg_err_if(var_set_value(var, value)); return 0; err: return ~0; }
/** * \brief Set the value of a buffer * * Explicitly set the value of \a ubuf to \a data. If needed the buffer * object will call ::u_buf_append to enlarge the storage needed to copy-in * the \a data value. * * \param ubuf a previously allocated ::u_buf_t object * \param data a reference to the memory block that will be copied into * the buffer * \param size size of \a data in bytes * * \retval 0 on success * \retval ~0 on error */ int u_buf_set (u_buf_t *ubuf, const void *data, size_t size) { dbg_return_if (ubuf == NULL, ~0); dbg_return_if (data == NULL, ~0); dbg_return_if (size == 0, ~0); dbg_err_if (u_buf_clear(ubuf)); dbg_err_if (u_buf_append(ubuf, data, size)); return 0; err: return ~0; }
mystruct_t *mystruct_create (void) { mystruct_t *myval = NULL; dbg_err_if ((myval = (mystruct_t *) u_zalloc(sizeof(mystruct_t))) == NULL); dbg_err_if ((myval->a = strdup("first string")) == NULL); dbg_err_if ((myval->b = strdup("second string")) == NULL); return myval; err: U_FREEF(myval, mystruct_free); return NULL; }
/* [parent] delete oldest session */ static int session_cmd_delold(ppc_t *ppc, int fd, unsigned char cmd, char *data, size_t size, void *vso) { session_opt_t *so = vso; u_unused_args(ppc, fd, cmd, data, size); dbg_err_if (vso == NULL); dbg_err_if (so_atom_delete_oldest(so)); return 0; err: return ~0; }
/** * \brief Create a new buffer * * Create a new buffer object and save its pointer to \a *ps. * * \param pubuf on success will get the new buffer object * * \return \c 0 on success, not zero on failure */ int u_buf_create(u_buf_t **pubuf) { u_buf_t *ubuf = NULL; dbg_err_if(pubuf == NULL); ubuf = (u_buf_t*)u_zalloc(sizeof(u_buf_t)); dbg_err_if(ubuf == NULL); *pubuf = ubuf; return 0; err: return ~0; }
int proxy_init(void) { dbg_err_if ((g_ctx.base = event_base_new()) == NULL); dbg_err_if ((g_ctx.dns = evdns_base_new(g_ctx.base, 1)) == NULL); dbg_err_if ((g_ctx.coap = ec_init(g_ctx.base, g_ctx.dns)) == NULL); dbg_err_if ((g_ctx.cache = ec_filesys_create(false)) == NULL); if (g_ctx.block_sz) dbg_err_if(ec_set_block_size(g_ctx.coap, g_ctx.block_sz)); return 0; err: proxy_term(); return -1; }
int server_init(void) { dbg_err_if((g_ctx.base = event_base_new()) == NULL); dbg_err_if((g_ctx.dns = evdns_base_new(g_ctx.base, 1)) == NULL); dbg_err_if((g_ctx.coap = ec_init(g_ctx.base, g_ctx.dns)) == NULL); dbg_err_if((g_ctx.fs = ec_filesys_create(g_ctx.rel_refs)) == NULL); if (g_ctx.bsz) dbg_err_if(ec_set_block_size(g_ctx.coap, g_ctx.bsz)); return 0; err: server_term(); return -1; }
int vhost_load_allowed_methods(const char *m, ec_method_mask_t *pmm) { size_t nelems, i; char **tv = NULL; dbg_return_if(m == NULL, -1); dbg_return_if(pmm == NULL, -1); *pmm = EC_METHOD_MASK_UNSET; dbg_err_if(u_strtok(m, " \t", &tv, &nelems)); for (i = 0; i < nelems; ++i) { if (!strcasecmp(tv[i], "GET")) *pmm |= EC_GET_MASK; else if (!strcasecmp(tv[i], "POST")) *pmm |= EC_POST_MASK; else if (!strcasecmp(tv[i], "PUT")) *pmm |= EC_PUT_MASK; else if (!strcasecmp(tv[i], "DELETE")) *pmm |= EC_DELETE_MASK; else dbg_err("unknown method %s", tv[i]); } u_strtok_cleanup(tv, nelems); return 0; err: if (tv) u_strtok_cleanup(tv, nelems); return -1; }
int vhost_load_contents(u_config_t *vhost, const char *origin) { size_t i; char wkc[1024] = { '\0' }; u_config_t *res, *contents; dbg_return_if(vhost == NULL, -1); dbg_return_if(origin == NULL, -1); /* Pick up the "contents" section. */ dbg_err_ifm(u_config_get_subkey(vhost, "contents", &contents), "no contents in virtual host !"); /* Load hosted resources. */ for (i = 0; (res = u_config_get_child_n(contents, "resource", i)) != NULL; ++i) { dbg_err_ifm(vhost_load_resource(res, origin), "error loading resource"); } dbg_err_ifm(i == 0, "no resources in virtual host"); /* Add the default /.well-known/core interface. */ dbg_err_if(u_snprintf(wkc, sizeof wkc, "%s/.well-known/core", origin)); CHAT("adding resource %s (AUTO)", wkc); dbg_err_ifm(ec_register_cb(g_ctx.coap, wkc, serve, NULL), "registering callback for %s failed", wkc); return 0; err: return -1; }
static ssize_t gzip_transform(codec_t *codec, char *dst, size_t *dcount, const char *src, size_t src_sz) { codec_gzip_t *iz; size_t consumed; dbg_return_if (codec == NULL, -1); dbg_return_if (src == NULL, -1); dbg_return_if (dst == NULL, -1); dbg_return_if (dcount == NULL || *dcount == 0, -1); dbg_return_if (src_sz == 0, -1); iz = (codec_gzip_t*)codec; iz->zstr.next_out = dst; iz->zstr.avail_out = *dcount; iz->zstr.next_in = (char*)src; iz->zstr.avail_in = src_sz; iz->err = iz->op(&iz->zstr, Z_NO_FLUSH); dbg_err_if(iz->err != Z_OK && iz->err != Z_STREAM_END); consumed = src_sz - iz->zstr.avail_in; /* consumed */ *dcount = *dcount - iz->zstr.avail_out; /* written */ return consumed; /* # of consumed input bytes */ err: u_dbg("%s", zError(iz->err)); return -1; }
/** * \brief Append a NUL-terminated string to a string object * * Append the NUL-terminated string \p buf to the ::u_string_t object \p s * * \param s an ::u_string_t object * \param buf NUL-terminated string that will be appended to \p s * \param len length of \p buf * * \retval 0 on success * \retval ~0 on failure */ int u_string_append (u_string_t *s, const char *buf, size_t len) { size_t nsz, min; dbg_return_if (s == NULL, ~0); dbg_return_if (buf == NULL, ~0); nop_return_if (len == 0, 0); /* nothing to do */ /* if there's not enough space on s->data alloc a bigger buffer */ if (s->data_len + len + 1 > s->data_sz) { min = s->data_len + len + 1; /* min required buffer length */ nsz = s->data_sz; while (nsz <= min) nsz += (BLOCK_SIZE << s->shift_cnt++); dbg_err_if (u_string_reserve(s, nsz)); } /* append this chunk to the data buffer */ strncpy(s->data + s->data_len, buf, len); s->data_len += len; s->data[s->data_len] = '\0'; return 0; err: return ~0; }
/* [parent] remove a sessioin*/ static int session_cmd_remove(ppc_t *ppc, int fd, unsigned char cmd, char *data, size_t size, void *vso) { session_opt_t *so = vso; u_unused_args(ppc, fd, cmd, size); dbg_err_if (data == NULL); dbg_err_if (vso == NULL); dbg_err_if(so_atom_remove(so, data /* filename */)); return 0; err: return ~0; }