Added stanza callbacks
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -23,4 +23,6 @@ src/stamp-h1
|
|||||||
.codelite/
|
.codelite/
|
||||||
stabber.project
|
stabber.project
|
||||||
stabber.workspace
|
stabber.workspace
|
||||||
|
compile_commands.json
|
||||||
|
stabber.tags
|
||||||
|
|
||||||
|
|||||||
@@ -4,22 +4,106 @@
|
|||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
#include "server/parser.h"
|
#include "server/parser.h"
|
||||||
|
#include "server/xmppclient.h"
|
||||||
|
|
||||||
static int depth = 0;
|
static int depth = 0;
|
||||||
static int do_reset = 0;
|
static int do_reset = 0;
|
||||||
GString *text = NULL;
|
|
||||||
static XML_Parser parser;
|
static XML_Parser parser;
|
||||||
|
static stream_start_func stream_start_cb = NULL;
|
||||||
|
static stream_end_func stream_end_cb = NULL;
|
||||||
|
static auth_func auth_cb = NULL;
|
||||||
|
static XMPPClient *xmppclient;
|
||||||
|
|
||||||
|
typedef struct xmpp_attr_t {
|
||||||
|
const char *name;
|
||||||
|
const char *value;
|
||||||
|
} XMPPAttr;
|
||||||
|
|
||||||
|
typedef struct xmpp_stanza_t {
|
||||||
|
const char *name;
|
||||||
|
GList *attrs;
|
||||||
|
GList *children;
|
||||||
|
GString *content;
|
||||||
|
struct xmpp_stanza_t *parent;
|
||||||
|
} XMPPStanza;
|
||||||
|
|
||||||
|
static XMPPStanza *curr_stanza;
|
||||||
|
static GList *stanzas;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_stanza(XMPPStanza *stanza)
|
||||||
|
{
|
||||||
|
printf("NAME : %s\n", stanza->name);
|
||||||
|
|
||||||
|
if (stanza->content && stanza->content->len > 0) {
|
||||||
|
printf("CONTENT: %s\n", stanza->content->str);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stanza->attrs) {
|
||||||
|
GList *curr_attr = stanza->attrs;
|
||||||
|
while (curr_attr) {
|
||||||
|
XMPPAttr *attr = curr_attr->data;
|
||||||
|
printf("ATTR : %s='%s'\n", attr->name, attr->value);
|
||||||
|
|
||||||
|
curr_attr = g_list_next(curr_attr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stanza->children) {
|
||||||
|
printf("CHILDREN:\n");
|
||||||
|
GList *curr_child = stanza->children;
|
||||||
|
while (curr_child) {
|
||||||
|
XMPPStanza *child = curr_child->data;
|
||||||
|
show_stanza(child);
|
||||||
|
curr_child = g_list_next(curr_child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
parser_show_stanzas(void)
|
||||||
|
{
|
||||||
|
GList *curr = stanzas;
|
||||||
|
while (curr) {
|
||||||
|
XMPPStanza *stanza = curr->data;
|
||||||
|
show_stanza(stanza);
|
||||||
|
printf("\n");
|
||||||
|
curr = g_list_next(curr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
start_element(void *data, const char *element, const char **attribute)
|
start_element(void *data, const char *element, const char **attribute)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
printf("LEVEL: %d\n", depth);
|
if (g_strcmp0(element, "stream:stream") == 0) {
|
||||||
printf("ELEM: %s\n", element);
|
stream_start_cb(xmppclient);
|
||||||
|
do_reset = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (i = 0; attribute[i]; i += 2) {
|
XMPPStanza *stanza = malloc(sizeof(XMPPStanza));
|
||||||
printf(" ATTR: %s='%s'\n", attribute[i], attribute[i + 1]);
|
stanza->name = strdup(element);
|
||||||
|
stanza->content = NULL;
|
||||||
|
stanza->children = NULL;
|
||||||
|
if (attribute[0]) {
|
||||||
|
for (i = 0; attribute[i]; i += 2) {
|
||||||
|
XMPPAttr *attr = malloc(sizeof(XMPPAttr));
|
||||||
|
attr->name = strdup(attribute[i]);
|
||||||
|
attr->value = strdup(attribute[i+1]);
|
||||||
|
stanza->attrs = g_list_append(stanza->attrs, attr);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
stanza->attrs = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (depth == 0) {
|
||||||
|
curr_stanza = stanza;
|
||||||
|
curr_stanza->parent = NULL;
|
||||||
|
} else {
|
||||||
|
stanza->parent = curr_stanza;
|
||||||
|
curr_stanza = stanza;
|
||||||
}
|
}
|
||||||
|
|
||||||
depth++;
|
depth++;
|
||||||
@@ -28,32 +112,38 @@ start_element(void *data, const char *element, const char **attribute)
|
|||||||
static void
|
static void
|
||||||
end_element(void *data, const char *element)
|
end_element(void *data, const char *element)
|
||||||
{
|
{
|
||||||
if (text) {
|
depth--;
|
||||||
printf(" TEXT: %s\n", text->str);
|
|
||||||
g_string_free(text, TRUE);
|
if (depth > 0) {
|
||||||
text = NULL;
|
curr_stanza->parent->children = g_list_append(curr_stanza->parent->children, curr_stanza);
|
||||||
|
curr_stanza = curr_stanza->parent;
|
||||||
|
} else {
|
||||||
|
stanzas = g_list_append(stanzas, curr_stanza);
|
||||||
}
|
}
|
||||||
|
|
||||||
depth--;
|
|
||||||
if (depth == 0) {
|
if (depth == 0) {
|
||||||
do_reset = 1;
|
do_reset = 1;
|
||||||
printf("\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
handle_data(void *data, const char *content, int length)
|
handle_data(void *data, const char *content, int length)
|
||||||
{
|
{
|
||||||
if (!text) {
|
if (!curr_stanza->content) {
|
||||||
text = g_string_new("");
|
curr_stanza->content = g_string_new("");
|
||||||
}
|
}
|
||||||
|
|
||||||
g_string_append_len(text, content, length);
|
g_string_append_len(curr_stanza->content, content, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
parser_init(void)
|
parser_init(XMPPClient *client, stream_start_func startcb, stream_end_func endcb, auth_func authcb)
|
||||||
{
|
{
|
||||||
|
xmppclient = client;
|
||||||
|
stream_start_cb = startcb;
|
||||||
|
stream_end_cb = endcb;
|
||||||
|
auth_cb = authcb;
|
||||||
|
|
||||||
parser = XML_ParserCreate(NULL);
|
parser = XML_ParserCreate(NULL);
|
||||||
XML_SetElementHandler(parser, start_element, end_element);
|
XML_SetElementHandler(parser, start_element, end_element);
|
||||||
XML_SetCharacterDataHandler(parser, handle_data);
|
XML_SetCharacterDataHandler(parser, handle_data);
|
||||||
@@ -76,7 +166,7 @@ parser_reset(void)
|
|||||||
{
|
{
|
||||||
if (do_reset == 1) {
|
if (do_reset == 1) {
|
||||||
parser_close();
|
parser_close();
|
||||||
parser_init();
|
parser_init(xmppclient, stream_start_cb, stream_end_cb, auth_cb);
|
||||||
do_reset = 0;
|
do_reset = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,16 @@
|
|||||||
#ifndef __H_PARSER
|
#ifndef __H_PARSER
|
||||||
#define __H_PARSER
|
#define __H_PARSER
|
||||||
|
|
||||||
void parser_init(void);
|
#include "server/xmppclient.h"
|
||||||
|
|
||||||
|
typedef void (*stream_start_func)(void * const userdata);
|
||||||
|
typedef void (*stream_end_func)(void * const userdata);
|
||||||
|
typedef void (*auth_func)(void * const userdata);
|
||||||
|
|
||||||
|
void parser_init(XMPPClient *client, stream_start_func startcb, stream_end_func endcb, auth_func authcb);
|
||||||
int parser_feed(char *chunk, int len);
|
int parser_feed(char *chunk, int len);
|
||||||
void parser_close(void);
|
void parser_close(void);
|
||||||
void parser_reset(void);
|
void parser_reset(void);
|
||||||
|
void parser_show_stanzas(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,12 +9,14 @@
|
|||||||
#include "server/parser.h"
|
#include "server/parser.h"
|
||||||
|
|
||||||
#define XML_START "<?xml version=\"1.0\"?>"
|
#define XML_START "<?xml version=\"1.0\"?>"
|
||||||
#define STREAM_REQ "<stream:stream to=\"localhost\" xml:lang=\"en\" version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">"
|
|
||||||
#define STREAM_RESP "<?xml version=\"1.0\"?><stream:stream from=\"localhost\" id=\"stream1\" xml:lang=\"en\" version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">"
|
#define STREAM_RESP "<stream:stream from=\"localhost\" id=\"stream1\" xml:lang=\"en\" version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">"
|
||||||
#define FEATURES "<stream:features></stream:features>"
|
#define FEATURES "<stream:features></stream:features>"
|
||||||
|
|
||||||
#define AUTH_REQ "<iq id=\"_xmpp_auth1\" type=\"set\"><query xmlns=\"jabber:iq:auth\"><username>stabber</username><password>password</password><resource>profanity</resource></query></iq>"
|
#define AUTH_REQ "<iq id=\"_xmpp_auth1\" type=\"set\"><query xmlns=\"jabber:iq:auth\"><username>stabber</username><password>password</password><resource>profanity</resource></query></iq>"
|
||||||
#define AUTH_RESP "<iq id=\"_xmpp_auth1\" type=\"result\"/>"
|
#define AUTH_RESP "<iq id=\"_xmpp_auth1\" type=\"result\"/>"
|
||||||
#define END_STREAM "</stream:stream>"
|
|
||||||
|
#define STREAM_END "</stream:stream>"
|
||||||
|
|
||||||
int
|
int
|
||||||
listen_for_xmlstart(XMPPClient *client)
|
listen_for_xmlstart(XMPPClient *client)
|
||||||
@@ -26,6 +28,8 @@ listen_for_xmlstart(XMPPClient *client)
|
|||||||
GString *stream = g_string_new("");
|
GString *stream = g_string_new("");
|
||||||
errno = 0;
|
errno = 0;
|
||||||
while ((read_size = recv(client->sock, buf, 1, 0)) > 0) {
|
while ((read_size = recv(client->sock, buf, 1, 0)) > 0) {
|
||||||
|
printf("%c", buf[0]);
|
||||||
|
fflush(stdout);
|
||||||
g_string_append_len(stream, buf, read_size);
|
g_string_append_len(stream, buf, read_size);
|
||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
if (g_strcmp0(stream->str, XML_START) == 0) {
|
if (g_strcmp0(stream->str, XML_START) == 0) {
|
||||||
@@ -48,13 +52,13 @@ listen_for_xmlstart(XMPPClient *client)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("RECV: %s\n", XML_START);
|
printf("\n");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
listen_for(XMPPClient *client, const char * const stanza)
|
listen_to(XMPPClient *client)
|
||||||
{
|
{
|
||||||
int read_size;
|
int read_size;
|
||||||
char buf[2];
|
char buf[2];
|
||||||
@@ -63,11 +67,12 @@ listen_for(XMPPClient *client, const char * const stanza)
|
|||||||
GString *stream = g_string_new("");
|
GString *stream = g_string_new("");
|
||||||
errno = 0;
|
errno = 0;
|
||||||
while ((read_size = recv(client->sock, buf, 1, 0)) > 0) {
|
while ((read_size = recv(client->sock, buf, 1, 0)) > 0) {
|
||||||
|
printf("%c", buf[0]);
|
||||||
|
parser_feed(buf, 1);
|
||||||
|
fflush(stdout);
|
||||||
|
parser_reset();
|
||||||
g_string_append_len(stream, buf, read_size);
|
g_string_append_len(stream, buf, read_size);
|
||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
if (g_strcmp0(stream->str, stanza) == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// error
|
// error
|
||||||
@@ -85,8 +90,6 @@ listen_for(XMPPClient *client, const char * const stanza)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("RECV: %s\n", stanza);
|
|
||||||
fflush(stdout);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,75 +107,32 @@ send_to(XMPPClient *client, const char * const stanza)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
debug_client(XMPPClient *client)
|
stream_start_callback(void *userdata)
|
||||||
{
|
{
|
||||||
int read_size;
|
printf("\n--> Stream start callback fired\n");
|
||||||
char buf[2];
|
XMPPClient *client = (XMPPClient*)userdata;
|
||||||
memset(buf, 0, sizeof(buf));
|
send_to(client, XML_START);
|
||||||
|
|
||||||
GString *stream = g_string_new("");
|
|
||||||
errno = 0;
|
|
||||||
while ((read_size = recv(client->sock, buf, 1, 0)) > 0) {
|
|
||||||
parser_feed(buf, 1);
|
|
||||||
fflush(stdout);
|
|
||||||
parser_reset();
|
|
||||||
g_string_append_len(stream, buf, read_size);
|
|
||||||
memset(buf, 0, sizeof(buf));
|
|
||||||
if (g_str_has_suffix(stream->str, END_STREAM)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// error
|
|
||||||
if (read_size == -1) {
|
|
||||||
perror("Error receiving on connection");
|
|
||||||
xmppclient_end_session(client);
|
|
||||||
g_string_free(stream, TRUE);
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// client closed
|
|
||||||
} else if (read_size == 0) {
|
|
||||||
printf("\n%s:%d - Client disconnected.\n", client->ip, client->port);
|
|
||||||
xmppclient_end_session(client);
|
|
||||||
g_string_free(stream, TRUE);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void connection_handler(XMPPClient *client)
|
|
||||||
{
|
|
||||||
int res = listen_for_xmlstart(client);
|
|
||||||
if (res == -1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = listen_for(client, STREAM_REQ);
|
|
||||||
if (res == -1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
send_to(client, STREAM_RESP);
|
send_to(client, STREAM_RESP);
|
||||||
send_to(client, FEATURES);
|
send_to(client, FEATURES);
|
||||||
|
printf("RECV: ");
|
||||||
|
}
|
||||||
|
|
||||||
res = listen_for(client, AUTH_REQ);
|
void
|
||||||
if (res == -1) {
|
auth_callback(void *userdata)
|
||||||
return;
|
{
|
||||||
}
|
printf("\n--> Auth callback fired\n");
|
||||||
|
XMPPClient *client = (XMPPClient*)userdata;
|
||||||
send_to(client, AUTH_RESP);
|
send_to(client, AUTH_RESP);
|
||||||
|
printf("RECV: ");
|
||||||
|
}
|
||||||
|
|
||||||
res = debug_client(client);
|
void
|
||||||
if (res == -1) {
|
stream_end_callback(void *userdata)
|
||||||
return;
|
{
|
||||||
}
|
printf("\n--> Stream end callback fired\n");
|
||||||
|
XMPPClient *client = (XMPPClient*)userdata;
|
||||||
printf("\nEnd stream receieved\n");
|
send_to(client, STREAM_END);
|
||||||
xmppclient_end_session(client);
|
|
||||||
fflush(stdout);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc , char *argv[])
|
int main(int argc , char *argv[])
|
||||||
@@ -239,8 +199,6 @@ int main(int argc , char *argv[])
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
parser_init();
|
|
||||||
|
|
||||||
puts("Waiting for incoming connections...");
|
puts("Waiting for incoming connections...");
|
||||||
|
|
||||||
// connection accept
|
// connection accept
|
||||||
@@ -253,8 +211,43 @@ int main(int argc , char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
XMPPClient *client = xmppclient_new(client_addr, client_socket);
|
XMPPClient *client = xmppclient_new(client_addr, client_socket);
|
||||||
|
parser_init(client, stream_start_callback, stream_end_callback, auth_callback);
|
||||||
|
printf("RECV: ");
|
||||||
|
int res = listen_for_xmlstart(client);
|
||||||
|
if (res == -1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
connection_handler(client);
|
printf("RECV: ");
|
||||||
|
listen_to(client);
|
||||||
|
|
||||||
|
parser_show_stanzas();
|
||||||
|
|
||||||
|
//
|
||||||
|
// res = listen_for(client, STREAM_REQ);
|
||||||
|
// if (res == -1) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// send_to(client, STREAM_RESP);
|
||||||
|
// send_to(client, FEATURES);
|
||||||
|
//
|
||||||
|
// res = listen_for(client, AUTH_REQ);
|
||||||
|
// if (res == -1) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// send_to(client, AUTH_RESP);
|
||||||
|
//
|
||||||
|
// res = debug_client(client);
|
||||||
|
// if (res == -1) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// printf("\nEnd stream receieved\n");
|
||||||
|
// xmppclient_end_session(client);
|
||||||
|
// fflush(stdout);
|
||||||
|
// return;
|
||||||
|
|
||||||
parser_close();
|
parser_close();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user