C Sendto doesn't work when doing it repeatedly - c

I'm facing a weird problem when using sendto() in C. I have this code:
struct sockaddr_in currSocket;
int currSocketLength = sizeof(currSocket);
memset((char *)&currSocket, 0, sizeof(currSocket));
currSocket.sin_family = AF_INET;
currSocket.sin_port = htons(serverPortNumber);
// add the server IP address
inet_aton(serverIP, &currSocket.sin_addr);
int send_result = sendto(masterSocket, bufferToSend , 1000, 0, (struct sockaddr *) &currSocket, currSocketLength);
if (send_result < 0) {
perror("Something is wrong");
}
bzero(bufferToSend, 1000);
This works great when I send data the first time in bufferToSend. However, if I want to send data again the exact same way (following the code above - create the socket and send it there), it doesn't get received. Why is this and how can I fix it?
Edit:
After looking into it some more, I've realized it's because my IP address string is wrong because it's being overwritten. It seems to have it's value change after I do a previous read.
// here the IP address is fine
printf("%s\n", ipAddress);
char bufferFromSocket[512];
int amount_read = read(masterSocket, bufferFromSocket, 512);
// here the IP address is overwritten by whatever is in bufferFromSocket
printf("%s\n", ipAddress);
Any ideas why this could be happening?

I hadn't allocated enough space for ipAddress. Had it as a pointer, when it should have been a char[] and I copy to it using strcpy.

Related

Raw socket programming - Why does printf() affect the packet sending?

Ok, it is a very weird problem. I was trying to create a raw socket ICMP packet to spoof the ping request.
int s;
s = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
And then
int one; // I should initialize it as 1, but I didn't.
const int *val = &one;
setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof(one));
....
It turns out that since I didn't initialize one as 1, the spoofed client cannot receive the ping reply. However, when I add a
unsigned char *ch = (unsigned char *)spoof;
just before the
close(s);,
it turns out that the spoofed client can receive the ping reply.
Why is that?
When you fail to initialize automatic storage, the value it gets depends on what it was last used for by your program or even the previous program that ran in the same VM space. Consequently, anything can happen. Adding the line of code just caused a different alignment of the one value on the stack. That junk in that variable in its new location allowed the raw socket to work. The other didn't. It was luck.

how to connect to a bit torrent tracker in c

I am tying to connect to a bit torrent tracker, http://tracker.thepiratebay.org. The gethostbyname() keeps returning null, how should I fix this? Also do you see anything else wrong with this code?
int sock;
struct sockaddr_in servAddr;
int portNum = 80;
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){
printf("fail create socket");
return 0;
}
char *path = "http://tracker.thepiratebay.org/";
struct hostent *hp = gethostbyname(path);
if(hp==NULL){
printf("null");
else{
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
memcpy( (char *) &servAddr.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length );
servAddr.sin_port = htons(portNum);
}
//send request to tracker server
if (send(sock, requestToSend, strlen(requestToSend), 0) != strlen(requestToSend)){
printf("fail send");
return 0;
}
The problem here is that http://tracker.thepiratebay.org/ is a URL, but gethostbyname() expects just the host name. The host name is tracker.thepiratebay.org.
It would make it much easier if you just use libcurl, which will handle all of that HTTP stuff for you. It is extremely common to use libcurl in applications that connect to HTTP servers; it is an excellent library. It's certainly easier than socket programming.
Use getaddrinfo()
The modern alternative to gethostbyname() is getaddrinfo(). It's not that gethostbyname() doesn't do what you want, rather, getaddrinfo() is simply better in every conceivable way.
struct addrinfo hint, *ap;
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
int r = getaddrinfo("tracker.thepiratebay.org", "http", &hint, &ap);
This will not only get you the address for the host you want, but it will also fill in the port number. You can use "http" as the port, or you can use "80" for the port, they are the same thing (as long as /etc/services has the right entry).
Other problems
This line is wrong.
memcpy( (char *) &servAddr.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length );
You don't know that gethostbyname() returned an IPv4 address, and it is foolish to try and copy it into a struct sockaddr_in. If gethostbyname() returned an IPv6 address, you have just smashed your stack and your program will crash -- or worse, it might not crash.
Either check that it returns an IPv4 address, or simply copy hp->h_addr into a generic struct sockaddr that was returned from malloc(hp->h_length). This is a bit ugly but it's the way it goes.
Finally, it is wrong to cast the arguments to memcpy(). It's not an error, but it's wrong. Don't do it, it can cause otherwise legitimate compiler errors to be suppressed, e.g., if you accidentally cast an int to char *.

C UDP networking, extract numbers from datagram message

I am developing a network software as a part of an university exam. The software is almost finished, but actually I am finishing the concurrent part (with fork()).
My needs are to exchange between client and server these two messages as handshake.
Here an example:
PING:3506:DOWNLOAD
PONG:5605
Here my way to handle these messages:
On client side, that is the host who sends PING:3506:DOWNLOAD, I wrote
int *childLocalPort;
childLocalPort = malloc(sizeof(int));
childLocalPort[0] = (SERV_PORT_OFFSET + getPort(&portArray, &pidArray, &arrayCounter, cpid));
char *pingProcedureString;
pingProcedureString = malloc(30*sizeof(char));
strcpy(pingProcedureString, "PING:");
char *itoaPortBuffer;
itoaPortBuffer = malloc(6*sizeof(char));
itoa((childLocalPort[0]), itoaPortBuffer, 10);
strcat(pingProcedureString, itoaPortBuffer);
strcat(pingProcedureString, ":DOWNLOAD");
if (sendto(sockfd, pingProcedureString, strlen(pingProcedureString), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("errore in sendto1");
exit(1);
}
free(itoaPortBuffer);
free(pingProcedureString);
n = recvfrom(sockfd, buff, MAXLINE, 0, NULL, NULL);
buff[n] = 0;
if(strcmp(buff,"PONG"))
{
int *childRemotePort;
childRemotePort = malloc(sizeof(int));
strtok(buff, ":");
childRemotePort[0] = ntohs(strtok(NULL, ":"));
printf("Remote port is %d\n", childRemotePort[0]);
close(pipeLocalPort[0]); /* Close unused read end */
write(pipeLocalPort[1], childLocalPort, sizeof(int)
close(pipeLocalPort[1]); /* Reader will see EOF */
close(pipeRemotePort[0]);
write(pipeRemotePort[1], childRemotePort, sizeof(int));
close(pipeRemotePort[1]);
}
On server side, that is the host who sends PONG:5605, I wrote
if ((n > 0) && strcmp(recvline,"PING"))
{
int *childRemotePort;
childRemotePort = malloc(sizeof(int));
strtok(recvline, ":");
char *buffTemp;
buffTemp = calloc(5, sizeof(char));
strcpy(buffTemp,strtok(NULL, ":"));
childRemotePort[0] = ntohs(atoi(buffTemp));
strtok(recvline, ":");
printf("Remote child client port is: %d\n", childRemotePort[0]);
}
As you can notice, the PONG part is missing, because I would like to focus on first non working part. The server receives correctly (as I can see from Wireshark) the message PING:3506:DOWNLOAD, but he tells me he received 19476 instead of 3506, and this is not true.
I also noticed that if I try to send numeric messages without converting them into network byte order, things get worse. I am fighting with this from many days, and I don't know anymore what to think about.
when you'r sending a PING from client , the client fails to do a ntohs on the integer port before converting to ascii but while receiving at server you do a ntohs , i think this is causing the error. Doing something on these lines on the client PING side might help however this is still error prone if the endianness of server and client is different.
char *itoaPortBuffer;
itoaPortBuffer = malloc(6*sizeof(char));
itoa(ntohs(childLocalPort[0]), itoaPortBuffer, 10);
strcat(pingProcedureString, itoaPortBuffer);
strcat(pingProcedureString, ":DOWNLOAD");

Invalid argument when using sendto

I am trying to send a buffer via UDP sockets in C but I always get an invalid argument error in sendto. I just don't find the error. Could anyone maybe help me. Thanks in advance.
Here's my code:
/**/ void IPCSend(char *pazClientAddress, int iClientPort, char *pazBuffer )
{
int iSocket;
/* */
if ((iSocket = socket(AF_INET, SOCK_DGRAM, 0)) != -1)
{
int iSendToReturn;
struct sockaddr_in sinServerAddress;
struct hostent *pstHost;
/* */
pstHost = (struct hostent *)gethostbyname((char *)pazClientAddress);
/* */
sinServerAddress.sin_family = AF_INET;
sinServerAddress.sin_addr = *((struct in_addr *)pstHost->h_addr);
sinServerAddress.sin_port = iPortNumber;
bzero(&(sinServerAddress.sin_zero),8);
/* */
fprintf(stdout,"sending '%s' to client '%s:%d'\n",pazBuffer,pazClientAddress,iClientPort);
iSendToReturn = sendto(iSocket, pazBuffer, sizeof(pazBuffer), 0, (struct sockaddr *)&sinServerAddress, sizeof(sinServerAddress));
/* */
if(iSendToReturn != -1)
fprintf(stdout,"%d bytes sent\n",iSendToReturn);
else
perror("SendTo");
/* */
close(iSocket);
}
else
fprintf(stdout,"could not connect to server\n");
}
Firstly, you're making the perennial error of novice C programmers: using sizeof to get the size of a pointer. The variable pazBuffer is a pointer, not an array, so the sizeof operator will not return the array length, which is what you want. Your IPCSend function needs to take in the length of pazBuffer as a separate parameter:
void IPCSend(char *pazClientAddress, int iClientPort, char *pazBuffer, size_t len)
As for the error you're getting - it might be related to something else. Invalid argument means that one of the parameters you're passing to sendto is somehow invalid. Since I see that you are properly initializing the socket descriptor, the problem might be that the send buffer is somehow not valid - possibly a null pointer or bad address. This means that the problem is not apparent from the code you posted, and is likely in some code that is calling IPCSend.

Passing structures using UDP

I have been trying to send and receive structures on the same machine using UDP and the server and client in this case run on the same machine and share common structure definitions (using a header file).
Hostent structure defn(UNIX built-in type) :
struct hostent{
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
}
Server Code snippet follows :
struct hostent* resolved_host = DNS_translate(DNSname);
if((numbytes = sendto(sockfd, (void*)&resolved_host, sizeof(struct hostent), 0, (struct sockaddr *)&client_addr, sizeof(struct sockaddr))) == -1)
{
perror("sendto failed");
exit(EXIT_FAILURE);
}
Client Code snippet follows:
struct hostent resolved_host;
int addr_len = sizeof(struct sockaddr);
if((numbytes = recvfrom(sockfd, (void*)&resolved_host, sizeof(struct hostent), 0, (struct sockaddr *)&server_addr, &addr_len)) == -1)
{
perror("recvfrom failed");
exit(EXIT_FAILURE);
}
The server sends and the client receives as normal (no error raised).
The *resolved_host* structure is filled in the server and all its data can be accessed with no problem.
However, if I now try to use the *resolved_host* structure in the client, I get a seg fault. For example:
printf("Name : %s\n", resolved_host.h_name);
raises a seg fault. (but works in the server)
Your struct is full of pointers. When you send it over the network, you send the actual addresses, not the data pointed to by those pointers.
Those addresses are invalid in the target process.
You will need to serialize the data yourself. See for examples:
Serialization/Deserialization of a struct to a char* in C
Serialization techniques
The structure contains pointers - so when you copy the structure over UDP you're only copying the values of those pointers (i.e. the addresses of some other pieces of data) and not the actual data itself.
When you receive those pointers in the server they no longer mean anything - those pointer addresses are meaningless to the other program.
You are sending pointers. Even on the same machine these are not valid in different address spaces.

Resources