bind to a specific device...(QNX v.6)

Hi,

Is it possible to bind ICMP-Socket to a specific device?

( In the code I wrote, there was not any error or warning
when I bind a socket to a specific device. But the message sent by
the socket didn’t arrive at a remote host. => I checked it by using Protocol
Analyzer )

JangHun

#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netdb.h>

#define SERV_ADDR “165.133.182.10”
#define SERV_PORT 20000


int icmp_socket;
struct sockaddr_in icmp_sockaddr;
struct sockaddr_in serv_sockaddr;
struct ifreq ifreq;

char msg[] = “Message from a client”;

void init_socket() {

bzero( (char *)&icmp_sockaddr, sizeof(icmp_sockaddr) );
icmp_sockaddr.sin_family = AF_INET;
icmp_sockaddr.sin_port = htons( 0 );
icmp_sockaddr.sin_addr.s_addr = htonl( INADDR_ANY );

bzero( (char *)&serv_sockaddr, sizeof(serv_sockaddr) );
serv_sockaddr.sin_family = AF_INET;
serv_sockaddr.sin_port = htons( SERV_PORT );
serv_sockaddr.sin_addr.s_addr = inet_addr( SERV_ADDR );
}

main() {

int res, opt;
char *ppp0 = “ppp0”;
struct protoent *icmp_proto_ent;
int icmp_proto;
char *proto_name;

init_socket();

icmp_socket = socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
if( icmp_socket < 0 ) {
printf( “can’t create a icmp_socket\n” );
exit( -1 );
}

opt = 1;

res = setsockopt( icmp_socket, SOL_SOCKET, SO_DONTROUTE,
(char *)&opt, sizeof(int) );
if( res < 0 ) {
printf( “Error : SO_DONTROUTE\n” );
exit( -1 );
}

res = setsockopt( icmp_socket, SOL_SOCKET, SO_BROADCAST,
(char *)&opt, sizeof(int) );
if( res < 0 ) {
printf( “Error : SO_BROADCAST\n” );
exit( -1 );
}


memset( &ifreq, 0, sizeof(ifreq) );
strcpy( ifreq.ifr_name, ppp0 );
res = setsockopt( icmp_socket, SOL_SOCKET, SO_BINDTODEVICE,
(char *)&ifreq, sizeof(ifreq) );
if( res < 0 ) {
printf( “Can’t bind to a specific device : %d\n”, res );
exit( -1 );
}

res = sendto( icmp_socket,
msg, sizeof(msg),
0,
(struct sockaddr *)&serv_sockaddr,
sizeof(serv_sockaddr) );
if( res < 0 ) {
printf( “Can’t send a message\n” );
exit( -1 );
}

printf( “Done\n” );

close( icmp_socket );

exit( 0 );
}

SO_BINDTODEVICE is currently only implemented for UDP
sockets.

-seanb

JangHun <khun25@hotmail.com> wrote:
: Hi,

: Is it possible to bind ICMP-Socket to a specific device?

: ( In the code I wrote, there was not any error or warning
: when I bind a socket to a specific device. But the message sent by
: the socket didn’t arrive at a remote host. => I checked it by using Protocol
: Analyzer )

: JangHun

: #include <sys/socket.h>
: #include <sys/types.h>
: #include <stdio.h>
: #include <netinet/in.h>
: #include <arpa/inet.h>
: #include <net/if.h>
: #include <netdb.h>

: #define SERV_ADDR “165.133.182.10”
: #define SERV_PORT 20000


: int icmp_socket;
: struct sockaddr_in icmp_sockaddr;
: struct sockaddr_in serv_sockaddr;
: struct ifreq ifreq;

: char msg[] = “Message from a client”;

: void init_socket() {

: bzero( (char *)&icmp_sockaddr, sizeof(icmp_sockaddr) );
: icmp_sockaddr.sin_family = AF_INET;
: icmp_sockaddr.sin_port = htons( 0 );
: icmp_sockaddr.sin_addr.s_addr = htonl( INADDR_ANY );

: bzero( (char *)&serv_sockaddr, sizeof(serv_sockaddr) );
: serv_sockaddr.sin_family = AF_INET;
: serv_sockaddr.sin_port = htons( SERV_PORT );
: serv_sockaddr.sin_addr.s_addr = inet_addr( SERV_ADDR );
: }

: main() {

: int res, opt;
: char *ppp0 = “ppp0”;
: struct protoent *icmp_proto_ent;
: int icmp_proto;
: char *proto_name;

: init_socket();

: icmp_socket = socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
: if( icmp_socket < 0 ) {
: printf( “can’t create a icmp_socket\n” );
: exit( -1 );
: }

: opt = 1;

: res = setsockopt( icmp_socket, SOL_SOCKET, SO_DONTROUTE,
: (char *)&opt, sizeof(int) );
: if( res < 0 ) {
: printf( “Error : SO_DONTROUTE\n” );
: exit( -1 );
: }

: res = setsockopt( icmp_socket, SOL_SOCKET, SO_BROADCAST,
: (char *)&opt, sizeof(int) );
: if( res < 0 ) {
: printf( “Error : SO_BROADCAST\n” );
: exit( -1 );
: }


: memset( &ifreq, 0, sizeof(ifreq) );
: strcpy( ifreq.ifr_name, ppp0 );
: res = setsockopt( icmp_socket, SOL_SOCKET, SO_BINDTODEVICE,
: (char *)&ifreq, sizeof(ifreq) );
: if( res < 0 ) {
: printf( “Can’t bind to a specific device : %d\n”, res );
: exit( -1 );
: }

: res = sendto( icmp_socket,
: msg, sizeof(msg),
: 0,
: (struct sockaddr *)&serv_sockaddr,
: sizeof(serv_sockaddr) );
: if( res < 0 ) {
: printf( “Can’t send a message\n” );
: exit( -1 );
: }

: printf( “Done\n” );

: close( icmp_socket );

: exit( 0 );
: }

Thanks alot…
So, did you mean there was not any way to write a message using a specific
device
with ICMP-Socket?

JangHun


“Sean Boudreau” <seanb@qnx.com> wrote in message
news:9uqj62$enm$1@nntp.qnx.com

SO_BINDTODEVICE is currently only implemented for UDP
sockets.

-seanb

JangHun <> khun25@hotmail.com> > wrote:
: Hi,

: Is it possible to bind ICMP-Socket to a specific device?

: ( In the code I wrote, there was not any error or warning
: when I bind a socket to a specific device. But the message sent by
: the socket didn’t arrive at a remote host. => I checked it by using
Protocol
: Analyzer )

: JangHun

: #include <sys/socket.h
: #include <sys/types.h
: #include <stdio.h
: #include <netinet/in.h
: #include <arpa/inet.h
: #include <net/if.h
: #include <netdb.h

: #define SERV_ADDR “165.133.182.10”
: #define SERV_PORT 20000


: int icmp_socket;
: struct sockaddr_in icmp_sockaddr;
: struct sockaddr_in serv_sockaddr;
: struct ifreq ifreq;

: char msg[] = “Message from a client”;

: void init_socket() {

: bzero( (char *)&icmp_sockaddr, sizeof(icmp_sockaddr) );
: icmp_sockaddr.sin_family = AF_INET;
: icmp_sockaddr.sin_port = htons( 0 );
: icmp_sockaddr.sin_addr.s_addr = htonl( INADDR_ANY );

: bzero( (char *)&serv_sockaddr, sizeof(serv_sockaddr) );
: serv_sockaddr.sin_family = AF_INET;
: serv_sockaddr.sin_port = htons( SERV_PORT );
: serv_sockaddr.sin_addr.s_addr = inet_addr( SERV_ADDR );
: }

: main() {

: int res, opt;
: char *ppp0 = “ppp0”;
: struct protoent *icmp_proto_ent;
: int icmp_proto;
: char *proto_name;

: init_socket();

: icmp_socket = socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
: if( icmp_socket < 0 ) {
: printf( “can’t create a icmp_socket\n” );
: exit( -1 );
: }

: opt = 1;

: res = setsockopt( icmp_socket, SOL_SOCKET, SO_DONTROUTE,
: (char *)&opt, sizeof(int) );
: if( res < 0 ) {
: printf( “Error : SO_DONTROUTE\n” );
: exit( -1 );
: }

: res = setsockopt( icmp_socket, SOL_SOCKET, SO_BROADCAST,
: (char *)&opt, sizeof(int) );
: if( res < 0 ) {
: printf( “Error : SO_BROADCAST\n” );
: exit( -1 );
: }


: memset( &ifreq, 0, sizeof(ifreq) );
: strcpy( ifreq.ifr_name, ppp0 );
: res = setsockopt( icmp_socket, SOL_SOCKET, SO_BINDTODEVICE,
: (char *)&ifreq, sizeof(ifreq) );
: if( res < 0 ) {
: printf( “Can’t bind to a specific device : %d\n”, res );
: exit( -1 );
: }

: res = sendto( icmp_socket,
: msg, sizeof(msg),
: 0,
: (struct sockaddr *)&serv_sockaddr,
: sizeof(serv_sockaddr) );
: if( res < 0 ) {
: printf( “Can’t send a message\n” );
: exit( -1 );
: }

: printf( “Done\n” );

: close( icmp_socket );

: exit( 0 );
: }

Not necessarily. The normal routing rules apply. In the end,
routes resolve to a particular interface.

-seanb

JangHun <khun25@hotmail.com> wrote:
: Thanks alot…
: So, did you mean there was not any way to write a message using a specific
: device
: with ICMP-Socket?

: JangHun


: “Sean Boudreau” <seanb@qnx.com> wrote in message
: news:9uqj62$enm$1@nntp.qnx.com
:>
:> SO_BINDTODEVICE is currently only implemented for UDP
:> sockets.
:>
:> -seanb
:>
:> JangHun <khun25@hotmail.com> wrote:
:> : Hi,
:>
:> : Is it possible to bind ICMP-Socket to a specific device?
:>
:> : ( In the code I wrote, there was not any error or warning
:> : when I bind a socket to a specific device. But the message sent by
:> : the socket didn’t arrive at a remote host. => I checked it by using
: Protocol
:> : Analyzer )
:>
:> : JangHun
:>
:> : #include <sys/socket.h>
:> : #include <sys/types.h>
:> : #include <stdio.h>
:> : #include <netinet/in.h>
:> : #include <arpa/inet.h>
:> : #include <net/if.h>
:> : #include <netdb.h>
:>
:> : #define SERV_ADDR “165.133.182.10”
:> : #define SERV_PORT 20000
:>
:>
:> : int icmp_socket;
:> : struct sockaddr_in icmp_sockaddr;
:> : struct sockaddr_in serv_sockaddr;
:> : struct ifreq ifreq;
:>
:> : char msg[] = “Message from a client”;
:>
:> : void init_socket() {
:>
:> : bzero( (char *)&icmp_sockaddr, sizeof(icmp_sockaddr) );
:> : icmp_sockaddr.sin_family = AF_INET;
:> : icmp_sockaddr.sin_port = htons( 0 );
:> : icmp_sockaddr.sin_addr.s_addr = htonl( INADDR_ANY );
:>
:> : bzero( (char *)&serv_sockaddr, sizeof(serv_sockaddr) );
:> : serv_sockaddr.sin_family = AF_INET;
:> : serv_sockaddr.sin_port = htons( SERV_PORT );
:> : serv_sockaddr.sin_addr.s_addr = inet_addr( SERV_ADDR );
:> : }
:>
:> : main() {
:>
:> : int res, opt;
:> : char *ppp0 = “ppp0”;
:> : struct protoent *icmp_proto_ent;
:> : int icmp_proto;
:> : char *proto_name;
:>
:> : init_socket();
:>
:> : icmp_socket = socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
:> : if( icmp_socket < 0 ) {
:> : printf( “can’t create a icmp_socket\n” );
:> : exit( -1 );
:> : }
:>
:> : opt = 1;
:>
:> : res = setsockopt( icmp_socket, SOL_SOCKET, SO_DONTROUTE,
:> : (char *)&opt, sizeof(int) );
:> : if( res < 0 ) {
:> : printf( “Error : SO_DONTROUTE\n” );
:> : exit( -1 );
:> : }
:>
:> : res = setsockopt( icmp_socket, SOL_SOCKET, SO_BROADCAST,
:> : (char *)&opt, sizeof(int) );
:> : if( res < 0 ) {
:> : printf( “Error : SO_BROADCAST\n” );
:> : exit( -1 );
:> : }
:>
:>
:> : memset( &ifreq, 0, sizeof(ifreq) );
:> : strcpy( ifreq.ifr_name, ppp0 );
:> : res = setsockopt( icmp_socket, SOL_SOCKET, SO_BINDTODEVICE,
:> : (char *)&ifreq, sizeof(ifreq) );
:> : if( res < 0 ) {
:> : printf( “Can’t bind to a specific device : %d\n”, res );
:> : exit( -1 );
:> : }
:>
:> : res = sendto( icmp_socket,
:> : msg, sizeof(msg),
:> : 0,
:> : (struct sockaddr *)&serv_sockaddr,
:> : sizeof(serv_sockaddr) );
:> : if( res < 0 ) {
:> : printf( “Can’t send a message\n” );
:> : exit( -1 );
:> : }
:>
:> : printf( “Done\n” );
:>
:> : close( icmp_socket );
:>
:> : exit( 0 );
:> : }
:>
:>