Consistently format using clang-format

master
Michael Santos 5 years ago
parent 4b85976242
commit e00a7fddd2

@ -14,108 +14,105 @@
*/
#include "xmppipe.h"
int
handle_message(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_stanza_t *child = NULL;
xmppipe_state_t *state = userdata;
char *message = NULL;
const char *type = NULL;
const char *from = NULL;
const char *to = NULL;
const char *ns;
char *etype = NULL;
char *efrom = NULL;
char *eto = NULL;
char *emessage = NULL;
char *symbol = "m";
if (xmpp_stanza_get_child_by_name(stanza, "delay"))
return 1;
int handle_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmpp_stanza_t *child = NULL;
xmppipe_state_t *state = userdata;
from = xmpp_stanza_get_attribute(stanza, "from");
if (from == NULL)
return 1;
char *message = NULL;
const char *type = NULL;
const char *from = NULL;
const char *to = NULL;
const char *ns;
to = xmpp_stanza_get_attribute(stanza, "to");
if (to == NULL)
return 1;
char *etype = NULL;
char *efrom = NULL;
char *eto = NULL;
char *emessage = NULL;
type = xmpp_stanza_get_type(stanza);
if (type == NULL)
return 1;
char *symbol = "m";
/* Check if the message is from us */
if (XMPPIPE_STREQ(type, "groupchat") && XMPPIPE_STREQ(from, state->mucjid))
return 1;
if (xmpp_stanza_get_child_by_name(stanza, "delay"))
return 1;
child = xmpp_stanza_get_child_by_name(stanza, "displayed");
if (child != NULL) {
ns = xmpp_stanza_get_ns(child);
if (XMPPIPE_STREQ(ns, "urn:xmpp:chat-markers:0")) {
if (!(state->opt & XMPPIPE_OPT_CHAT_MARKER))
return 1;
from = xmpp_stanza_get_attribute(stanza, "from");
if (from == NULL)
return 1;
symbol = "M";
}
}
to = xmpp_stanza_get_attribute(stanza, "to");
if (to == NULL)
return 1;
child = xmpp_stanza_get_child_by_name(stanza, "body");
type = xmpp_stanza_get_type(stanza);
if (type == NULL)
return 1;
if (child != NULL) {
message = xmpp_stanza_get_text(child);
if (message != NULL) {
if (state->encode) {
size_t len = strlen(message);
unsigned char *buf = NULL;
size_t n = 0;
/* Check if the message is from us */
if (XMPPIPE_STREQ(type, "groupchat") && XMPPIPE_STREQ(from, state->mucjid))
return 1;
xmpp_base64_decode_bin(state->ctx, message, len, &buf, &n);
child = xmpp_stanza_get_child_by_name(stanza, "displayed");
if (child != NULL) {
ns = xmpp_stanza_get_ns(child);
if (XMPPIPE_STREQ(ns, "urn:xmpp:chat-markers:0")) {
if (!(state->opt & XMPPIPE_OPT_CHAT_MARKER))
return 1;
symbol = "M";
}
}
if (buf == NULL) {
/* Not a base64 message */
return 1;
}
child = xmpp_stanza_get_child_by_name(stanza, "body");
emessage = xmppipe_nfmt_encode((char *)buf, n);
xmpp_free(state->ctx, buf);
}
else {
emessage = xmppipe_fmt_encode(message);
}
if (child != NULL) {
message = xmpp_stanza_get_text(child);
if (message != NULL) {
if (state->encode) {
size_t len = strlen(message);
unsigned char *buf = NULL;
size_t n = 0;
xmpp_free(state->ctx, message);
xmpp_base64_decode_bin(state->ctx, message, len, &buf, &n);
if (buf == NULL) {
/* Not a base64 message */
return 1;
}
goto XMPPIPE_STDOUT;
}
child = xmpp_stanza_get_child_by_name(stanza, "subject");
if (child != NULL) {
message = xmpp_stanza_get_text(child);
if (message != NULL)
emessage = xmppipe_fmt_encode(message);
symbol = "S";
xmpp_free(state->ctx, message);
emessage = xmppipe_nfmt_encode((char *)buf, n);
xmpp_free(state->ctx, buf);
} else {
emessage = xmppipe_fmt_encode(message);
}
xmpp_free(state->ctx, message);
}
goto XMPPIPE_STDOUT;
}
child = xmpp_stanza_get_child_by_name(stanza, "subject");
if (child != NULL) {
message = xmpp_stanza_get_text(child);
if (message != NULL)
emessage = xmppipe_fmt_encode(message);
symbol = "S";
xmpp_free(state->ctx, message);
}
XMPPIPE_STDOUT:
etype = xmppipe_fmt_encode(type);
efrom = xmppipe_fmt_encode(from);
eto = xmppipe_fmt_encode(to);
etype = xmppipe_fmt_encode(type);
efrom = xmppipe_fmt_encode(from);
eto = xmppipe_fmt_encode(to);
(void)printf("%s:%s:%s:%s:%s\n", symbol, etype, efrom, eto,
emessage == NULL ? "" : emessage);
(void)printf("%s:%s:%s:%s:%s\n", symbol, etype, efrom, eto,
emessage == NULL ? "" : emessage);
state->interval = 0;
state->interval = 0;
free(etype);
free(efrom);
free(eto);
free(emessage);
free(etype);
free(efrom);
free(eto);
free(emessage);
return 1;
return 1;
}

@ -14,76 +14,70 @@
*/
#include "xmppipe.h"
void
xmppipe_muc_join(xmppipe_state_t *state)
{
xmpp_stanza_t *presence = NULL;
xmpp_stanza_t *x = NULL;
void xmppipe_muc_join(xmppipe_state_t *state) {
xmpp_stanza_t *presence = NULL;
xmpp_stanza_t *x = NULL;
presence = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(presence, "presence");
xmppipe_stanza_set_attribute(presence, "to", state->mucjid);
presence = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(presence, "presence");
xmppipe_stanza_set_attribute(presence, "to", state->mucjid);
x = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(x, "x");
xmppipe_stanza_set_ns(x, "http://jabber.org/protocol/muc");
x = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(x, "x");
xmppipe_stanza_set_ns(x, "http://jabber.org/protocol/muc");
xmppipe_stanza_add_child(presence, x);
xmppipe_stanza_add_child(presence, x);
xmppipe_send(state, presence);
(void)xmpp_stanza_release(presence);
xmppipe_send(state, presence);
(void)xmpp_stanza_release(presence);
}
void
xmppipe_muc_subject(xmppipe_state_t *state, char *buf)
{
xmpp_stanza_t *message = NULL;
xmpp_stanza_t *subject= NULL;
xmpp_stanza_t *text= NULL;
void xmppipe_muc_subject(xmppipe_state_t *state, char *buf) {
xmpp_stanza_t *message = NULL;
xmpp_stanza_t *subject = NULL;
xmpp_stanza_t *text = NULL;
message = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(message, "message");
xmppipe_stanza_set_attribute(message, "to", state->out);
xmppipe_stanza_set_attribute(message, "type", "groupchat");
message = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(message, "message");
xmppipe_stanza_set_attribute(message, "to", state->out);
xmppipe_stanza_set_attribute(message, "type", "groupchat");
subject = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(subject, "subject");
subject = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(subject, "subject");
text = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_text(text, buf);
text = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_text(text, buf);
xmppipe_stanza_add_child(subject, text);
xmppipe_stanza_add_child(message, subject);
xmppipe_stanza_add_child(subject, text);
xmppipe_stanza_add_child(message, subject);
xmppipe_send(state, message);
(void)xmpp_stanza_release(message);
xmppipe_send(state, message);
(void)xmpp_stanza_release(message);
}
void
xmppipe_muc_unlock(xmppipe_state_t *state)
{
xmpp_stanza_t *iq = NULL;
xmpp_stanza_t *q= NULL;
xmpp_stanza_t *x = NULL;
iq = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(iq, "iq");
xmppipe_stanza_set_attribute(iq, "to", state->out);
xmppipe_stanza_set_attribute(iq, "id", "create1");
xmppipe_stanza_set_attribute(iq, "type", "set");
q = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(q, "query");
xmppipe_stanza_set_ns(q, "http://jabber.org/protocol/muc#owner");
x = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(x, "x");
xmppipe_stanza_set_ns(x, "jabber:x:data");
xmppipe_stanza_set_attribute(x, "type", "submit");
xmppipe_stanza_add_child(q, x);
xmppipe_stanza_add_child(iq, q);
xmppipe_send(state, iq);
(void)xmpp_stanza_release(iq);
void xmppipe_muc_unlock(xmppipe_state_t *state) {
xmpp_stanza_t *iq = NULL;
xmpp_stanza_t *q = NULL;
xmpp_stanza_t *x = NULL;
iq = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(iq, "iq");
xmppipe_stanza_set_attribute(iq, "to", state->out);
xmppipe_stanza_set_attribute(iq, "id", "create1");
xmppipe_stanza_set_attribute(iq, "type", "set");
q = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(q, "query");
xmppipe_stanza_set_ns(q, "http://jabber.org/protocol/muc#owner");
x = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(x, "x");
xmppipe_stanza_set_ns(x, "jabber:x:data");
xmppipe_stanza_set_attribute(x, "type", "submit");
xmppipe_stanza_add_child(q, x);
xmppipe_stanza_add_child(iq, q);
xmppipe_send(state, iq);
(void)xmpp_stanza_release(iq);
}

@ -14,21 +14,18 @@
*/
#include "xmppipe.h"
int
handle_null(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmppipe_state_t *state = userdata;
const char *name = NULL;
int handle_null(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmppipe_state_t *state = userdata;
const char *name = NULL;
name = xmpp_stanza_get_name(stanza);
if (name == NULL)
return 1;
name = xmpp_stanza_get_name(stanza);
if (name == NULL)
return 1;
if (XMPPIPE_STREQ(name, "iq")
|| XMPPIPE_STREQ(name, "message")
|| XMPPIPE_STREQ(name, "presence"))
state->sm_ack_recv++;
if (XMPPIPE_STREQ(name, "iq") || XMPPIPE_STREQ(name, "message") ||
XMPPIPE_STREQ(name, "presence"))
state->sm_ack_recv++;
return 1;
return 1;
}

@ -14,35 +14,32 @@
*/
#include "xmppipe.h"
int
handle_ping_reply(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmppipe_state_t *state = userdata;
state->keepalive_fail = 0;
return 1;
int handle_ping_reply(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmppipe_state_t *state = userdata;
state->keepalive_fail = 0;
return 1;
}
void
xmppipe_ping(xmppipe_state_t *state)
{
xmpp_stanza_t *iq = NULL;
xmpp_stanza_t *ping = NULL;
void xmppipe_ping(xmppipe_state_t *state) {
xmpp_stanza_t *iq = NULL;
xmpp_stanza_t *ping = NULL;
iq = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(iq, "iq");
xmppipe_stanza_set_type(iq, "get");
xmppipe_stanza_set_id(iq, "c2s1");
xmppipe_stanza_set_attribute(iq, "from", xmpp_conn_get_bound_jid(state->conn));
iq = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(iq, "iq");
xmppipe_stanza_set_type(iq, "get");
xmppipe_stanza_set_id(iq, "c2s1");
xmppipe_stanza_set_attribute(iq, "from",
xmpp_conn_get_bound_jid(state->conn));
ping = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(ping, "ping");
xmppipe_stanza_set_ns(ping, "urn:xmpp:ping");
ping = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(ping, "ping");
xmppipe_stanza_set_ns(ping, "urn:xmpp:ping");
xmppipe_stanza_add_child(iq, ping);
xmppipe_stanza_add_child(iq, ping);
xmppipe_send(state, iq);
(void)xmpp_stanza_release(iq);
xmppipe_send(state, iq);
(void)xmpp_stanza_release(iq);
state->keepalive_fail++;
state->keepalive_fail++;
}

@ -14,125 +14,120 @@
*/
#include "xmppipe.h"
int
handle_presence(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmppipe_state_t *state = userdata;
xmpp_stanza_t *x = NULL;
xmpp_stanza_t *item = NULL;
const char *from = NULL;
const char *to = NULL;
const char *type = NULL;
const char *code = NULL;
char *efrom = NULL;
char *eto = NULL;
char *etype = NULL;
int me = 0;
from = xmpp_stanza_get_attribute(stanza, "from");
to = xmpp_stanza_get_attribute(stanza, "to");
if (from == NULL || to == NULL)
return 1;
x = xmpp_stanza_get_child_by_ns(stanza,
"http://jabber.org/protocol/muc#user");
if (x) {
for (item = xmpp_stanza_get_children(x); item != NULL;
item = xmpp_stanza_get_next(item)) {
const char *name = xmpp_stanza_get_name(item);
if (name && XMPPIPE_STREQ(name, "status")) {
code = xmpp_stanza_get_attribute(item, "code");
if (code && XMPPIPE_STREQ(code, "110")) {
/* Check for nick conflict */
if (XMPPIPE_STRNEQ(from, state->mucjid)) {
free(state->mucjid);
state->mucjid= xmppipe_strdup(from);
}
xmppipe_next_state(state, XMPPIPE_S_READY);
me = 1;
break;
}
/* code ignored */
}
int handle_presence(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmppipe_state_t *state = userdata;
xmpp_stanza_t *x = NULL;
xmpp_stanza_t *item = NULL;
const char *from = NULL;
const char *to = NULL;
const char *type = NULL;
const char *code = NULL;
char *efrom = NULL;
char *eto = NULL;
char *etype = NULL;
int me = 0;
from = xmpp_stanza_get_attribute(stanza, "from");
to = xmpp_stanza_get_attribute(stanza, "to");
if (from == NULL || to == NULL)
return 1;
x = xmpp_stanza_get_child_by_ns(stanza,
"http://jabber.org/protocol/muc#user");
if (x) {
for (item = xmpp_stanza_get_children(x); item != NULL;
item = xmpp_stanza_get_next(item)) {
const char *name = xmpp_stanza_get_name(item);
if (name && XMPPIPE_STREQ(name, "status")) {
code = xmpp_stanza_get_attribute(item, "code");
if (code && XMPPIPE_STREQ(code, "110")) {
/* Check for nick conflict */
if (XMPPIPE_STRNEQ(from, state->mucjid)) {
free(state->mucjid);
state->mucjid = xmppipe_strdup(from);
}
xmppipe_next_state(state, XMPPIPE_S_READY);
me = 1;
break;
}
/* code ignored */
}
}
}
type = xmpp_stanza_get_attribute(stanza, "type");
type = xmpp_stanza_get_attribute(stanza, "type");
if (type == NULL)
type = "available";
if (type == NULL)
type = "available";
if (me != 0 && XMPPIPE_STREQ(type, "available")) {
state->occupants++;
}
else if (XMPPIPE_STREQ(type, "unavailable") && (state->occupants > 0)) {
state->occupants--;
}
if (me != 0 && XMPPIPE_STREQ(type, "available")) {
state->occupants++;
} else if (XMPPIPE_STREQ(type, "unavailable") && (state->occupants > 0)) {
state->occupants--;
}
if (state->status == XMPPIPE_S_READY && state->occupants > 0)
xmppipe_next_state(state, XMPPIPE_S_READY_AVAIL);
if (state->status == XMPPIPE_S_READY && state->occupants > 0)
xmppipe_next_state(state, XMPPIPE_S_READY_AVAIL);
if (state->status == XMPPIPE_S_READY_AVAIL && state->occupants == 0)
xmppipe_next_state(state, XMPPIPE_S_READY_EMPTY);
if (state->status == XMPPIPE_S_READY_AVAIL && state->occupants == 0)
xmppipe_next_state(state, XMPPIPE_S_READY_EMPTY);
etype = xmppipe_fmt_encode(type);
efrom = xmppipe_fmt_encode(from);
eto = xmppipe_fmt_encode(to);
etype = xmppipe_fmt_encode(type);
efrom = xmppipe_fmt_encode(from);
eto = xmppipe_fmt_encode(to);
(void)printf("p:%s:%s:%s\n", etype, efrom, eto);
(void)printf("p:%s:%s:%s\n", etype, efrom, eto);
state->interval = 0;
state->interval = 0;
free(etype);
free(efrom);
free(eto);
free(etype);
free(efrom);
free(eto);
return 1;
return 1;
}
int
handle_presence_error(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmppipe_state_t *state = userdata;
xmpp_stanza_t *error = NULL;
xmpp_stanza_t *child = NULL;
int handle_presence_error(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmppipe_state_t *state = userdata;
xmpp_stanza_t *error = NULL;
xmpp_stanza_t *child = NULL;
const char *from = NULL;
const char *to = NULL;
const char *code = NULL;
const char *text = NULL;
const char *from = NULL;
const char *to = NULL;
const char *code = NULL;
const char *text = NULL;
from = xmpp_stanza_get_attribute(stanza, "from");
to = xmpp_stanza_get_attribute(stanza, "to");
from = xmpp_stanza_get_attribute(stanza, "from");
to = xmpp_stanza_get_attribute(stanza, "to");
if (from == NULL || to == NULL)
return 1;
if (from == NULL || to == NULL)
return 1;
/* Check error is to our JID (user@example.org/binding) */
if (XMPPIPE_STRNEQ(to, xmpp_conn_get_bound_jid(conn)))
return 1;
/* Check error is to our JID (user@example.org/binding) */
if (XMPPIPE_STRNEQ(to, xmpp_conn_get_bound_jid(conn)))
return 1;
/* Check error is from our resource in the MUC (room@example.org/nick) */
if (XMPPIPE_STRNEQ(from, state->mucjid))
return 1;
/* Check error is from our resource in the MUC (room@example.org/nick) */
if (XMPPIPE_STRNEQ(from, state->mucjid))
return 1;
error = xmpp_stanza_get_child_by_name(stanza, "error");
if (error == NULL)
return 1;
error = xmpp_stanza_get_child_by_name(stanza, "error");
if (error == NULL)
return 1;
code = xmpp_stanza_get_attribute(error, "code");
child = xmpp_stanza_get_child_by_name(error, "text");
if (child)
text = xmpp_stanza_get_text(child);
code = xmpp_stanza_get_attribute(error, "code");
child = xmpp_stanza_get_child_by_name(error, "text");
if (child)
text = xmpp_stanza_get_text(child);
errx(EXIT_FAILURE, "%s: %s", code ? code : "no error code specified",
text ? text : "no description");
errx(EXIT_FAILURE, "%s: %s", code ? code : "no error code specified",
text ? text : "no description");
}

@ -14,92 +14,84 @@
*/
#include "xmppipe.h"
int
handle_sm_enabled(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmppipe_state_t *state = userdata;
state->sm_enabled = 1;
return 0;
int handle_sm_enabled(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmppipe_state_t *state = userdata;
state->sm_enabled = 1;
return 0;
}
int
handle_sm_request(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmppipe_state_t *state = userdata;
int handle_sm_request(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmppipe_state_t *state = userdata;
xmpp_stanza_t *a = NULL;
char h[11] = {0};
xmpp_stanza_t *a = NULL;
char h[11] = {0};
if (state->sm_request % state->sm_request_interval != 0)
return 1;
if (state->sm_request % state->sm_request_interval != 0)
return 1;
(void)snprintf(h, sizeof(h), "%u", state->sm_ack_recv);
(void)snprintf(h, sizeof(h), "%u", state->sm_ack_recv);
/* <a xmlns='urn:xmpp:sm:3' h='1'/> */
a = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(a, "a");
xmppipe_stanza_set_ns(a, "urn:xmpp:sm:3");
xmppipe_stanza_set_attribute(a, "h", h);
/* <a xmlns='urn:xmpp:sm:3' h='1'/> */
a = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(a, "a");
xmppipe_stanza_set_ns(a, "urn:xmpp:sm:3");
xmppipe_stanza_set_attribute(a, "h", h);
xmpp_send(state->conn, a);
(void)xmpp_stanza_release(a);
xmpp_send(state->conn, a);
(void)xmpp_stanza_release(a);
return 1;
return 1;
}
int
handle_sm_ack(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmppipe_state_t *state = userdata;
const char *h = NULL;
u_int32_t ack = 0;
const char *errstr = NULL;
h = xmpp_stanza_get_attribute(stanza, "h");
if (h == NULL)
return 1;
ack = strtonum(h, 0, UINT_MAX-1, &errstr);
if (errstr)
goto XMPPIPE_STREAMERR;
if (state->verbose)
(void)fprintf(stderr, "SM: request=%u ack=%u last=%u\n",
state->sm_request, ack, state->sm_ack_sent);
state->sm_request_unack = 0;
/* Number of stanzas received by server exceeds the number sent by
* the client.
*/
if (ack > state->sm_request)
goto XMPPIPE_STREAMERR;
/* Server count not incremented since last request (stanzas may have
* been dropped).
*
* Could resend dropped stanzas.
*
*/
if (ack == state->sm_ack_sent)
goto XMPPIPE_STREAMERR;
state->sm_ack_sent = ack;
int handle_sm_ack(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmppipe_state_t *state = userdata;
const char *h = NULL;
u_int32_t ack = 0;
const char *errstr = NULL;
h = xmpp_stanza_get_attribute(stanza, "h");
if (h == NULL)
return 1;
ack = strtonum(h, 0, UINT_MAX - 1, &errstr);
if (errstr)
goto XMPPIPE_STREAMERR;
if (state->verbose)
(void)fprintf(stderr, "SM: request=%u ack=%u last=%u\n", state->sm_request,
ack, state->sm_ack_sent);
state->sm_request_unack = 0;
/* Number of stanzas received by server exceeds the number sent by
* the client.
*/
if (ack > state->sm_request)
goto XMPPIPE_STREAMERR;
/* Server count not incremented since last request (stanzas may have
* been dropped).
*
* Could resend dropped stanzas.
*
*/
if (ack == state->sm_ack_sent)
goto XMPPIPE_STREAMERR;
state->sm_ack_sent = ack;
return 1;
XMPPIPE_STREAMERR:
xmppipe_stream_close(state);
errx(EXIT_FAILURE, "ack sequence mismatch: request=%u, ack=%u\n",
state->sm_request, state->sm_ack_sent);
xmppipe_stream_close(state);
errx(EXIT_FAILURE, "ack sequence mismatch: request=%u, ack=%u\n",
state->sm_request, state->sm_ack_sent);
}
void
xmppipe_stream_close(xmppipe_state_t *state)
{
if (state->sm_enabled)
xmpp_send_raw_string(state->conn, "</stream:stream>");
void xmppipe_stream_close(xmppipe_state_t *state) {
if (state->sm_enabled)
xmpp_send_raw_string(state->conn, "</stream:stream>");
}

@ -20,51 +20,49 @@
/* OPENBSD ORIGINAL: lib/libc/stdlib/strtonum.c */
#ifndef HAVE_STRTONUM
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#define INVALID 1
#define TOOSMALL 2
#define TOOLARGE 3
#define INVALID 1
#define TOOSMALL 2
#define TOOLARGE 3
long long
strtonum(const char *numstr, long long minval, long long maxval,
const char **errstrp)
{
long long ll = 0;
char *ep;
int error = 0;
struct errval {
const char *errstr;
int err;
} ev[4] = {
{ NULL, 0 },
{ "invalid", EINVAL },
{ "too small", ERANGE },
{ "too large", ERANGE },
};
long long strtonum(const char *numstr, long long minval, long long maxval,
const char **errstrp) {
long long ll = 0;
char *ep;
int error = 0;
struct errval {
const char *errstr;
int err;
} ev[4] = {
{NULL, 0},
{"invalid", EINVAL},
{"too small", ERANGE},
{"too large", ERANGE},
};
ev[0].err = errno;
errno = 0;
if (minval > maxval)
error = INVALID;
else {
ll = strtoll(numstr, &ep, 10);
if (numstr == ep || *ep != '\0')
error = INVALID;
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
error = TOOSMALL;
else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
error = TOOLARGE;
}
if (errstrp != NULL)
*errstrp = ev[error].errstr;
errno = ev[error].err;
if (error)
ll = 0;
ev[0].err = errno;
errno = 0;
if (minval > maxval)
error = INVALID;
else {
ll = strtoll(numstr, &ep, 10);
if (numstr == ep || *ep != '\0')
error = INVALID;
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
error = TOOSMALL;
else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
error = TOOLARGE;
}
if (errstrp != NULL)
*errstrp = ev[error].errstr;
errno = ev[error].err;
if (error)
ll = 0;
return (ll);
return (ll);
}
#endif /* HAVE_STRTONUM */

@ -14,73 +14,71 @@
*/
#include "xmppipe.h"
int
handle_version(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_stanza_t *reply = NULL;
xmpp_stanza_t *query = NULL;
xmpp_stanza_t *name = NULL;
xmpp_stanza_t *version = NULL;
xmpp_stanza_t *text = NULL;
xmpp_stanza_t *child = NULL;
const char *ns = NULL;
const char *id = NULL;
const char *from = NULL;
xmppipe_state_t *state = userdata;
xmpp_ctx_t *ctx = state->ctx;
reply = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(reply, "iq");
xmppipe_stanza_set_type(reply, "result");
id = xmpp_stanza_get_attribute(stanza, "id");
if (id == NULL)
return 1;
int handle_version(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
void *const userdata) {
xmpp_stanza_t *reply = NULL;
xmpp_stanza_t *query = NULL;
xmpp_stanza_t *name = NULL;
xmpp_stanza_t *version = NULL;
xmpp_stanza_t *text = NULL;
xmpp_stanza_t *child = NULL;
const char *ns = NULL;
const char *id = NULL;
const char *from = NULL;
xmppipe_state_t *state = userdata;
xmpp_ctx_t *ctx = state->ctx;
reply = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(reply, "iq");
xmppipe_stanza_set_type(reply, "result");
id = xmpp_stanza_get_attribute(stanza, "id");
if (id == NULL)
return 1;
xmppipe_stanza_set_id(reply, id);
xmppipe_stanza_set_id(reply, id);
from = xmpp_stanza_get_attribute(stanza, "from");
if (from == NULL)
return 1;
from = xmpp_stanza_get_attribute(stanza, "from");
if (from == NULL)
return 1;
xmppipe_stanza_set_attribute(reply, "to", from);
xmppipe_stanza_set_attribute(reply, "to", from);
query = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(query, "query");
query = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(query, "query");
child = xmpp_stanza_get_children(stanza);
if (child == NULL) {
(void)xmpp_stanza_release(query);
return 1;
}
child = xmpp_stanza_get_children(stanza);
if (child == NULL) {
(void)xmpp_stanza_release(query);
return 1;
}
ns = xmpp_stanza_get_ns(child);
if (ns)
xmppipe_stanza_set_ns(query, ns);
ns = xmpp_stanza_get_ns(child);
if (ns)
xmppipe_stanza_set_ns(query, ns);
name = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(name, "name");
xmppipe_stanza_add_child(query, name);
name = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(name, "name");
xmppipe_stanza_add_child(query, name);
text = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_text(text, "xmppipe");
xmppipe_stanza_add_child(name, text);
text = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_text(text, "xmppipe");
xmppipe_stanza_add_child(name, text);
version = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(version, "version");
xmppipe_stanza_add_child(query, version);
version = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(version, "version");
xmppipe_stanza_add_child(query, version);
text = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_text(text, XMPPIPE_VERSION);
xmppipe_stanza_add_child(version, text);
text = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_text(text, XMPPIPE_VERSION);
xmppipe_stanza_add_child(version, text);
xmppipe_stanza_add_child(reply, query);
xmppipe_stanza_add_child(reply, query);
xmppipe_send(state, reply);
(void)xmpp_stanza_release(reply);
xmppipe_send(state, reply);
(void)xmpp_stanza_release(reply);
return 1;
return 1;
}

File diff suppressed because it is too large Load Diff

@ -12,110 +12,110 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <ctype.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include <err.h>
#include <sys/types.h>
#include <unistd.h>
#include <strophe.h>
#define XMPPIPE_VERSION "0.12.0"
#define XMPPIPE_RESOURCE "xmppipe"
#define XMPPIPE_STREQ(a,b) (strcmp((a),(b)) == 0)
#define XMPPIPE_STRNEQ(a,b) (strcmp((a),(b)) != 0)
#define XMPPIPE_STREQ(a, b) (strcmp((a), (b)) == 0)
#define XMPPIPE_STRNEQ(a, b) (strcmp((a), (b)) != 0)
#define BASE64_LENGTH(n) ((((n) + 2) / 3) * 4)
#define BASE64_LENGTH(n) ((((n) + 2) / 3) * 4)
enum {
XMPPIPE_S_DISCONNECTED,
XMPPIPE_S_CONNECTING,
XMPPIPE_S_CONNECTED,
XMPPIPE_S_MUC_SERVICE_LOOKUP,
XMPPIPE_S_MUC_FEATURE_LOOKUP,
XMPPIPE_S_MUC_WAITJOIN,
XMPPIPE_S_MUC_JOIN,
XMPPIPE_S_MUC_UNLOCK,
XMPPIPE_S_READY,
XMPPIPE_S_READY_AVAIL,
XMPPIPE_S_READY_EMPTY,
XMPPIPE_S_DISCONNECTED,
XMPPIPE_S_CONNECTING,
XMPPIPE_S_CONNECTED,
XMPPIPE_S_MUC_SERVICE_LOOKUP,
XMPPIPE_S_MUC_FEATURE_LOOKUP,
XMPPIPE_S_MUC_WAITJOIN,
XMPPIPE_S_MUC_JOIN,
XMPPIPE_S_MUC_UNLOCK,
XMPPIPE_S_READY,
XMPPIPE_S_READY_AVAIL,
XMPPIPE_S_READY_EMPTY,
};
enum {
XMPPIPE_OPT_DISCARD = 1 << 0, /* Throw away stdin if no occupants in MUC */
XMPPIPE_OPT_DISCARD_TO_STDOUT = 1 << 1, /* Throw away stdin and send to local stdout */
XMPPIPE_OPT_EOF = 1 << 2, /* Keep running on stdin EOF */
XMPPIPE_OPT_SIGPIPE = 1 << 3, /* Exit if no occupants in MUC */
XMPPIPE_OPT_GROUPCHAT = 1 << 4, /* Use groupchat */
XMPPIPE_OPT_CHAT_MARKER = 1 << 5, /* Display chat markers */
XMPPIPE_OPT_DISCARD = 1 << 0, /* Throw away stdin if no occupants in MUC */
XMPPIPE_OPT_DISCARD_TO_STDOUT =
1 << 1, /* Throw away stdin and send to local stdout */
XMPPIPE_OPT_EOF = 1 << 2, /* Keep running on stdin EOF */
XMPPIPE_OPT_SIGPIPE = 1 << 3, /* Exit if no occupants in MUC */
XMPPIPE_OPT_GROUPCHAT = 1 << 4, /* Use groupchat */
XMPPIPE_OPT_CHAT_MARKER = 1 << 5, /* Display chat markers */
};
enum {
XMPPIPE_FMT_TEXT = 0,
XMPPIPE_FMT_CSV
};
enum { XMPPIPE_FMT_TEXT = 0, XMPPIPE_FMT_CSV };
typedef struct {
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
int handled;
char *room; /* room, room@conference.xmpp.example.com */
char *server; /* xmpp.example.com */
char *resource; /* nick */
char *mucservice; /* conference.xmpp.example.com */
char *mucjid; /* room@conference.xmpp.example.com/nick */
char *subject; /* topic/subject for MUC */
char *out; /* room@conference.xmpp.example.com */
int status;
int occupants;
u_int32_t poll; /* milliseconds */
u_int32_t keepalive; /* periodically send a keepalive (milliseconds) */
u_int32_t keepalive_fail; /* number of consecutive keepalives without a reply */
u_int32_t keepalive_limit; /* number of keepalives without a reply */
u_int32_t interval; /* time since last keepalive (milliseconds) */
size_t bufsz; /* size of read buffer */
int sm_enabled; /* stanzas: iq, message, presence */
u_int32_t sm_request; /* count of sent stanzas */
u_int32_t sm_request_unack; /* count of unacknowledged stream management requests */
u_int32_t sm_request_interval; /* request ack every interval stanzas */
u_int32_t sm_ack_recv; /* count of stanzas received from server */
u_int32_t sm_ack_sent; /* server's count of stanzas we've sent */
u_int32_t sm_unacked;
u_int32_t sm_fc;
int opt;
int verbose;
int encode; /* base64 encode/decode data to MUC */
int format; /* input format: stdin, colon */
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
int handled;
char *room; /* room, room@conference.xmpp.example.com */
char *server; /* xmpp.example.com */
char *resource; /* nick */
char *mucservice; /* conference.xmpp.example.com */
char *mucjid; /* room@conference.xmpp.example.com/nick */
char *subject; /* topic/subject for MUC */
char *out; /* room@conference.xmpp.example.com */
int status;
int occupants;
u_int32_t poll; /* milliseconds */
u_int32_t keepalive; /* periodically send a keepalive (milliseconds) */
u_int32_t
keepalive_fail; /* number of consecutive keepalives without a reply */
u_int32_t keepalive_limit; /* number of keepalives without a reply */
u_int32_t interval; /* time since last keepalive (milliseconds) */
size_t bufsz; /* size of read buffer */
int sm_enabled; /* stanzas: iq, message, presence */
u_int32_t sm_request; /* count of sent stanzas */
u_int32_t
sm_request_unack; /* count of unacknowledged stream management requests */
u_int32_t sm_request_interval; /* request ack every interval stanzas */
u_int32_t sm_ack_recv; /* count of stanzas received from server */
u_int32_t sm_ack_sent; /* server's count of stanzas we've sent */
u_int32_t sm_unacked;
u_int32_t sm_fc;
int opt;
int verbose;
int encode; /* base64 encode/decode data to MUC */
int format; /* input format: stdin, colon */
} xmppipe_state_t;
void event_loop(xmppipe_state_t *state);
int handle_stdin(xmppipe_state_t *state, int fd, char *buf, size_t len);
/* handlers */
int handle_message(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_null(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_ping_reply(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_presence(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_message(xmpp_conn_t *const, xmpp_stanza_t *const, void *const);
int handle_null(xmpp_conn_t *const, xmpp_stanza_t *const, void *const);
int handle_ping_reply(xmpp_conn_t *const, xmpp_stanza_t *const, void *const);
int handle_presence(xmpp_conn_t *const, xmpp_stanza_t *const, void *const);
void xmppipe_ping(xmppipe_state_t *);
int handle_presence_error(xmpp_conn_t * const, xmpp_stanza_t * const,
void * const);
int handle_sm_ack(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_sm_enabled(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_sm_request(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_version(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_presence_error(xmpp_conn_t *const, xmpp_stanza_t *const,
void *const);
int handle_sm_ack(xmpp_conn_t *const, xmpp_stanza_t *const, void *const);
int handle_sm_enabled(xmpp_conn_t *const, xmpp_stanza_t *const, void *const);
int handle_sm_request(xmpp_conn_t *const, xmpp_stanza_t *const, void *const);
int handle_version(xmpp_conn_t *const, xmpp_stanza_t *const, void *const);
void xmppipe_muc_join(xmppipe_state_t *);
void xmppipe_muc_subject(xmppipe_state_t *, char *);
@ -147,13 +147,13 @@ void *xmppipe_malloc(size_t);
void *xmppipe_calloc(size_t, size_t);
xmpp_stanza_t *xmppipe_stanza_new(xmpp_ctx_t *);
void xmppipe_stanza_set_attribute(xmpp_stanza_t * const, const char * const,
const char * const);
void xmppipe_stanza_set_id(xmpp_stanza_t * const, const char * const);
void xmppipe_stanza_set_name(xmpp_stanza_t *, const char * const);
void xmppipe_stanza_set_ns(xmpp_stanza_t * const, const char * const);
void xmppipe_stanza_set_text(xmpp_stanza_t *, const char * const);
void xmppipe_stanza_set_type(xmpp_stanza_t * const, const char * const);
void xmppipe_stanza_set_attribute(xmpp_stanza_t *const, const char *const,
const char *const);
void xmppipe_stanza_set_id(xmpp_stanza_t *const, const char *const);
void xmppipe_stanza_set_name(xmpp_stanza_t *, const char *const);
void xmppipe_stanza_set_ns(xmpp_stanza_t *const, const char *const);
void xmppipe_stanza_set_text(xmpp_stanza_t *, const char *const);
void xmppipe_stanza_set_type(xmpp_stanza_t *const, const char *const);
void xmppipe_stanza_add_child(xmpp_stanza_t *, xmpp_stanza_t *);
int xmppipe_sandbox_init(xmppipe_state_t *state);
@ -161,10 +161,10 @@ int xmppipe_sandbox_stdin(xmppipe_state_t *state);
int xmppipe_conn_fd(xmppipe_state_t *state);
int b64_ntop(u_char const *src, size_t srclength, char *target,
size_t targsize);
size_t targsize);
int b64_pton(char const *src, u_char *target, size_t targsize);
#ifndef HAVE_STRTONUM
long long strtonum(const char *numstr, long long minval, long long maxval,
const char **errstrp);
const char **errstrp);
#endif

@ -14,11 +14,11 @@
*/
#include "xmppipe.h"
#include <sys/time.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
/* Retrieve the XMPP socket opened by libstrophe.
@ -31,28 +31,26 @@
*
* The alternative is hardcoding the offsets based on the libstrophe version.
*/
int
xmppipe_conn_fd(xmppipe_state_t *state)
{
int fd = 0;
struct rlimit rl = {0};
struct stat st = {0};
int xmppipe_conn_fd(xmppipe_state_t *state) {
int fd = 0;
struct rlimit rl = {0};
struct stat st = {0};
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
return -1;
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
return -1;
for (fd = rl.rlim_cur; fd > STDERR_FILENO; fd--) {
if (fcntl(fd, F_GETFD, 0) < 0)
continue;
for (fd = rl.rlim_cur; fd > STDERR_FILENO; fd--) {
if (fcntl(fd, F_GETFD, 0) < 0)
continue;
if (fstat(fd, &st) < 0)
return -1;
if (fstat(fd, &st) < 0)
return -1;
if (!S_ISSOCK(st.st_mode))
continue;
if (!S_ISSOCK(st.st_mode))
continue;
return fd;
}
return fd;
}
return -1;
return -1;
}

@ -14,142 +14,136 @@
*/
#include "xmppipe.h"
#include <sys/select.h>
#include <netinet/in.h>
#include <resolv.h>
#include <sys/select.h>
void
event_loop(xmppipe_state_t *state)
{
int fd = STDIN_FILENO;
int eof = 0;
char *buf = NULL;
void event_loop(xmppipe_state_t *state) {
int fd = STDIN_FILENO;
int eof = 0;
char *buf = NULL;
if (xmppipe_set_nonblock(fd) < 0)
return;
if (xmppipe_set_nonblock(fd) < 0)
return;
buf = xmppipe_calloc(state->bufsz, 1);
buf = xmppipe_calloc(state->bufsz, 1);
for (;;) {
if (state->status == XMPPIPE_S_DISCONNECTED)
goto XMPPIPE_EXIT;
if (state->sm_enabled) {
if (state->sm_ack_sent > state->sm_request)
errx(EXIT_FAILURE, "h too large: sent=%u, server responded=%u",
state->sm_request, state->sm_ack_sent);
if ((state->sm_request_unack > state->sm_unacked) ||
(state->sm_request - state->sm_ack_sent > state->sm_fc)) {
if (state->verbose)
(void)fprintf(stderr, "WAIT: request=%u ack_sent=%u unack=%u\n",
state->sm_request, state->sm_ack_sent,
state->sm_request_unack);
goto XMPPIPE_POLL;
}
}
for ( ; ; ) {
if (state->status == XMPPIPE_S_DISCONNECTED)
goto XMPPIPE_EXIT;
if (eof) {
if (state->opt & XMPPIPE_OPT_EOF)
goto XMPPIPE_POLL;
if (state->sm_enabled) {
if (state->sm_ack_sent > state->sm_request)
errx(EXIT_FAILURE, "h too large: sent=%u, server responded=%u",
if (state->sm_enabled && (state->sm_ack_sent < state->sm_request)) {
if (state->verbose)
(void)fprintf(stderr, "POLLING: request: %d ack: %d\n",
state->sm_request, state->sm_ack_sent);
goto XMPPIPE_POLL;
} else
goto XMPPIPE_EXIT;
}
if ( (state->sm_request_unack > state->sm_unacked)
|| (state->sm_request - state->sm_ack_sent > state->sm_fc)) {
if (state->verbose)
(void)fprintf(stderr,
"WAIT: request=%u ack_sent=%u unack=%u\n",
state->sm_request, state->sm_ack_sent,
state->sm_request_unack);
goto XMPPIPE_POLL;
}
}
if (eof) {
if (state->opt & XMPPIPE_OPT_EOF)
goto XMPPIPE_POLL;
if (state->sm_enabled && (state->sm_ack_sent < state->sm_request)) {
if (state->verbose)
(void)fprintf(stderr, "POLLING: request: %d ack: %d\n",
state->sm_request, state->sm_ack_sent);
goto XMPPIPE_POLL;
}
else
goto XMPPIPE_EXIT;
}
switch (handle_stdin(state, fd, buf, state->bufsz-1)) {
case -1:
goto XMPPIPE_EXIT;
case 0:
if (!(state->opt & XMPPIPE_OPT_EOF) && !state->sm_enabled)
goto XMPPIPE_EXIT;
eof = 1;
break;
case 1:
break;
default:
(void)memset(buf, '\0', state->bufsz);
break;
}
state->interval += state->poll;
XMPPIPE_POLL:
if (state->keepalive > 0 && state->interval > state->keepalive) {
xmppipe_ping(state);
state->interval = 0;
}
if (state->keepalive_fail > state->keepalive_limit)
errx(EXIT_FAILURE, "no response to keepalives");
xmpp_run_once(state->ctx, state->poll);
state->interval += state->poll;
if ((state->opt & XMPPIPE_OPT_SIGPIPE)
&& state->status == XMPPIPE_S_READY_EMPTY)
goto XMPPIPE_EXIT;
switch (handle_stdin(state, fd, buf, state->bufsz - 1)) {
case -1:
goto XMPPIPE_EXIT;
case 0:
if (!(state->opt & XMPPIPE_OPT_EOF) && !state->sm_enabled)
goto XMPPIPE_EXIT;
eof = 1;
break;
case 1:
break;
default:
(void)memset(buf, '\0', state->bufsz);
break;
}
state->interval += state->poll;
XMPPIPE_POLL:
if (state->keepalive > 0 && state->interval > state->keepalive) {
xmppipe_ping(state);
state->interval = 0;
}
if (state->keepalive_fail > state->keepalive_limit)
errx(EXIT_FAILURE, "no response to keepalives");
xmpp_run_once(state->ctx, state->poll);
state->interval += state->poll;
if ((state->opt & XMPPIPE_OPT_SIGPIPE) &&
state->status == XMPPIPE_S_READY_EMPTY)
goto XMPPIPE_EXIT;
}
XMPPIPE_EXIT:
free(buf);
return;
free(buf);
return;
}
int
handle_stdin(xmppipe_state_t *state, int fd, char *buf, size_t len)
{
fd_set rfds;
struct timeval tv = {0};
ssize_t n = 0;
int rv = 0;
int handle_stdin(xmppipe_state_t *state, int fd, char *buf, size_t len) {
fd_set rfds;
struct timeval tv = {0};
ssize_t n = 0;
int rv = 0;
tv.tv_sec = 0;
tv.tv_usec = state->poll * 1000;
tv.tv_sec = 0;
tv.tv_usec = state->poll * 1000;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
rv = select(fd+1, &rfds, NULL, NULL, &tv);
rv = select(fd + 1, &rfds, NULL, NULL, &tv);
if (rv < 0) {
warn("select");
return -1;
}
if (rv < 0) {
warn("select");
return -1;
}
if (FD_ISSET(fd, &rfds)) {
n = read(fd, buf, len);
if (n <= 0)
return n;
if (state->verbose > 2)
(void)fprintf(stderr, "STDIN:%s\n", buf);
/* read and discard the data */
if ((state->opt & XMPPIPE_OPT_DISCARD) && state->occupants == 0) {
if (state->opt & XMPPIPE_OPT_DISCARD_TO_STDOUT) {
char *enc = NULL;
enc = xmppipe_fmt_encode(buf);
(void)printf("!:%s\n", enc);
free(enc);
}
return 2;
}
xmppipe_send_stanza(state, buf, n);
state->interval = 0;
return 3;
if (FD_ISSET(fd, &rfds)) {
n = read(fd, buf, len);
if (n <= 0)
return n;
if (state->verbose > 2)
(void)fprintf(stderr, "STDIN:%s\n", buf);
/* read and discard the data */
if ((state->opt & XMPPIPE_OPT_DISCARD) && state->occupants == 0) {
if (state->opt & XMPPIPE_OPT_DISCARD_TO_STDOUT) {
char *enc = NULL;
enc = xmppipe_fmt_encode(buf);
(void)printf("!:%s\n", enc);
free(enc);
}
return 2;
}
return 1;
xmppipe_send_stanza(state, buf, n);
state->interval = 0;
return 3;
}
return 1;
}

@ -17,94 +17,83 @@
static unsigned char rfc3986[256];
int
xmppipe_fmt_init()
{
int i = 0;
for (i = 0; i < 256; i++)
rfc3986[i] = isalnum(i)
|| i == '~' || i == '-' || i == '.' || i == '_'
|| i == '@' || i == '/'
? i : 0;
return 0;
int xmppipe_fmt_init() {
int i = 0;
for (i = 0; i < 256; i++)
rfc3986[i] = isalnum(i) || i == '~' || i == '-' || i == '.' || i == '_' ||
i == '@' || i == '/'
? i
: 0;
return 0;
}
char *
xmppipe_nfmt_encode(const char *s, size_t len)
{
char *buf = xmppipe_calloc(len * 3 + 1, 1);
char *p = buf;
size_t i = 0;
for (i = 0; i < len; i++) {
unsigned char c = s[i];
if (rfc3986[c]) {
*p = c;
p++;
}
else {
p += sprintf(p, "%%%02X", c);
}
char *xmppipe_nfmt_encode(const char *s, size_t len) {
char *buf = xmppipe_calloc(len * 3 + 1, 1);
char *p = buf;
size_t i = 0;
for (i = 0; i < len; i++) {
unsigned char c = s[i];
if (rfc3986[c]) {
*p = c;
p++;
} else {
p += sprintf(p, "%%%02X", c);
}
}
return buf;
return buf;
}
char *
xmppipe_fmt_encode(const char *s)
{
return xmppipe_nfmt_encode(s, strlen(s));
char *xmppipe_fmt_encode(const char *s) {
return xmppipe_nfmt_encode(s, strlen(s));
}
char *
xmppipe_nfmt_decode(const char *s, size_t len)
{
char *buf;
char *p;
size_t i = 0;
char fmt[3] = {0};
char *endptr;
char *xmppipe_nfmt_decode(const char *s, size_t len) {
char *buf;
char *p;
size_t i = 0;
char fmt[3] = {0};
char *endptr;
buf = xmppipe_calloc(len+1, 1);
p = buf;
buf = xmppipe_calloc(len + 1, 1);
p = buf;
for (i = 0; i < len; i++) {
unsigned char c = s[i];
if (c == '%') {
unsigned char n = 0;
for (i = 0; i < len; i++) {
unsigned char c = s[i];
if (c == '%') {
unsigned char n = 0;
if (i + 2 > len) goto XMPPIPE_ERR;
if (i + 2 > len)
goto XMPPIPE_ERR;
(void)memcpy(fmt, s+i+1, 2);
(void)memcpy(fmt, s + i + 1, 2);
errno = 0;
n = strtol(fmt, &endptr, 16);
errno = 0;
n = strtol(fmt, &endptr, 16);
if ( (errno != 0) || (endptr == fmt))
goto XMPPIPE_ERR;
if ((errno != 0) || (endptr == fmt))
goto XMPPIPE_ERR;
*p++ = n;
i += 2;
}
else {
*p++ = c;
}
*p++ = n;
i += 2;
} else {
*p++ = c;
}
}
return buf;
return buf;
XMPPIPE_ERR:
free(buf);
return NULL;
free(buf);
return NULL;
}
char *
xmppipe_fmt_decode(const char *s)
{
if (s == NULL)
return NULL;
char *xmppipe_fmt_decode(const char *s) {
if (s == NULL)
return NULL;
return xmppipe_nfmt_decode(s, strlen(s));
return xmppipe_nfmt_decode(s, strlen(s));
}

@ -13,61 +13,57 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef XMPPIPE_SANDBOX_capsicum
#include <sys/types.h>
#include <sys/capability.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/capability.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>
#include "xmppipe.h"
int
xmppipe_sandbox_init(xmppipe_state_t *state)
{
struct rlimit rl = {0};
int xmppipe_sandbox_init(xmppipe_state_t *state) {
struct rlimit rl = {0};
return setrlimit(RLIMIT_NPROC, &rl);
return setrlimit(RLIMIT_NPROC, &rl);
}
int
xmppipe_sandbox_stdin(xmppipe_state_t *state)
{
struct rlimit rl = {0};
cap_rights_t policy_read;
cap_rights_t policy_write;
cap_rights_t policy_rw;
int xmppipe_sandbox_stdin(xmppipe_state_t *state) {
struct rlimit rl = {0};
cap_rights_t policy_read;
cap_rights_t policy_write;
cap_rights_t policy_rw;
int fd = -1;
int fd = -1;
fd = xmppipe_conn_fd(state);
if (fd < 0)
return -1;
fd = xmppipe_conn_fd(state);
if (fd < 0)
return -1;
rl.rlim_cur = fd;
rl.rlim_max = fd;
rl.rlim_cur = fd;
rl.rlim_max = fd;
if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
return -1;
if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
return -1;
(void)cap_rights_init(&policy_read, CAP_READ, CAP_EVENT);
(void)cap_rights_init(&policy_write, CAP_WRITE);
(void)cap_rights_init(&policy_rw, CAP_READ, CAP_WRITE,
CAP_FSTAT, CAP_FCNTL, CAP_EVENT);
(void)cap_rights_init(&policy_read, CAP_READ, CAP_EVENT);
(void)cap_rights_init(&policy_write, CAP_WRITE);
(void)cap_rights_init(&policy_rw, CAP_READ, CAP_WRITE, CAP_FSTAT, CAP_FCNTL,
CAP_EVENT);
if (cap_rights_limit(STDIN_FILENO, &policy_read) < 0)
return -1;
if (cap_rights_limit(STDIN_FILENO, &policy_read) < 0)
return -1;
if (cap_rights_limit(STDOUT_FILENO, &policy_write) < 0)
return -1;
if (cap_rights_limit(STDOUT_FILENO, &policy_write) < 0)
return -1;
if (cap_rights_limit(STDERR_FILENO, &policy_write) < 0)
return -1;
if (cap_rights_limit(STDERR_FILENO, &policy_write) < 0)
return -1;
if (cap_rights_limit(fd, &policy_rw) < 0)
return -1;
if (cap_rights_limit(fd, &policy_rw) < 0)
return -1;
return cap_enter();
return cap_enter();
}
#endif

@ -15,15 +15,7 @@
#ifdef XMPPIPE_SANDBOX_null
#include "xmppipe.h"
int
xmppipe_sandbox_init(xmppipe_state_t *state)
{
return 0;
}
int xmppipe_sandbox_init(xmppipe_state_t *state) { return 0; }
int
xmppipe_sandbox_stdin(xmppipe_state_t *state)
{
return 0;
}
int xmppipe_sandbox_stdin(xmppipe_state_t *state) { return 0; }
#endif

@ -13,18 +13,14 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef XMPPIPE_SANDBOX_pledge
#include <unistd.h>
#include "xmppipe.h"
#include <unistd.h>
int
xmppipe_sandbox_init(xmppipe_state_t *state)
{
return pledge("stdio inet dns", NULL);
int xmppipe_sandbox_init(xmppipe_state_t *state) {
return pledge("stdio inet dns", NULL);
}
int
xmppipe_sandbox_stdin(xmppipe_state_t *state)
{
return pledge("stdio", NULL);
int xmppipe_sandbox_stdin(xmppipe_state_t *state) {
return pledge("stdio", NULL);
}
#endif

@ -13,33 +13,30 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef XMPPIPE_SANDBOX_rlimit
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/time.h>
#include "xmppipe.h"
int
xmppipe_sandbox_init(xmppipe_state_t *state)
{
struct rlimit rl_zero = {0};
int xmppipe_sandbox_init(xmppipe_state_t *state) {
struct rlimit rl_zero = {0};
return setrlimit(RLIMIT_NPROC, &rl_zero);
return setrlimit(RLIMIT_NPROC, &rl_zero);
}
int
xmppipe_sandbox_stdin(xmppipe_state_t *state)
{
struct rlimit rl = {0};
int xmppipe_sandbox_stdin(xmppipe_state_t *state) {
struct rlimit rl = {0};
rl.rlim_cur = XMPPIPE_SANDBOX_RLIMIT_NOFILE;
rl.rlim_max = XMPPIPE_SANDBOX_RLIMIT_NOFILE;
rl.rlim_cur = XMPPIPE_SANDBOX_RLIMIT_NOFILE;
rl.rlim_max = XMPPIPE_SANDBOX_RLIMIT_NOFILE;
if (rl.rlim_cur == (rlim_t)-1) {
int fd = xmppipe_conn_fd(state);
if (fd < 0) return -1;
rl.rlim_cur = rl.rlim_max = fd + 1;
}
if (rl.rlim_cur == (rlim_t)-1) {
int fd = xmppipe_conn_fd(state);
if (fd < 0)
return -1;
rl.rlim_cur = rl.rlim_max = fd + 1;
}
return setrlimit(RLIMIT_NOFILE, &rl);
return setrlimit(RLIMIT_NOFILE, &rl);
}
#endif

@ -13,13 +13,13 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef XMPPIPE_SANDBOX_seccomp
#include <stddef.h>
#include <errno.h>
#include <sys/syscall.h>
#include <linux/audit.h>
#include <linux/filter.h>
#include <sys/prctl.h>
#include <linux/seccomp.h>
#include <stddef.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include "xmppipe.h"
@ -30,27 +30,28 @@
/* Use a signal handler to emit violations when debugging */
#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
# undef SECCOMP_FILTER_FAIL
# define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP
#undef SECCOMP_FILTER_FAIL
#define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP
#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
/* Simple helpers to avoid manual errors (but larger BPF programs). */
#define SC_DENY(_nr, _errno) \
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 1), \
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno))
#define SC_ALLOW(_nr) \
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 1), \
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
#define SC_ALLOW_ARG(_nr, _arg_nr, _arg_val) \
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 4), \
/* load first syscall argument */ \
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
offsetof(struct seccomp_data, args[(_arg_nr)])), \
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_arg_val), 0, 1), \
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), \
/* reload syscall number; all rules expect it in accumulator */ \
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
offsetof(struct seccomp_data, nr))
#define SC_DENY(_nr, _errno) \
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_##_nr, 0, 1) \
, BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (_errno))
#define SC_ALLOW(_nr) \
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_##_nr, 0, 1) \
, BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW)
#define SC_ALLOW_ARG(_nr, _arg_nr, _arg_val) \
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_##_nr, 0, 4) \
, /* load first syscall argument */ \
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \
offsetof(struct seccomp_data, args[(_arg_nr)])), \
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_arg_val), 0, 1), \
BPF_STMT( \
BPF_RET + BPF_K, SECCOMP_RET_ALLOW), /* reload syscall number; all \
rules expect it in \
accumulator */ \
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct seccomp_data, nr))
/*
* http://outflux.net/teach-seccomp/
@ -61,337 +62,327 @@
#define arch_nr (offsetof(struct seccomp_data, arch))
#if defined(__i386__)
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386
#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386
#elif defined(__x86_64__)
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64
#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64
#elif defined(__arm__)
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM
#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM
#elif defined(__aarch64__)
# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
#else
# warning "seccomp: unsupported platform"
# define SECCOMP_AUDIT_ARCH 0
#endif
int
xmppipe_sandbox_init(xmppipe_state_t *state)
{
struct sock_filter filter[] = {
/* Ensure the syscall arch convention is as expected. */
BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
offsetof(struct seccomp_data, arch)),
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
/* Load the syscall number for checking. */
BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
offsetof(struct seccomp_data, nr)),
/* Syscalls to allow */
/* dns */
#warning "seccomp: unsupported platform"
#define SECCOMP_AUDIT_ARCH 0
#endif
int xmppipe_sandbox_init(xmppipe_state_t *state) {
struct sock_filter filter[] = {
/* Ensure the syscall arch convention is as expected. */
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct seccomp_data, arch)),
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
BPF_STMT(BPF_RET + BPF_K, SECCOMP_FILTER_FAIL),
/* Load the syscall number for checking. */
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct seccomp_data, nr)),
/* Syscalls to allow */
/* dns */
#ifdef __NR_socket
SC_ALLOW(socket),
SC_ALLOW(socket),
#endif
#ifdef __NR_recvfrom
SC_ALLOW(recvfrom),
SC_ALLOW(recvfrom),
#endif
#ifdef __NR_recv
SC_ALLOW(recv),
SC_ALLOW(recv),
#endif
#ifdef __NR_recvmsg
SC_ALLOW(recvmsg),
SC_ALLOW(recvmsg),
#endif
#ifdef __NR_sendto
SC_ALLOW(sendto),
SC_ALLOW(sendto),
#endif
#ifdef __NR_send
SC_ALLOW(send),
SC_ALLOW(send),
#endif
#ifdef __NR_sendmsg
SC_ALLOW(sendmsg),
SC_ALLOW(sendmsg),
#endif
#ifdef __NR_sendmmsg
SC_ALLOW(sendmmsg),
SC_ALLOW(sendmmsg),
#endif
#ifdef __NR_connect
SC_ALLOW(connect),
SC_ALLOW(connect),
#endif
#ifdef __NR_bind
SC_ALLOW(bind),
SC_ALLOW(bind),
#endif
#ifdef __NR_stat
SC_ALLOW(stat),
SC_ALLOW(stat),
#endif
#ifdef __NR_stat64
SC_ALLOW(stat64),
SC_ALLOW(stat64),
#endif
#ifdef __NR_uname
SC_ALLOW(uname),
SC_ALLOW(uname),
#endif
/* /etc/resolv.conf */
/* /etc/resolv.conf */
#ifdef __NR_open
SC_ALLOW(open),
SC_ALLOW(open),
#endif
#ifdef __NR_openat
SC_ALLOW(openat),
SC_ALLOW(openat),
#endif
#ifdef __NR_close
SC_ALLOW(close),
SC_ALLOW(close),
#endif
/* inet */
/* inet */
#ifdef __NR_getpeername
SC_ALLOW(getpeername),
SC_ALLOW(getpeername),
#endif
#ifdef __NR_getsockname
SC_ALLOW(getsockname),
SC_ALLOW(getsockname),
#endif
#ifdef __NR_setsockopt
SC_ALLOW(setsockopt),
SC_ALLOW(setsockopt),
#endif
#ifdef __NR_getsockopt
SC_ALLOW(getsockopt),
SC_ALLOW(getsockopt),
#endif
#ifdef __NR_lseek
SC_ALLOW(lseek),
SC_ALLOW(lseek),
#endif
#ifdef __NR__llseek
SC_ALLOW(_llseek),
SC_ALLOW(_llseek),
#endif
/* uuid */
/* uuid */
#ifdef __NR_gettimeofday
SC_ALLOW(gettimeofday),
SC_ALLOW(gettimeofday),
#endif
#ifdef __NR_getpid
SC_ALLOW(getpid),
SC_ALLOW(getpid),
#endif
#ifdef __NR_brk
SC_ALLOW(brk),
SC_ALLOW(brk),
#endif
#ifdef __NR_clock_gettime
SC_ALLOW(clock_gettime),
SC_ALLOW(clock_gettime),
#endif
#ifdef __NR_exit_group
SC_ALLOW(exit_group),
SC_ALLOW(exit_group),
#endif
#ifdef __NR_fcntl
SC_ALLOW(fcntl),
SC_ALLOW(fcntl),
#endif
#ifdef __NR_fcntl64
SC_ALLOW(fcntl64),
SC_ALLOW(fcntl64),
#endif
#ifdef __NR_fstat
SC_ALLOW(fstat),
SC_ALLOW(fstat),
#endif
#ifdef __NR_fstat64
SC_ALLOW(fstat64),
SC_ALLOW(fstat64),
#endif
#ifdef __NR_getrandom
SC_ALLOW(getrandom),
SC_ALLOW(getrandom),
#endif
#ifdef __NR_getppid
SC_ALLOW(getppid),
SC_ALLOW(getppid),
#endif
#ifdef __NR_gettid
SC_ALLOW(gettid),
SC_ALLOW(gettid),
#endif
#ifdef __NR_gettimeofday
SC_ALLOW(gettimeofday),
SC_ALLOW(gettimeofday),
#endif
#ifdef __NR_getuid
SC_ALLOW(getuid),
SC_ALLOW(getuid),
#endif
#ifdef __NR_getuid32
SC_ALLOW(getuid32),
SC_ALLOW(getuid32),
#endif
#ifdef __NR_ioctl
SC_ALLOW(ioctl),
SC_ALLOW(ioctl),
#endif
#ifdef __NR_mmap
SC_ALLOW(mmap),
SC_ALLOW(mmap),
#endif
#ifdef __NR_munmap
SC_ALLOW(munmap),
SC_ALLOW(munmap),
#endif
#ifdef __NR_mprotect
SC_ALLOW(mprotect),
SC_ALLOW(mprotect),
#endif
#ifdef __NR_poll
SC_ALLOW(poll),
SC_ALLOW(poll),
#endif
#ifdef __NR_read
SC_ALLOW(read),
SC_ALLOW(read),
#endif
#ifdef __NR__newselect
SC_ALLOW(_newselect),
SC_ALLOW(_newselect),
#endif
#ifdef __NR_select
SC_ALLOW(select),
SC_ALLOW(select),
#endif
#ifdef __NR_stat
SC_ALLOW(stat),
SC_ALLOW(stat),
#endif
#ifdef __NR_stat64
SC_ALLOW(stat64),
SC_ALLOW(stat64),
#endif
#ifdef __NR_write
SC_ALLOW(write),
SC_ALLOW(write),
#endif
#ifdef __NR_mmap
SC_ALLOW(mmap),
SC_ALLOW(mmap),
#endif
#ifdef __NR_mmap2
SC_ALLOW(mmap2),
SC_ALLOW(mmap2),
#endif
#ifdef __NR_access
SC_ALLOW(access),
SC_ALLOW(access),
#endif
#ifdef __NR_lseek
SC_ALLOW(lseek),
SC_ALLOW(lseek),
#endif
#ifdef __NR_prctl
SC_ALLOW(prctl),
SC_ALLOW(prctl),
#endif
#ifdef __NR_futex
SC_ALLOW(futex),
SC_ALLOW(futex),
#endif
#ifdef __NR_sysinfo
SC_ALLOW(sysinfo),
SC_ALLOW(sysinfo),
#endif
#ifdef __NR_restart_syscall
SC_ALLOW(restart_syscall),
SC_ALLOW(restart_syscall),
#endif
/* Default deny */
BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL)
};
/* Default deny */
BPF_STMT(BPF_RET + BPF_K, SECCOMP_FILTER_FAIL)};
struct sock_fprog prog = {
.len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
.filter = filter,
};
struct sock_fprog prog = {
.len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
.filter = filter,
};
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
return -1;
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
return -1;
return prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
return prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
}
int
xmppipe_sandbox_stdin(xmppipe_state_t *state)
{
struct sock_filter filter[] = {
/* Ensure the syscall arch convention is as expected. */
BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
offsetof(struct seccomp_data, arch)),
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
/* Load the syscall number for checking. */
BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
offsetof(struct seccomp_data, nr)),
/* Syscalls to non-fatally deny */
int xmppipe_sandbox_stdin(xmppipe_state_t *state) {
struct sock_filter filter[] = {
/* Ensure the syscall arch convention is as expected. */
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct seccomp_data, arch)),
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
BPF_STMT(BPF_RET + BPF_K, SECCOMP_FILTER_FAIL),
/* Load the syscall number for checking. */
BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct seccomp_data, nr)),
/* Syscalls to non-fatally deny */
#ifdef __NR_open
SC_DENY(open, EACCES),
SC_DENY(open, EACCES),
#endif
#ifdef __NR_close
SC_DENY(close, EBADF),
SC_DENY(close, EBADF),
#endif
/* Syscalls to allow */
/* Syscalls to allow */
#ifdef __NR_brk
SC_ALLOW(brk),
SC_ALLOW(brk),
#endif
#ifdef __NR_clock_gettime
SC_ALLOW(clock_gettime),
SC_ALLOW(clock_gettime),
#endif
#ifdef __NR_exit_group
SC_ALLOW(exit_group),
SC_ALLOW(exit_group),
#endif
#ifdef __NR_fcntl
SC_ALLOW(fcntl),
SC_ALLOW(fcntl),
#endif
#ifdef __NR_fcntl64
SC_ALLOW(fcntl64),
SC_ALLOW(fcntl64),
#endif
#ifdef __NR_fstat
SC_ALLOW(fstat),
SC_ALLOW(fstat),
#endif
#ifdef __NR_fstat64
SC_ALLOW(fstat64),
SC_ALLOW(fstat64),
#endif
#ifdef __NR_getppid
SC_ALLOW(getppid),
SC_ALLOW(getppid),
#endif
#ifdef __NR_gettid
SC_ALLOW(gettid),
SC_ALLOW(gettid),
#endif
#ifdef __NR_gettimeofday
SC_ALLOW(gettimeofday),
SC_ALLOW(gettimeofday),
#endif
#ifdef __NR_getuid
SC_ALLOW(getuid),
SC_ALLOW(getuid),
#endif
#ifdef __NR_getuid32
SC_ALLOW(getuid32),
SC_ALLOW(getuid32),
#endif
#ifdef __NR_ioctl
SC_ALLOW(ioctl),
SC_ALLOW(ioctl),
#endif
#ifdef __NR_mmap
SC_ALLOW(mmap),
SC_ALLOW(mmap),
#endif
#ifdef __NR_munmap
SC_ALLOW(munmap),
SC_ALLOW(munmap),
#endif
#ifdef __NR_mprotect
SC_ALLOW(mprotect),
SC_ALLOW(mprotect),
#endif
#ifdef __NR_poll
SC_ALLOW(poll),
SC_ALLOW(poll),
#endif
#ifdef __NR_read
SC_ALLOW(read),
SC_ALLOW(read),
#endif
#ifdef __NR__newselect
SC_ALLOW(_newselect),
SC_ALLOW(_newselect),
#endif
#ifdef __NR_select
SC_ALLOW(select),
SC_ALLOW(select),
#endif
#ifdef __NR_stat
SC_ALLOW(stat),
SC_ALLOW(stat),
#endif
#ifdef __NR_stat64
SC_ALLOW(stat64),
SC_ALLOW(stat64),
#endif
#ifdef __NR_write
SC_ALLOW(write),
SC_ALLOW(write),
#endif
#ifdef __NR_restart_syscall
SC_ALLOW(restart_syscall),
SC_ALLOW(restart_syscall),
#endif
/* Default deny */
BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL)
};
/* Default deny */
BPF_STMT(BPF_RET + BPF_K, SECCOMP_FILTER_FAIL)};
struct sock_fprog prog = {
.len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
.filter = filter,
};
struct sock_fprog prog = {
.len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
.filter = filter,
};
return prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
return prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
}
#endif

@ -14,199 +14,187 @@
*/
#include "xmppipe.h"
void
xmppipe_send_stanza(xmppipe_state_t *state, char *buf, size_t len)
{
switch (state->format) {
case XMPPIPE_FMT_TEXT:
xmppipe_send_message(state,
state->out,
(state->opt & XMPPIPE_OPT_GROUPCHAT) ? "groupchat" : "chat",
buf, len);
return;
case XMPPIPE_FMT_CSV:
xmppipe_send_stanza_fmt(state, buf, len);
break;
void xmppipe_send_stanza(xmppipe_state_t *state, char *buf, size_t len) {
switch (state->format) {
case XMPPIPE_FMT_TEXT:
xmppipe_send_message(
state, state->out,
(state->opt & XMPPIPE_OPT_GROUPCHAT) ? "groupchat" : "chat", buf, len);
return;
case XMPPIPE_FMT_CSV:
xmppipe_send_stanza_fmt(state, buf, len);
break;
default:
if (state->verbose)
(void)fprintf(stderr, "unsupported format: %d\n", state->format);
return;
}
}
void xmppipe_send_stanza_fmt(xmppipe_state_t *state, char *buf, size_t len) {
char *to = NULL;
char *type = NULL;
char *default_type;
int i;
size_t n;
char *tmp;
char *start;
char *end;
char *body = NULL;
int valid = 0;
default_type = (state->opt & XMPPIPE_OPT_GROUPCHAT) ? "groupchat" : "chat";
tmp = xmppipe_strdup(buf);
start = tmp;
/* trailing newline */
end = strchr(start, '\n');
if (end != NULL)
*end = '\0';
for (i = 0; start != NULL; i++) {
end = strchr(start, ':');
if (end != NULL)
*end++ = '\0';
n = strlen(start);
if (state->verbose)
(void)fprintf(stderr, "message:%d:%s\n", i, n == 0 ? "<empty>" : start);
switch (i) {
case 0:
if (n != 1) {
if (state->verbose)
(void)fprintf(stderr, "stanza required\n");
goto XMPPIPE_DONE;
}
switch (start[0]) {
case 'm':
break;
case 'p':
/* unsupported: fall through */
default:
if (state->verbose)
(void)fprintf(stderr, "unsupported format: %d\n", state->format);
(void)fprintf(stderr, "unsupported stanza: %c\n", start[0]);
return;
}
}
goto XMPPIPE_DONE;
}
break;
void
xmppipe_send_stanza_fmt(xmppipe_state_t *state, char *buf, size_t len)
{
char *to = NULL;
char *type = NULL;
char *default_type;
case 1:
type = xmppipe_fmt_decode((n == 0) ? default_type : start);
if (type == NULL)
goto XMPPIPE_DONE;
break;
int i;
size_t n;
char *tmp;
char *start;
char *end;
char *body = NULL;
int valid = 0;
case 2:
to = xmppipe_fmt_decode((n == 0) ? state->out : start);
if (to == NULL)
goto XMPPIPE_DONE;
default_type = (state->opt & XMPPIPE_OPT_GROUPCHAT) ? "groupchat" : "chat";
break;
tmp = xmppipe_strdup(buf);
start = tmp;
case 3:
break;
/* trailing newline */
end = strchr(start, '\n');
if (end != NULL)
*end = '\0';
for (i = 0; start != NULL; i++) {
end = strchr(start, ':');
if (end != NULL)
*end++ = '\0';
n = strlen(start);
if (state->verbose)
(void)fprintf(stderr, "message:%d:%s\n", i,
n == 0 ? "<empty>" : start);
switch (i) {
case 0:
if (n != 1) {
if (state->verbose)
(void)fprintf(stderr, "stanza required\n");
goto XMPPIPE_DONE;
}
switch (start[0]) {
case 'm':
break;
case 'p':
/* unsupported: fall through */
default:
if (state->verbose)
(void)fprintf(stderr, "unsupported stanza: %c\n", start[0]);
goto XMPPIPE_DONE;
}
break;
case 1:
type = xmppipe_fmt_decode((n == 0) ? default_type : start);
if (type == NULL)
goto XMPPIPE_DONE;
break;
case 2:
to = xmppipe_fmt_decode((n == 0) ? state->out : start);
if (to == NULL)
goto XMPPIPE_DONE;
break;
case 3:
break;
case 4:
body = xmppipe_fmt_decode(start);
if (body == NULL)
goto XMPPIPE_DONE;
valid = 1;
break;
default:
goto XMPPIPE_DONE;
}
case 4:
body = xmppipe_fmt_decode(start);
if (body == NULL)
goto XMPPIPE_DONE;
valid = 1;
break;
start = end;
default:
goto XMPPIPE_DONE;
}
start = end;
}
XMPPIPE_DONE:
if (valid == 1)
xmppipe_send_message(state, to, type, body, strlen(body));
else
if (state->verbose)
(void)fprintf(stderr, "invalid input\n");
free(tmp);
free(type);
free(to);
free(body);
if (valid == 1)
xmppipe_send_message(state, to, type, body, strlen(body));
else if (state->verbose)
(void)fprintf(stderr, "invalid input\n");
free(tmp);
free(type);
free(to);
free(body);
}
void
xmppipe_send_message(xmppipe_state_t *state, char *to, char *type, char *buf,
size_t len)
{
xmpp_stanza_t *message = NULL;
xmpp_stanza_t *body = NULL;
xmpp_stanza_t *text = NULL;
char *id = NULL;
void xmppipe_send_message(xmppipe_state_t *state, char *to, char *type,
char *buf, size_t len) {
xmpp_stanza_t *message = NULL;
xmpp_stanza_t *body = NULL;
xmpp_stanza_t *text = NULL;
char *id = NULL;
id = xmpp_uuid_gen(state->ctx);
id = xmpp_uuid_gen(state->ctx);
if (id == NULL) {
errx(EXIT_FAILURE, "unable to allocate message id");
}
if (id == NULL) {
errx(EXIT_FAILURE, "unable to allocate message id");
}
message = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(message, "message");
xmppipe_stanza_set_type(message, type);
xmppipe_stanza_set_attribute(message, "to", to);
xmppipe_stanza_set_id(message, id);
message = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(message, "message");
xmppipe_stanza_set_type(message, type);
xmppipe_stanza_set_attribute(message, "to", to);
xmppipe_stanza_set_id(message, id);
body = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(body, "body");
body = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(body, "body");
text = xmppipe_stanza_new(state->ctx);
text = xmppipe_stanza_new(state->ctx);
if (state->encode) {
size_t len = strlen(buf);
char *b64 = xmpp_base64_encode(state->ctx, (unsigned char *)buf, len);
if (state->encode) {
size_t len = strlen(buf);
char *b64 = xmpp_base64_encode(state->ctx, (unsigned char *)buf, len);
if (b64 == NULL)
errx(EXIT_FAILURE, "encode: invalid input: %zu", len);
if (b64 == NULL)
errx(EXIT_FAILURE, "encode: invalid input: %zu", len);
xmppipe_stanza_set_text(text, b64);
xmpp_free(state->ctx, b64);
}
else {
xmppipe_stanza_set_text(text, buf);
}
xmppipe_stanza_set_text(text, b64);
xmpp_free(state->ctx, b64);
} else {
xmppipe_stanza_set_text(text, buf);
}
xmppipe_stanza_add_child(body, text);
xmppipe_stanza_add_child(message, body);
xmppipe_stanza_add_child(body, text);
xmppipe_stanza_add_child(message, body);
xmppipe_send(state, message);
(void)xmpp_stanza_release(message);
xmpp_free(state->ctx, id);
xmppipe_send(state, message);
(void)xmpp_stanza_release(message);
xmpp_free(state->ctx, id);
}
void
xmppipe_send(xmppipe_state_t *state, xmpp_stanza_t *const stanza)
{
xmpp_stanza_t *r = NULL;
void xmppipe_send(xmppipe_state_t *state, xmpp_stanza_t *const stanza) {
xmpp_stanza_t *r = NULL;
state->sm_request++;
state->sm_request++;
xmpp_send(state->conn, stanza);
xmpp_send(state->conn, stanza);
if (state->sm_enabled == 0)
return;
if (state->sm_enabled == 0)
return;
if (state->sm_request % state->sm_request_interval != 0)
return;
if (state->sm_request % state->sm_request_interval != 0)
return;
r = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(r, "r");
xmppipe_stanza_set_ns(r, "urn:xmpp:sm:3");
xmpp_send(state->conn, r);
state->sm_request_unack++;
r = xmppipe_stanza_new(state->ctx);
xmppipe_stanza_set_name(r, "r");
xmppipe_stanza_set_ns(r, "urn:xmpp:sm:3");
xmpp_send(state->conn, r);
state->sm_request_unack++;
(void)xmpp_stanza_release(r);
(void)xmpp_stanza_release(r);
}

@ -16,257 +16,223 @@
#include <fcntl.h>
#define XMPPIPE_NULL(a_) \
if ((a_) == NULL) { \
errx(3, "invalid argument: %s/%s (%s:%d)", \
__FUNCTION__, \
#a_, \
__FILE__, \
__LINE__); }
const char * const xmppipe_states[] = {
"XMPPIPE_S_DISCONNECTED",
"XMPPIPE_S_CONNECTING",
"XMPPIPE_S_CONNECTED",
"XMPPIPE_S_MUC_SERVICE_LOOKUP",
"XMPPIPE_S_MUC_FEATURE_LOOKUP",
"XMPPIPE_S_MUC_WAITJOIN",
"XMPPIPE_S_MUC_JOIN",
"XMPPIPE_S_MUC_UNLOCK",
"XMPPIPE_S_READY",
"XMPPIPE_S_READY_AVAIL",
"XMPPIPE_S_READY_EMPTY"
};
int
xmppipe_set_nonblock(int fd)
{
int flags = 0;
flags = fcntl(fd, F_GETFD);
if (flags < 0)
return -1;
if (fcntl(fd, F_SETFD, flags|O_NONBLOCK) < 0)
return -1;
return 0;
#define XMPPIPE_NULL(a_) \
if ((a_) == NULL) { \
errx(3, "invalid argument: %s/%s (%s:%d)", __FUNCTION__, #a_, __FILE__, \
__LINE__); \
}
const char *const xmppipe_states[] = {"XMPPIPE_S_DISCONNECTED",
"XMPPIPE_S_CONNECTING",
"XMPPIPE_S_CONNECTED",
"XMPPIPE_S_MUC_SERVICE_LOOKUP",
"XMPPIPE_S_MUC_FEATURE_LOOKUP",
"XMPPIPE_S_MUC_WAITJOIN",
"XMPPIPE_S_MUC_JOIN",
"XMPPIPE_S_MUC_UNLOCK",
"XMPPIPE_S_READY",
"XMPPIPE_S_READY_AVAIL",
"XMPPIPE_S_READY_EMPTY"};
int xmppipe_set_nonblock(int fd) {
int flags = 0;
flags = fcntl(fd, F_GETFD);
if (flags < 0)
return -1;
if (fcntl(fd, F_SETFD, flags | O_NONBLOCK) < 0)
return -1;
return 0;
}
char *
xmppipe_getenv(const char *s)
{
char *p = getenv(s);
char *xmppipe_getenv(const char *s) {
char *p = getenv(s);
if (p == NULL)
return NULL;
if (p == NULL)
return NULL;
return xmppipe_strdup(p);
return xmppipe_strdup(p);
}
char *
xmppipe_strdup(const char *s)
{
char *buf = NULL;
char *xmppipe_strdup(const char *s) {
char *buf = NULL;
if (s == NULL)
errx(2, "invalid string");
if (s == NULL)
errx(2, "invalid string");
buf = strdup(s);
if (buf == NULL)
err(3, "xmppipe_strdup");
buf = strdup(s);
if (buf == NULL)
err(3, "xmppipe_strdup");
return buf;
return buf;
}
void *
xmppipe_malloc(size_t size)
{
char *buf = NULL;
void *xmppipe_malloc(size_t size) {
char *buf = NULL;
if (size == 0 || size > 0xffff)
errx(2, "invalid size: %zd", size);
if (size == 0 || size > 0xffff)
errx(2, "invalid size: %zd", size);
buf = malloc(size);
if (buf == NULL)
err(3, "xmppipe_malloc: %zu", size);
buf = malloc(size);
if (buf == NULL)
err(3, "xmppipe_malloc: %zu", size);
return buf;
return buf;
}
void *
xmppipe_calloc(size_t nmemb, size_t size)
{
char *buf = NULL;
void *xmppipe_calloc(size_t nmemb, size_t size) {
char *buf = NULL;
buf = calloc(nmemb, size);
if (buf == NULL)
err(3, "xmppipe_calloc: %zu/%zu", nmemb, size);
buf = calloc(nmemb, size);
if (buf == NULL)
err(3, "xmppipe_calloc: %zu/%zu", nmemb, size);
return buf;
return buf;
}
xmpp_stanza_t *
xmppipe_stanza_new(xmpp_ctx_t *ctx)
{
xmpp_stanza_t *s = xmpp_stanza_new(ctx);
xmpp_stanza_t *xmppipe_stanza_new(xmpp_ctx_t *ctx) {
xmpp_stanza_t *s = xmpp_stanza_new(ctx);
if (s == NULL)
err(3, "xmppipe_stanza_new");
if (s == NULL)
err(3, "xmppipe_stanza_new");
return s;
return s;
}
void
xmppipe_stanza_set_attribute(xmpp_stanza_t * const stanza,
const char * const key, const char * const value)
{
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(key);
XMPPIPE_NULL(value);
void xmppipe_stanza_set_attribute(xmpp_stanza_t *const stanza,
const char *const key,
const char *const value) {
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(key);
XMPPIPE_NULL(value);
if (xmpp_stanza_set_attribute(stanza, key, value) < 0)
err(3, "xmppipe_stanza_set_attribute");
if (xmpp_stanza_set_attribute(stanza, key, value) < 0)
err(3, "xmppipe_stanza_set_attribute");
}
void
xmppipe_stanza_set_id(xmpp_stanza_t * const stanza, const char * const id)
{
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(id);
void xmppipe_stanza_set_id(xmpp_stanza_t *const stanza, const char *const id) {
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(id);
if (xmpp_stanza_set_id(stanza, id) < 0)
err(3, "xmppipe_stanza_set_id");
if (xmpp_stanza_set_id(stanza, id) < 0)
err(3, "xmppipe_stanza_set_id");
}
void
xmppipe_stanza_set_name(xmpp_stanza_t *stanza, const char * const name)
{
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(name);
switch (xmpp_stanza_set_name(stanza, name)) {
case 0:
return;
case XMPP_EMEM:
err(3, "xmppipe_stanza_set_name");
case XMPP_EINVOP:
err(4, "invalid operation");
default:
err(5, "unknown error");
}
void xmppipe_stanza_set_name(xmpp_stanza_t *stanza, const char *const name) {
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(name);
switch (xmpp_stanza_set_name(stanza, name)) {
case 0:
return;
case XMPP_EMEM:
err(3, "xmppipe_stanza_set_name");
case XMPP_EINVOP:
err(4, "invalid operation");
default:
err(5, "unknown error");
}
}
void
xmppipe_stanza_set_ns(xmpp_stanza_t * const stanza, const char * const ns)
{
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(ns);
void xmppipe_stanza_set_ns(xmpp_stanza_t *const stanza, const char *const ns) {
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(ns);
if (xmpp_stanza_set_ns(stanza, ns) < 0)
err(3, "xmppipe_stanza_set_ns");
if (xmpp_stanza_set_ns(stanza, ns) < 0)
err(3, "xmppipe_stanza_set_ns");
}
void
xmppipe_stanza_set_text(xmpp_stanza_t *stanza, const char * const text)
{
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(text);
void xmppipe_stanza_set_text(xmpp_stanza_t *stanza, const char *const text) {
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(text);
if (xmpp_stanza_set_text(stanza, text) < 0)
err(3, "xmppipe_stanza_set_text");
if (xmpp_stanza_set_text(stanza, text) < 0)
err(3, "xmppipe_stanza_set_text");
}
void
xmppipe_stanza_set_type(xmpp_stanza_t * const stanza, const char * const type)
{
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(type);
void xmppipe_stanza_set_type(xmpp_stanza_t *const stanza,
const char *const type) {
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(type);
if (xmpp_stanza_set_type(stanza, type) < 0)
err(3, "xmppipe_stanza_set_type");
if (xmpp_stanza_set_type(stanza, type) < 0)
err(3, "xmppipe_stanza_set_type");
}
void
xmppipe_stanza_add_child(xmpp_stanza_t * stanza, xmpp_stanza_t * child)
{
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(child);
void xmppipe_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child) {
XMPPIPE_NULL(stanza);
XMPPIPE_NULL(child);
if (xmpp_stanza_add_child(stanza, child) < 0)
err(3, "xmppipe_stanza_add_child");
if (xmpp_stanza_add_child(stanza, child) < 0)
err(3, "xmppipe_stanza_add_child");
}
char *
xmppipe_servername(char *jid)
{
char *buf = xmppipe_strdup(jid);
char *p = strchr(buf, '@');
char *q;
char *xmppipe_servername(char *jid) {
char *buf = xmppipe_strdup(jid);
char *p = strchr(buf, '@');
char *q;
if (p == NULL) {
free(buf);
return NULL;
}
if (p == NULL) {
free(buf);
return NULL;
}
*p++ = '\0';
*p++ = '\0';
q = xmppipe_strdup(p);
free(buf);
q = xmppipe_strdup(p);
free(buf);
return q;
return q;
}
char *
xmppipe_conference(char *room, char *mucservice)
{
size_t len = strlen(room) + 1 + strlen(mucservice) + 1;
char *buf = xmppipe_malloc(len);
char *xmppipe_conference(char *room, char *mucservice) {
size_t len = strlen(room) + 1 + strlen(mucservice) + 1;
char *buf = xmppipe_malloc(len);
(void)snprintf(buf, len, "%s@%s", room, mucservice);
(void)snprintf(buf, len, "%s@%s", room, mucservice);
return buf;
return buf;
}
char *
xmppipe_mucjid(char *muc, char *resource)
{
size_t len = strlen(muc) + 1 + strlen(resource) + 1;
char *buf = xmppipe_malloc(len);
char *xmppipe_mucjid(char *muc, char *resource) {
size_t len = strlen(muc) + 1 + strlen(resource) + 1;
char *buf = xmppipe_malloc(len);
(void)snprintf(buf, len, "%s/%s", muc, resource);
(void)snprintf(buf, len, "%s/%s", muc, resource);
return buf;
return buf;
}
char *
xmppipe_roomname(char *label)
{
char *buf = NULL;
size_t len = 64;
char name[16] = {0};
char *xmppipe_roomname(char *label) {
char *buf = NULL;
size_t len = 64;
char name[16] = {0};
if (gethostname(name, sizeof(name)-1) < 0) {
(void)snprintf(name, sizeof(name)-1, "%s", XMPPIPE_RESOURCE);
}
if (gethostname(name, sizeof(name) - 1) < 0) {
(void)snprintf(name, sizeof(name) - 1, "%s", XMPPIPE_RESOURCE);
}
buf = xmppipe_malloc(len);
(void)snprintf(buf, len, "%s-%s-%d", label, name, getuid());
buf = xmppipe_malloc(len);
(void)snprintf(buf, len, "%s-%s-%d", label, name, getuid());
return buf;
return buf;
}
void
xmppipe_next_state(xmppipe_state_t *state, int status)
{
if (state->verbose)
(void)fprintf(stderr, "next state: %s (%d) -> %s (%d)\n",
(state->status < 0 || state->status > XMPPIPE_S_READY_EMPTY) ? "unknown" : xmppipe_states[state->status],
state->status,
(state->status < 0 || state->status > XMPPIPE_S_READY_EMPTY) ? "unknown" : xmppipe_states[status],
status);
state->status = status;
void xmppipe_next_state(xmppipe_state_t *state, int status) {
if (state->verbose)
(void)fprintf(stderr, "next state: %s (%d) -> %s (%d)\n",
(state->status < 0 || state->status > XMPPIPE_S_READY_EMPTY)
? "unknown"
: xmppipe_states[state->status],
state->status,
(state->status < 0 || state->status > XMPPIPE_S_READY_EMPTY)
? "unknown"
: xmppipe_states[status],
status);
state->status = status;
}

Loading…
Cancel
Save