Reputation: 15
I become use raw socket and i have this problem:
bad file descriptor
the code is:
#include<stdio.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd; //file descriptor raw socket
int ioInt;
struct ifreq req; //struttura per la chiamata di ioctl
fd=socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
if(fd==-1) {
if(errno==EPROTONOSUPPORT)
perror("socket o protocollo non supportato dal dominio");
if(errno==EACCES)
perror("mancano i privilegi per creare il socket");
if(errno==EINVAL)
perror("protocollo sconosciuto o dominio non disponibile");
if(errno==ENOBUFS || errno==ENOMEM)
perror("memoria non sufficente per la creazione del socket");
}
strncpy (req.ifr_name, "eth0", sizeof(req.ifr_name) - 1);
ioInt=ioctl(fd, SIOCGIFINDEX, &req);
if(ioInt==-1)
perror("SIOCGIFINDEX");
return 0;
}
I must access to eth0 and want send e receive a packet in that. I use linux mint 12
Upvotes: 1
Views: 3236
Reputation: 39797
You're failing to print an error when fd==-1 and errno is not one of the values you are looking for.
I ran your code (as a non-privileged user) and fd came back as -1 with errno == EPERM
.
You're most likely not running as root. You must run as root to get a raw socket.
Upvotes: 3