Added auth callback, handle close stream
This commit is contained in:
@@ -15,7 +15,6 @@ static XMPPClient *xmppclient;
|
||||
static XMPPStanza *curr_stanza;
|
||||
|
||||
static stream_start_func stream_start_cb = NULL;
|
||||
static stream_end_func stream_end_cb = NULL;
|
||||
static auth_func auth_cb = NULL;
|
||||
|
||||
static void
|
||||
@@ -33,6 +32,7 @@ start_element(void *data, const char *element, const char **attribute)
|
||||
stanza->name = strdup(element);
|
||||
stanza->content = NULL;
|
||||
stanza->children = NULL;
|
||||
stanza->attrs = NULL;
|
||||
if (attribute[0]) {
|
||||
for (i = 0; attribute[i]; i += 2) {
|
||||
XMPPAttr *attr = malloc(sizeof(XMPPAttr));
|
||||
@@ -40,8 +40,6 @@ start_element(void *data, const char *element, const char **attribute)
|
||||
attr->value = strdup(attribute[i+1]);
|
||||
stanza->attrs = g_list_append(stanza->attrs, attr);
|
||||
}
|
||||
} else {
|
||||
stanza->attrs = NULL;
|
||||
}
|
||||
|
||||
if (depth == 0) {
|
||||
@@ -60,6 +58,10 @@ end_element(void *data, const char *element)
|
||||
{
|
||||
depth--;
|
||||
|
||||
if (stanza_get_child_by_ns(curr_stanza, "jabber:iq:auth")) {
|
||||
auth_cb(xmppclient);
|
||||
}
|
||||
|
||||
if (depth > 0) {
|
||||
stanza_add_child(curr_stanza->parent, curr_stanza);
|
||||
curr_stanza = curr_stanza->parent;
|
||||
@@ -83,11 +85,10 @@ handle_data(void *data, const char *content, int length)
|
||||
}
|
||||
|
||||
void
|
||||
parser_init(XMPPClient *client, stream_start_func startcb, stream_end_func endcb, auth_func authcb)
|
||||
parser_init(XMPPClient *client, stream_start_func startcb, auth_func authcb)
|
||||
{
|
||||
xmppclient = client;
|
||||
stream_start_cb = startcb;
|
||||
stream_end_cb = endcb;
|
||||
auth_cb = authcb;
|
||||
|
||||
parser = XML_ParserCreate(NULL);
|
||||
@@ -112,7 +113,7 @@ parser_reset(void)
|
||||
{
|
||||
if (do_reset == 1) {
|
||||
parser_close();
|
||||
parser_init(xmppclient, stream_start_cb, stream_end_cb, auth_cb);
|
||||
parser_init(xmppclient, stream_start_cb, auth_cb);
|
||||
do_reset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,9 @@
|
||||
#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);
|
||||
void parser_init(XMPPClient *client, stream_start_func startcb, auth_func authcb);
|
||||
int parser_feed(char *chunk, int len);
|
||||
void parser_close(void);
|
||||
void parser_reset(void);
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#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 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 STREAM_END "</stream:stream>"
|
||||
@@ -58,6 +57,27 @@ listen_for_xmlstart(XMPPClient *client)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
send_to(XMPPClient *client, const char * const stanza)
|
||||
{
|
||||
int sent = 0;
|
||||
int to_send = strlen(stanza);
|
||||
char *marker = (char*)stanza;
|
||||
while (to_send > 0 && ((sent = write(client->sock, marker, to_send)) > 0)) {
|
||||
to_send -= sent;
|
||||
marker += sent;
|
||||
}
|
||||
printf("SENT: %s\n", stanza);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void
|
||||
stream_end(XMPPClient *client)
|
||||
{
|
||||
printf("\n--> Stream end callback fired\n");
|
||||
send_to(client, STREAM_END);
|
||||
}
|
||||
|
||||
int
|
||||
listen_to(XMPPClient *client)
|
||||
{
|
||||
@@ -73,6 +93,10 @@ listen_to(XMPPClient *client)
|
||||
fflush(stdout);
|
||||
parser_reset();
|
||||
g_string_append_len(stream, buf, read_size);
|
||||
if (g_str_has_suffix(stream->str, "</stream:stream>")) {
|
||||
stream_end(client);
|
||||
break;
|
||||
}
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
|
||||
@@ -94,20 +118,6 @@ listen_to(XMPPClient *client)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
send_to(XMPPClient *client, const char * const stanza)
|
||||
{
|
||||
int sent = 0;
|
||||
int to_send = strlen(stanza);
|
||||
char *marker = (char*)stanza;
|
||||
while (to_send > 0 && ((sent = write(client->sock, marker, to_send)) > 0)) {
|
||||
to_send -= sent;
|
||||
marker += sent;
|
||||
}
|
||||
printf("SENT: %s\n", stanza);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void
|
||||
stream_start_callback(void *userdata)
|
||||
{
|
||||
@@ -128,14 +138,6 @@ auth_callback(void *userdata)
|
||||
printf("RECV: ");
|
||||
}
|
||||
|
||||
void
|
||||
stream_end_callback(void *userdata)
|
||||
{
|
||||
printf("\n--> Stream end callback fired\n");
|
||||
XMPPClient *client = (XMPPClient*)userdata;
|
||||
send_to(client, STREAM_END);
|
||||
}
|
||||
|
||||
int main(int argc , char *argv[])
|
||||
{
|
||||
int port = 0;
|
||||
@@ -212,7 +214,7 @@ int main(int argc , char *argv[])
|
||||
}
|
||||
|
||||
XMPPClient *client = xmppclient_new(client_addr, client_socket);
|
||||
parser_init(client, stream_start_callback, stream_end_callback, auth_callback);
|
||||
parser_init(client, stream_start_callback, auth_callback);
|
||||
printf("RECV: ");
|
||||
int res = listen_for_xmlstart(client);
|
||||
if (res == -1) {
|
||||
@@ -224,31 +226,7 @@ int main(int argc , char *argv[])
|
||||
|
||||
stanza_show_all();
|
||||
|
||||
//
|
||||
// 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;
|
||||
xmppclient_end_session(client);
|
||||
|
||||
parser_close();
|
||||
|
||||
|
||||
@@ -58,3 +58,36 @@ stanza_add_child(XMPPStanza *parent, XMPPStanza *child)
|
||||
{
|
||||
parent->children = g_list_append(parent->children, child);
|
||||
}
|
||||
|
||||
XMPPStanza*
|
||||
stanza_get_child_by_ns(XMPPStanza *stanza, char *ns)
|
||||
{
|
||||
if (!stanza) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!stanza->children) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GList *curr_child = stanza->children;
|
||||
while (curr_child) {
|
||||
XMPPStanza *child = curr_child->data;
|
||||
if (!child->attrs) {
|
||||
return NULL;
|
||||
}
|
||||
GList *curr_attr = child->attrs;
|
||||
while (curr_attr) {
|
||||
XMPPAttr *attr = curr_attr->data;
|
||||
if ((g_strcmp0(attr->name, "xmlns") == 0) && (g_strcmp0(attr->value, ns) == 0)) {
|
||||
return child;
|
||||
}
|
||||
|
||||
curr_attr = g_list_next(curr_attr);
|
||||
}
|
||||
|
||||
curr_child = g_list_next(curr_child);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -18,5 +18,6 @@ void stanza_show(XMPPStanza *stanza);
|
||||
void stanza_show_all(void);
|
||||
void stanza_add(XMPPStanza *stanza);
|
||||
void stanza_add_child(XMPPStanza *parent, XMPPStanza *child);
|
||||
XMPPStanza* stanza_get_child_by_ns(XMPPStanza *stanza, char *ns);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user