Esempio n. 1
0
File: array.c Progetto: fdotli/libu
/**
 *  \brief  Create a new array object
 *
 *  \param t        the type of the elements in this array, i.e. one of 
 *                  the standard C types (which have 1:1 mapping with 
 *                  \c U_ARRAY_TYPE_*'s) or a pointer type (select 
 *                  \c U_ARRAY_TYPE_PTR in this case)
 *  \param nslots   the initial number of slots to be created  (set it to \c 0 
 *                  if you want the default)
 *  \param pda      the newly created array object as a result argument
 *
 *  \retval  0  on success 
 *  \retval -1  on error
 */
int u_array_create (u_array_type_t t, size_t nslots, u_array_t **pda)
{
    u_array_t *da = NULL;
    size_t max_nslots;

    dbg_return_if (pda == NULL, -1);
    dbg_return_if (!U_ARRAY_TYPE_IS_VALID(t), -1);

    da = u_zalloc(sizeof(u_array_t));
    warn_err_sif (da == NULL);

    da->type = t;

    if (nslots == 0)
        da->nslots = U_ARRAY_NSLOTS_DFL;
    else if (nslots > (max_nslots = MAX_NSLOTS(da)))
        da->nslots = max_nslots;
    else
        da->nslots = nslots;

    da->base = u_zalloc(da->nslots * sizeof_type[da->type]);
    warn_err_sif (da->base == NULL);

    *pda = da;

    return 0;
err:
    u_array_free(da);
    return -1;
}
Esempio n. 2
0
/**
 *  \brief  Make room for a new ::u_uri_t object
 *
 *  Make room for a new ::u_uri_t object at \p *pu.  The returned object is 
 *  completely empty: use the needed setter methods to fill it before passing
 *  it to the encoder.
 *
 *  \param  opts    bitmask of or'ed ::u_uri_opts_t values
 *  \param  pu      Reference to an ::u_uri_t that, on success, will point to 
 *                  the newly created object
 *
 *  \retval  0  on success
 *  \retval ~0  on error
 */ 
int u_uri_new (u_uri_opts_t opts, u_uri_t **pu)
{
    u_uri_t *u = NULL;

    dbg_return_if (pu == NULL, ~0);

    dbg_err_sif ((u = u_zalloc(sizeof(u_uri_t))) == NULL);

    u->scheme = NULL;
    u->userinfo = NULL;
    u->user = NULL;
    u->pwd = NULL;
    u->host = NULL;
    u->port = NULL;
    u->authority = NULL;
    u->path = NULL;
    u->query = NULL;
    u->fragment = NULL;
    u->opts = opts;

    *pu = u;

    return 0;
err:
    if (u)
        u_uri_free(u);
    return ~0;
}
Esempio n. 3
0
ec_rep_t *ec_rep_new(ec_res_t *res, const uint8_t *data, size_t data_sz, 
        ec_mt_t media_type)
{
    ec_rep_t *rep = NULL;

    dbg_err_sif ((rep = u_zalloc(sizeof *rep)) == NULL);
    rep->res = res;

    if (data && data_sz)
    {
        dbg_err_if ((rep->data = u_memdup(data, data_sz)) == NULL);
        rep->data_sz = data_sz;
    }

    rep->media_type = media_type;

    /* Attach a random etag on registration. */
    evutil_secure_rng_get_bytes(rep->etag, sizeof rep->etag);

    return rep;
err:
    if (rep)
        ec_rep_free(rep); 
    return NULL;
}
Esempio n. 4
0
/**
 * Create XML document
 * @param soap SOAP handler
 * @param rootNsUri Root Namespace URI
 * @param rootName Root node name
 * @return XML document
 */
WsXmlDocH
ws_xml_create_doc( const char *rootNsUri, const char *rootName)
{
	WsXmlDocH wsDoc = (WsXmlDocH) u_zalloc(sizeof(*wsDoc));
	WsXmlNodeH rootNode;
	WsXmlNsH ns;
	char prefix[12];

	if (wsDoc == NULL) {
		error("No memory");
		return NULL;
	}
	if (!xml_parser_create_doc(wsDoc, rootName) ) {
		error("xml_parser_create_doc failed");
		u_free(wsDoc);
		return NULL;
	}

	if (rootNsUri == NULL) {
		return wsDoc;
	}

	rootNode = ws_xml_get_doc_root((WsXmlDocH) wsDoc);

	ws_xml_make_default_prefix(rootNode, rootNsUri, prefix,
				   sizeof(prefix));
	ns = xml_parser_ns_add(rootNode, rootNsUri, prefix);
	if (ns == NULL) {
		error("xml_parser_ns_add failed");
		ws_xml_destroy_doc(wsDoc);
		return NULL;
	}
	ws_xml_set_node_name(rootNode, rootNsUri, NULL);
	return wsDoc;
}
CimxmlMessage *cimxml_message_new() {
	CimxmlMessage *cimxml_msg = u_zalloc(sizeof(CimxmlMessage));
	u_buf_create(&cimxml_msg->request);
	u_buf_create(&cimxml_msg->response);
	cimxml_msg->status.code = CIMXML_STATUS_OK;
	return cimxml_msg;
}
Esempio n. 6
0
int session_mem_create(session_opt_t *so, request_t *rq, response_t *rs, 
        session_t **pss)
{
    session_t *ss = NULL;

    dbg_err_if (so == NULL);
    dbg_err_if (rq == NULL);
    dbg_err_if (rs == NULL);
    dbg_err_if (pss == NULL);

    ss = u_zalloc(sizeof(session_t));
    dbg_err_if(ss == NULL);

    ss->load = session_mem_load;
    ss->save = session_mem_save;
    ss->remove = session_mem_remove;
    ss->term = session_mem_term;
    ss->mtime = time(0);
    ss->so = so;

    dbg_err_if(session_prv_init(ss, rq, rs));

    *pss = ss;

    return 0;
err:
    if(ss)
        session_free(ss);
    return ~0;
}
Esempio n. 7
0
WsXmlDocH
xml_parser_memory_to_doc( const char *buf, size_t size,
		const char *encoding, unsigned long options)
{
	WsXmlDocH Doc = NULL;
	xmlDocPtr xmlDoc;
	if (!buf || !size ) {
		return NULL;
	}
	xmlDoc = xmlReadMemory(buf, (int) size, NULL, encoding,
			XML_PARSE_NONET | XML_PARSE_NSCLEAN);
	if (xmlDoc == NULL) {
		return NULL;
	}
	Doc = (WsXmlDocH) u_zalloc(sizeof(*Doc));
	if (Doc == NULL) {
		xmlFreeDoc(xmlDoc);
		return NULL;
	}

	xmlDoc->_private = Doc;
	Doc->parserDoc = xmlDoc;

	return Doc;
}
Esempio n. 8
0
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;
}
Esempio n. 9
0
static int klog_args_new (klog_args_t **pka)
{
    dbg_return_if (pka == NULL, ~0);

    *pka = u_zalloc(sizeof(klog_args_t));
    dbg_return_if (*pka == NULL, ~0);

    return 0;
}
Esempio n. 10
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;
}
Esempio n. 11
0
int eventlistener_init(int port, char *servicepath, int authnotneeded, int is_debug)
{
	if(listener) return 0;
	listener = u_zalloc(sizeof(struct eventlistener_ctx));
	if(listener == NULL) {
		debug("Insufficient memory!");
		return -1;
	}
	listener->port = port;
	listener->servicepath = u_strdup(servicepath);
	listener->authnotneeded = authnotneeded;
    listener->debug = is_debug;
	return 0;
}
Esempio n. 12
0
File: pqueue.c Progetto: fdotli/libu
int pq_create (size_t nitems, pq_t **ppq)
{
    pq_t *pq = NULL;

    dbg_return_if (ppq == NULL, ~0);
    dbg_return_if (nitems < 2, ~0); /* Expect at least 2 elements. */

    /* Make room for both the queue head and items' array. */
    dbg_err_sif ((pq = u_zalloc(sizeof *pq)) == NULL);
    dbg_err_sif ((pq->q = u_zalloc((nitems + 1) * sizeof(pq_item_t))) == NULL);

    /* Init the index of last element in array: valid elements are stored 
     * at index'es [1..nitems]. */
    pq->nelems = 0;
    pq->nitems = nitems;

    *ppq = pq;

    return 0;
err:
    pq_free(pq);
    return ~0;
}
Esempio n. 13
0
static int is_existing_filter_epr(WsXmlNodeH node, filter_t **f)
{
	char *uri;
	WsXmlNodeH xmlnode = ws_xml_get_child(node, 0, XML_NS_WS_MAN, WSM_RESOURCE_URI);
	if(xmlnode == NULL)
		return -1;
	uri = ws_xml_get_node_text(xmlnode);
	if(strcmp(uri, CIM_ALL_AVAILABLE_CLASSES) == 0)
		return -1;
	xmlnode = ws_xml_get_child(node, 0, XML_NS_WS_MAN, WSM_SELECTOR_SET);
	if(xmlnode == NULL)
		return -1;
	*f = u_zalloc(sizeof(filter_t));
	return 0;
}
Esempio n. 14
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;
}
Esempio n. 15
0
File: list.c Progetto: fdotli/libu
/**
 *  \brief  Create a new list object
 *
 *  \param  plist   the newly created ::u_list_t object as a result argument
 *
 *  \retval  0  on success
 *  \retval ~0  on failure
 */ 
int u_list_create (u_list_t **plist)
{
    u_list_t *list = NULL;

    list = u_zalloc(sizeof(u_list_t));
    dbg_err_sif (list == NULL); 

    TAILQ_INIT(&list->head);

    *plist = list;

    return 0;
err:
    if(list)
        u_free(list);
    return ~0;
}
Esempio n. 16
0
File: buf.c Progetto: fdotli/libu
/**
 *  \brief  Create a new buffer
 *
 *  Create a new ::u_buf_t object and save its reference to \p *ps.
 *
 *  \param  pubuf   result argument: on success it will get the new ::u_buf_t
 *                  object
 *
 *  \retval  0  on success
 *  \retval ~0  on error
 */
int u_buf_create (u_buf_t **pubuf)
{
    u_buf_t *ubuf = NULL;

    dbg_return_if (pubuf == NULL, ~0);

    dbg_err_sif ((ubuf = u_zalloc(sizeof(u_buf_t))) == NULL);

    ubuf->len = 0;
    ubuf->size = 0;
    ubuf->data = NULL;

    *pubuf = ubuf;

    return 0;
err:
    return ~0;
}
Esempio n. 17
0
int xml_parser_node_set(WsXmlNodeH node, int what, const char *str)
{
	int retVal = -1;
	xmlNodePtr xmlNode = (xmlNodePtr) node;
	iWsNode *wsNode = (iWsNode *) xmlNode->_private;
	xmlNsPtr xmlNs;

	switch (what) {
	case XML_TEXT_VALUE:
		if (wsNode == NULL)
			xmlNode->_private = wsNode =
				u_zalloc(sizeof(iWsNode));

		if (wsNode != NULL) {
			if (wsNode->valText != NULL) {
				xmlFree(wsNode->valText);
				wsNode->valText = NULL;
			}
			xmlNodeSetContent(xmlNode, BAD_CAST str);
			retVal = 0;
		}
		break;

	case XML_LOCAL_NAME:
		xmlNodeSetName(xmlNode, BAD_CAST str);
		retVal = 0;
		break;

	case XML_NS_URI:
		if ((xmlNs =
					(xmlNsPtr) xml_parser_ns_find(node, str, NULL, 1,
						1)) != NULL) {
			xmlNode->ns = xmlNs;
			retVal = 0;
		} else
			retVal = 1;
		break;
	default:
		retVal = 1;
		break;
	}

	return retVal;
}
Esempio n. 18
0
void wsmand_shutdown_add_handler(WsmandShutdownFn fn, void *user_data)
{
	ShutdownHandler *handler;
	lnode_t *n;

	if (fn == NULL)
		return;

	handler = u_zalloc(sizeof(ShutdownHandler));
	handler->fn = fn;
	handler->user_data = user_data;

	n = lnode_create(handler);

	if (!shutdown_handlers)
		shutdown_handlers = list_create(LISTCOUNT_T_MAX);

	list_prepend(shutdown_handlers, n);
}
Esempio n. 19
0
/**
 *  \brief  Create a new string
 *
 *  Create a new ::u_string_t object and save its reference to \p *ps.
 *  If \p buf is not \c NULL (and <code>len > 0</code>), the string will be 
 *  initialized with the content of \p buf
 *
 *  \param  buf initial string value, or \c NULL
 *  \param  len length of \p buf
 *  \param  ps  result argument that will get the newly created ::u_string_t
 *              object on success
 *
 *  \retval  0  on success
 *  \retval ~0  on failure
 */
int u_string_create (const char *buf, size_t len, u_string_t **ps)
{
    u_string_t *s = NULL;

    dbg_return_if (ps == NULL, ~0);

    dbg_err_sif ((s = u_zalloc(sizeof(u_string_t))) == NULL);

    s->data = null;

    if (buf)
        dbg_err_if (u_string_append(s, buf, len));

    *ps = s;

    return 0;
err:
    if (s)
        u_string_free(s);
    return ~0;
}
Esempio n. 20
0
ec_res_t *ec_resource_new(const char *uri, ec_method_mask_t methods, 
        uint32_t max_age)
{
    ec_res_t *res = NULL;

    dbg_return_if (uri == NULL, NULL);
    dbg_return_if (!(EC_IS_METHOD_MASK(methods)), NULL);

    dbg_err_sif ((res = u_zalloc(sizeof *res)) == NULL);
    dbg_err_if (u_strlcpy(res->uri, uri, sizeof res->uri));
    res->methods = methods;
    res->max_age = max_age ? max_age : EC_COAP_DEFAULT_MAX_AGE;
    TAILQ_INIT(&res->reps);
    (void) ec_res_attrs_init(res);

    return res;
err:
    if (res)
        ec_resource_free(res);
    return NULL;
}
Esempio n. 21
0
static xmlNodePtr
make_new_xml_node(xmlNodePtr base,
		const char *uri, const char *name, const char *value, int xmlescape)
{
	xmlNodePtr newNode = NULL;
	xmlNsPtr ns = NULL;
	if (uri == NULL ||
			(ns =
			 (xmlNsPtr) xml_parser_ns_find((WsXmlNodeH) base, uri, NULL, 1,
				 1)) != NULL) {
		if ((newNode = xmlNewNode(ns, BAD_CAST name)) != NULL) {
			if (value != NULL){		
				if (xmlescape == 1)
					xmlNodeAddContent(newNode, BAD_CAST value);
				else
					xmlNodeSetContent(newNode, BAD_CAST value);
			}
			newNode->_private = u_zalloc(sizeof(iWsNode));
		}
	}
	return newNode;
}
Esempio n. 22
0
/**
 * \brief   Create a cipher \c codec_t object 
 *  
 * Create a gzip \c codec_t object at \p *piz suitable for compression or
 * decompression (depending on \p op).  
 *  
 * \param   op      one of \c GZIP_COMPRESS or \c GZIP_UNCOMPRESS
 * \param   piz     the created codec as a value-result arguement
 *  
 * \return \c 0 on success, \c ~0 otherwise
 */
int codec_gzip_create(int op, codec_t **piz)
{
    codec_gzip_t *iz = NULL;

    dbg_return_if (piz == NULL, ~0);

    iz = u_zalloc(sizeof(codec_gzip_t));
    dbg_err_if(iz == NULL);

    iz->codec.transform = gzip_transform;
    iz->codec.flush = gzip_flush;
    iz->codec.free = gzip_free;
    iz->action = op; 

    switch(op)
    {
    case GZIP_COMPRESS:
        iz->op = deflate;
        iz->opEnd = deflateEnd;
        dbg_err_if(deflateInit2(&iz->zstr, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
                    -MAX_WBITS, 8, Z_DEFAULT_STRATEGY));
        break;
    case GZIP_UNCOMPRESS:
        iz->op = inflate;
        iz->opEnd = inflateEnd;
        dbg_err_if(inflateInit2(&iz->zstr, -MAX_WBITS) != Z_OK);
        break;
    default:
        dbg_err_if("bad gzip op");
    }

    *piz = (codec_t*)iz;

    return 0;
err:
    U_FREE(iz);
    return ~0;
}
Esempio n. 23
0
WsXmlDocH
xml_parser_file_to_doc( const char *filename,
		const char *encoding, unsigned long options)
{
	xmlDocPtr xmlDoc;
	WsXmlDocH Doc = NULL;

	xmlDoc = xmlReadFile(filename, encoding,
			XML_PARSE_NONET | XML_PARSE_NSCLEAN);
	if (xmlDoc == NULL) {
		return NULL;
	}
	Doc = (WsXmlDocH) u_zalloc(sizeof(*Doc));
	if (Doc == NULL) {
		xmlFreeDoc(xmlDoc);
		return NULL;
	}
	xmlDoc->_private = Doc;
	Doc->parserDoc = xmlDoc;

	return Doc;

}
Esempio n. 24
0
char *xml_parser_node_query(WsXmlNodeH node, int what)
{
	char *ptr = NULL;
	xmlNodePtr xmlNode = (xmlNodePtr) node;
	iWsNode *wsNode = (iWsNode *) xmlNode->_private;

	switch (what) {
	case XML_TEXT_VALUE:
		if (wsNode == NULL)
			xmlNode->_private = wsNode =
				u_zalloc(sizeof(iWsNode));

		if (wsNode != NULL) {
			if (wsNode->valText == NULL) {
				wsNode->valText =
					(char *) xmlNodeGetContent(xmlNode);
			}
			ptr = wsNode->valText;
		}
		break;
	case XML_LOCAL_NAME:
		ptr = (char *) xmlNode->name;
		break;
	case XML_NS_URI:
		if (xmlNode->ns != NULL)
			ptr = (char *) xmlNode->ns->href;
		break;
	case XML_NS_PREFIX:
		if (xmlNode->ns != NULL)
			ptr = (char *) xmlNode->ns->prefix;
		break;
	default:
		break;
	}

	return ptr;
}
Esempio n. 25
0
int var_bin_create(const char *name, const unsigned char *data, size_t size, 
        var_t **pv)
{
    var_t *v = NULL;

    dbg_return_if (name == NULL, ~0);
    dbg_return_if (data == NULL, ~0);
    dbg_return_if (pv == NULL, ~0);

    v = u_zalloc(sizeof(var_t));
    dbg_err_if(v == NULL);

    dbg_err_if(u_string_create(name, strlen(name), &v->sname));

    dbg_err_if(var_set_bin_value(v, data, size));

    *pv = v;

    return 0;
err:
    if(v)
        var_free(v);
    return ~0;
}
void
wsmc_handler( WsManClient *cl,
		WsXmlDocH rqstDoc,
		void* user_data)
{
#define curl_err(str)  debug("Error = %d (%s); %s", \
		r, curl_easy_strerror(r), str);
	WsManConnection *con = cl->connection;
	CURL *curl = NULL;
	CURLcode r;
	char *upwd = NULL;
	char *usag = NULL;
	struct curl_slist *headers=NULL;
	char *buf = NULL;
	int len;
	char *soapact_header = NULL;
	long http_code;
	long auth_avail = 0;
	char *_user = NULL, *_pass = NULL;
	u_buf_t *response = NULL;
	//char *soapaction;
	char *tmp_str = NULL;

	if (!cl->initialized && wsmc_transport_init(cl, NULL)) {
		cl->last_error = WS_LASTERR_FAILED_INIT;
		return;
	}
	if (cl->transport == NULL) {
		cl->transport = init_curl_transport(cl);
                if (cl->transport == NULL) {
                        return;
                }
	}
	curl = (CURL *)cl->transport;

	r = curl_easy_setopt(curl, CURLOPT_URL, cl->data.endpoint);
	if (r != CURLE_OK) {
		cl->fault_string = u_strdup(curl_easy_strerror(r));
		curl_err("Could not curl_easy_setopt(curl, CURLOPT_URL, ...)");
		goto DONE;
	}

	r = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_handler);
	if (r != CURLE_OK) {
		cl->fault_string = u_strdup(curl_easy_strerror(r));
		curl_err("Could not curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ..)");
		goto DONE;
	}
	u_buf_create(&response);
	r = curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
	if (r != CURLE_OK) {
		cl->fault_string = u_strdup(curl_easy_strerror(r));
		curl_err("Could not curl_easy_setopt(curl, CURLOPT_WRITEDATA, ..)");
		goto DONE;
	}
	char content_type[64];
	snprintf(content_type, 64, "Content-Type: application/soap+xml;charset=%s", cl->content_encoding);
	headers = curl_slist_append(headers, content_type);
	tmp_str = wsman_transport_get_agent(cl);
	usag = malloc(12 + strlen(tmp_str) + 1);
	if (usag == NULL) {
		r = CURLE_OUT_OF_MEMORY;
		cl->fault_string = u_strdup("Could not malloc memory");
		curl_err("Could not malloc memory");
		goto DONE;
	}

	sprintf(usag, "User-Agent: %s", tmp_str);
	free(tmp_str);
	headers = curl_slist_append(headers, usag);

#if 0
	soapaction = ws_xml_get_xpath_value(rqstDoc, "/s:Envelope/s:Header/wsa:Action");
	if (soapaction) {
		soapact_header = malloc(12 + strlen(soapaction) + 1);
		if (soapact_header) {
			sprintf(soapact_header, "SOAPAction: %s", soapaction);
			headers = curl_slist_append(headers, soapact_header);
		}
		u_free(soapaction);
	}
#endif

	r = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
	if (r != CURLE_OK) {
		cl->fault_string = u_strdup(curl_easy_strerror(r));
		curl_err("Could not curl_easy_setopt(curl, CURLOPT_HTTPHEADER, ..)");
		goto DONE;
	}

	ws_xml_dump_memory_enc(rqstDoc, &buf, &len, cl->content_encoding);
#if 0
	int count = 0;
	while(count < len) {
		printf("%c",buf[count++]);
	}
#endif
	debug("*****set post buf len = %d******",len);
	r = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);
	if (r != CURLE_OK) {
		cl->fault_string = u_strdup(curl_easy_strerror(r));
		curl_err("Could not curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ..)");
		goto DONE;
	}
	r = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);
	if (r != CURLE_OK) {
		cl->fault_string = u_strdup(curl_easy_strerror(r));
		curl_err("Could not curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, ..)");
		goto DONE;
	}

	int iDone = 0;
	while (1) {
		u_free(_user);
		u_free(_pass);
		_user = wsmc_get_user(cl);
		_pass = wsmc_get_password(cl);
		if (_user && _pass && cl->data.auth_set) {
			r = curl_easy_setopt(curl, CURLOPT_HTTPAUTH, cl->data.auth_set);
			if (r != CURLE_OK) {
				cl->fault_string = u_strdup(curl_easy_strerror(r));
				curl_err("curl_easy_setopt(CURLOPT_HTTPAUTH) failed");
				goto DONE;
			}
			u_free(upwd);
			upwd = u_strdup_printf(  "%s:%s", _user ,  _pass);
			if (!upwd) {
				r = CURLE_OUT_OF_MEMORY;
				cl->fault_string = u_strdup("Could not malloc memory");
				curl_err("Could not malloc memory");
				goto DONE;
			}
			r = curl_easy_setopt(curl, CURLOPT_USERPWD, upwd);
			if (r != CURLE_OK) {
				cl->fault_string = u_strdup(curl_easy_strerror(r));
				curl_err("curl_easy_setopt(curl, CURLOPT_USERPWD, ..) failed");
				goto DONE;
			}
		}

		if (wsman_debug_level_debugged(DEBUG_LEVEL_MESSAGE)) {
			curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
		}

		r = curl_easy_perform(curl);
		if (r != CURLE_OK) {
			cl->fault_string = u_strdup(curl_easy_strerror(r));
			curl_err("curl_easy_perform failed");
			goto DONE;
		}

		r = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
		if (r != CURLE_OK) {
			cl->fault_string = u_strdup(curl_easy_strerror(r));
			curl_err("curl_easy_getinfo(CURLINFO_RESPONSE_CODE) failed");
			goto DONE;
		}

		switch (http_code) 
		{
			case 200:
			case 400:
			case 500:
				// The resource was successfully retrieved or WSMan server 
				// returned a HTTP status code. You can use WinHttpReadData to 
				// read the contents of the server's response.
				iDone = 1;
				break;
			case 401:
				// The server requires authentication.
				break;
			default:
				// The status code does not indicate success.
				r = WS_LASTERR_OTHER_ERROR;
				iDone = 1;
				break;
		}

		if(iDone == 1) {
			break;
		}

		/* we are here because of authentication required */
		r = curl_easy_getinfo(curl, CURLINFO_HTTPAUTH_AVAIL, &auth_avail);
		if (r != CURLE_OK) {
			cl->fault_string = u_strdup(curl_easy_strerror(r));
			curl_err("curl_easy_getinfo(CURLINFO_HTTPAUTH_AVAIL) failed");
			goto DONE;
		}

		cl->data.auth_set = reauthenticate(cl, cl->data.auth_set, auth_avail,
                        &cl->data.user, &cl->data.pwd);
                u_buf_clear(response);
                if (cl->data.auth_set == 0) {
                    /* FIXME: user wants to cancel authentication */
#if LIBCURL_VERSION_NUM >= 0x70D01
                    r = CURLE_LOGIN_DENIED;
#else
					/* Map the login failure error to CURLE_LOGIN_DENIED (67) so that we
					 * get the same error code in case of login failure
					 */
					r = 67;
#endif
                    curl_err("user/password wrong or empty.");
		    break;
                }
        }
#if 0
	unsigned char *mbbuf = NULL;
	iconv_t cd;
	if(strcmp(cl->content_encoding, "UTF-8")) {
                cd = iconv_open("UTF-8", cl->content_encoding);
                if(cd == -1) {
			cl->last_error = WS_LASTERR_BAD_CONTENT_ENCODING;
			goto DONE2;
                }
                mbbuf = u_zalloc(u_buf_len(response));
                size_t outbuf_len = u_buf_len(response);
                size_t inbuf_len = outbuf_len;
                char *inbuf = u_buf_ptr(response);
                char *outbuf = mbbuf;
                size_t coverted = iconv(cd, &inbuf, &inbuf_len, &outbuf, &outbuf_len);
		  iconv_close(cd);
                if( coverted == -1) {
			cl->last_error = WS_LASTERR_BAD_CONTENT_ENCODING;
			goto DONE2;
                }
                u_buf_append(con->response, mbbuf, u_buf_len(response) - inbuf_len);
        }
	u_free(mbbuf);
#endif
	u_buf_append(con->response, u_buf_ptr(response), u_buf_len(response));
DONE:
	curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
	cl->response_code = http_code;
	cl->last_error = convert_to_last_error(r);

	debug("curl error code: %d.", r);
	debug("cl->response_code: %d.", cl->response_code);
	debug("cl->last_error code: %d.", cl->last_error);

	curl_slist_free_all(headers);
	u_buf_free(response);
	u_free(soapact_header);
	u_free(usag);
	u_free(upwd);
	u_free(_pass);
	u_free(_user);
#ifdef _WIN32
	ws_xml_free_memory(buf);
#else
	u_free(buf);
#endif

	return;
#undef curl_err

}
static WsNotificationInfoH
create_notification_entity(WsSubscribeInfo *subsInfo, WsXmlNodeH node)
{
	char *classname = NULL;
	char *class_namespace = NULL;
	WsXmlNodeH indicationnode = NULL;
	WsNotificationInfoH notificationinfo = NULL;
	notificationinfo = u_zalloc(sizeof(*notificationinfo));
	if (notificationinfo == NULL) {
		return NULL;
	}
	WsXmlNodeH instance = ws_xml_get_child(node, 0, NULL, CIMXML_EXPMETHODCALL);
	if (instance == NULL) {
		u_free(notificationinfo);
		return NULL;
	}
	instance = ws_xml_get_child(instance, 0, NULL, CIMXML_EXPPARAMVALUE);
	if (instance == NULL) {
		u_free(notificationinfo);
		return NULL;
	}
	instance = ws_xml_get_child(instance, 0, NULL, CIMXML_INSTANCE);
	if (instance == NULL) {
		u_free(notificationinfo);
		return NULL;
	}
	WsXmlAttrH attr = ws_xml_find_node_attr(instance, NULL, CIMXML_CLASSNAME);
	if (attr) {
		classname = ws_xml_get_attr_value(attr);
		class_namespace = get_cim_indication_namespace(subsInfo, classname);
		notificationinfo->EventAction = u_strdup_printf("%s/%s", class_namespace, classname);
	}
	notificationinfo->EventContent = ws_xml_create_doc(notificationinfo->EventAction, classname);
	indicationnode = ws_xml_get_doc_root(notificationinfo->EventContent);
    //Parse "PROPERTY"
    int n = 0;
    while ( (node = ws_xml_get_child(instance, n++, NULL, CIMXML_PROPERTY)) ) {
        attr = ws_xml_find_node_attr(node, NULL, CIMXML_NAME);
        char *property = NULL;
        char *value = NULL;
        if ( attr ) {
            property = ws_xml_get_attr_value(attr);
        }
        value = ws_xml_get_node_text(node);
        ws_xml_add_child(indicationnode, notificationinfo->EventAction, property, value);
    }
 
    //Parse "PROPERTY.ARRAY"
    n = 0;
    while ( (node = ws_xml_get_child(instance, n++, NULL, CIMXML_PROPERTYARRAY)) ) {
        attr = ws_xml_find_node_attr(node, NULL, CIMXML_NAME);
        char *property = NULL;
        if ( attr ) {
            property = ws_xml_get_attr_value(attr);
            WsXmlNodeH valarraynode = ws_xml_get_child(node, 0, NULL, CIMXML_VALUEARRAY);
            if ( valarraynode ) {
                int m = 0;
                WsXmlNodeH valnode = NULL;
                while ( (valnode = ws_xml_get_child(valarraynode, m++, NULL, CIMXML_VALUE)) ) {
                    char *value = ws_xml_get_node_text(valnode);
                    ws_xml_add_child(indicationnode, notificationinfo->EventAction, property, value);
                }
            }
        }
    }
	if (class_namespace)
		u_free(class_namespace);
	return notificationinfo;
}
Esempio n. 28
0
int kache_store_evcoap_response(kache_evcoap_t *ke, ec_client_t *cli, char *uri)
{
    dbg_return_if (ke == NULL, -1);
    dbg_return_if (cli == NULL, -1);
    ec_rc_t rc;
    kache_obj_t *obj = NULL;
    kache_rep_t *rep = NULL;
    ec_opts_t *opts = ec_client_get_response_options(cli);
    size_t pl_sz;
    size_t etag_sz;
    ev_uint8_t *pl;
    ev_uint16_t ct;
    uint8_t *etag;

    obj = kache_get(ke->kache,uri); //TODO uri?

    //Content type
    ec_opts_get_content_type(
                opts, 
                &ct);
    kache_content_type_t *mt;
    dbg_err_if((mt = malloc(sizeof(kache_content_type_t)))==NULL);
    kache_ct_from_ecct(mt,&ct);

    // Check if there is already a corresponding representation
    // and delete it
    if(obj)
    {
        rep = kache_get_rep_by_media_type(obj,mt);
        if(rep)
        {
            (void) kache_remove_rep(obj,rep);
            kache_clear_kache_rep(rep);
        }
        else
            dbg_err_if( (rep = kache_init_kache_rep()) == NULL);

    }
    else
    {
        dbg_err_if((obj = kache_init_kache_obj()) == NULL);

        obj->key = malloc(strlen(uri)+1);
        strcpy(obj->key,uri);

        obj->protocol_type = COAP;
        dbg_err_if( (rep = kache_init_kache_rep()) == NULL);
        kache_set(ke->kache,"uri",obj,NULL);
    }
    rep->media_type = mt;
    // Constructing the cached resource representation

    // Payload
    pl = ec_response_get_payload(cli, &pl_sz);
    if(pl)
    {
        dbg_err_if((rep->payload = malloc(pl_sz )) == NULL);
        memcpy(rep->payload, pl, pl_sz);
        rep->payload_size = pl_sz;
    }

    // ts
    dbg_err_if( (rep->ts = u_zalloc(sizeof(struct timeval))) == NULL );
    dbg_err_if( gettimeofday(rep->ts,NULL) );

    // Other options
    
    dbg_err_if(kache_store_ec_options_to_rep(opts,rep));
    //max_age
    if(rep->max_age == 0)
        rep->max_age = 60;
    max_age = rep->max_age;

    //Timer
    kache_timer_arg_t *arg;
    dbg_err_if((arg = malloc(sizeof(kache_timer_arg_t)))==NULL);
    arg->rep = rep;
    arg->obj = obj;
    arg->kache = ke->kache;
    kache_set_rep_timer(ke->base,
            rep,
            rep->max_age,
            kache_evcoap_timer_cb,
            (void *) arg
            );
    //Per protocol data (placeholder)
    kache_evcoap_data_t *data;
    data = kache_init_evcoap_data();
    rep->per_protocol_data = data;
    
    //Add representation to obj
    dbg_err_if(kache_add_rep(obj,rep));
    return 0;
err:
    if(obj)
        free(obj);
    if(rep)
        free(rep);
    return -1;
}
Esempio n. 29
0
SoapDispatchH wsman_dispatch_entry_new(void)
{
	return (SoapDispatchH) u_zalloc(sizeof(struct __dispatch_t));
}
Esempio n. 30
0
WsXmlDocH ws_xml_create_doc_by_import(WsXmlNodeH node)
{
	WsXmlDocH wsDoc = (WsXmlDocH) u_zalloc(sizeof(*wsDoc));
	xml_parser_create_doc_by_import(wsDoc, node);
	return wsDoc;
}