Sponsored Content
Top Forums Programming Incompatibility an old c and gcc 4.3.1.urgency plz Post 302221695 by galaxy.ice on Tuesday 5th of August 2008 03:10:10 AM
Old 08-05-2008
Data Incompatibility an old c and gcc 4.3.1.urgency plz

I couldn't compile this code in gcc (SUSE Linux) 4.3.1 20080507.
got me these!

sniffer.c: In function ‘int CreateRawSocket(int)':
sniffer.c:16: error: ‘htons' was not declared in this scope
sniffer.c: In function ‘int BindRawSocketToInterface(char*, int, int)':
sniffer.c:31: error: ‘bzero' was not declared in this scope
sniffer.c:37: error: ‘strncpy' was not declared in this scope
sniffer.c:48: error: ‘htons' was not declared in this scope
sniffer.c: In function ‘int main(int, char**)':
sniffer.c:103: error: invalid conversion from ‘int*' to ‘socklen_t*'
sniffer.c:103: error: initializing argument 6 of ‘ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*)'

how i can fix it?

Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<features.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#include<errno.h>
#include<sys/ioctl.h>
#include<net/if.h>


int CreateRawSocket(int protocol_to_sniff)
{
	int rawsock;

	if((rawsock = socket(PF_PACKET, SOCK_RAW, htons(protocol_to_sniff)))== -1)
	{
		perror("Error creating raw socket: ");
		exit(-1);
	}

	return rawsock;
}

int BindRawSocketToInterface(char *device, int rawsock, int protocol)
{
	
	struct sockaddr_ll sll;
	struct ifreq ifr;

	bzero(&sll, sizeof(sll));
	bzero(&ifr, sizeof(ifr));
	
	/* First Get the Interface Index  */


	strncpy((char *)ifr.ifr_name, device, IFNAMSIZ);
	if((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1)
	{
		printf("Error getting Interface index !\n");
		exit(-1);
	}

	/* Bind our raw socket to this interface */

	sll.sll_family = AF_PACKET;
	sll.sll_ifindex = ifr.ifr_ifindex;
	sll.sll_protocol = htons(protocol); 


	if((bind(rawsock, (struct sockaddr *)&sll, sizeof(sll)))== -1)
	{
		perror("Error binding raw socket to interface\n");
		exit(-1);
	}

	return 1;
	
}

void PrintPacketInHex(unsigned char *packet, int len)
{
	unsigned char *p = packet;

	printf("\n\n---------Packet---Starts----\n\n");
	
	while(len--)
	{
		printf("%.2x ", *p);
		p++;
	}

	printf("\n\n--------Packet---Ends-----\n\n");

}


main(int argc, char **argv)
{
	int raw;
	unsigned char packet_buffer[2048]; 
	int len;
	int packets_to_sniff;
	struct sockaddr_ll packet_info;
	int packet_info_size = sizeof(packet_info);

	/* create the raw socket */

	raw = CreateRawSocket(ETH_P_IP);

	/* Bind socket to interface */

	BindRawSocketToInterface(argv[1], raw, ETH_P_IP);

	/* Get number of packets to sniff from user */

	packets_to_sniff = atoi(argv[2]);

	/* Start Sniffing and print Hex of every packet */
	
	while(packets_to_sniff--)
	{
		if((len = recvfrom(raw, packet_buffer, 2048, 0, (struct sockaddr*)&packet_info, &packet_info_size)) == -1)
		{
			perror("Recv from returned -1: ");
			exit(-1);
		}
		else
		{
			/* Packet has been received successfully !! */

			PrintPacketInHex(packet_buffer, len);
		}
	}
	
	
	return 0;
}

 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

plz Help How should I configure cc compiler output file plz help???

i.e configuration of C compiler :confused: (4 Replies)
Discussion started by: atiato
4 Replies

2. UNIX for Dummies Questions & Answers

Help Plz!

Ok, here's the deal. I'm running WinXp. I was merging two partitions using partition magic when I lost power to my box. I have very IMPORTANT data on one partition and the other is blank, hence the attempted merge. After the power loss, I booted my system up. Now, I was getting errors... (2 Replies)
Discussion started by: Silicon Demon
2 Replies

3. Programming

plz help!

i'm pretty new to unix programming. i just wanna know how exactly fork(), waitpid() works. i have read some from the book, but it's still confusing. especially with those < 0 and == 0. plz help!!!! (1 Reply)
Discussion started by: ligerdave
1 Replies

4. UNIX Desktop Questions & Answers

Help Plz!!

Hello People ! :))I'm Sorry may be offtopic, I want ask you to help me with this problemwhere I can find FAQ about DB2.Tell me where I can find it.Good Luck !!! %) (1 Reply)
Discussion started by: Zo0oMGo0oG
1 Replies

5. UNIX for Dummies Questions & Answers

plz help me

how i can download the game plz tell me ????? ty :) :) :) (3 Replies)
Discussion started by: Sora21
3 Replies

6. Solaris

Installing gcc - recieve error message gcc : cannot execute

AIM- Install Oracle 11g on Solaris using VMWare Steps 1.Logged on as root 2.Created subfolders à /usr/local/bin & /usr/local/bin/gcc 3.Downloaded gcc & libiconv & unzipped them on my harddrive & burnt them on CD 4.Copied files from CD to /usr/local/bin/gcc 5.Terminal (root) à pkgadd -d... (8 Replies)
Discussion started by: Ackers
8 Replies

7. UNIX for Dummies Questions & Answers

OpenSSH incompatibility with Sun SSH ??

I am using OpenSSH on my sun servers. There is another suite of sun servers attempting to SSH into my machines. I am running ssh -vv: OpenSSH_4.4p1, OpenSSL 0.9.8d 28 Sep 2006 the other guy is running the sun version of ssh (i don't know what/if any differences there are between... (7 Replies)
Discussion started by: EWTGPAC
7 Replies

8. UNIX for Dummies Questions & Answers

PLZ help..!!

plz help me with this question : 1111 | aaaa | pppp | 23/9/87 3333 | dddd | oooo | 6/9/8 4444 | gggg | rrrr | 5/8/9 5555 | qqqq | eeee ans: 5555 | qqqq | eeee depending upon how many "|" are in the question be quick.. (8 Replies)
Discussion started by: adityamitra
8 Replies

9. Shell Programming and Scripting

recent test -e ksh incompatibility in hpux?

On a very new (11.31) hpux machine, I can no longer execute shell fragements like: if ; then . .profile.foo fi and get "ksh: test: argument expected" if I convert this to -d or -f as appropriate (which I've not had to do on older versions of hpux (11.23) nor any other unix platform... (9 Replies)
Discussion started by: Peeter Joot
9 Replies
ivykis(3)						    ivykis programmer's manual							 ivykis(3)

NAME
iv_examples - ivykis examples EXAMPLE
ivykis is initialised by calling iv_init(3). This function is the first function to call when dealing with ivykis -- it has to be called before registering file descriptors or timers. The ivykis main event loop is started by calling iv_main(3). This function generally does not return, except when iv_quit(3) is called somewhere during execution of the program. An application asks ivykis to monitor a certain file descriptor by filling out a structure of type 'struct iv_fd' with a file descriptor number and a callback function, and calling the function iv_fd_register. The first example program waits for data from standard input, and writes a message to standard out whenever something is received: #include <stdio.h> #include <stdlib.h> #include <iv.h> struct iv_fd fd_stdin; static void callback(void *dummy) { char buf[1024]; int len; len = read(fd_stdin.fd, buf, sizeof(buf)); if (len <= 0) { if (len < 0) { if (errno == EAGAIN) return; perror("read"); } exit(1); } printf("read %d bytes of data from stdin ", len); } int main() { iv_init(); IV_FD_INIT(&fd_stdin); fd_stdin.fd = 0; fd_stdin.handler_in = callback; iv_fd_register(&fd_stdin); iv_main(); iv_deinit(); return 0; } The application is responsible for memory management of 'struct iv_fd's passed to ivykis. For example, it should not free memory that con- tains such structures that are still registered with ivykis (i.e. haven't had iv_fd_unregister called on them). iv_fd_register transparently sets the passed file descriptor to nonblocking mode, in anticipation of its future usage. File descriptor callbacks are called in a level-triggered fashion. Therefore, the way of dealing with fd_stdin in the example callback function is safe. In case there arrives data between read and detecting EAGAIN, ivykis will re-call the callback function after it returns. Also, if there are more than 1024 bytes waiting in the input buffer, ivykis will re-call the callback function until all data from stdin have been drained. EXAMPLE 2 The second example accepts connections on TCP port 6667, and waits on each of the connections for data. When data is received on any con- nection, a message is printed to standard out. #include <stdio.h> #include <stdlib.h> #include <iv.h> #include <netinet/in.h> struct connection { struct iv_fd fd; /* other per-connection data goes here */ }; struct listening_socket { struct iv_fd fd; /* other per-listening socket data goes here */ }; static void connection_handler(void *_conn) { struct connection *conn = (struct connection *)_conn; char buf[1024]; int len; len = read(conn->fd.fd, buf, sizeof(buf)); if (len <= 0) { if (len < 0 && errno == EAGAIN) return; iv_fd_unregister(&conn->fd); close(conn->fd.fd); free(conn); return; } printf("got %d bytes of data from %p ", len, conn); } static void listening_socket_handler(void *_sock) { struct listening_socket *sock = (struct listening_socket *)_sock; struct sockaddr_in addr; socklen_t addrlen; struct connection *conn; int fd; addrlen = sizeof(addr); fd = accept(sock->fd.fd, (struct sockaddr *)&addr, &addrlen); if (fd < 0) { if (errno == EAGAIN) return; perror("accept"); exit(1); } conn = malloc(sizeof(*conn)); if (conn == NULL) { fprintf(stderr, "listening_socket_handler: memory allocation error, dropping connection"); close(fd); return; } IV_FD_INIT(&conn->fd); conn->fd.fd = fd; conn->fd.cookie = (void *)conn; conn->fd.handler_in = connection_handler; iv_fd_register(&conn->fd); } int main() { struct listening_socket s; struct sockaddr_in addr; int fd; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); exit(1); } addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(6667); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); exit(1); } if (listen(fd, 4) < 0) { perror("listen"); exit(1); } iv_init(); IV_FD_INIT(&s.fd); s.fd.fd = fd; s.fd.cookie = (void *)&s; s.fd.handler_in = listening_socket_handler; iv_fd_register(&s.fd); iv_main(); iv_deinit(); return 0; } As illustrated, it is possible to pass cookies into callback functions. This is useful for conveying information on which higher-level entity (such as 'connection' or 'listening socket') generated the event for which the callback was called. Note how it is possible to unregister and even free a 'struct iv_fd' in its own callback function. There is logic in ivykis to deal with this case. EXAMPLE 3 This example extends the previous example by a per-connection timer that disconnects the client after too long a period of inactivity. Lines not present in example 2 or different than in example 2 are indicated by '//XXXX' in the right-hand margin. #include <stdio.h> #include <stdlib.h> #include <iv.h> #include <netinet/in.h> #define CONNECTION_TIMEOUT(10) struct connection { struct iv_fd fd; struct iv_timer disconnect_timeout; //XXXX /* other per-connection data goes here */ }; struct listening_socket { struct iv_fd fd; /* other per-listening socket data goes here */ }; static void connection_handler(void *_conn) { struct connection *conn = (struct connection *)_conn; char buf[1024]; int len; len = read(conn->fd.fd, buf, sizeof(buf)); if (len <= 0) { if (len < 0 && errno == EAGAIN) return; iv_timer_unregister(&conn->disconnect_timeout); //XXXX iv_fd_unregister(&conn->fd); close(conn->fd.fd); free(conn); return; } printf("got %d bytes of data from %p ", len, conn); iv_timer_unregister(&conn->disconnect_timeout); //XXXX iv_validate_now(); //XXXX conn->disconnect_timeout.expires = iv_now; //XXXX conn->disconnect_timeout.expires.tv_sec += CONNECTION_TIMEOUT;//XXXX iv_timer_register(&conn->disconnect_timeout); //XXXX } static void disconnect_timeout_expired(void *_conn) //XXXX { //XXXX struct connection *conn = (struct connection *)_conn; //XXXX iv_fd_unregister(&conn->fd); //XXXX close(conn->fd.fd); //XXXX free(conn); //XXXX } //XXXX static void listening_socket_handler(void *_sock) { struct listening_socket *sock = (struct listening_socket *)_sock; struct sockaddr_in addr; socklen_t addrlen; struct connection *conn; int fd; addrlen = sizeof(addr); fd = accept(sock->fd.fd, (struct sockaddr *)&addr, &addrlen); if (fd < 0) { if (errno == EAGAIN) return; perror("accept"); exit(1); } conn = malloc(sizeof(*conn)); if (conn == NULL) { fprintf(stderr, "listening_socket_handler: memory allocation error, dropping connection"); close(fd); return; } IV_FD_INIT(&conn->fd); conn->fd.fd = fd; conn->fd.cookie = (void *)conn; conn->fd.handler_in = connection_handler; iv_fd_register(&conn->fd); IV_TIMER_INIT(&conn->disconnect_timeout); //XXXX iv_validate_now(); //XXXX conn->disconnect_timeout.cookie = (void *)conn; //XXXX conn->disconnect_timeout.handler = disconnect_timeout_expired;//XXXX conn->disconnect_timeout.expires = iv_now; //XXXX conn->disconnect_timeout.expires.tv_sec += CONNECTION_TIMEOUT;//XXXX iv_timer_register(&conn->disconnect_timeout); //XXXX } int main() { struct listening_socket s; struct sockaddr_in addr; int fd; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); exit(1); } addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(6667); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); exit(1); } if (listen(fd, 4) < 0) { perror("listen"); exit(1); } iv_init(); IV_FD_INIT(&s.fd); s.fd.fd = fd; s.fd.cookie = (void *)&s; s.fd.handler_in = listening_socket_handler; iv_fd_register(&s.fd); iv_main(); iv_deinit(); return 0; } The global variable 'iv_now' contains the current time-of-day. However, it is updated lazily, and its contents might be stale at any given time. Before using it, iv_validate_now(3) must be called. EXAMPLE 4 The fourth example demonstrates how to use a custom fatal error handler that does not write the message to syslog. #include <stdio.h> #include <iv.h> static void fatal_error(const char *msg) { fprintf(stderr, "ivykis: FATAL ERROR: %s ", msg); } int main() { iv_init(); iv_set_fatal_msg_handler(fatal_error); iv_fatal("Programmatically triggered fatal error %d.", 42); printf("This code is never reached. "); iv_deinit(); return 0; } This program will abort immediately, with the error message printed to the standard error stream. SEE ALSO
ivykis(3), iv_fatal(3), iv_fd(3), iv_timer(3), iv_task(3), iv_init(3), iv_time(3) ivykis 2003-03-29 ivykis(3)
All times are GMT -4. The time now is 01:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy