LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Cannot get interface flags: Invalid argument (https://www.linuxquestions.org/questions/programming-9/cannot-get-interface-flags-invalid-argument-636558/)

nasim751 04-20-2008 02:52 AM

Cannot get interface flags: Invalid argument
 
hello,
i am trying to setup network device but unfortunately can't get interface.
i got few feedback from like you people even though the problem is in right place. How can i get this result.Please give me your feedback.

This is the function..........

error : Cannot get interface flags: Invalid argument
Code:

Description: Set up the network device so we can read it.


initdevice(int fd_flags, u_long dflags)

{
    struct ifreq ifr;
    int fd, flags = 0;

    if((fd = socket(PF_INET, SOCK_PACKET, htons(0x0003))) < 0)
    {
        perror("Cannot open device socket");
        exit(-1);
    }

    /* Get the existing interface flags */

    strcpy(ifr.ifr_name, Gdevice);
    if(ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
    {
        perror("Cannot get interface flags");
        exit(-1);
    }

    ifr.ifr_flags |= IFF_PROMISC;
    if(ioctl(fd, SIOCSIFFLAGS,  &ifr) < 0)
    {
        perror("Cannot set interface flags");
        exit(-1);
    }
   
    return(fd);
}

Main body..............
Code:

int SIOCGIFFLAGS;
int SIOCSIFFLAGS;


extern int errno;
extern char *optarg;
extern int optind, opterr;

void do_tcp(), do_udp(), do_icmp(), print_info(), process_packet();
void addtcp(), addudp(), clear_pktin(), buildnet();
void doargs(), usage(), addfloodinfo(), rmfloodinfo();
struct scaninfo *doicare(), *addtarget();
char *anetaddr(), *ether_ntoa();
u_char *readdevice();


main(argc, argv)
int argc;
char *argv[];
{
    int pktlen = 0, i, netfd;
    u_char *pkt;
    char hostname[32];
    struct hostent *hp;
    time_t t;

    doargs(argc, argv);
    openlog("WATCHER", 0, LOG_DAEMON);
    if(gethostname(hostname, sizeof(hostname)) < 0)
    {
        perror("gethostname");
        exit(-1);
    }
    if((hp = gethostbyname(hostname)) == NULL)
    {
        fprintf(stderr, "Cannot find own address\n");
        exit(-1);
    }
    memcpy((char *)&Gmaddr, hp->h_addr, hp->h_length);
    buildnet();
    if((netfd = initdevice(O_RDWR, 0)) < 0)
        exit(-1);

    /* Now read packets forever and process them. */

    t = time((time_t *)0);
    while(pkt = readdevice(netfd, &pktlen))
    {
        process_packet(pkt, pktlen);
        if(time((time_t *)0) - t > Gtimer)
        {
            /* Times up.  Print what we found and clean out old stuff. */

            for(Gsi = Gsilist, i = 0; Gsi; Gsi = Gsi->next, i++)
            {
                clear_pktin(Gsi);
                print_info();
                Gsi->icmpcnt = 0;
            }
            t = time((time_t *)0);
        }
    }
}


dmail 04-21-2008 12:22 PM

Does the following not need a cast?
Code:

    if(ioctl(fd, SIOCGIFFLAGS, (caddr_t)&ifr) == -1 )
Also for looking at the docs I do not think checking if less than zero is correct (yet have seen it may times in examples)
Quote:

Upon successful completion, ioctl() shall return a value other than -1 that depends upon the STREAMS device control function. Otherwise, it shall return -1 and set errno to indicate the error.
Anyway third time lucky in posting this in the correct forum :)


All times are GMT -5. The time now is 01:12 PM.