Unix Programming - sending a packet using raw socket

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2006 > sending a packet using raw socket





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author sending a packet using raw socket
manu

2006-04-02, 7:41 pm

hi all,
I am new to this group and the subject..
i am trying to send a packet using raw sockets in the below program.
I have very limited knowledge abt socket programming. so pleeze dnt
laugh at the program.
and help me.

can we construcr packet like this and send ?
wat is the reason for the errors?

OUTPUT:-
error in send
error in recieve

source code:

#include<netdb.h>
#include<netinet/in.h>
#include<sys/types.h>

struct pac
{
int a;
};

int main(int argc,char *av[])
{
int sock,on=1;
char msg[20];
struct pac *s;
struct protoent *proto;
struct sockaddr whereto;
struct sockaddr_in *to=(struct sockaddr_in *) &whereto;
struct sockaddr_in from;
int st;
int i;

bzero((char *)&whereto, sizeof(struct sockaddr));

to->sin_family=PF_INET;
to->sin_addr.s_addr = inet_addr(av[0]);

s=(struct ospf *)msg;

s->a=10;

if((proto=getprotobyname("ospf"))==NULL)
{
printf("error\n");
}

if((sock=socket(PF_INET,SOCK_RAW,proto->p_proto)) <0)
{
printf("cant create socket\n");
}

if((setsockopt(sock,SOL_SOCKET,SO_DEBUG,
&on,sizeof(on))) < 0)
{
printf("error3\n");
}

if((i = sendto( s, msg, 2, 0, &whereto, sizeof(struct sockaddr)))<0)
{
printf("error in sendto\n");
}

else
printf("success\n");

for(;;)
{
int len=sizeof(msg);
int fromlen=sizeof(from);

if((st=recvfrom(s,msg,len, 0, &from, &fromlen)) < 0)
{
printf("error in recv \n");
exit(1);
}

}
return 0;
}

pleeze help me.
thanking in advance..
Manu

Rainer Temme

2006-04-02, 7:41 pm

manu wrote:
> can we construcr packet like this and send ?
> wat is the reason for the errors?
>
> OUTPUT:-
> error in send
> error in recieve


Hi Manu,

how about printing the errno as well? (It usually gives
some idea about the type of error that has occured).

Rainer
Rainer Temme

2006-04-02, 7:41 pm

manu wrote:
> #include<netdb.h>
> #include<netinet/in.h>
> #include<sys/types.h>
>
> struct pac
> {
> int a;
> };
>
> int main(int argc,char *av[])
> {
> int sock,on=1;
> char msg[20];
> struct pac *s;
> struct protoent *proto;
> struct sockaddr whereto;
> struct sockaddr_in *to=(struct sockaddr_in *) &whereto;
> struct sockaddr_in from;
> int st;
> int i;
>
> bzero((char *)&whereto, sizeof(struct sockaddr));
>
> to->sin_family=PF_INET;
> to->sin_addr.s_addr = inet_addr(av[0]);
>
> s=(struct ospf *)msg;


where is struct ospf defined ?

>
> s->a=10;
>
> if((proto=getprotobyname("ospf"))==NULL)


on my linux-system the name of this protocol is "ospfigp" ...
check name against /etc/protocols

> {
> printf("error\n");


exit here ...

> }
>


.... instead of dereferencing "proto->" which could be NULL.

> if((sock=socket(PF_INET,SOCK_RAW,proto->p_proto)) <0)
> {
> printf("cant create socket\n");
> }
>
> if((setsockopt(sock,SOL_SOCKET,SO_DEBUG,
&on,sizeof(on))) < 0)
> {
> printf("error3\n");
> }
>
> if((i = sendto( s, msg, 2, 0, &whereto, sizeof(struct sockaddr)))<0)


you might want to sento(sock,...) rather than to s
(s is a "struct pac *" not an int) ... haven't you checked for
compiler warnings?

> {
> printf("error in sendto\n");
> }
>
> else
> printf("success\n");
>
> for(;;)
> {
> int len=sizeof(msg);
> int fromlen=sizeof(from);
>
> if((st=recvfrom(s,msg,len, 0, &from, &fromlen)) < 0)


same ... recvfrom(sock,...) rather than from s ...

don't forget, that with rawsockets, you can receive the IP-header
as well, ... you need more space ... make msg longer ... the shortest IP
header is already 20 bytes long.


> {
> printf("error in recv \n");
> exit(1);
> }
>
> }
> return 0;
> }


PS: by the way ... on most systems your code needs root-privs to run.

Rainer
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com