Google Answers Logo
View Question
 
Q: A Problem Extend From My Daemon-Problem. ( No Answer,   0 Comments )
Question  
Subject: A Problem Extend From My Daemon-Problem.
Category: Computers > Programming
Asked by: adamhsu-ga
List Price: $10.00
Posted: 18 Sep 2003 00:12 PDT
Expires: 18 Sep 2003 23:42 PDT
Question ID: 257924
I ever ask a question about daemon.
The Title is
"How can I get the ip of a machine that connect to my machine(daemon)"

I think the answer is correct after I read the answer.
But I can't get the same result with the same code on my solaris 5.8.
When I telnet 127.0.0.1 at port = 9999,I always get the message => "IP
is 0.0.0.0".
I can't get a message like "IP is 127.0.0.1".

Now,I want to know how to let the server response a message like "IP
is 192.168.166.11",
when I telnet to the server.

example : 
	The Solaris' ip is 192.168.166.123 And The port is 9999
	My ip is 192.168.166.11	
	At my machine,I use "telnet 192.168.166.123 9999"
	I wish the solaris server can response the message "IP is
192.168.166.11"

I supply the code for the daemon program

---------------------------------------------------------------------------------------------------------------------
Other information for this.
After I read the answer,I can't see any question about it.
So,I think "The code is correct,But I ignore something then let the
program can't get the ip of the peer".
And If you want to know why I ask the problem,you can see the
question-answer.
"How can I get the ip of a machine that connect to my
machine(daemon)",
Question ID: 254937 

---------------------------------------------------------------------------------------------------------------------
the following is the sample code(I have modify one line for
sun-solaris).
// Sample Code
#include <stdio.h>   
#include <string.h>   
#include <strings.h>   
#include <stdlib.h>   
#include <sys/socket.h> 
#include <netinet/in.h> 
 
struct sockaddr_in mysock; 
socklen_t namelen; // <= I change int type to socklen_t type for
sun-solaris
 
int main(void)   
{   
 char ap_command[1024];   
 int errcode; 
 char myname[]="mydaemon"; 
 
 namelen = sizeof(struct sockaddr_in); 
 if (getpeername(0, (struct sockaddr *)&mysock, &namelen)) { 
   fprintf(stderr, "%s : Failed to get peer name\n", myname); 
   perror(myname); 
 } 
 else 
   fprintf(stdout, "IP is %d.%d.%d.%d\n", 
    (mysock.sin_addr.s_addr>>24) & 255, 
    (mysock.sin_addr.s_addr>>16) & 255, 
    (mysock.sin_addr.s_addr>>8) & 255, 
    mysock.sin_addr.s_addr & 255); 
      
 memset(ap_command,'\0',1024);    
 setvbuf(stdout,NULL,_IOLBF,0);    
 while(fgets(ap_command,sizeof(ap_command),stdin) != NULL)   
 {   
  printf("ECHO:%s\n\r",ap_command);   
  memset(ap_command,'\0',1024);   
 }   
 return(0);   
}

---------------------------------------------------------------------------------------------------------------------

Clarification of Question by adamhsu-ga on 18 Sep 2003 03:34 PDT
Actually,at Solaris:
1. inetd call accept and then return the socket's descriptor and the
ip-address of the peer.
2. then call fork and create the child-process of the inetd.
   Since the child-process can see his father's socket,so the
child-process also can use the socket's descriptor .
3. when the child-process call exec to run the real server.The memory
of the real server's process will replace the memory of the
child-process.he sockethe server process to tNow,we lose the
ip-address of the peer.But the socket that have connected alaways open
after exec.
4. for my daemon-program,I want to know how to get the socket's
descriptor that we lose.
5. someone tell me the following things:
   "Getpeername is called using file 0(stdin) as its first parameter.
This is because inetd connect I/O to the socket"
   I think what he say is correct.

Request for Question Clarification by maniac-ga on 18 Sep 2003 19:31 PDT
Hello Adamhsu,

I would be glad to answer your question without further payment. To do
that, I suggest you
 - please close this question
 - make a request for clarification at
  http://answers.google.com/answers/main?cmd=threadview&id=254937
so I can respond without further fee.

The fix may be as simple as replacing the fprintf statement with
  fprintf(stdout, "IP is %s\n", inet_ntoa(mysock.sin_addr);
but if that does not work - I have some other suggestions that can be
made.

  --Maniac
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy