- 论坛徽章:
- 0
|
大家好。
我再练习raw socket编程,
写了个小程序。但是在运行的时候(sendto)出现“Permission denied”
我已经加了setuid(getuid()); 怎么会没有权限呢???
哪位帮我改改啊???
谢谢谢谢:::::::
新手写的代码,大家见谅!
代码:
#define DST 10.117.4.11
#define P 80
struct ipheader {
unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
unsigned char ip_tos;
unsigned short int ip_len;
unsigned short int ip_id;
unsigned short int ip_off;
unsigned char ip_ttl;
unsigned char ip_p;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
}; /* total ip header length: 20 bytes (=160 bits) */
struct icmpheader {
unsigned char icmp_type;
unsigned char icmp_code;
unsigned short int icmp_cksum;
/* The following data structures are ICMP type specific */
unsigned short int icmp_id;
unsigned short int icmp_seq;
}; /* total icmp header length: 8 bytes (=64 bits) */
struct tcpheader {
unsigned short int th_sport;
unsigned short int th_dport;
unsigned int th_seq;
unsigned int th_ack;
unsigned char th_x2:4, th_off:4;
unsigned char th_flags;
unsigned short int th_win;
unsigned short int th_sum;
unsigned short int th_urp;
}; /* total tcp header length: 20 bytes (=160 bits) */
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
int main(void)
{
int sockfd=socket(PF_INET,SOCK_RAW,IPPROTO_TCP);
printf("socket created succefully and fd is %d\n",sockfd);
char buff[4096];
struct ipheader *iph;
struct tcpheader *tcph;
iph=(struct ipheader *) buff;
tcph=(struct tcpheader *)(buff+sizeof(struct ipheader));
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(P);
sin.sin_addr.s_addr=inet_addr("DST");
memset(buff,0,4096);
setuid(getuid());
printf("my uid is %d\n",getuid());
/*start to make packet*/
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ipheader) + sizeof (struct tcpheader); / * no payload */
iph->ip_id = htonl (54321); /* the value doesn't matter here */
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = 6;
iph->ip_sum = 0; /* set it to 0 before computing the actu al checksum later */
iph->ip_src = inet_addr ("1.2.3.4");/* SYN's can be blindly spoofed */
iph->ip_dst = sin.sin_addr.s_addr;
tcph->th_sport = htons (1234); /* arbitrary port */
tcph->th_dport = htons (P);
tcph->th_seq = random ();/* in a SYN packet, the sequence is a random */
tcph->th_ack = 0;/* number, and the ack sequence is 0 in the 1st packet */
tcph->th_x2 = 0;
tcph->th_off = 0; /* first and only tcp segment */
tcph->th_flags = 0x02; /* initial connection request */
tcph->th_win = htonl (65535); /* maximum allowed window size */
tcph->th_sum = 0;/* if you set a checksum to zero, your kernel's IP stac k
should fill in the correct checksum during transmission */
tcph->th_urp = 0;
iph->ip_sum = csum ((unsigned short *) buff, iph->ip_len >> 1);
/*t the kernel knows the header is included in the data, and doesn't
insert its own header into the packet before our data */
{ /* lets do it the ugly way.. */
int one = 1;
const int *val = &one;
if (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
printf ("Warning: Cannot set HDRINCL!\n");
}
while (1)
{
if (sendto (sockfd, /* our socket */
buff, /* the buffer containing headers and data */
iph->ip_len, /* total length of our datagram */
0, /* routing flags, normally always 0 */
(struct sockaddr *) &sin, /* socket addr, just like in */
sizeof (sin)) < 0) /* a normal send() */
{
perror("error:");
}
else
printf ("date send");
}
return 0;
} |
|