Fix typo (reported by dtaylor@xfiles.nildram.co.uk)
[platform/upstream/net-tools.git] / lib / af.c
1 /*
2  * lib/af.c     This file contains the top-level part of the protocol
3  *              support functions module for the NET-2 base distribution.
4  *
5  * Version:     lib/af.c 1.13 (1996-02-21)
6  *
7  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
8  *              Copyright 1993 MicroWalt Corporation
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 <sys/types.h>
17 #include <sys/socket.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <errno.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include "config.h"
25 #include "net-support.h"
26 #include "pathnames.h"
27 #include "intl.h"
28
29
30 int flag_unx = 0;
31 int flag_ipx = 0;
32 int flag_ax25 = 0;
33 int flag_ddp = 0;
34 int flag_netrom = 0;
35 int flag_inet = 0;
36 int flag_inet6 = 0;
37 int flag_econet = 0;
38
39
40 struct aftrans_t {
41         char *alias;
42         char *name;
43         int  *flag;
44 } aftrans[]={
45         {"ax25",        "ax25",         &flag_ax25},
46         {"ip",          "inet",         &flag_inet},
47         {"ip6",         "inet6",        &flag_inet6},
48         {"ipx",         "ipx",          &flag_ipx},
49         {"appletalk",   "ddp",          &flag_ddp},
50         {"netrom",      "netrom",       &flag_netrom},
51         {"inet",        "inet",         &flag_inet},
52         {"inet6",       "inet6",        &flag_inet6},
53         {"ddp",         "ddp",          &flag_ddp},
54         {"unix",        "unix",         &flag_unx},
55         {"tcpip",       "inet",         &flag_inet},
56         {"econet",      "ec",           &flag_econet},
57         {0,             0,              0}
58 };
59
60 char            afname[256]="";
61
62 extern  struct aftype   unspec_aftype;
63 extern  struct aftype   unix_aftype;
64 extern  struct aftype   inet_aftype;
65 extern  struct aftype   inet6_aftype;
66 extern  struct aftype   ax25_aftype;
67 extern  struct aftype   netrom_aftype;
68 extern  struct aftype   ipx_aftype;
69 extern  struct aftype   ddp_aftype;
70 extern  struct aftype   ec_aftype;
71
72 static short sVafinit = 0;
73
74 static struct aftype *aftypes[] = {
75 #if HAVE_AFUNIX
76   &unix_aftype,
77 #endif
78 #if HAVE_AFINET
79   &inet_aftype,
80 #endif
81 #if HAVE_AFINET6
82   &inet6_aftype,
83 #endif
84 #if HAVE_AFAX25
85   &ax25_aftype,
86 #endif
87 #if HAVE_AFNETROM
88   &netrom_aftype,
89 #endif
90 #if HAVE_AFIPX
91   &ipx_aftype,
92 #endif
93 #if HAVE_AFATALK
94   &ddp_aftype,
95 #endif
96 #if HAVE_AFECONET
97   &ec_aftype,
98 #endif
99   &unspec_aftype,
100   NULL
101 };
102
103 void afinit ()
104 {
105   unspec_aftype.title = _("UNSPEC");
106 #if HAVE_AFINET
107   unix_aftype.title = _("UNIX Domain");
108 #endif
109 #if HAVE_AFINET
110   inet_aftype.title = _("DARPA Internet");
111 #endif
112 #if HAVE_AFINET6
113   inet6_aftype.title = _("IPv6");
114 #endif
115 #if HAVE_AFAX25
116   ax25_aftype.title = _("AMPR AX.25");
117 #endif
118 #if HAVE_AFNETROM
119   netrom_aftype.title = _("AMPR NET/ROM");
120 #endif
121 #if HAVE_AFIPX
122   ipx_aftype.title = _("IPX");
123 #endif
124 #if HAVE_AFATALK
125   ddp_aftype.title = _("Appletalk DDP");
126 #endif
127 #if HAVE_AFECONET
128   ec_aftype.title = _("Econet");
129 #endif
130   sVafinit = 1;
131 }
132
133 /* set the default AF list from the program name or a constant value    */
134 void
135 aftrans_def(char *tool, char *argv0, char *dflt)
136 {
137   char *tmp;
138   char *buf;
139   
140   strcpy(afname, dflt);
141
142   if (!(tmp = strrchr(argv0, '/')))
143         tmp = argv0;                    /* no slash?! */
144   else
145         tmp++;
146   
147   if (!(buf = strdup(tmp)))
148         return;
149
150   if (strlen(tool) >= strlen(tmp)) {
151         free(buf);
152         return;
153   }
154   tmp = buf+(strlen(tmp)-strlen(tool));
155   
156   if (strcmp(tmp, tool)!=0) {
157         free(buf);
158         return;
159   }
160   
161   *tmp = '\0';
162   if ((tmp = strchr(buf,'_')))
163         *tmp = '\0';
164   
165   afname[0]='\0';
166   if (aftrans_opt(buf))
167         strcpy(afname, buf);
168
169   free(buf);
170 }
171
172
173 /* Check our protocol family table for this family. */
174 struct aftype *
175 get_aftype(const char *name)
176 {
177   struct aftype **afp;
178
179   if (!sVafinit)
180     afinit ();
181   
182   afp = aftypes;
183   while (*afp != NULL) {
184         if (!strcmp((*afp)->name, name)) return(*afp);
185         afp++;
186   }
187   if (index(name,','))
188         fprintf(stderr,_("Please don't supply more than one address family.\n"));
189   return(NULL);
190 }
191
192
193 /* Check our protocol family table for this family. */
194 struct aftype *
195 get_afntype(int af)
196 {
197   struct aftype **afp;
198
199   if (!sVafinit)
200     afinit ();
201   
202   afp = aftypes;
203   while (*afp != NULL) {
204         if ((*afp)->af == af) return(*afp);
205         afp++;
206   }
207   return(NULL);
208 }
209
210
211 int aftrans_opt(const char *arg)
212 {
213         struct aftrans_t *paft;
214         char *tmp1, *tmp2;
215         char buf[256];
216                 
217         strncpy(buf,arg,sizeof(buf));
218         buf[sizeof(buf)-1]='\0';
219         
220         tmp1=buf;
221         
222         while(tmp1) {
223         
224                 tmp2=index(tmp1,',');
225
226                 if (tmp2)
227                         *(tmp2++)='\0';
228                         
229                 paft=aftrans;
230                 for(paft=aftrans;paft->alias;paft++) {
231                         if (strcmp(tmp1,paft->alias))
232                                 continue;
233                         if (strlen(paft->name)+strlen(afname)+1 >= sizeof(afname)) {
234                                 fprintf(stderr,_("Too much address family arguments.\n"));                              
235                                 return(0);
236                         }
237                         if (paft->flag)
238                                 (*paft->flag)++;
239                         if (afname[0])
240                                 strcat(afname,",");
241                         strcat(afname,paft->name);
242                         break;
243                 }
244                 if (!paft->alias) {
245                         fprintf(stderr,_("Unknown address family `%s'.\n"),tmp1);                       
246                         return(1);
247                 }
248                 tmp1=tmp2;
249         }
250
251         return(0);
252 }