ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
'recvfrom' function is not working. This function is a part of my traceroute program. 'recvfrom' doesn't return anything. The control stops here itself. Can anyone help me? Following is the code: recv_v4.c
Code:
#include "trace.h"
/*
* Return: -3 on timeout
* -2 on ICMP time exceeded in transit (caller keeps going)
* -1 on ICMP port unreachable (caller is done)
* >= 0 return value is some other ICMP unreachable code
*/
int recv_v4(int seq, struct timeval *tv)
{
int hlen1, hlen2, icmplen;
socklen_t len;
ssize_t n;
struct ip *ip, *hip;
struct icmp *icmp;
struct udphdr *udp;
alarm(3);
for ( ; ; ) {
len = pr->salen;
n = recvfrom(recvfd, recvbuf, sizeof(recvbuf), 0, pr->sarecv, &len);
if (n < 0) {
if (errno == EINTR)
return(-3); /* alarm expired */
else
{
printf("\nrecvfrom error");
exit(1);
}
}
gettimeofday(tv, NULL); /* get time of packet arrival */
ip = (struct ip *) recvbuf; /* start of IP header */
hlen1 = ip->ip_hl << 2; /* length of IP header */
icmp = (struct icmp *) (recvbuf + hlen1); /* start of ICMP header */
if ( (icmplen = n - hlen1) < 8)
{
printf("\nicmplen (%d) < 8", icmplen);
exit(1);
}
if (icmp->icmp_type == ICMP_TIMXCEED &&
icmp->icmp_code == ICMP_TIMXCEED_INTRANS) {
if (icmplen < 8 + 20 + 8)
{
printf("\nicmplen (%d) < 8 + 20 + 8", icmplen);
exit(1);
}
hip = (struct ip *) (recvbuf + hlen1 + 8);
hlen2 = hip->ip_hl << 2;
udp = (struct udphdr *) (recvbuf + hlen1 + 8 + hlen2);
if (hip->ip_p == IPPROTO_UDP &&
udp->uh_sport == htons(sport) &&
udp->uh_dport == htons(dport + seq))
return(-2); /* we hit an intermediate router */
} else if (icmp->icmp_type == ICMP_UNREACH) {
if (icmplen < 8 + 20 + 8)
{
printf("icmplen (%d) < 8 + 20 + 8", icmplen);
exit(1);
}
hip = (struct ip *) (recvbuf + hlen1 + 8);
hlen2 = hip->ip_hl << 2;
udp = (struct udphdr *) (recvbuf + hlen1 + 8 + hlen2);
if (hip->ip_p == IPPROTO_UDP &&
udp->uh_sport == htons(sport) &&
udp->uh_dport == htons(dport + seq)) {
if (icmp->icmp_code == ICMP_UNREACH_PORT)
return(-1); /* have reached destination */
else
return(icmp->icmp_code); /* 0, 1, 2, ... */
}
} else if (verbose) {
printf(" (from %s: type = %d, code = %d)\n",
Sock_ntop_host(pr->sarecv, pr->salen),
icmp->icmp_type, icmp->icmp_code);
}
/* Some other ICMP error, recvfrom() again */
}
}
'recvfrom' function is not working. This function is a part of my traceroute program. 'recvfrom' doesn't return anything. The control stops here itself. Can anyone help me?
If your socket is set up using the default settings, then it is blocking socket. Thus recvfrom() could potentially block indefinitely. When the alarm() expires, recvfrom() will return a result value of less than 0; errno will be set to EINTR. So when you indicate that recvfrom() is not returning anything, can you please elaborate more?
Perhaps your recvfrom() is not receiving data? Are you sure that the socket is being sent any data?
P.S. In lieu of using alarm(), you should probably do something like this:
I tried your suggestion "dwhitney67" still no result.
What i mean to say control stops there itself is, suppose if i give 'printf' statement after recvfrom function 'printf' is not printing. But it is timing out i.e. sig_alrm function is called, still control doesn't come to 'if (n < 0)' statement.
I tried your code 'dwhitney', nothing is being printed. It is hanging it seems.
Yes. I am having signal handler. It prints 'inside signal' and then it is hanging it seems.
Following is the code.
Code:
#include "trace.h"
int gotalarm;
void
sig_alrm(int signo)
{
printf("\n inside signal ");
gotalarm = 1; /* set flag to note that alarm occurred */
return; /* and interrupt the recvfrom() */
}
I tried your code 'dwhitney', nothing is being printed. It is hanging it seems.
Here are some basic questions:
1) What OS are you using?
2) What compiler are you using?
3) What steps did you take to build the sample application I offered?
Also, a suggestion for your application... when using printf(), output a newline character at the end of the text you wish to see; this forces a flush of the output stream. Otherwise the output you are hoping to see is buffered until some byte limit is reached (1K bytes, 4K bytes, ??).
Here are some basic questions:
1) What OS are you using?
2) What compiler are you using?
3) What steps did you take to build the sample application I offered?
I am using Windows XP Professional OS,
CYGWIN gcc compiler
gcc test.c
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.