James Winton <spamthis@home.com> wrote:
Hi there,
I’m writing a program that needs access to network interface information
contained in the ifnet structure (in net/if.h). There is no “sysctl” in
QNX4, and as far as I can tell, there are no ioctls to give me the data
either. Is there a special request to get Net or Socket to give me that
info?
I’m using Watcom C 10.6, under 4.25J
Thanks,
-Jim
“There’s always one more bug…”
– Bubba DuPree (1986)
Here is a simple program I cooked up. Should work on both Q4 and Q6.
Make sure you all -lsocket on the compile line.
Of course the hostname environment variable is not overly usefull. In
Q6 I am using confstr, and in Q4 I am using code snippet 2 to set and read
the host name from Sock[l]et.
regards,
Tom
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <sys/iomsg.h>
#include <net/if.h>
#include <sys/sockio.h>
#include <errno.h>
#include <sys/netmgr.h>
#include <arpa/inet.h>
int main()
{
int sock;
struct ifreq ifreq;
struct sockaddr_in server;
strcpy( ifreq.ifr_name, “en0” );
sock = socket(AF_INET, SOCK_STREAM, 0);
if ( sock < 0 )
{
perror(“socket”);
return 1;
}
ioctl( sock, SIOCGIFADDR, &ifreq );
memcpy( &server, &ifreq.ifr_ifru.ifru_addr, sizeof( server ));
printf( “IP Address = %s\n”, inet_ntoa( server.sin_addr ));
printf( “Host Name = %s\n”, getenv(“HOSTNAME”) );
ioctl( sock, SIOCGIFNETMASK, &ifreq );
memcpy( &server, &ifreq.ifr_ifru.ifru_addr, sizeof( server ));
printf( “Mask = %s\n”, inet_ntoa( server.sin_addr ));
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
////////////////////////////////////////////////////////////////////////////////
#define _SOCK_GETHOSTNAME 0x193
struct _sock_gethostname {
msg_t code;
short namelen;
char name[];
};
struct _sock_gethostname_reply {
msg_t status;
short namelen;
char name[];
};
#define _SOCK_SETHOSTNAME 0x194
struct _sock_sethostname {
msg_t code;
short namelen;
char name[];
};
struct _sock_sethostname_reply {
msg_t status;
short namelen;
char name[];
};
typedef union SocketMsg {
struct _sock_gethostname gethostname;
struct _sock_gethostname_reply gethostnameReply;
struct sock_sethostname sethostname;
struct sock_sethostname_reply sethostnameReply;
char zero[ 300 ];
} SocketMsg_t;
////////////////////////////////////////////////////////////////////////////////
int NetworkManager::GetHostName( char *name, size_t len )
{
SocketMsg msg;
pid_t pid;
msg.gethostname.code = _SOCK_GETHOSTNAME;
msg.gethostname.namelen = (short) _min( len, 256 );
pid = qnx_name_locate( 0, “qnx/socket”, 1024, 0 );
Send( pid, &msg, &msg, sizeof( msg ), sizeof( msg ) );
if( msg.gethostnameReply.status == 0 )
{
strncpy( name_, msg.gethostnameReply.name, msg.gethostnameReply.namelen );
}
else
{
name_[ 0 ] = ‘\0’;
}
return msg.gethostnameReply.status;
}
////////////////////////////////////////////////////////////////////////////////
int NetworkManager::SetHostName( char *name_ )
{
SocketMsg msg;
pid_t pid;
msg.sethostname.code = SOCK_SETHOSTNAME;
msg.sethostname.namelen = (short) strlen( name ) + 1; // must include null terminator
strcpy( msg.gethostnameReply.name, name_ );
pid = qnx_name_locate( 0, “qnx/socket”, 1024, 0 );
Send( pid, &msg, &msg, sizeof( msg ), sizeof( msg ) );
return msg.sethostnameReply.status;
}