I need to use the following code in my c code and compile.
dd if=/dev/urandom bs=1 count=400 of=/dev/udp/SrcAddress.ai_addr/8000
This is giving the error
‘dd’ was not declared in this scope
How can I use this command inside my c code?
I need to use the following code in my c code and compile.
dd if=/dev/urandom bs=1 count=400 of=/dev/udp/SrcAddress.ai_addr/8000
This is giving the error
‘dd’ was not declared in this scope
How can I use this command inside my c code?
You cannot use dd
like that. Your C program is not a terminal and you cannot perform shell commands by just writing them in your code. One way to perform what you want is by using popen
as following.
#include <stdio.h>
FILE *fd = popen("dd if=/dev/urandom bs=1 count=400 of=/dev/udp/SrcAddress.ai_addr/8000","w");
pclose(fd);
It is possible that what you actually want to do is write the equivalent of this dd
operation in C. That would look something like this.
int send_n_random_bytes_udp(unsigned int n, const struct sockaddr_in *dest)
{
int rng = open("/dev/urandom", O_RDONLY);
if (rng == -1) return -1;
int sk = socket(AF_INET, SOCK_DGRAM);
if (sk == -1) { close(rng); return -1; }
int status = 0;
for (unsigned int i = 0; i < n; i++) {
char byte;
if (read(rng, &byte, 1) != 1 ||
sendto(sk, &byte, 1, 0,
(const struct sockaddr *)dest,
sizeof(struct sockaddr_in)) != 1) {
status = -1;
break;
}
}
close(sk);
close(rng);
return status;
}