From: Thadeu Lima de Souza Cascardo Date: Tue, 12 Sep 2006 17:47:17 +0000 (+0000) Subject: Proxies connections to jabber.org X-Git-Tag: v0.1.3~142 X-Git-Url: http://git.cascardo.eti.br/?a=commitdiff_plain;ds=sidebyside;h=dc4dfca351e3d95b3baeb54f0fb544cd5cb6b593;p=cascardo%2Frnetproxy.git Proxies connections to jabber.org One program proxies connections to jabber.org, but with no SRV lookup. It uses pure sockets. The other program reads from a configuration file the address and port to bind to, using GLib and GNet. --- dc4dfca351e3d95b3baeb54f0fb544cd5cb6b593 diff --git a/conf b/conf new file mode 100644 index 0000000..4bf9862 --- /dev/null +++ b/conf @@ -0,0 +1,4 @@ +[global] + +address = 0.0.0.0 +port = 5222 diff --git a/config.c b/config.c new file mode 100644 index 0000000..b7a4611 --- /dev/null +++ b/config.c @@ -0,0 +1,35 @@ +#include +#include +#include + +void new_client (GServer* server, GConn* conn, gpointer data) +{ +} + +int main () +{ + + GKeyFile *keyfile; + GInetAddr* inetaddr; + gchar* conf_address; + gint port; + + gnet_init (); + + keyfile = g_key_file_new (); + + g_key_file_load_from_file (keyfile, "conf", G_KEY_FILE_NONE, NULL); + + conf_address = g_key_file_get_string (keyfile, "global", "address", NULL); + port = g_key_file_get_integer (keyfile, "global", "port", NULL); + + printf ("Listen address is %s:%d\n", conf_address, port); + + inetaddr = gnet_inetaddr_new_nonblock (conf_address, port); + gnet_server_new (inetaddr, port, new_client, NULL); + + g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE)); + + return 0; + +} diff --git a/proxy.c b/proxy.c new file mode 100644 index 0000000..6765b0c --- /dev/null +++ b/proxy.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int server_create () +{ + + int fd; + struct sockaddr_in saddr; + + fd = socket (PF_INET, SOCK_STREAM, 0); + + saddr.sin_family = AF_INET; + saddr.sin_port = htons (5222); + saddr.sin_addr.s_addr = INADDR_ANY; + + bind (fd, (struct sockaddr*) &saddr, sizeof (struct sockaddr_in)); + + listen (fd, 5); + + return fd; + +} + +int server_loop (int fd) +{ + int client; + struct sockaddr_in saddr; + socklen_t saddr_size; + saddr_size = sizeof (struct sockaddr_in); + client = accept (fd, (struct sockaddr*) &saddr, &saddr_size); + fprintf (stderr, "Accepted connection from %s:%d.\n", + inet_ntoa (saddr.sin_addr), ntohs (saddr.sin_port)); + return client; +} + +int client_create () +{ + + int fd; + struct sockaddr_in saddr; + struct hostent* host; + + host = gethostbyname ("jabber.org"); + + fd = socket (PF_INET, SOCK_STREAM, 0); + + saddr.sin_family = AF_INET; + saddr.sin_port = htons (5222); + saddr.sin_addr = *(struct in_addr*) host->h_addr; + + if (connect (fd, (struct sockaddr*) &saddr, sizeof (struct sockaddr_in)) < 0) + { + fprintf (stderr, "%s\n", strerror (errno)); + exit (1); + } + else + { + fprintf (stderr, "Connected to jabber.org:5222.\n"); + } + + return fd; + +} + +int client_loop (int client, int server) +{ + + char buffer[256]; + int r; + + fd_set fds; + + while (1) + { + FD_ZERO (&fds); + FD_SET (client, &fds); + FD_SET (server, &fds); + select (FD_SETSIZE, &fds, NULL, NULL, NULL); + if (FD_ISSET (client, &fds)) + { + r = read (client, buffer, 256); + if (r <= 0) + { + close (client); + close (server); + return r; + } + write (server, buffer, r); + } + if (FD_ISSET (server, &fds)) + { + r = read (server, buffer, 256); + if (r <= 0) + { + close (client); + close (server); + return r; + } + write (client, buffer, r); + } + } + + return r; + +} + +int main () +{ + int proxy; + int client; + int server; + proxy = server_create (); + client = server_loop (proxy); + server = client_create (); + if (client_loop (client, server) < 0) + { + fprintf (stderr, "%s\n", strerror (errno)); + exit (1); + } + return 0; +}