I am currently trying to write a tftp in C. But i have some difficulties to read the packet that I sent.
Here is what I have :
The struct that I am sending :
typedef struct {
short type;
short block_num;
char* data;
} data_t;
the function (server's side) wich send the packet :
if ( s = sendto (serverSocket, data, 512 ,0,
(struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 )
{
perror("sendto");
exit(1);
}
For some reason s = 0, I am not sure it is correct.
Client's side :
char buffer[512 ];
if ( (n = recv(serverSocket, buffer, 512-1,0)) < 0 )
{
perror("recv");
exit(1);
}
data_t * data = malloc (1024);
data->data = malloc(512);
create_data(buffer,data)
and the create_data function :
int create_data( char * buffer, data_t* request)
{
data_t * tmp = (data_t*) buffer;
request->type = tmp->type;
request->block_num = tmp->block_num;
strcpy(request->data, tmp->data);
return 0;
}
Unfortunatly it does not work, it seems the issue comes from strcpy in create_data , but I don't know why.
here is the code for serverSocket creation (server's side)
memset( (char *) &serv_addr,0, sizeof(serv_addr) );
serv_addr.sin_family = PF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(SERV_PORT);
/*
* Ouvrir socket UDP
*/
if ((serverSocket = socket(PF_INET, SOCK_DGRAM, 0)) <0)
{
perror ("erreur socket");
exit (1);
}
ttl=1;
if (setsockopt(serverSocket, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) <0)
{
perror ("setsockopt");
exit (1);
}
if ( bind(serverSocket,(struct sockaddr *) & serv_addr, sizeof(serv_addr) ) < 0 )
{
perror("servecho: erreur bind\n");
exit(1);
}
request = malloc(sizeof(request_t));
....
sendto returns number of bytes sent, so if it is returning 0, no bytes have been sent.
In the server part of code, you seem to have a sendto with destination as server address ? (which has INETADDR_ANY as the address). This could be the reason for malfunction. The procedure should be to have the server do a recvfrom first (after bind) and then respond back with sendto, to same address received in recvfrom.
Here is a good example for Server and Client.
Received data or data to be sent is binary, you cannot use strcpy, strcpy will stop at the first null byte '\0', you need to use memcpy.
Good practice: While allocating memory data_t * data = malloc (1024);, I recommend you use malloc( sizeof(data_t));
Related
The below code is from Git. It joins a multicast group and receives packets.
Here we loop and receive the data in a buffer called msgbuf:
while (1)
{
char msgbuf[MSGBUFSIZE];
const int addrlen = sizeof(addr);
const int nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *) &addr, &addrlen);
How do I choose the size for the buffer msgBuf? Does it just have to be the max packet size? Or do I need to store multiple packets whilst I process the first?
Full code:
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("Command line args should be multicast group and port\n");
printf("(e.g. for SSDP, `listener 239.255.255.250 1900`)\n");
return 1;
}
char* group = argv[1]; // e.g. 239.255.255.250 for SSDP
int port = atoi(argv[2]); // 0 if error, which is an invalid port
if(port <= 0)
{
perror("Invalid port");
return 1;
}
// create what looks like an ordinary UDP socket
//
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
perror("socket");
return 1;
}
// allow multiple sockets to use the same PORT number
//
u_int yes = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*) &yes, sizeof(yes)) < 0)
{
perror("Reusing ADDR failed");
return 1;
}
// set up destination address
//
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY); // differs from sender
addr.sin_port = htons(port);
// bind to receive address
//
if (bind(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
{
perror("bind");
return 1;
}
// use setsockopt() to request that the kernel join a multicast group
//
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(group);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*) &mreq, sizeof(mreq)) < 0)
{
perror("setsockopt");
return 1;
}
// now just enter a read-print loop
//
while (1)
{
char msgbuf[MSGBUFSIZE];
const int addrlen = sizeof(addr);
const int nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *) &addr, &addrlen);
if (nbytes < 0)
{
perror("recvfrom");
return 1;
}
msgbuf[nbytes] = '\0';
puts(msgbuf);
}
return 0;
}
Unlike TCP which combines packets into a stream, UDP respects packet boundaries so recvfrom only gets one packet at a time.
So MSGBUFSIZE only needs to be as big as a single packet. If you're not using jumbo packets that would be 1500, otherwise it would be 9000.
as noted by #Ingo, in this code you should be using:
char msgbuf[MSGBUFSIZE + 1];
the + 1 is because recvfrom can write upto MSGBUFSIZE bytes into the array, and you then write another NUL byte at the end.
as far as choosing a value for MSGBUFSIZE, that would depend on the protocol specification. given that most physical networks would struggle to send more than 1500 bytes without fragmentation something like 2048 might be a reasonable value. you could also check for nbytes == MSGBUFSIZE (maybe also using MSG_TRUNC) and report a "packet truncated" warning, but this basically wouldn't happen for packets routed over the public internet
in response to:
do I need to store multiple packets whilst I process the first?
you'd normally let the network stack take care of that. recv maintains packet/datagram boundaries and hence will always start writing the next packet at the supplied buffer address. again it depends on the protocol how you detect and handle errors, e.g. missing or out-of-order packets, and timeouts
I'm writing a simple network application and I need to craft a UDP packet and send it to a specific host.
int main(void){
// Message to be sent.
char message[] = "This is something";
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
if(sockfd < 0){
perror("Error creating socket");
exit(1);
}
struct sockaddr_in this, other;
this.sin_family = AF_INET;
other.sin_family = AF_INET;
this.sin_port = htons(8080);
other.sin_port = htons(8000);
this.sin_addr.s_addr = INADDR_ANY;
other.sin_addr.s_addr = inet_addr("10.11.4.99");
if(bind(sockfd, (struct sockaddr *)&this, sizeof(this)) < 0){
printf("Bind failed\n");
exit(1);
}
char packet[64] = {0};
struct udphdr *udph = (struct udphdr *) packet;
strcpy(packet + sizeof(struct udphdr), message);
udph->uh_sport = htons(8080);
udph->uh_dport = htons(8000);
udph->uh_ulen = htons(sizeof(struct udphdr) + sizeof(message));
udph->uh_sum = 0;
if(sendto(sockfd, packet, udph->uh_ulen, 0, (struct sockaddr *) &other, sizeof(other)) < 0)
perror("Error");
else
printf("Packet sent successfully\n");
close(sockfd);
return 0;
}
Everything is working fine till the call to sendto(). The sendto() is giving "Bad address". can anyone point me where I'm going wrong? Is there any problem with binding a port to a raw socket?
The code transform the length of the messag (udph->uh_len) to network byte order (htons). This is not needed, as the parameter type of size_t. Only port number (in sockaddr structures) need the htons conversion.
udph->uh_ulen = sizeof(struct udphdr) + sizeof(message);
Current code produce large number (>8000) in uh_ulen, causing the send to fail.
I am writing an educational Man in the Middle application (Linux sockets). What I am struggling with is how to resend TCP/UDP and ICMP packets coming from victim1 to victim2? My approach below seems not to be working:
unsigned char buffer[BUF_SZ];
struct ethhdr *eth_head = (struct ethhdr *)(buffer);
struct sockaddr_ll sock_adr_resnd = {0};
sock_adr_resnd.sll_family = AF_PACKET;
sock_adr_resnd.sll_ifindex = interface_i;
sock_adr_resnd.sll_protocol = htons(ETH_P_ALL);
sock_adr_resnd.sll_halen = MAC_LEN;
memcpy(sock_adr_resnd.sll_addr, source_mac, MAC_LEN); // my MAC
if ((sct = socket(AF_PACKET, SOCK_DGRAM, 0)) < 0) { //recieve all
perror("Socket open error ");
exit (EXIT_FAILURE);
}
if (bind(sct, (struct sockaddr *) &sock_adr_resnd, sizeof(sock_adr_resnd)) < 0) {
printf("Failed to bind socket \n");
}
int res_len = sizeof(sock_adr_resnd);
if (recvfrom(sct, buffer, BUF_SZ, 0, (struct sockaddr*)&sock_adr_resnd, (socklen_t *)&res_len) < 0)
{
process = 0; // nothing accepted
}
// change mac address to actual destination
memcpy(sock_adr.sll_addr, vic_mac1, MAC_LEN);
memcpy(eth_head->h_source, vic_mac1, MAC_LEN);
if (process) {
if (sendto(sct, buffer, BUF_SZ, 0, (struct sockaddr *)&sock_adr_resnd, sizeof(sock_adr_resnd)) < 0)
{
close(sct);
perror("sendto: ");
exit (EXIT_FAILURE);
}
}
What I am confused about is how the socket should be set. Shouldn't it be SOCK_RAW? Does the packet have to be proccessed differently based on type - UDP, TCP, ICMP?
The issue was that it is better to use RAW packets - so u can get the dest/source IP and also when recieving, there is no need to post anything about the source recieveng from, so in the end the changes are like so:
Change SOCK_DGRAM to SOCK_RAW
Pass just buffer to recvfrom() function
State the type of socket when creating it
You don't have to bind
if ((sct = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
perror("Socket open error ");
exit (EXIT_FAILURE);
}
if (recvfrom(sct, buffer, BUF_SZ, 0, NULL, NULL) < 0)
{
process = 0; // nothing accepted
}
I'm attempting to write a TCP socket interface for my program and I'm pulling my hair out with an accept() error (I think). For this I've created some boiled down test code.
First I do a little set up
int server_socket = 0;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
int accepted_connection = 0;
struct sockaddr_in server_address;
server_address.sin_port = htons(9001);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
struct sockaddr_in client_address;
client_address.sin_port = 0;
client_address.sin_family = AF_INET;
char * server_socket_read_buffer[100] = {0};
int server_socket_read_length = 0;
All pretty simple stuff. Just allocate some variables. Next I bind and listen
if (bind(server_socket,(struct sockaddr *)&server_address, sizeof(server_address)) < 0)
{
perror("Bind() on server_socket has failed\n");
}
if (listen(server_socket, 10) < 0)
{
perror("Listen() on server_socket has failed\n");
}
Next is the part where I believe I have my problem
printf("Attempting accept!\n");
if (accepted_connection = accept(server_socket, (struct sockaddr *)NULL, NULL) < 0)
{
perror("Accept failed\n");
}
sleep(10);
if (server_socket_read_length = read(accepted_connection, &server_socket_read_buffer, server_socket_read_length) < 0)
{
perror("Read failed\n");
}
printf("Read %d bytes from socket\n", server_socket_read_length);
for (int i = 0; i<server_socket_read_length;i++)
{
printf("%x\n",server_socket_read_buffer[i]);
}
This compiles and runs. When I use nc with the command 'nc 127.0.0.1 9001' I get a connection, but no data is read. In particular I get 0 bytes of data. I thought this might be due to the NULLs in the accept line, but changing those to a proper struct and length prevent my code from compiling.
If anyone can shed some light on what I'm doing wrong I would be very grateful.
There are a couple of errors:
INADDR_ANY is in host byte order and needs to be converted to network one like htonl(INADDR_ANY). But it does not matter since constant INADDR_ANY is defined as 0.
This
char * server_socket_read_buffer[100]
should be
char server_socket_read_buffer[100]
This
read(accepted_connection, &server_socket_read_buffer, server_socket_read_length)
should be
read(accepted_connection, server_socket_read_buffer, sizeof server_socket_read_buffer)
You are passing in server_socket_read_length = 0 which causes a maximum read length of zero. Pass the buffer size. The declaration of server_socket_read_buffer is incorrect as well. Probably you should allocate a bigger buffer (like 4KB) on the heap.
Also remove the sleep.
The rest is probably working because nc obtains a connection and you are able to accept and read without error.
So after more struggle I found my final answer. The block
if (accepted_connection = accept(server_socket, (struct sockaddr *)NULL, NULL) < 0)
{
//code
}
Doesn't work. My read later on was blocking because accepted_connection wasn't a valid socket. Changing to
(accepted_connection = accept(server_socket, (struct sockaddr *)NULL, NULL);
if accepted_connection < 0)
{
//code
}
resolved my issue. As far as I can gather the file descriptor wasn't being created inline with the if() and reading data from an integer isn't very helpful.
Thanks for the input everyone.
I'm trying to read data from a UDP socket, but after reading the first 255 bytes, read() seems to drop the rest of the data on the socket and block until another data-gram comes in.
Here's the network code I'm using:
int sock;
struct sockaddr_in remote_addr, self_addr;
uint8_t network_init(uint16_t port)
{
memset((char *) &remote_addr, 0, sizeof(remote_addr));
remote_addr.sin_family = AF_INET;
remote_addr.sin_addr.s_addr = inet_addr("192.168.1.22");
remote_addr.sin_port = htons(3001);
memset((char *) &self_addr, 0, sizeof(self_addr));
self_addr.sin_family = AF_INET;
self_addr.sin_addr.s_addr = INADDR_ANY;
self_addr.sin_port = htons(3001);
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
fprintf(stderr, "Could not create socket.");
return 1;
}
else if (bind(sock, (struct sockaddr *) &self_addr, sizeof(self_addr)) != 0)
{
fprintf(stderr, "Could not bind to socket.");
return 1;
}
return 0;
}
void network_send(uint8_t *data, uint8_t len)
{
sendto(sock, data, len, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
}
void read_data()
{
int len = 0;
ioctl(sock, FIONREAD, &len);
// We have data
if (len > 0)
{
char *buffer = (char *) malloc(256);
uint8_t buflen;
printf("==== %d | Data:\n", len);
while (len > 0)
{
buflen = min(255, len);
len = len - buflen;
buffer[buflen] = '\0';
printf("len: %d, buflen: %d,\n",len, buflen);
read(sock, buffer, buflen);
printf("%s\n", buffer);
}
printf("\n");
}
}
Here's the command I'm using to send data:
echo -n '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567' | nc -u localhost 3001
And here's the output:
==== 257 | Data:
len: 2, buflen: 255,
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345
len: 0, buflen: 2,
^C
Also, after performing this read, ioctl(sock, FIONREAD, &len); produces a length result of 0. My suspicion is that for some reason, read() is clearing out the rest of the data before it has a chance to be read, but I can't seem to find any reference to this behaviour in any documentation.
I'm developing on an Ubuntu linux machine (x86_64).
With UDP sockets, each call to read() reads a whole datagram out of the kernel. If the read buffer isn't big enough for the entire datagram, the rest of it will be discarded. It's not like a stream socket, where you can keep calling until you get everything.
Since FIONREAD tells you the number of bytes in the message, you should use that as the size to malloc() rather than using 256:
if (len > 0) {
char *buffer = malloc(len);
...
P.S. Do I cast the result of malloc?