LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2008, 02:49 PM   #1
esmeco
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Rep: Reputation: 0
Help needed linux socket programming in c


Good evening everyone!

I'm doing a small client / server application for sharing files in C, and I am trying to implement the following:

The client of my application sends to the address 255.255.255.255 a message requesting a particular file.In the network there is only one server, however, there may be multiple clients.
The server responds with an ACK if it has the file specified by the client.Until now,everythings' allright.
The question arises now,as I want the client, after receiving the confirmation (ACK) from the server, making sure that the reply message comes from the server or if the message is of uknown origin.As so,I have the following code in Client:

Code:
 ...
msg_tam = recvfrom (socketfd, buffer, BUFFERSIZE, 0, (struct sockaddr *) & serv_addr, & Nbytes);

     if (msg_tam <0)
      Abort ("...");

     buffer [msg_tam] = 0;

    if (SERV_HOST_PORT == ntohs (serv_addr.sin_port) & & strcmp (SERV_HOST_ADDR, (char *) inet_ntoa (serv_addr.sin_addr) == 0)
(
...
)
else
     printf ( "message of unknown origin!");
The SERV_HOST_ADDR is set to "255.255.255.255" so the client can send the file request to the network and the port is also set with a value.Well, the problem arises when the call to the function recvfrom is made, where the ip address of who sent the message is filled in the structure.That address is ,thus, the address where the server is.
As so, when making the comparison between the SERV_HOST_ADDR in the if statement with the address serv_addr.sin_addr filled in the recvfrom call, they will never be the same (even if the reply has come from the server) ,as a it will always enter the else statement.

I would appreciate if someone could help me solve this problem, so that it would be a way the client could check you check whether the reply comes from the server where the client sent the message or from another place.

Thanks in advance for your help!
 
Old 10-27-2008, 04:54 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Well, in a normal situation (and I assume this is your case), 255.255.255.255 is a broadcast address and it cannot be assigned to any host. It means that the server has a different IP and that's the one which is in serv_addr. And it will never be that address. You need to find out the server's address somehow.

Also, when it comes to "making sure", everyone can send a message with any source address (if raw sockets are available, but still...). So if it's important, you need to use a different method to check if it was really from the server (in fact, some cryptographic stuff fits here very well...). If you want to know the details, just let me know.
 
Old 10-27-2008, 05:57 PM   #3
esmeco
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks for the reply!
I really much would like to know how it is done,as I tried the code below
but it didn't work!

Code:
if (host->h_addr_list == NULL)
        printf("No h_addr_list present.\n");
    else
    {
        printf("h_addr_list:\n");
        for (i = 0; he->h_addr_list[i] != 0; i++) 
        {
            memcpy(&addr, he->h_addr_list[i], sizeof(struct in_addr));
            printf("  Addr #%d: %s\n",i,inet_ntoa(addr));
        }
    }
What I wanted to achieve with this code was to obtain all the adresses available,but only two appear in this list which are 127.0.0.1 and 127.0.1.1,but no sign of the 192.xxx.xxx.xxx that is my ethernet address,and also my serv_addr adress!
 
Old 10-28-2008, 06:08 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
But which function do you use to get the address? It's not a piece from the same fragment as before... So, do you want to obtain the addres on the server or get the right one from the message?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LINUX C an socket programming hasek Programming 5 06-12-2005 10:34 AM
Linux Socket Programming source code saied Programming 4 05-24-2005 02:18 AM
socket programming in linux ray5_83 Programming 4 11-18-2004 01:06 AM
Linux Socket Programming velan Programming 4 06-24-2004 04:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration