int set_is_equal(const Set *st1, const Set *st2){ if(set_len(st1) != set_len(st2)){ return 0; } return set_is_subset(st1, st2); }
int set_is_subset(const Set *st, const Set *st1){ if(set_len(st) < set_len(st1)){ return 0; } Node *pCurrentNode = list_head(st1); while(pCurrentNode){ if(!set_is_member(st, list_node_data(pCurrentNode))){ return 0; } pCurrentNode = list_node_next(pCurrentNode); } return 1; }
void notify_ops(CHANNEL * chan, const char *fmt, ...) { LIST *list; CHANUSER *chanUser; char buf[256]; int len; va_list ap; va_start(ap, fmt); vsnprintf(buf + 4, sizeof(buf) - 4, fmt, ap); va_end(ap); len = strlen(buf + 4); set_len(buf, len); set_tag(buf, MSG_SERVER_NOSUCH); for (list = chan->users; list; list = list->next) { chanUser = list->data; ASSERT(chanUser->magic == MAGIC_CHANUSER); if(ISUSER(chanUser->user->con) && ((chanUser->flags & ON_CHANNEL_OPERATOR) || chanUser->user->level > LEVEL_USER)) { queue_data(chanUser->user->con, buf, 4 + len); } } }
int bin_init(str *mod_name, int cmd_type, short version) { if (!send_buffer) { send_buffer = pkg_malloc(BUF_SIZE); if (!send_buffer) { LM_ERR("No more pkg memory!\n"); return -1; } } /* binary packet header: marker + pkg_len */ memcpy(send_buffer, BIN_PACKET_MARKER, BIN_PACKET_MARKER_SIZE); cpos = send_buffer + BIN_PACKET_MARKER_SIZE + PKG_LEN_FIELD_SIZE; /* bin version */ memcpy(cpos, &version, sizeof(version)); cpos += VERSION_FIELD_SIZE; /* module name */ memcpy(cpos, &mod_name->len, LEN_FIELD_SIZE); cpos += LEN_FIELD_SIZE; memcpy(cpos, mod_name->s, mod_name->len); cpos += mod_name->len; memcpy(cpos, &cmd_type, sizeof(cmd_type)); cpos += sizeof(cmd_type); set_len(send_buffer, cpos); return 0; }
/* * adds a new integer value at the 'cpos' position in the buffer * * @return: * > 0: success, the size of the buffer * < 0: internal buffer limit reached */ int bin_push_int(int info) { if (!cpos || (cpos + sizeof(info) - send_buffer) > BUF_SIZE) return -1; memcpy(cpos, &info, sizeof(info)); cpos += sizeof(info); set_len(send_buffer, cpos); return (int)(cpos - send_buffer); }
int emcute_willupd_msg(const void *data, size_t len) { assert(data && (len > 0)); if (gateway.port == 0) { return EMCUTE_NOGW; } if (len > (EMCUTE_BUFSIZE - 4)) { return EMCUTE_OVERFLOW; } mutex_lock(&txlock); size_t pos = set_len(tbuf, (len + 1)); len += (pos + 1); tbuf[pos++] = WILLMSGUPD; memcpy(&tbuf[pos], data, len); return syncsend(WILLMSGRESP, len, true); }
/* * copies the given string at the 'cpos' position in the buffer * allows null strings (NULL content or NULL param) * * @return: * > 0: success, the size of the buffer * < 0: internal buffer limit reached */ int bin_push_str(const str *info) { if (!cpos || (cpos - send_buffer + LEN_FIELD_SIZE + (info ? info->len : 0)) > BUF_SIZE) return -1; if (!info || info->len == 0 || !info->s) { memset(cpos, 0, LEN_FIELD_SIZE); cpos += LEN_FIELD_SIZE; return (int)LEN_FIELD_SIZE; } memcpy(cpos, &info->len, LEN_FIELD_SIZE); cpos += LEN_FIELD_SIZE; memcpy(cpos, info->s, info->len); cpos += info->len; set_len(send_buffer, cpos); return (int)(cpos - send_buffer); }
int emcute_pub(emcute_topic_t *topic, const void *data, size_t len, unsigned flags) { int res = EMCUTE_OK; assert((topic->id != 0) && data && (len > 0) && !(flags & ~PUB_FLAGS)); if (gateway.port == 0) { return EMCUTE_NOGW; } if (len >= (EMCUTE_BUFSIZE - 9)) { return EMCUTE_OVERFLOW; } if (flags & EMCUTE_QOS_2) { return EMCUTE_NOTSUP; } mutex_lock(&txlock); size_t pos = set_len(tbuf, (len + 6)); len += (pos + 6); tbuf[pos++] = PUBLISH; tbuf[pos++] = flags; byteorder_htolebufs(&tbuf[pos], topic->id); pos += 2; byteorder_htolebufs(&tbuf[pos], id_next); waitonid = id_next++; pos += 2; memcpy(&tbuf[pos], data, len); if (flags & EMCUTE_QOS_1) { res = syncsend(PUBACK, len, true); } else { sock_udp_send(&sock, tbuf, len, &gateway); mutex_unlock(&txlock); } return res; }
static struct val *__alloc(enum val_type type, const char *s, size_t len, bool heapalloc, bool mustdup) { struct val *val; bool copy; ASSERT((type == VT_STR) || (type == VT_SYM)); /* sanity check */ if (mustdup) ASSERT(!heapalloc); /* determine the real length of the string */ len = s ? strnlen(s, len) : 0; /* check preallocated strings */ val = __get_preallocated(type, s, len); if (val) goto out; /* can we inline it? */ copy = __inlinable(len); /* we'll be storing a pointer - strdup as necessary */ if (!copy && mustdup) { char *tmp; tmp = malloc(len + 1); if (!tmp) { val = ERR_PTR(-ENOMEM); goto out; } __copy(tmp, s, len); /* we're now using the heap */ heapalloc = true; s = tmp; } val = __val_alloc(type); if (IS_ERR(val)) goto out; val->static_alloc = copy || !heapalloc; val->inline_alloc = copy; set_len(val, len); if (copy) { __copy(val->_set_str_inline, s, len); if (heapalloc) free((char *) s); } else { val->_set_str_ptr = s; } return val; out: if (heapalloc) free((char *) s); return val; }
static int get_op_len(const char *re, int re_len) { return re[0] == '[' ? set_len(re + 1, re_len - 1) + 1 : op_len(re); }
int emcute_con(sock_udp_ep_t *remote, bool clean, const char *will_topic, const void *will_msg, size_t will_msg_len, unsigned will_flags) { int res; size_t len; assert(!will_topic || (will_topic && will_msg && !(will_flags & ~PUB_FLAGS))); mutex_lock(&txlock); /* check for existing connections and copy given UDP endpoint */ if (gateway.port != 0) { return EMCUTE_NOGW; } memcpy(&gateway, remote, sizeof(sock_udp_ep_t)); /* figure out which flags to set */ uint8_t flags = (clean) ? EMCUTE_CS : 0; if (will_topic) { flags |= EMCUTE_WILL; } /* compute packet size */ len = (strlen(cli_id) + 6); tbuf[0] = (uint8_t)len; tbuf[1] = CONNECT; tbuf[2] = flags; tbuf[3] = PROTOCOL_VERSION; byteorder_htolebufs(&tbuf[4], EMCUTE_KEEPALIVE); memcpy(&tbuf[6], cli_id, strlen(cli_id)); /* configure 'state machine' and send the connection request */ if (will_topic) { size_t topic_len = strlen(will_topic); if ((topic_len > EMCUTE_TOPIC_MAXLEN) || ((will_msg_len + 4) > EMCUTE_BUFSIZE)) { gateway.port = 0; return EMCUTE_OVERFLOW; } res = syncsend(WILLTOPICREQ, len, false); if (res != EMCUTE_OK) { gateway.port = 0; return res; } /* now send WILLTOPIC */ size_t pos = set_len(tbuf, (topic_len + 2)); len = (pos + topic_len + 2); tbuf[pos++] = WILLTOPIC; tbuf[pos++] = will_flags; memcpy(&tbuf[pos], will_topic, strlen(will_topic)); res = syncsend(WILLMSGREQ, len, false); if (res != EMCUTE_OK) { gateway.port = 0; return res; } /* and WILLMSG afterwards */ pos = set_len(tbuf, (will_msg_len + 1)); len = (pos + will_msg_len + 1); tbuf[pos++] = WILLMSG; memcpy(&tbuf[pos], will_msg, will_msg_len); } res = syncsend(CONNACK, len, true); if (res != EMCUTE_OK) { gateway.port = 0; } return res; }
void payload_data_add(uint8_t *data, size_t len) { set_len(data - len_size, len); super::payload_data_add(data - len_size, len + len_size); }