Tidied socket reads/writes

This commit is contained in:
James Booth
2015-05-12 00:15:52 +01:00
parent d392c77b03
commit 39fe147a8a
2 changed files with 120 additions and 144 deletions

View File

@@ -14,22 +14,19 @@
#define AUTH_RESP "<iq id=\"_xmpp_auth1\" type=\"result\"/>" #define AUTH_RESP "<iq id=\"_xmpp_auth1\" type=\"result\"/>"
#define END_STREAM "</stream:stream>" #define END_STREAM "</stream:stream>"
void connection_handler(XMPPClient *client) int
listen_for(XMPPClient *client, const char * const stanza)
{ {
int read_size; int read_size;
// client loop
while(1) {
char buf[2]; char buf[2];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
// wait for stream
GString *stream = g_string_new(""); GString *stream = g_string_new("");
errno = 0; errno = 0;
gboolean received = FALSE; gboolean received = FALSE;
while ((!received) && ((read_size = recv(client->sock, buf, 1, 0)) > 0)) { while ((!received) && ((read_size = recv(client->sock, buf, 1, 0)) > 0)) {
g_string_append_len(stream, buf, read_size); g_string_append_len(stream, buf, read_size);
if (g_strcmp0(stream->str, STREAM_REQ) == 0) { if (g_strcmp0(stream->str, stanza) == 0) {
received = TRUE; received = TRUE;
} }
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
@@ -40,95 +37,46 @@ void connection_handler(XMPPClient *client)
perror("Error receiving on connection"); perror("Error receiving on connection");
xmppclient_end_session(client); xmppclient_end_session(client);
g_string_free(stream, TRUE); g_string_free(stream, TRUE);
break; return -1;
// client closed // client closed
} else if (read_size == 0) { } else if (read_size == 0) {
printf("\n%s:%d - Client disconnected.\n", client->ip, client->port); printf("\n%s:%d - Client disconnected.\n", client->ip, client->port);
xmppclient_end_session(client); xmppclient_end_session(client);
g_string_free(stream, TRUE); g_string_free(stream, TRUE);
break; return -1;
}
} else { printf("RECV: %s\n", stanza);
printf("RECV: %s\n", STREAM_REQ);
fflush(stdout); fflush(stdout);
g_string_free(stream, TRUE); return 0;
}
// send stream response void
send_to(XMPPClient *client, const char * const stanza)
{
int sent = 0; int sent = 0;
int to_send = strlen(STREAM_RESP); int to_send = strlen(stanza);
char *marker = STREAM_RESP; char *marker = (char*)stanza;
while (to_send > 0 && ((sent = write(client->sock, marker, to_send)) > 0)) { while (to_send > 0 && ((sent = write(client->sock, marker, to_send)) > 0)) {
to_send -= sent; to_send -= sent;
marker += sent; marker += sent;
} }
printf("SENT: %s\n", stanza);
printf("SENT: %s\n", STREAM_RESP);
fflush(stdout); fflush(stdout);
memset(buf, 0, sizeof(buf));
// send features
sent = 0;
to_send = strlen(FEATURES);
marker = FEATURES;
while (to_send > 0 && ((sent = write(client->sock, marker, to_send)) > 0)) {
to_send -= sent;
marker += sent;
} }
printf("SENT: %s\n", FEATURES); int
fflush(stdout); debug_client(XMPPClient *client)
{
int read_size;
char buf[2];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
// wait for auth request printf("RECV: ");
stream = g_string_new("");
errno = 0;
gboolean received = FALSE;
while ((!received) && ((read_size = recv(client->sock, buf, 1, 0)) > 0)) {
g_string_append_len(stream, buf, read_size);
if (g_strcmp0(stream->str, AUTH_REQ) == 0) {
received = TRUE;
}
memset(buf, 0, sizeof(buf));
}
// error
if (read_size == -1) {
perror("Error receiving on connection");
xmppclient_end_session(client);
g_string_free(stream, TRUE);
break;
// 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);
break;
} else {
printf("RECV: %s\n", AUTH_REQ);
fflush(stdout);
g_string_free(stream, TRUE);
// send auth response
sent = 0;
to_send = strlen(AUTH_RESP);
marker = AUTH_RESP;
while (to_send > 0 && ((sent = write(client->sock, marker, to_send)) > 0)) {
to_send -= sent;
marker += sent;
}
printf("SENT: %s\n", AUTH_RESP);
fflush(stdout); fflush(stdout);
memset(buf, 0, sizeof(buf)); GString *stream = g_string_new("");
// wait until stream closed
stream = g_string_new("");
errno = 0; errno = 0;
gboolean received = FALSE; gboolean received = FALSE;
while ((!received) && ((read_size = recv(client->sock, buf, 1, 0)) > 0)) { while ((!received) && ((read_size = recv(client->sock, buf, 1, 0)) > 0)) {
@@ -146,23 +94,45 @@ void connection_handler(XMPPClient *client)
perror("Error receiving on connection"); perror("Error receiving on connection");
xmppclient_end_session(client); xmppclient_end_session(client);
g_string_free(stream, TRUE); g_string_free(stream, TRUE);
break; return -1;
// client closed // client closed
} else if (read_size == 0) { } else if (read_size == 0) {
printf("\n%s:%d - Client disconnected.\n", client->ip, client->port); printf("\n%s:%d - Client disconnected.\n", client->ip, client->port);
xmppclient_end_session(client); xmppclient_end_session(client);
g_string_free(stream, TRUE); g_string_free(stream, TRUE);
break; return -1;
}
} else { return 0;
printf("\nEnd stream receieved"); }
void connection_handler(XMPPClient *client)
{
int 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); xmppclient_end_session(client);
break; fflush(stdout);
} return;
}
}
}
} }
int main(int argc , char *argv[]) int main(int argc , char *argv[])
@@ -244,5 +214,9 @@ int main(int argc , char *argv[])
XMPPClient *client = xmppclient_new(client_addr, client_socket); XMPPClient *client = xmppclient_new(client_addr, client_socket);
connection_handler(client); connection_handler(client);
while (recv(listen_socket, NULL, 1, 0) > 0) {}
shutdown(listen_socket, 2);
close(listen_socket);
return 0; return 0;
} }

View File

@@ -24,6 +24,8 @@ xmppclient_new(struct sockaddr_in client_addr, int socket)
void void
xmppclient_end_session(XMPPClient *client) xmppclient_end_session(XMPPClient *client)
{ {
while (recv(client->sock, NULL, 1, 0) > 0) {}
shutdown(client->sock, 2);
close(client->sock); close(client->sock);
free(client->ip); free(client->ip);