BIND TO DEVICE

Hi,
I want to bind socket descriptor to ppp device.
The following is my code. I don’t know what’s wrong.
Error message is ‘invalid argument’.
Help me!!


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


main()
{
int sd;
struct sockaddr_in cli_addr, serv_addr;
char device[10] = “ppp0”;

if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf(“can’t create socket\n”);
return -1;
}

bzero((char *)&cli_addr, sizeof(cli_addr));
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
cli_addr.sin_port = htons(0);
if (bind(sd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)) < 0) {
printf(“bind fail”);
close(sd);
return -1;
}

// ERROR IS OCCURED IN HERE !!!
if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, device,
strlen(device)+1) < 0) {
printf(“Unable to bind socket to device %s.\n”, device);
perror(“fail”);
close(sd);
return -1;
}

bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(“192.168.0.1”);
serv_addr.sin_port = htons(433);
if (sendto(sd, “0123456789”, 10, 0, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0) {
printf(“send error\n”);
}

close(sd);
}

Thanks,
Hoon

I already made a ppp device before the test.


“Hoon Jung” <hjung@3ic.co.kr> wrote in message
news:9ru0nr$t1m$1@inn.qnx.com

Hi,
I want to bind socket descriptor to ppp device.
The following is my code. I don’t know what’s wrong.
Error message is ‘invalid argument’.
Help me!!


#include <sys/types.h
#include <sys/socket.h
#include <netinet/in.h
#include <arpa/inet.h


main()
{
int sd;
struct sockaddr_in cli_addr, serv_addr;
char device[10] = “ppp0”;

if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf(“can’t create socket\n”);
return -1;
}

bzero((char *)&cli_addr, sizeof(cli_addr));
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
cli_addr.sin_port = htons(0);
if (bind(sd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)) < 0) {
printf(“bind fail”);
close(sd);
return -1;
}

// ERROR IS OCCURED IN HERE !!!
if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, device,
strlen(device)+1) < 0) {
printf(“Unable to bind socket to device %s.\n”, device);
perror(“fail”);
close(sd);
return -1;
}

bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(“192.168.0.1”);
serv_addr.sin_port = htons(433);
if (sendto(sd, “0123456789”, 10, 0, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0) {
printf(“send error\n”);
}

close(sd);
}

Thanks,
Hoon

SO_BINDTODEVICE takes a struct ifreq, as follows:

-seanb



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>

int main(int argc, char *argv[])
{
int s, rc, opt;
struct sockaddr_in sad;
struct ifreq ifreq;
char *buf;
unsigned len;

if(argc < 5)
{
fprintf(stderr, “usage: %s iface dst_ip port len\n”, argv[0]);
return 1;
}

len = strtoul(argv[4], NULL, 0);

if(!(buf = malloc(len)))
{
fprintf(stderr, “No mem\n”);
return 1;
}

if((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror(“socket”);
return 1;
}

opt = 1;
if(setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&opt, sizeof(int)) == -1)
{
perror(“SO_DONTROUTE”);
return 1;
}

if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(int)) == -1)
{
perror(“SO_BROADCAST”);
return 1;
}

memset(&ifreq, 0, sizeof(ifreq));
strcpy(ifreq.ifr_name, argv[1]);
if(setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, (char *)&ifreq, sizeof(ifreq)) == -1)
{
fprintf(stderr, “SO_BINTDODEVICE: %s: %s”, argv[1], strerror(errno));
return 1;
}

memset(&sad, 0, sizeof(sad));
sad.sin_family = AF_INET;
sad.sin_addr.s_addr = inet_addr(argv[2]);
sad.sin_port = htons(strtoul(argv[3], NULL, 0));
if((rc = sendto(s, buf, len, 0, (struct sockaddr *)&sad, sizeof(sad))) == -1)
{
perror(“sendto”);
return 1;
}
printf("%d bytes sent\n", rc);
close(s);
return 0;
}


Hoon Jung <hjung@3ic.co.kr> wrote:
: Hi,
: I want to bind socket descriptor to ppp device.
: The following is my code. I don’t know what’s wrong.
: Error message is ‘invalid argument’.
: Help me!!


: #include <sys/types.h>
: #include <sys/socket.h>
: #include <netinet/in.h>
: #include <arpa/inet.h>


: main()
: {
: int sd;
: struct sockaddr_in cli_addr, serv_addr;
: char device[10] = “ppp0”;

: if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
: printf(“can’t create socket\n”);
: return -1;
: }

: bzero((char *)&cli_addr, sizeof(cli_addr));
: cli_addr.sin_family = AF_INET;
: cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
: cli_addr.sin_port = htons(0);
: if (bind(sd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)) < 0) {
: printf(“bind fail”);
: close(sd);
: return -1;
: }

: // ERROR IS OCCURED IN HERE !!!
: if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, device,
: strlen(device)+1) < 0) {
: printf(“Unable to bind socket to device %s.\n”, device);
: perror(“fail”);
: close(sd);
: return -1;
: }

: bzero((char *)&serv_addr, sizeof(serv_addr));
: serv_addr.sin_family = AF_INET;
: serv_addr.sin_addr.s_addr = inet_addr(“192.168.0.1”);
: serv_addr.sin_port = htons(433);
: if (sendto(sd, “0123456789”, 10, 0, (struct sockaddr *)&serv_addr,
: sizeof(serv_addr)) < 0) {
: printf(“send error\n”);
: }

: close(sd);
: }

: Thanks,
: Hoon

Thanks for your help !!!

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

SO_BINDTODEVICE takes a struct ifreq, as follows:

-seanb



#include <stdio.h
#include <stdlib.h
#include <string.h
#include <unistd.h
#include <errno.h
#include <sys/types.h
#include <sys/socket.h
#include <netinet/in.h
#include <arpa/inet.h
#include <net/if.h

int main(int argc, char *argv[])
{
int s, rc, opt;
struct sockaddr_in sad;
struct ifreq ifreq;
char *buf;
unsigned len;

if(argc < 5)
{
fprintf(stderr, “usage: %s iface dst_ip port len\n”, argv[0]);
return 1;
}

len = strtoul(argv[4], NULL, 0);

if(!(buf = malloc(len)))
{
fprintf(stderr, “No mem\n”);
return 1;
}

if((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror(“socket”);
return 1;
}

opt = 1;
if(setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&opt, sizeof(int))
== -1)
{
perror(“SO_DONTROUTE”);
return 1;
}

if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(int))
== -1)
{
perror(“SO_BROADCAST”);
return 1;
}

memset(&ifreq, 0, sizeof(ifreq));
strcpy(ifreq.ifr_name, argv[1]);
if(setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, (char *)&ifreq,
sizeof(ifreq)) == -1)
{
fprintf(stderr, “SO_BINTDODEVICE: %s: %s”, argv[1], strerror(errno));
return 1;
}

memset(&sad, 0, sizeof(sad));
sad.sin_family = AF_INET;
sad.sin_addr.s_addr = inet_addr(argv[2]);
sad.sin_port = htons(strtoul(argv[3], NULL, 0));
if((rc = sendto(s, buf, len, 0, (struct sockaddr *)&sad, sizeof(sad)))
== -1)
{
perror(“sendto”);
return 1;
}
printf("%d bytes sent\n", rc);
close(s);
return 0;
}


Hoon Jung <> hjung@3ic.co.kr> > wrote:
: Hi,
: I want to bind socket descriptor to ppp device.
: The following is my code. I don’t know what’s wrong.
: Error message is ‘invalid argument’.
: Help me!!


: #include <sys/types.h
: #include <sys/socket.h
: #include <netinet/in.h
: #include <arpa/inet.h


: main()
: {
: int sd;
: struct sockaddr_in cli_addr, serv_addr;
: char device[10] = “ppp0”;

: if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
: printf(“can’t create socket\n”);
: return -1;
: }

: bzero((char *)&cli_addr, sizeof(cli_addr));
: cli_addr.sin_family = AF_INET;
: cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
: cli_addr.sin_port = htons(0);
: if (bind(sd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)) < 0) {
: printf(“bind fail”);
: close(sd);
: return -1;
: }

: // ERROR IS OCCURED IN HERE !!!
: if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, device,
: strlen(device)+1) < 0) {
: printf(“Unable to bind socket to device %s.\n”, device);
: perror(“fail”);
: close(sd);
: return -1;
: }

: bzero((char *)&serv_addr, sizeof(serv_addr));
: serv_addr.sin_family = AF_INET;
: serv_addr.sin_addr.s_addr = inet_addr(“192.168.0.1”);
: serv_addr.sin_port = htons(433);
: if (sendto(sd, “0123456789”, 10, 0, (struct sockaddr *)&serv_addr,
: sizeof(serv_addr)) < 0) {
: printf(“send error\n”);
: }

: close(sd);
: }

: Thanks,
: Hoon