Commit b4670094 authored by Jørn Åne de Jong's avatar Jørn Åne de Jong

Rewrite receive code to support TTL

parent 61a479e5
......@@ -937,31 +937,62 @@ static void get_str_addr(struct sockaddr_storage src_addr,char buffer[]){
*/
static int rec_n_print(unsigned short port, unsigned long limit)
{
long rec_bytes = 0; /* Bytes read */
ssize_t rec_bytes = 0; /* Bytes read */
int src_len = sizeof(struct sockaddr_storage);
int ttl = 64;
int received_ttl;
struct sockaddr_storage src_addr;
struct msghdr msgh;
struct iovec iov;
struct timeval time1;
struct udp_data *udp_ptr;
struct sockaddr_storage d_add;
struct cmsghdr *cmptr = NULL;
#define CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int))
char buffer[PMAXSIZE], str1[INET6_ADDRSTRLEN], str2[INET6_ADDRSTRLEN];
/* Initialize some variables */
memset(buffer,0,PMAXSIZE);
memset(str1,0,INET6_ADDRSTRLEN);
memset(str2,0,INET6_ADDRSTRLEN);
char srcport[10];
while(1)
cmptr = malloc(CONTROLLEN);
iov.iov_base = &buffer;
iov.iov_len = sizeof(buffer);
memset(&msgh, 0, sizeof(msgh));
msgh.msg_name = &src_addr;
msgh.msg_namelen = sizeof(src_addr);
msgh.msg_iov = &iov;
msgh.msg_iovlen = 1;
msgh.msg_control = (caddr_t) cmptr;
msgh.msg_controllen = CONTROLLEN;
if(setsockopt(main_socket, IPPROTO_IP, IP_RECVTTL, &ttl,sizeof(ttl))>0)
{
rec_bytes = recvfrom(main_socket, buffer, PMAXSIZE, 0,
(struct sockaddr *)&src_addr, (socklen_t *)&src_len);
RUDEBUG1("crude: unable to receive TTL values: %s\n",strerror(errno));
}
for( ; ; )
{
rec_bytes = recvmsg(main_socket, &msgh, MSG_WAITALL);
if(rec_bytes <= 0)
{
RUDEBUG1("crude: error when receiving packet: %s\n",strerror(errno));
}
else
{
gettimeofday(&time1, NULL);
struct cmsghdr *cmsg;
int *ttlptr;
received_ttl = -1;
for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_TTL) &&
(cmsg->cmsg_len) ){
ttlptr = (int *) CMSG_DATA(cmsg);
received_ttl = *ttlptr;
break;
}
}
gettimeofday(&time1, NULL);
pkt_count++;
udp_ptr = (struct udp_data*)buffer;
d_add = udp_ptr->dest_addr;
......@@ -970,13 +1001,13 @@ static int rec_n_print(unsigned short port, unsigned long limit)
get_str_addr(d_add,str2);
printf("ID=%lu SEQ=%lu SRC=%s:%hu DST=%s:%hu "
"Tx=%lu.%06lu Rx=%ld.%06ld SIZE=%ld\n",
"Tx=%lu.%06lu Rx=%ld.%06ld TTL=%d SIZE=%ld\n",
(unsigned long)ntohl(udp_ptr->flow_id),
(unsigned long)ntohl(udp_ptr->sequence_number),
str1, ntohs(((struct sockaddr_in *)&src_addr)->sin_port), str2, port,
(unsigned long)ntohl(udp_ptr->tx_time_seconds),
(unsigned long)ntohl(udp_ptr->tx_time_useconds),
time1.tv_sec, time1.tv_usec, rec_bytes);
time1.tv_sec, time1.tv_usec, received_ttl, rec_bytes);
fflush(stdout);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment