Reputation: 567
I want to extract the checksum from the UDP and use it as an argument to another function. I have the below code.
void connection_init()
{
bool sender_registered = false, receiver_registered = false;
int localsockfd, globalsockfd;
struct sockaddr_in localaddr, globaladdr, trolladdr, senderaddr, receiveraddr, cliaddr;
localsockfd = socket(AF_INET, SOCK_DGRAM, 0);
globalsockfd = socket(AF_INET, SOCK_DGRAM, 0);
//localaddr -- This address and port is used by tcpd to receives data from local client or server process
bzero(&localaddr, sizeof(localaddr));
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons(14683);
localaddr.sin_addr.s_addr = htonl(INADDR_ANY);
//globaladdr -- This address and port is used to communicate with troll process
bzero(&globaladdr, sizeof(globaladdr));
globaladdr.sin_family = AF_INET;
globaladdr.sin_port = htons(14684);
globaladdr.sin_addr.s_addr = htonl(INADDR_ANY);
//trolladdr -- Troll process address
bzero(&trolladdr, sizeof(trolladdr));
trolladdr.sin_family = AF_INET;
trolladdr.sin_port = htons(14685);
inet_pton(AF_INET, "127.0.0.1", &trolladdr.sin_addr);
bind(localsockfd, (struct sockaddr *)&localaddr, sizeof(localaddr));
bind(globalsockfd, (struct sockaddr *)&globaladdr, sizeof(globaladdr));
fd_set rset;
FD_ZERO(&rset);
int maxfd1;
int len = sizeof(cliaddr);
int receivedbytes;
char buffer[1000] = {0x0};
struct message msg;
openlog(NULL, LOG_PID, LOG_USER);
int filesize = 0;
}
Later down the program I'm doing a recvfrom() like this:
receivedbytes = recvfrom(localsockfd, &msg, sizeof(msg), 0, (struct sockaddr *)&cliaddr, &len);
If I want to display the checksum after I receive, how should I do it?
Upvotes: 0
Views: 2008
Reputation: 1919
You can't. (Not directly at least.) You're also talking to 127.0.0.1: it's probable that the UDP checksum is never computed (since the packet won't go onto the wire, and therefore won't be corrupted).
Options:
Receive using a SOCK_RAW socket: this will retain the UDP header in the received buffer. As noted above though, you probably won't have a UDP checksum if you're talking via the loopback interface, and will have to handle UDP checksum validation yourself.
Compute the checksum yourself. You won't have received the datagram in your program if the checksum failed (because the kernel would reject it), and you have everything that you need in order to to compute it, so you can just reconstruct it yourself in the receiver.
I'm not entirely clear on why you need the UDP checksum. It isn't guaranteed to be there, and isn't a particularly strong integrity check?
Upvotes: 2