[insert project logo here (125x200px max)]

Navigator

Mailinglists

Please report any errors or ommissions you find to our `Help' mailinglist, or post a message in the Forums.

Copyright and Licensing Information

Snap is (c) Jonathan T. Moore, 1999-2002 and licensed under the GNU General Public License (GPL).

All other parts of Splash are (c) Willem de Bruijn, 2002-2003 and licensed under the BSD Open Source License.

All sourcecode is made publicly available.

Acknowledgement

Splash and the Splash website are hosted by SourceForge.net

SourceForge.net Logo

osi-open source certified logo

Splash - Documentation

SNMP Plus a Lightweight API for SNAP Handling

Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

snap_svc/snap_svc_route.c

Go to the documentation of this file.
00001 /* snap service library                 */
00002 /* (c) Willem de Bruijn, 2002, 2003     */
00003 /* Licensed under the BSD License       */
00004 /* snap_svc_route library sourcefile    */
00005 
00006 /* platform includes (linux) */
00007 #include <sys/ioctl.h>
00008 #include <sys/socket.h>
00009 #include <asm/types.h>
00010 #include <netinet/in.h>
00011 #include <net/route.h>
00012 
00013 /* libc includes */
00014 #include <stdlib.h>
00015 #include <string.h>
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include <unistd.h>
00019 
00020 /* local includes */
00021 #include "d_printf.h"
00022 #include "snap_svc.h"
00023 #include "snap_svc_route.h"  /* change this to your headerfile */
00024 
00025 /* library handling functions */
00026 void snap_external_svclib_init(){
00027     d_printf(50,"snap_svc_route : initialized\n");
00028 }
00029 
00030 void snap_external_svclib_done(){
00031     d_printf(50,"snap_svc_route : closed\n");
00032 }
00033 
00034 /* there is where functions inside the library should be listed for registration */
00035 void snap_external_svclib_getnextfunc(char** snapsvc_name, snapsvc_func_proto* snapsvc_func, int* snapsvc_args, int* snapsvc_rets){
00036 
00037     switch (svc_fun_counter){
00038         case 0  :       (*snapsvc_name) = strdup ("route_add");
00039                         (*snapsvc_func) = (snapsvc_func_proto) &route_add;
00040                         (*snapsvc_args) = 4;
00041                         (*snapsvc_rets) = SVC_SNMP_TYPE_NULL;
00042                         break;
00043         case 1  :       (*snapsvc_name) = strdup ("route_del");
00044                         (*snapsvc_func) = (snapsvc_func_proto) &route_del;
00045                         (*snapsvc_args) = 4;
00046                         (*snapsvc_rets) = SVC_SNMP_TYPE_NULL;
00047                         break;
00048         /* unknown handler -> return NULL as end-of-list symbol */
00049         default :   (*snapsvc_name) = NULL;
00050                     (*snapsvc_func) = NULL;
00051                     (*snapsvc_args) = 0;
00052                     (*snapsvc_rets) = 0;
00053     }
00054     svc_fun_counter ++;
00055 }
00056 
00057 /* ADD SERVICES BELOW */
00058 /* declarations of snap service handlers */
00059 
00060 /* quickly add an IP address */
00061 __u32 ip_to__u32(__u8 ip1, __u8 ip2, __u8 ip3, __u8 ip4){
00062     return (ip1 << 24) + (ip2 << 16) + (ip3 << 8) + (ip4);
00063 }
00064 
00065 /* quickly display an IP address */
00066 void printip(__u32 ip, int dLevel){
00067      d_printf(dLevel,"read ip_address: %u:%u:%u:%u\n",ip >> 24, (ip << 8) >> 24, (ip << 16) >> 24, (ip << 24) >> 24);
00068 }
00069 
00070 void make_sockaddr_in(__u32 ip_in, struct sockaddr * outaddr){
00071     
00072     ((struct sockaddr_in *) outaddr)->sin_family = AF_INET;
00073     ((struct sockaddr_in *) outaddr)->sin_addr.s_addr = htonl(ip_in);
00074 }
00075 /* 
00076     handle a route update request
00077 
00078     the action field should contain a 
00079     0 if you want to DELete a route
00080     1 if you want to ADD a route
00081     
00082     the other fields should be self explanatory
00083 */
00084 int handle_request(short action, __u32 ip_dest, __u32 ip_mask, __u32 ip_gateway, char* device){
00085     struct rtentry rt;  /* routetable entry */
00086     int skfd;           /* socket filedescriptor */
00087      
00088     d_printf(110,"snap_svc_route : handling %d, %ul, %ul, %ul, %s\n", action, ip_dest, ip_mask, ip_gateway, device);
00089     printip(ip_dest,150);
00090     printip(ip_mask,150);
00091     printip(ip_gateway,150);
00092     
00093     /* clean out the structure */
00094     memset((char *) &rt, 0, sizeof(struct rtentry));
00095     
00096     /* fill the primary elements */
00097     make_sockaddr_in(ip_dest, &(rt.rt_dst));
00098     make_sockaddr_in(ip_mask, &(rt.rt_genmask));    /* the netmask */
00099     make_sockaddr_in(ip_gateway, &(rt.rt_gateway));
00100     rt.rt_dev = strdup (device);
00101     
00102     
00103     
00104     /* fill in the flags */
00105     if (ip_mask == 0xffffffff)  /* netmask tells us this is a hostroute */
00106         rt.rt_flags = (RTF_UP | RTF_HOST);
00107     else    /* netroute */
00108         rt.rt_flags = RTF_UP;
00109     if (ip_gateway) /* using a gateway */
00110         rt.rt_flags |= RTF_GATEWAY;
00111 
00112     /* Create a socket to the INET kernel. */
00113     if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00114         perror("socket");
00115         return (1);
00116     }
00117     
00118     /* execute the request */
00119     if (action){    /* ADD request */   
00120         if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
00121             perror("SIOCADDRT");
00122             close(skfd);
00123             return (1);
00124         }
00125     }
00126     else{           /* DEL request */
00127         if (ioctl(skfd, SIOCDELRT, &rt) < 0) {
00128             perror("SIOCDELRT");
00129             close(skfd);
00130             return (1);
00131         }
00132     }
00133     
00134     /* Close the socket. */
00135     close(skfd);
00136     
00137     /* free the datastructure */
00138     if (rt.rt_dev){
00139         free (rt.rt_dev);
00140         rt.rt_dev = NULL;
00141     }
00142     
00143     return 0;
00144 } 
00145 
00146 int route_add(__u32 ip_dest, __u32 ip_mask, __u32 ip_gateway, char* device){
00147     return handle_request(1, ntohl(ip_dest), ntohl(ip_mask), ntohl(ip_gateway), device);
00148 }
00149 
00150 int route_del(__u32 ip_dest, __u32 ip_mask, __u32 ip_gateway, char* device){
00151     return handle_request(0, ip_dest, ip_mask, ip_gateway, device);
00152 }