M
Mike King
I think I'm dealing with a bug in a library or some weird problem. The
following code compiles and works as I'd expect on Linux and cygwin, but on
the machine that matters (HP PA64, HPUX 11.11) it identifies the peer as
0.0.0.0 in both the accept() and getpeername() return vals, also identifies
the peer port as 0. I'm using gcc 4.1.2, downloaded from HP's site,
compiling with 'gcc sockprob.c -o sockprob', then running it with
'./sockprob.c MY.I.P.ADD 7890', then connect to the server socket with
'telnet MY.I.P.ADD 7890'. Cygwin and Linux show the ip and port of the
connector, but HPUX is showing 0.0.0.0. I run a 'netstat -n' and it shows
the correct ip/port for the server and client. I just don't get it. Does
anyone have any advice?
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
char* ipstring;
int port1;
void parseargs(int, char**);
int main(int argc, char* args[])
{ struct sockaddr_in adr1; /* address for the server */
int ss1, as1; /* file descriptors for server socket, active
socket */
struct sockaddr_in from1; /* address for peer */
socklen_t from1_len;
parseargs(argc, args);
if((ss1 = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{ perror("socket failed");
return 1;
}
memset(&adr1, 0, sizeof(adr1));
adr1.sin_family = AF_INET;
adr1.sin_port = htons(port1);
if(!inet_aton(ipstring, (struct in_addr*)&(adr1.sin_addr)))
{ perror("inet_aton failed");
return 1;
}
if(bind(ss1, (struct sockaddr*) &adr1, sizeof(adr1)))
{ perror("bind failed");
return 1;
}
if(listen(ss1, 1) < 0)
{ perror("listen failed");
return 1;
}
from1_len = sizeof(from1);
memset(&from1, 0, sizeof(from1));
from1.sin_family = AF_INET;
if((as1 = accept(ss1, (struct sockaddr*) &from1, &from1_len)) == -1)
{ perror("Error accepting");
return 1;
}
printf("peer address from accept() is %s %d\n",
inet_ntoa(from1.sin_addr), ntohs(from1.sin_port));
if(getpeername(as1, (struct sockaddr*) &from1, &from1_len))
{ perror("getpeername failed");
return 1;
}
printf("peer add/port from getpeername() is %s %d\n",
inet_ntoa(from1.sin_addr), ntohs(from1.sin_port));
return 0;
}
void parseargs(int argc, char* args[])
{
/* command line is sockprob ip port */
ipstring = args[1];
sscanf(args[2], "%d", &port1);
return;
}
following code compiles and works as I'd expect on Linux and cygwin, but on
the machine that matters (HP PA64, HPUX 11.11) it identifies the peer as
0.0.0.0 in both the accept() and getpeername() return vals, also identifies
the peer port as 0. I'm using gcc 4.1.2, downloaded from HP's site,
compiling with 'gcc sockprob.c -o sockprob', then running it with
'./sockprob.c MY.I.P.ADD 7890', then connect to the server socket with
'telnet MY.I.P.ADD 7890'. Cygwin and Linux show the ip and port of the
connector, but HPUX is showing 0.0.0.0. I run a 'netstat -n' and it shows
the correct ip/port for the server and client. I just don't get it. Does
anyone have any advice?
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
char* ipstring;
int port1;
void parseargs(int, char**);
int main(int argc, char* args[])
{ struct sockaddr_in adr1; /* address for the server */
int ss1, as1; /* file descriptors for server socket, active
socket */
struct sockaddr_in from1; /* address for peer */
socklen_t from1_len;
parseargs(argc, args);
if((ss1 = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{ perror("socket failed");
return 1;
}
memset(&adr1, 0, sizeof(adr1));
adr1.sin_family = AF_INET;
adr1.sin_port = htons(port1);
if(!inet_aton(ipstring, (struct in_addr*)&(adr1.sin_addr)))
{ perror("inet_aton failed");
return 1;
}
if(bind(ss1, (struct sockaddr*) &adr1, sizeof(adr1)))
{ perror("bind failed");
return 1;
}
if(listen(ss1, 1) < 0)
{ perror("listen failed");
return 1;
}
from1_len = sizeof(from1);
memset(&from1, 0, sizeof(from1));
from1.sin_family = AF_INET;
if((as1 = accept(ss1, (struct sockaddr*) &from1, &from1_len)) == -1)
{ perror("Error accepting");
return 1;
}
printf("peer address from accept() is %s %d\n",
inet_ntoa(from1.sin_addr), ntohs(from1.sin_port));
if(getpeername(as1, (struct sockaddr*) &from1, &from1_len))
{ perror("getpeername failed");
return 1;
}
printf("peer add/port from getpeername() is %s %d\n",
inet_ntoa(from1.sin_addr), ntohs(from1.sin_port));
return 0;
}
void parseargs(int argc, char* args[])
{
/* command line is sockprob ip port */
ipstring = args[1];
sscanf(args[2], "%d", &port1);
return;
}