main_gtk_proxy_server.c File Reference

#include <gtk/gtk.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sqlite3.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

Go to the source code of this file.

Defines

#define BUFFER   4096

Functions

void proxy_server_main ()
static int callback (void *NotUsed, int argc, char **argv, char **azColName)
 This is a callback function for sqlite.
static int callback2 (void *NotUsed, int argc, char **argv, char **azColName)
 This is a callback function for sqlite.
void block (GtkWidget *widget1, gpointer data)
 This function is called when clicks on Block IP button.
void unblock (GtkWidget *widget1, gpointer data)
 This function is called when clicks on Unblock IP button.
void display_log (GtkWidget *widget1, gpointer data)
 This function is called when clicks on View Log button.
void display_usage (GtkWidget *widget1, gpointer data)
 This function is called when clicks on IP Statistics button.
void on_window_destroy (GtkWidget *widget, gpointer data)
 This function is called when someone tries to close top level window.
void start_server (GtkWidget *widget1, gpointer data)
 This function is called when someone clicks on Start Server button It will run proxy server as its child.
void stop_server (GtkWidget *widget1, gpointer data)
 This function is called when someone clicks on Stop Server button It will kill proxy server running as its child.
int main (int argc, char *argv[])
 This is where program starts.

Variables

sqlite3 * db1
pid_t pid
char * zErrMsg = 0
int rc
char sql_query [BUFFER]
GtkTextBuffer * buffer
GtkTextIter end_iter
GtkWidget * start
GtkWidget * stop
char str [BUFFER]

Define Documentation

#define BUFFER   4096

Definition at line 21 of file main_gtk_proxy_server.c.

Referenced by get_host_file_descriptor(), and proxy_server_main().


Function Documentation

void block ( GtkWidget *  widget1,
gpointer  data 
)

This function is called when clicks on Block IP button.

It will insert IP/url entry in table blocked_ip in database.

Parameters:
widget1 Widget that generated the event
data IP/url to be blocked is passed through gtkentry

Definition at line 86 of file main_gtk_proxy_server.c.

References callback(), db1, rc, sql_query, and zErrMsg.

Referenced by main().

00087 {
00088     const gchar * text = gtk_entry_get_text(data);
00089     char host_ip[100];
00090     struct hostent *host_struct;
00091         struct in_addr h_addr;    /* internet address */
00092     host_struct = (struct hostent *)gethostbyname(text);
00093         if (host_struct == NULL)
00094     {
00095             fprintf(stderr,"ERROR, no such host\n");
00096             //return -1;
00097         }
00098         h_addr.s_addr = *((unsigned long *) host_struct->h_addr_list[0]);
00099     strcpy(host_ip, inet_ntoa(h_addr));
00100 
00101     sprintf(sql_query, "INSERT INTO blocked_ip values('%s')", host_ip);
00102     rc = sqlite3_exec (db1, sql_query, callback, 0, &zErrMsg);
00103     sprintf(sql_query, "INSERT INTO blocked_ip values('%s')", text);
00104     rc = sqlite3_exec (db1, sql_query, callback, 0, &zErrMsg);
00105 }

static int callback ( void *  NotUsed,
int  argc,
char **  argv,
char **  azColName 
) [static]

This is a callback function for sqlite.

It is called when any SELECT query is executed in sql database. It prints all the values in a record.

Parameters:
argc no. of tuples in a record which is passed automatically by sqlite
argv values in a record which is also passed automatically by sqlite

Definition at line 43 of file main_gtk_proxy_server.c.

Referenced by block(), and unblock().

00044 {
00045     NotUsed = 0;
00046     int i;
00047     for (i = 0; i < argc; i++)
00048     {
00049     printf ("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
00050     }
00051     printf ("\n");
00052     return 0;
00053 }

static int callback2 ( void *  NotUsed,
int  argc,
char **  argv,
char **  azColName 
) [static]

This is a callback function for sqlite.

It is called by display_log() and display_usage(). It displays the SELECT * query result in text_view.

Parameters:
argc no. of tuples in a record which is passed automatically by sqlite
argv values in a record which is also passed automatically by sqlite

Definition at line 61 of file main_gtk_proxy_server.c.

References buffer, end_iter, and str.

Referenced by display_log(), and display_usage().

00062 {
00063     NotUsed = 0;
00064     int i;
00065     for (i = 0; i < argc; i++)
00066     {
00067     if((i==2 || i==3) && (atoi(argv[i]))%1000 == 0)
00068         sprintf (str," %s   \t\t", argv[i] ? argv[i] : "NULL");
00069     else
00070         sprintf (str," %s   \t", argv[i] ? argv[i] : "NULL");
00071     gtk_text_buffer_get_end_iter(buffer, &end_iter);
00072     gtk_text_buffer_insert(buffer, &end_iter, str, -1);
00073     }
00074     sprintf (str,"\n");
00075     gtk_text_buffer_get_end_iter(buffer, &end_iter);
00076     gtk_text_buffer_insert(buffer, &end_iter, str, -1);
00077     return 0;
00078 }

void display_log ( GtkWidget *  widget1,
gpointer  data 
)

This function is called when clicks on View Log button.

It will access database and display logs of each request with date/time, requested url, client' IP, and bytes transfered in the request

Parameters:
widget1 Widget that generated the event
data Additional data passed by widget while generating the signal

Definition at line 140 of file main_gtk_proxy_server.c.

References callback2(), db1, rc, sql_query, and zErrMsg.

Referenced by main().

00141 {
00142     gtk_text_buffer_set_text (data, " Client_IP  \t\t\t\t Time \t\t  Downloads\tUploads \t\tURL_Requested\n----------------------------------------------------------------------------------------------------------------------------\n", -1);
00143 
00144     sprintf(sql_query, "SELECT * from log_details");
00145     rc = sqlite3_exec (db1, sql_query, callback2, 0, &zErrMsg);
00146 }

void display_usage ( GtkWidget *  widget1,
gpointer  data 
)

This function is called when clicks on IP Statistics button.

It will access database and display per IP statistics of network usage.

Parameters:
widget1 Widget that generated the event
data Additional data passed by widget while generating the signal

Definition at line 154 of file main_gtk_proxy_server.c.

References callback2(), db1, rc, sql_query, and zErrMsg.

Referenced by main().

00155 {
00156     gtk_text_buffer_set_text (data, " Client_IP  \t\tDownloads \t  Uploads\n-------------------------------------------------------------\n", -1);
00157     sprintf(sql_query, "SELECT * from usage_details");
00158     rc = sqlite3_exec (db1, sql_query, callback2, 0, &zErrMsg);
00159 }

int main ( int  argc,
char *  argv[] 
)

This is where program starts.

This creates a window with labels, buttons, gtkentry, text_view in it. This program creates User Interface for accessing various services provided by Proxy Server. The Services are, block any website to access, per user IP statistics with logging. The proxy server service is started and stopped using this Interface.

Parameters:
argc Number of arguments passed to program
argv Array of arugments passed to program

Definition at line 225 of file main_gtk_proxy_server.c.

References block(), buffer, db1, display_log(), display_usage(), on_window_destroy(), rc, start, start_server(), stop, stop_server(), and unblock().

00226 {
00227 
00228     GtkWidget *window;
00229     GtkWidget *text_view;
00230     GtkWidget *entry,*port;
00231     GtkWidget *vbox;
00232     GtkWidget *bbox;
00233     GtkWidget *table;
00234     GtkWidget *scroll_window;
00235     GtkWidget *label,*label1,*label2;
00236     GtkWidget *block_ip,*unblock_ip,*log,*ip_stats;
00237 
00238     //Opening database connection
00239     rc = sqlite3_open ("proxyDB.sqlite", &db1);
00240     if (rc)
00241     {
00242         fprintf (stderr, "Can't open database: %s\n", sqlite3_errmsg (db1));
00243         sqlite3_close (db1);
00244         exit (1);
00245     }
00246   
00247     gtk_init (&argc, &argv);
00248 
00249     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
00250     gtk_window_set_title (GTK_WINDOW (window), "Proxy Server");
00251     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
00252     gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
00253     g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (on_window_destroy), NULL);  
00254     
00255     scroll_window = gtk_scrolled_window_new(NULL, NULL);
00256 
00257     
00258     entry = gtk_entry_new();
00259     gtk_entry_set_text(GTK_ENTRY(entry), "0.0.0.0");
00260     
00261 
00262     port = gtk_entry_new();
00263     gtk_entry_set_text(GTK_ENTRY(port), "8080");
00264     
00265     label1 = gtk_label_new("IP Address/Website");
00266     label2 = gtk_label_new("Port");
00267     
00268 
00269     vbox = gtk_vbox_new (FALSE, 2);
00270     gtk_container_add (GTK_CONTAINER (window), vbox);
00271     
00272     label=gtk_label_new("\nIn order to configure  Proxy Server to a particular port give 'Port Number',\n          to Block/Unblock IP/destinataions give 'IP Address/Website', \n                and to View Logs and Per IP Statistitcs Press Buttons. \n\n");
00273     gtk_box_pack_start (GTK_BOX (vbox), label, 0, 0, 0);
00274     
00275     table = gtk_table_new(1, 1, TRUE);
00276     gtk_box_pack_start(GTK_BOX(vbox), table, 0, 0, 0);
00277     gtk_table_attach_defaults(GTK_TABLE(table), label1, 0, 1, 0, 1 );   
00278     gtk_table_attach_defaults(GTK_TABLE(table), entry, 1, 2, 0, 1 );
00279     gtk_table_attach_defaults(GTK_TABLE(table), label2, 2.5, 3, 0, 1 ); 
00280     gtk_table_attach_defaults(GTK_TABLE(table), port, 3, 4, 0, 1 );
00281   
00282     
00283 
00284     bbox = gtk_hbutton_box_new ();
00285     gtk_box_pack_start (GTK_BOX (vbox), bbox, 0, 0, 0);
00286 
00287 
00288     text_view = gtk_text_view_new ();
00289     gtk_container_add (GTK_CONTAINER (scroll_window), text_view);
00290     gtk_box_pack_start (GTK_BOX (vbox), scroll_window, 1, 1, 0);
00291     buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
00292 
00293     block_ip = gtk_button_new_with_label ("Block IP");
00294     g_signal_connect(G_OBJECT(block_ip),"clicked",G_CALLBACK(block), (gpointer)entry);
00295     gtk_container_add (GTK_CONTAINER (bbox), block_ip);
00296 
00297     unblock_ip = gtk_button_new_with_label ("UnBlock IP");
00298     g_signal_connect(G_OBJECT(unblock_ip),"clicked",G_CALLBACK(unblock), (gpointer)entry);
00299     gtk_container_add (GTK_CONTAINER (bbox), unblock_ip);
00300 
00301     log = gtk_button_new_with_label ("View Log");
00302     g_signal_connect(G_OBJECT(log),"clicked",G_CALLBACK(display_log), (gpointer)buffer);
00303     gtk_container_add (GTK_CONTAINER (bbox), log);
00304 
00305     ip_stats = gtk_button_new_with_label ("IP Statistics");
00306     g_signal_connect(G_OBJECT(ip_stats),"clicked",G_CALLBACK(display_usage), (gpointer)buffer);
00307     gtk_container_add (GTK_CONTAINER (bbox), ip_stats);
00308 
00309     start = gtk_button_new_with_label ("Start Server");
00310     g_signal_connect(G_OBJECT(start),"clicked",G_CALLBACK(start_server), (gpointer)port);
00311     gtk_container_add (GTK_CONTAINER (bbox), start);
00312 
00313     stop = gtk_button_new_with_label ("Stop Server");
00314     g_signal_connect(G_OBJECT(stop),"clicked",G_CALLBACK(stop_server), (gpointer)start);
00315     gtk_container_add (GTK_CONTAINER (bbox), stop);
00316     gtk_widget_set_sensitive(stop, FALSE);
00317 
00318     gtk_widget_show_all (window);
00319 
00320     gtk_main ();
00321     return 0;
00322 }

void on_window_destroy ( GtkWidget *  widget,
gpointer  data 
)

This function is called when someone tries to close top level window.

It will also kill proxy server running as its child.

Parameters:
widget1 Widget that generated the event
data Additional data passed by widget while generating the signal

Definition at line 167 of file main_gtk_proxy_server.c.

References pid.

Referenced by main().

00168 {
00169     kill(pid, SIGTERM);
00170     gtk_main_quit ();
00171 }

void proxy_server_main (  ) 

Referenced by start_server().

void start_server ( GtkWidget *  widget1,
gpointer  data 
)

This function is called when someone clicks on Start Server button It will run proxy server as its child.

It will disable Start button

Parameters:
widget1 Widget that generated the event
data Port no. for listen passed as string from gtkentry

Definition at line 179 of file main_gtk_proxy_server.c.

References buffer, pid, proxy_server_main(), stop, and str.

Referenced by main().

00180 {
00181      const gchar * text_port = gtk_entry_get_text(data);
00182      int port = atoi(text_port);
00183      pid = fork();
00184      if (pid == 0) 
00185      {
00186           proxy_server_main(port);
00187      }
00188      else
00189      {
00190       //disabling start button
00191       gtk_widget_set_sensitive(widget1, FALSE);
00192       gtk_widget_set_sensitive(stop, TRUE);
00193       if(port > 0)
00194         sprintf(str, "Proxy Server Started Listening on Port %d\n",port);
00195       else
00196         sprintf(str, "Proxy Server Started Listening on Port 8080\n");
00197       gtk_text_buffer_set_text (buffer, str ,-1);
00198      }
00199 }

void stop_server ( GtkWidget *  widget1,
gpointer  data 
)

This function is called when someone clicks on Stop Server button It will kill proxy server running as its child.

It will disable Stop button and enable Start button

Parameters:
widget1 Widget that generated the event
data Port no. for listen passed as string from gtkentry

Definition at line 208 of file main_gtk_proxy_server.c.

References buffer, pid, and start.

Referenced by main().

00209 {
00210      kill(pid, SIGTERM);
00211      gtk_text_buffer_set_text (buffer, "Server Stopped..." ,-1);
00212      //disabling stop button
00213      gtk_widget_set_sensitive(widget1, FALSE);
00214      gtk_widget_set_sensitive(start, TRUE);
00215 }

void unblock ( GtkWidget *  widget1,
gpointer  data 
)

This function is called when clicks on Unblock IP button.

It will delete IP/url entry from database from blocked_ip table.

Parameters:
widget1 Widget that generated the event
data IP/url to be unblocked is passed through gtkentry

Definition at line 113 of file main_gtk_proxy_server.c.

References callback(), db1, rc, sql_query, and zErrMsg.

Referenced by main().

00114 {
00115     const gchar * text = gtk_entry_get_text(data);
00116     char host_ip[100];
00117     struct hostent *host_struct;
00118         struct in_addr h_addr;    /* internet address */
00119     host_struct = (struct hostent *)gethostbyname(text);
00120         if (host_struct == NULL)
00121     {
00122             fprintf(stderr,"ERROR, no such host\n");
00123             //return -1;
00124         }
00125         h_addr.s_addr = *((unsigned long *) host_struct->h_addr_list[0]);
00126     strcpy(host_ip, inet_ntoa(h_addr));
00127     sprintf(sql_query, "DELETE from blocked_ip where blocked_ip='%s' ", host_ip);
00128     rc = sqlite3_exec (db1, sql_query, callback, 0, &zErrMsg);
00129     sprintf(sql_query, "DELETE from blocked_ip where blocked_ip='%s'", text);
00130     rc = sqlite3_exec (db1, sql_query, callback, 0, &zErrMsg);
00131 }


Variable Documentation

GtkTextBuffer* buffer

Definition at line 29 of file main_gtk_proxy_server.c.

Referenced by callback2(), main(), start_server(), and stop_server().

sqlite3* db1

Definition at line 23 of file main_gtk_proxy_server.c.

Referenced by block(), display_log(), display_usage(), main(), and unblock().

GtkTextIter end_iter

Definition at line 30 of file main_gtk_proxy_server.c.

Referenced by callback2().

pid_t pid

Definition at line 25 of file main_gtk_proxy_server.c.

Referenced by on_window_destroy(), start_server(), and stop_server().

int rc

Definition at line 27 of file main_gtk_proxy_server.c.

Referenced by block(), display_log(), display_usage(), main(), and unblock().

char sql_query[BUFFER]

Definition at line 28 of file main_gtk_proxy_server.c.

Referenced by block(), display_log(), display_usage(), and unblock().

GtkWidget* start

Definition at line 31 of file main_gtk_proxy_server.c.

Referenced by main(), and stop_server().

GtkWidget * stop

Definition at line 31 of file main_gtk_proxy_server.c.

Referenced by main(), and start_server().

char str[BUFFER]

Definition at line 32 of file main_gtk_proxy_server.c.

Referenced by callback2(), and start_server().

char* zErrMsg = 0

Definition at line 26 of file main_gtk_proxy_server.c.

Referenced by block(), display_log(), display_usage(), and unblock().

 All Data Structures Files Functions Variables Typedefs Defines

Generated on Wed Apr 7 23:49:15 2010 for Doxygentestproject by  doxygen 1.6.1