I'm writing a function that is supposed to do some operations and then return (using its arguments) the address of the device that is interacting with (i.e. that used sendto) the recvfrom inside the function.
Here's how I call the function, instantiating cliaddr before.
struct sockaddr_in cliaddr;
memset((void *) &cliaddr, 0, sizeof(cliaddr));
rcv_string(sockfd, &return_string, &cliaddr);
// Here I'll need to use cliaddr, that's why I need it outside too
Here's the implementation of the function:
int rcv_string(int sockfd, char **return_string, struct sockaddr_in *sndaddr) {
// ...
char buff[PACKETSZ + 2];
memset(&buff, 0, sizeof(buff)); // Clean buffer
socklen_t *plen = malloc(sizeof(struct sockaddr *));
if ((recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr *) sndaddr, plen)) < 0) {
perror("recvfrom");
return -1;
}
char *snd_ip = malloc(INET_ADDRSTRLEN * sizeof(char));
if (inet_ntop(AF_INET, &((*sndaddr).sin_addr.s_addr), snd_ip, INET_ADDRSTRLEN * sizeof(char)) == NULL) {
perror("inet_ntop");
return -1;
}
printf("Received '%s' from '%s'.\n", buff, snd_ip);
// ...
}
Now, even if the ip address of the sending device is 192.168.1.251 I get the following output:
Received '0packetx' from '0.0.0.0'.
The received buffer is formatted correctly, but the address is evidently wrong. Why? Does it have to do with the definition of the address variable outside the function?
EDIT
If after the memset of cliaddr I add also those 3 lines:
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliaddr.sin_port = htons(SERV_PORT);
I get a random behavior. Sometimes I get 0.0.0.0, sometimes 127.0.0.1 and sometimes the correct address (192.168.1.251).
you are passing the wrong length to recvfrom
socklen_t *plen = malloc(sizeof(struct sockaddr *));
if ((recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr *) sndaddr, plen))
Why you malloc is a mystery, but you need
socklen_t *plen = malloc(sizeof(socklen_t));
*plen = sizeof(struct sockaddr_in );
much simpler is
socklen_t plen = sizeof(struct sockaddr_in);
if ((recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr *) sndaddr, &plen))
It should work if you fill plen with valid sizeof data.
*plen = sizeof(struct sockaddr_in);
You can put that right before the recvfrom call.
Related
I'm currently setting up a UDP socket for a school assignment but I can't figure out how to properly send sockaddr as a parameter into a function. The input arguments for SendData is defined the same way they are in the sendto function.
void SendData(int fileDescriptor, const struct sockaddr_in *destAddrUdp, unsigned char buffer[MAXMSG])
{
/* crc */
int strlength = strlen(buffer);
unsigned char SCRC = CRC(buffer, strlength-1);
buffer[strlength-1] = SCRC;
buffer[strlength] = '\0';
/* send */
if (sendto(fileDescriptor, buffer, strlen(buffer), 0, (struct sockaddr*) &destAddrUdp,sizeof(destAddrUdp)) < 0) {
fprintf(stderr, "Could not send data\n");
}
} `
When the below code is called from main it works however when it's called from SendData the return value from sendto is -1.
if (sendto(fileDescriptor, buffer, strlen(buffer), 0, (struct sockaddr*) &destAddrUdp,sizeof(destAddrUdp)) < 0) {
fprintf(stderr, "Could not send data\n");
}
If the program is compiled I get warning: passing argument 5 of ‘recvfrom’ from incompatible pointer type. on the sendto functioncall in SendData.
The function call for SendData in my main program is: SendData(sockfd, (struct sockaddr *) &destAddrUdp, buffer);
The main program in case it is relevant:
unsigned char SCRC;
unsigned char strlength;
unsigned char buffer[MAXMSG-5];
unsigned char header[MAXMSG];
struct rtp_struct *sendstruct;
/* option 2 */
struct sockaddr_in destAddrUdp;
int sockfd;
char dstHost[15];
printf("input host IP:\n>");
fgets(dstHost, 15, stdin);
/* Create socket */
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
fprintf(stderr, "Can't create UDP socket\n");
/* set configurations */
memset (&destAddrUdp, 0, sizeof(destAddrUdp)); //set zero
destAddrUdp.sin_family = AF_INET; //internet use
destAddrUdp.sin_addr.s_addr = inet_addr(dstHost); //set so anyone can connect
destAddrUdp.sin_port = htons(dstUdpPort); //set the port to use
/* Generate CRC table */
GenerateCRCTable();
/* get string and send */
while(1) {
printf("\n>");
fgets(buffer, MAXMSG, stdin);
if(strncmp(buffer,"quit\n",MAXMSG) != 0){ //if read string == quit, see else
strlength = strlen(buffer);
SCRC = CRC(buffer, strlength-1);
buffer[strlength-1] = SCRC;
buffer[strlength] = '\0';
SendData(sockfd, (struct sockaddr *) &destAddrUdp, buffer);
/*if (sendto(sockfd, buffer, strlength, 0, (struct sockaddr *) &destAddrUdp,sizeof(destAddrUdp)) < 0){
fprintf(stderr, "Could not send data\n");}*/
}
else {
close(sockfd);
exit(EXIT_SUCCESS);
}
}
}
In your SendData function, the parameter destAddrUdp has type const struct sockaddr_in *. When you then make this call:
sendto(fileDescriptor, buffer, strlen(buffer), 0,
(struct sockaddr*) &destAddrUdp, sizeof(destAddrUdp)
The expression &destAddrUdp has type const struct sockaddr_in **, so the pointer type is incompatible. Also, sizeof(destAddrUdp) is returning the size of a pointer, not the size of the struct.
You failed to account for the differing types of destAddrUdp in SendData and main. The correct call to sendto would be:
sendto(fileDescriptor, buffer, strlen(buffer), 0,
(struct sockaddr*) destAddrUdp, sizeof(*destAddrUdp)
The problem is the sizeof(destAddrUdp) and the (struct sockaddr*) &destAddrUdp.
In your main, destAddrUdp is an struct sockaddr_in, in your function however, its a sockaddr_in* so you can't use them equally. First, sizeof(destAddrUdp) in your function will give you the size of a pointer, second, &destAddrUdp in your function will give you a destAddrUdp**. Thats not what you want.
Try sendto(fileDescriptor, buffer, strlen(buffer), 0, (struct sockaddr*) destAddrUdp,sizeof(*destAddrUdp))
I've declared a pointer to the following struct in my main function
struct sockaddr_in* server;
I'm using this struct to a function that returns a socket descriptor bound to this struct.
int openSocket(char* ip_addr, int port, struct sockaddr_in* server){
int sockfd, len;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0){
perror("Failed to open socket: ");
exit(-1);
}
len = sizeof(server);
bzero(&server, len);
server->sin_family= AF_INET;
inet_aton(ip_addr, &server->sin_addr);
server->sin_port= htons(port);
if((bind(sockfd, (struct sockaddr*)&server, len)) < 0){
perror("Bind failed: ");
exit(-1);
}
return sockfd;
}
However when I try to read the individual fields in the struct that was passed into the function, I get a seg fault. That is when I run the following code in my main
sockfd = openSocket(vector->ip_addr, vector->port, server);
printf("%s %d\n", inet_ntoa(server->sin_addr), htons(server->sin_port) );
The values being passed into the openSocket function are correct.
I get a segmentation fault. Any help appreciated.
After I looked through your codes again, I found there are three mistakes.
In main(), you just declared one pointer to struct sockaddr_in, but you didn't assign it, you can define struct sockaddr_in server and pass &server to openSocket().
In openSocket, "server" is declared to pointer, but your statements len = sizeof(server); bzero(&server, len); are not correct, this is where your segmentation violation occurs.
In openSocket(), bind(sockfd, (struct sockaddr*)&server, len) is not correct, you don't need to use &server, just use server.
So you should change your code as below:
len = sizeof(server); ----> len = sizeof(struct sockaddr_in)
bzero(&server, len); ----> bzero(server, len);
if((bind(sockfd, (struct sockaddr*)&server, len)) < 0){ ----->
if((bind(sockfd, (struct sockaddr*)server, len)) < 0){
struct sockaddr_in* server;
sockfd = openSocket(vector->ip_addr, vector->port, server);
printf("%s %d\n", inet_ntoa(server->sin_addr), htons(server->sin_port) );
---->
struct sockaddr_in server;
sockfd = openSocket(vector->ip_addr, vector->port, &server);
printf("%s %d\n", inet_ntoa(server.sin_addr), htons(server.sin_port) );
You just declared "server" as a pointer to struct sockaddr_in, but you didn't assign it. You can do it like below:
struct sockaddr_in server;
sockfd = openSocket(vector->ip_addr, vector->port, &server);
printf("%s %d\n", inet_ntoa(server.sin_addr), htons(server.sin_port) );
bind(sockfd, (struct sockaddr*)&server, len)
The problem is here. server is already a pointer. You should not take its address.
bind(sockfd, (struct sockaddr*)server, len)
And, as pointed out by #nos, you need to initialize it somewhere. There's no reason for the static variable to be a pointer. It can just be a struct sockaddr_in.
Right now I'm just trying to get my "server" to reply back immediately after receiving a UDP datagram(in preparation for more complicated stuff).
However, I keep getting a Invalid Argument error on the sendTo (at RunTime). Can anyone help point out why? I have written other sendTo's and looked at sample code on the Internet and cannot pinpoint why.
The recvfrom is working fine.
Thanks!
void *receive(void *socket)
{
int* socket_ptr = (int *) socket;
int socket_desc = *socket_ptr;
int recv_length;
socklen_t addrlen = sizeof (struct sockaddr_in);
while(1){
struct Message msg;
struct sockaddr_in * incoming = malloc (sizeof (struct sockaddr_in));
if ((recv_length = recvfrom (socket_desc, &msg, sizeof(msg), 0,
(struct sockaddr *) incoming, &addrlen))
== -1)
{
}else
{
struct CoordMessage * cm = malloc(sizeof (struct CoordMessage));
cm->msgID = BEGINSUCCESS;
cm->tid = msg.tid;
if (sendto(socket_desc, cm, sizeof(struct CoordMessage), 0, (struct sockaddr *) incoming, sizeof(incoming)) < 0){
perror("TEMP Send failed ");
exit(1);
}
}
}
}
sizeof(incoming) in the sendto call is just the size of a pointer, you need to pass the sizeof the struct that you got from the recvfrom call. Also the malloc is causing a memory leak, because you never free the memory.
I suggest declaring the struct as
struct sockaddr_in incoming = {0};
and then passing the address of the struct to recvfrom and sendto, e.g.
if ((recv_length = recvfrom (socket_desc, &msg, sizeof(msg), 0,
(struct sockaddr *) &incoming, &addrlen))
== -1)
and
if (sendto(socket_desc, cm, sizeof(struct CoordMessage), 0, (struct sockaddr *) &incoming, addrlen) < 0)
The same thing applies to the cm variable, if you malloc it, you need to free it. But you could just declare it, and pass the address.
I have an odd problem with a UDP server I'm working on. The very first udp packet received has no information on the source of the packet. Subsequent udp packets all appear to be fine and correctly display the ip address from which the packet was received. I have no clue what is causing this behavior, probably some stupid mistake, or some obscure bug. I'm using on a Linux machine running Debian.
fd_set master;
fd_set read_fds;
int fdmax;
int i;
int bytes_sent;
int bytes_recv;
socklen_t addr_len;
struct sockaddr_storage their_addr;
// provides users information needed to connect
serv_info *server_info;
server_info = (serv_info*) serv_config;
// Create UDP listener socket
int info_sock = createDGRAMSocket(NULL, server_info->port, 1);
char buffer[1024];
int len;
int send_response;
FD_SET(info_sock, &master);
fdmax = info_sock;
bytes_recv = recvfrom(i, buffer, sizeof(buffer), 0, (struct sockaddr *)&their_addr, &addr_len);
printf("Info started \n");
while (running) {
read_fds = master;
select(fdmax+1, &read_fds, NULL, NULL, NULL);
for (i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) {
bytes_recv = recvfrom(i, buffer, sizeof(buffer), 0, (struct sockaddr *)&their_addr, &addr_len);
printf("length %u: %s\n", bytes_recv, buffer);
send_response = 0;
switch (buffer[0]) {
// Handle different packet types
}
struct sockaddr_in *sin = (struct sockaddr_in *)&their_addr;
unsigned char *ip = (unsigned char *)&sin->sin_addr.s_addr;
printf("IP: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
if (send_response) {
bytes_sent = sendto(info_sock, buffer, len, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr_storage));
if (bytes_sent < 0) {
printf("[ERROR] Packet Send Failed %d (%s) %d\n", bytes_sent, buffer, len);
}
}
}
}
};
close(info_sock);
You need to initialize addr_len to sizeof(their_addr). According to the man page:
The argument addrlen is a value-result argument, which the caller should initialize
before the call to the size of the buffer associated with src_addr, and
modified on return to indicate the actual size of the source address. The returned
address is truncated if the buffer provided is too small; in this case, addrlen will
return a value greater than was supplied to the call.
Since you aren't initializing addr_len it seems to be taking on a value of 0 (this is highly undefined behavior). In this case, recvfrom() will not fill in the their_addr buffer, but as the man page indicates addr_len will return a value greater than was supplied to the call. So after the first call addr_len is taking on a value that is allowing the next calls to recvfrom() to properly fill in the their_addr buffer. Relying on this is unsafe though.
Hi I have programmed linux daemon who sends files in udp packets.
problem is that in string "abc\0asdf" it sends only abc not null character and asdf (all characters after null symbol),
there is udp client code, which send packets:
int sock;
struct sockaddr_in server_addr;
struct hostent *host;
host= (struct hostent *) gethostbyname((char *)ip);
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
memset(server_addr.sin_zero,0,8);
and code which send buffer:
if (sendto(sock, buf, sizeof buf, 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))==-1)
in serverside I need to receive binary buffer:
defining socket code:
int sock;
int addr_len, bytes_read;
struct sockaddr_in server_addr , client_addr;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = INADDR_ANY;
//bzero(&(server_addr.sin_zero),8);
memset(server_addr.sin_zero,0,8);
if (bind(sock,(struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1){
perror("Bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
diep("sendto()");
and receive buffer (in big loop):
bytes_read = recvfrom(sock,buf,sizeof (buf),0,
(struct sockaddr *)&client_addr, &addr_len);
does anyone know why I didn't receive full buffer?
By looking at the comments, the error is most likely that you treat the received buffer as a string.
If you want to print/output the buffer, you need to convert the null character into something else first.
You should use a for loop to print your received buffer instead of printf:
for (int i=0; i<bytes_read; i++)
printf("%c",buf[i]);
This is incorrect (formatting changed so it fits on a screen for me):
if (sendto(sock,
buf,
sizeof buf,
0,
(struct sockaddr *)&server_addr,
sizeof(struct sockaddr))==-1)
You want sizeof(server_addr) as the length. This will be larger than sizeof(struct sockaddr).
Also, from the manpage:
Return Value
On success, these calls return the number of characters sent. On error, -1 is returned, and errno is set appropriately.
You haven't accounted for the case where it returns some value less than sizeof(buf). Not sure how that can happen but it seems to be something to handle.
My comment on the overall approach is similar to what #jgauffin says. buf is just bytes. It's only a convention for C strings that '\0' terminates them, not a requirement. Typically when using binary byte buffers you also track the size. You're just assuming that all of sizeof(buf) will be used which doesn't make sense. (Suggestion: Perhaps part of your sendto payload should include the size of the message that follows?)