LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   "resource temporarily unavailable" in recv in socket programming (http://www.linuxquestions.org/questions/programming-9/resource-temporarily-unavailable-in-recv-in-socket-programming-4175449814/)

mrjmrj 02-12-2013 09:01 AM

"resource temporarily unavailable" in recv in socket programming
 
I want to read and write over Wanpipe driver (a network device driver for Sangoma cards) via socket programming but i get this message error: "resource temporarily unavailable". The card is working and i see it send and receive packets in ifconfig. I have included my code and would be very pleased if somebody help me in this.
A related question: I set the socket to blocking mode but the recv message does not block? how could i block the recv?

Code:

int main(void)
{
int sd;
int buflen=WP_HEADER + MAX_PACKET;
char buf[buflen];
struct wan_sockaddr_ll sa;
sd = socket(AF_WANPIPE, SOCK_RAW,0);
if (sd < 0) /* if socket failed to initialize, exit */
{
perror("Error Creating Socket");
exit(1);
}
printf("Socket Descriptor:%d\n",sd);
memset(&sa,0,sizeof(struct wan_sockaddr_ll));

strncpy((char*)sa.sll_card,"wanpipe1",sizeof(sa.sl l_card));
strncpy((char*)sa.sll_device,"w1g1",sizeof(sa.sll_ device));       
sa.sll_protocol = htons(PVC_PROT);
sa.sll_family = AF_WANPIPE;

if(bind(sd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
{
perror("error bind failed");
close(sd);
exit(1);
}


int data=0;
int ret=ioctl(sd,FIONBIO,&data);
if (ret < 0) {
perror("ioctl error!");
close(sd);
return 1;
}

fd_set read_fds;
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
FD_ZERO(&read_fds);
FD_SET(sd,&read_fds);
if(select(sd+1, &read_fds, NULL, NULL, &timeout) < 0)
{
perror("select() error!");
exit(1);
}

if (FD_ISSET(sd,&read_fds))
printf("There is data for reading\n");
else
printf("There is no data for reading\n");

// MSG_WAITALL | MSG_PEEK | MSG_OOB
int r=recv(sd,buf,buflen,0);
if (r < 0) {
perror("Wanpipe raw socket reading");
close(sd);
exit(1);
}
printf("\nNumber of bytes read into the buffer: %d",r);
printf("\nThe read buffer: ");
puts(buf);
close(sd);
}

thank you in advance.

JohnGraham 02-12-2013 12:00 PM

First of all, please place all code in code tags.

Second, is your select() timing out? You appear to be recv()ing whether or not you've seen FD_ISSET().

mrjmrj 02-12-2013 01:57 PM

select() is timing out and the else message ("There is no data for reading\n") is in the output. then for recv i have "resource temporarily unavailable". if i put recv under [if (FD_ISSET(sd,&read_fds))] it is not called. In other words both select() and recv() indicate that there is no data! but as I said my card is receiving and sending. ifconfig shows there are transmitted and received packets. I do not know the problem is because of my code (select or recv) or from the card.

JohnGraham 02-12-2013 06:40 PM

First, I don't know anything about the AF_WANPIPE protocol, so I could be off on this. Your problem may be what you're bind()ing to. About these two lines:

Code:

strncpy((char*)sa.sll_card,"wanpipe1",sizeof(sa.sl l_card));
strncpy((char*)sa.sll_device,"w1g1",sizeof(sa.sll_ device));

What are sizeof(sa.sll_card) and sizeof(sa.sll_device) (Check by printf()ing these sizes) and are they definitely char buffers, not pointers (I suspect so, but I'd check)? Also, is there definitely space to write all of the card/device strings in there with a null-terminator? (Remember strncpy() won't append one if there isn't enough space).

Also, do you realy need to htons(PVC_PROT), or do you just want PVC_PROT? And is it definitely htons(), not htonl() you want?

If all else fails, could you post the definition of the wan_sockaddr_ll struct? I can't seem to find it on my computer.

mrjmrj 02-13-2013 06:29 AM

thank you for reply.
i changed the code to:

Code:

strcpy( sa.sll_device,"w1g1");
strcpy( sa.sll_card,"wanpipe1");

but it still does not work :( other definition are:

Code:

#define PVC_PROT              0x17
#define        AF_WANPIPE        25
struct wan_sockaddr_ll
{
        unsigned short        sll_family;
        unsigned short        sll_protocol;
        int                sll_ifindex;
        unsigned short        sll_hatype;
        unsigned char        sll_pkttype;
        unsigned char        sll_halen;
        unsigned char        sll_addr[8];
        unsigned char  sll_device[14];
        unsigned char        sll_card[14];

        unsigned int        sll_active_ch;
        unsigned char        sll_prot;
        unsigned char        sll_prot_opt;
        unsigned short  sll_mult_cnt;
        unsigned char        sll_seven_bit_hdlc;
};

i am using a new address family (AF_WANPIPE). maybe i should include some headers files or sth else. do you think so?
in addition bind() does not return error. Does that mean the binding is ok?

JohnGraham 02-13-2013 08:27 AM

Quote:

Originally Posted by mrjmrj (Post 4890626)
maybe i should include some headers files or sth else. do you think so?

Do you get any errors or warnings when you compile? I mean any errors or warnings - pass the -Wall and -Wextra flags to gcc if you're not already, then fix any warnings and retry.


Quote:

Originally Posted by mrjmrj (Post 4890626)
in addition bind() does not return error. Does that mean the binding is ok?

That means it's bound to *something* - I suspect it's just not the right thing. Try removing the htons() around the PVC_PROT, this may not need to be in network byte order (after all, sa_family doesn't need to be).

How certain are you that the packets you expect are arriving? How are you looking at them? If the above two points don't work, try and get a Wireshark/tcpdump trace that shows the packets you expect to see and post it, along with which specific packets you want to see.

mrjmrj 02-20-2013 08:43 AM

thank you JohnGraham
my problem is solved. it was hardware problem :)

JohnGraham 02-21-2013 05:01 AM

Quote:

Originally Posted by mrjmrj (Post 4895917)
thank you JohnGraham
my problem is solved. it was hardware problem :)

Glad to hear it. Please mark the thread as solved so other people know without reading through the whole thread - there should be a link just above the first post.


All times are GMT -5. The time now is 09:06 AM.