Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems using ioctl to add route to kernel packet forwarding database

Status
Not open for further replies.

richdthomas

Programmer
Sep 1, 2002
19
GB
Hi,

I am trying to add a route within a C program.

I have referred to the man pages as much as I can (there is no example code
for adding a route, just for listing the routes).

At the bottom of this posting is the makefile and C file.

The error that ioctl results in is errno 25, "Not a typewriter" ENOTTY.

I have tried the socket() and write() method instead of the open() and
ioctl(). This results in the socket() function failing.

I am running SCO OpenServer 5.0.5.

Please can anyone assist me with this.

Thanks in advance.

Richard Thomas.

rich_d_thomas@bigfoot.com

makefile:
# filename: makefile
# author: R D Thomas
# created: 24 Aug 02
# version: 1.0
# release: 001 24 Aug 02

CFLAGS =
OBJS = routeadd.o

# For SCO Openserver
LFLAGS = -lsocket -lnsl

EXE = routeadd

# For SCO OpenServer 5.0.5, the cc command must be the full path name
# (/udk/usr/ccs/bin/cc) to prevent the following error message at runtime:
#dynamic linker: ./routeadd: can't set protections on segment of length
0xff1 at
#0x8048000
#Killed */

$(EXE): $(OBJS)
/udk/usr/ccs/bin/cc $(CFLAGS) -o $(EXE) $(OBJS) $(LFLAGS)

#-----------------------------------------------------------------

routeadd.o: routeadd.c
CFLOW:
cflow $(CFLAGS) $(CSRC) >/tmp/cflow.out


routeadd.c:
/***************************************************************************
***/
/* module: Route add utility
*/
/* filename: routeadd.c
*/
/* author: Richard Thomas
*/
/* version: 1.000 24-Aug-2002 - Baseline code
*/
/***************************************************************************
***/

#include "stdio.h"
#include "string.h"
#include "unistd.h"

#include <sys/types.h>
#include <sys/stream.h>
#include <sys/socket.h>
#include <sys/stropts.h>
#include <net/if.h>
#include <net/route.h>
#include <paths.h>

#include <fcntl.h>
#include <errno.h>


#define MAXLEN 1000 /* Longest input line
*/
#define MAXLINES 200 /* Maximum number of lines
*/

#define PROGIDENT &quot;routeadd v1.000&quot; /* Program ident
*/
#define DATE &quot;24 Aug 2002&quot; /* Date program was last modified
*/

#define LOGFILE &quot;./test.log&quot;

main(int argc, char *argv[]){


struct rt_data_msg{ /* Structure for entire
*/
/* routing message
*/
struct rt_msghdr add_rtm; /* Structure of routing
*/
/* message header
*/
struct sockaddr rt_dst; /* Structure for
destination */
struct sockaddr rt_gate; /* Structure for gateway
*/
struct sockaddr rt_mask; /* Structure for netmask
*/
};



int fd; /* channel for passing
*/
/* routing control messages
*/
int size; /* routing control message
*/
/* sizes
*/
int w; /* Write socket return code
*/

char line[MAXLEN+1]; /* Line buffer
*/
char choice[MAXLEN+1]; /* Buffer to store user
*/
/* choice in
*/

struct rt_data_msg add_rt; /* Routing mesage
*/

printf(&quot;\n&quot;);
printf(PROGIDENT);
printf(&quot;\n&quot;);
printf(&quot;written by R. Thomas &quot;);
printf(DATE);
printf(&quot;\n\n&quot;);

strcpy(line,argv[1]);

printf(&quot;%s\n&quot;,line);

/* open channel for passing routing control messages */
fd=open(_PATH_ROUTE,O_RDWR);
/*fd=socket(PF_ROUTE,SOCK_RAW,AF_INET);*/

if(fd<0){
printf(&quot;Error in opening channel, %s errno=%d fd=%d\n&quot;,strerror(errno),
errno,fd);
exit(fd);
}

printf(&quot;Successfully opened channel, fd=%d\n&quot;,fd);


/* Initialise all elements of message structure to 0 */
size=sizeof(struct rt_data_msg);
memset(&add_rt,0,size);


add_rt.add_rtm.rtm_msglen=size; /* Set routing message size
*/
add_rt.add_rtm.rtm_version=RTM_VERSION; /* Set routing message version
*/
add_rt.add_rtm.rtm_type=RTM_ADD; /* Set routing message type to
*/
/* route add
*/
add_rt.add_rtm.rtm_pid=getpid(); /* Set originating PID to PID of
*/
/* this process
*/
add_rt.add_rtm.rtm_addrs=RTA_DST|RTA_GATEWAY|RTA_NETMASK;
/* Set the bitmask to indicate
*/
/* destination, gateway and
netmaks */
/* are present
*/
add_rt.add_rtm.rtm_seq=1234; /* Set the sequence number to a
*/
/* random value
*/
add_rt.add_rtm.rtm_flags=RTF_UP|RTF_HOST|RTF_STATIC;
/* Set the flags to indicate the
*/
/* route is usable, the
destination */
/* is a host and this is a static
*/
/* route
*/
add_rt.add_rtm.rtm_inits=RTV_EXPIRE|RTV_RPIPE|RTV_SPIPE|RTV_SSTHRESH;
/* Set inits to indicate that we
*/
/* are initialising the hopcount,
*/
/* _recvpipe, _sendpipe and
/* _ssthresh
*/


inet_aton(&quot;192.168.120.1&quot;,&add_rt.rt_dst); /* Set the destination
address*/
inet_aton(&quot;10.10.0.66&quot;,&add_rt.rt_gate); /* Set the gateway address
*/
inet_aton(&quot;255.255.255.255&quot;,&add_rt.rt_mask); /* Set the nestmask address
*/

w=ioctl(fd,RTSTR_SEND,&add_rt); /* Send the routing message
*/
/*w=write(fd,(char *)&add_rt,size);*/ /* to the kernel
*/

printf(&quot;%s errno=%d w=%d\n&quot;,strerror(errno),errno,w);

close(fd);

}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top