enable -fno-strict-aliasing until the code base gets a hefty clean up to fix all...
[platform/upstream/net-tools.git] / lib / x25_sr.c
1 /*
2  * lib/x25_sr.c This file contains an implementation of the "X.25"
3  *              route change support functions.
4  *
5  * Version:     @(#)x25_sr.c    1.00    08/15/98
6  *
7  * Author:      Stephane Fillod, <sfillod@charybde.gyptis.frmug.org>
8  *              based on inet_sr.c
9  *
10  *              This program is free software; you can redistribute it
11  *              and/or  modify it under  the terms of  the GNU General
12  *              Public  License as  published  by  the  Free  Software
13  *              Foundation;  either  version 2 of the License, or  (at
14  *              your option) any later version.
15  */
16 #include "config.h"
17
18 #if HAVE_AFX25
19 #include <asm/types.h>
20 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/ioctl.h>
24 #include <linux/x25.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <netdb.h>
28 #include <resolv.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include "version.h"
34 #include "net-support.h"
35 #include "pathnames.h"
36 #define  EXTERN
37 #if 0
38 #include "net-locale.h"
39 #endif
40 #include "intl.h"
41
42 #include "net-features.h"
43
44 extern     struct aftype   x25_aftype;
45
46 static int skfd = -1;
47
48
49 static int usage(void)
50 {
51   fprintf(stderr,"Usage: x25_route [-v] del Target[/mask] [dev] If\n");
52   fprintf(stderr,"       x25_route [-v] add Target[/mask] [dev] If\n");
53   return(E_USAGE);
54 }
55
56
57 static int X25_setroute(int action, int options, char **args)
58 {
59   struct x25_route_struct rt;
60   struct sockaddr_x25 sx25;
61   char target[128];
62   signed int sigdigits;
63
64   if (*args == NULL)
65         return(usage());
66
67   strcpy(target, *args++);
68
69   /* Clean out the x25_route_struct structure. */
70   memset((char *) &rt, 0, sizeof(rt));
71
72
73   if ((sigdigits = x25_aftype.input(0, target, (struct sockaddr *)&sx25)) < 0) {
74         x25_aftype.herror(target);
75         return (1);
76   }
77   rt.sigdigits=sigdigits;
78
79   /* this works with 2.4 and 2.6 headers struct x25_address vs. typedef */
80   memcpy(&rt.address, &sx25.sx25_addr, sizeof(sx25.sx25_addr));
81
82   while (*args) {
83         if (!strcmp(*args,"device") || !strcmp(*args,"dev")) {
84                 args++;
85                 if (!*args)
86                         return(usage());
87         } else
88                 if (args[1])
89                         return(usage());
90         if (rt.device[0])
91                 return(usage());
92         strcpy(rt.device, *args);
93         args++;
94   }
95   if (rt.device[0]=='\0')
96         return(usage());
97
98   /* sanity checks.. */
99         if (rt.sigdigits > 15) {
100                 fprintf(stderr, _("route: bogus netmask %d\n"), rt.sigdigits);
101                 return(E_OPTERR);
102         }
103
104         if (rt.sigdigits > strlen(rt.address.x25_addr)) {
105                 fprintf(stderr, _("route: netmask doesn't match route address\n"));
106                 return(E_OPTERR);
107         }
108
109   /* Create a socket to the X25 kernel. */
110   if ((skfd = socket(AF_X25, SOCK_SEQPACKET, 0)) < 0) {
111         perror("socket");
112         return(E_SOCK);
113   }
114   
115   /* Tell the kernel to accept this route. */
116   if (action==RTACTION_DEL) {
117         if (ioctl(skfd, SIOCDELRT, &rt) < 0) {
118                 perror("SIOCDELRT");
119                 close(skfd);
120                 return(E_SOCK);
121         }
122   } else {
123         if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
124                 perror("SIOCADDRT");
125                 close(skfd);
126                 return(E_SOCK);
127         }
128   }
129
130   /* Close the socket. */
131   (void) close(skfd);
132   return(0);
133 }
134
135 int X25_rinput(int action, int options, char **args)
136 {
137   if (action == RTACTION_FLUSH) {
138         fprintf(stderr,"Flushing `x25' routing table not supported\n");
139         return(usage());
140   }     
141   if (options & FLAG_CACHE) {
142         fprintf(stderr,"Modifying `x25' routing cache not supported\n");
143         return(usage());
144   }     
145   if ((*args == NULL) || (action == RTACTION_HELP))
146         return(usage());
147   
148   return(X25_setroute(action, options, args));
149 }
150 #endif  /* HAVE_AFX25 */