fixing debian bug #151130 reported by Dan Jacobson where netstat
[oss-qm-packages.git] / lib / af.c
blob4f002c43681adfdade65a35e3b7a65ff55f1fcad
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.
5 * Version: $Id: af.c,v 1.13 2000/05/20 13:38:10 pb Exp $
7 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
8 * Copyright 1993 MicroWalt Corporation
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.
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 #include "util.h"
30 int flag_unx;
31 int flag_ipx;
32 int flag_ax25;
33 int flag_ddp;
34 int flag_netrom;
35 int flag_inet;
36 int flag_inet6;
37 int flag_econet;
38 int flag_x25 = 0;
39 int flag_ash;
42 struct aftrans_t {
43 char *alias;
44 char *name;
45 int *flag;
46 } aftrans[] = {
49 "ax25", "ax25", &flag_ax25
52 "ip", "inet", &flag_inet
55 "ip6", "inet6", &flag_inet6
58 "ipx", "ipx", &flag_ipx
61 "appletalk", "ddp", &flag_ddp
64 "netrom", "netrom", &flag_netrom
67 "inet", "inet", &flag_inet
70 "inet6", "inet6", &flag_inet6
73 "ddp", "ddp", &flag_ddp
76 "unix", "unix", &flag_unx
79 "tcpip", "inet", &flag_inet
82 "econet", "ec", &flag_econet
85 "x25", "x25", &flag_x25
88 "ash", "ash", &flag_ash
91 0, 0, 0
95 char afname[256] = "";
97 extern struct aftype unspec_aftype;
98 extern struct aftype unix_aftype;
99 extern struct aftype inet_aftype;
100 extern struct aftype inet6_aftype;
101 extern struct aftype ax25_aftype;
102 extern struct aftype netrom_aftype;
103 extern struct aftype ipx_aftype;
104 extern struct aftype ddp_aftype;
105 extern struct aftype ec_aftype;
106 extern struct aftype x25_aftype;
107 extern struct aftype rose_aftype;
108 extern struct aftype ash_aftype;
110 static short sVafinit = 0;
112 struct aftype *aftypes[] =
114 #if HAVE_AFUNIX
115 &unix_aftype,
116 #endif
117 #if HAVE_AFINET
118 &inet_aftype,
119 #endif
120 #if HAVE_AFINET6
121 &inet6_aftype,
122 #endif
123 #if HAVE_AFAX25
124 &ax25_aftype,
125 #endif
126 #if HAVE_AFNETROM
127 &netrom_aftype,
128 #endif
129 #if HAVE_AFROSE
130 &rose_aftype,
131 #endif
132 #if HAVE_AFIPX
133 &ipx_aftype,
134 #endif
135 #if HAVE_AFATALK
136 &ddp_aftype,
137 #endif
138 #if HAVE_AFECONET
139 &ec_aftype,
140 #endif
141 #if HAVE_AFASH
142 &ash_aftype,
143 #endif
144 #if HAVE_AFX25
145 &x25_aftype,
146 #endif
147 &unspec_aftype,
148 NULL
151 void afinit()
153 unspec_aftype.title = _("UNSPEC");
154 #if HAVE_AFUNIX
155 unix_aftype.title = _("UNIX Domain");
156 #endif
157 #if HAVE_AFINET
158 inet_aftype.title = _("DARPA Internet");
159 #endif
160 #if HAVE_AFINET6
161 inet6_aftype.title = _("IPv6");
162 #endif
163 #if HAVE_AFAX25
164 ax25_aftype.title = _("AMPR AX.25");
165 #endif
166 #if HAVE_AFNETROM
167 netrom_aftype.title = _("AMPR NET/ROM");
168 #endif
169 #if HAVE_AFIPX
170 ipx_aftype.title = _("Novell IPX");
171 #endif
172 #if HAVE_AFATALK
173 ddp_aftype.title = _("Appletalk DDP");
174 #endif
175 #if HAVE_AFECONET
176 ec_aftype.title = _("Econet");
177 #endif
178 #if HAVE_AFX25
179 x25_aftype.title = _("CCITT X.25");
180 #endif
181 #if HAVE_AFROSE
182 rose_aftype.title = _("AMPR ROSE");
183 #endif
184 #if HAVE_AFASH
185 ash_aftype.title = _("Ash");
186 #endif
187 sVafinit = 1;
190 /* set the default AF list from the program name or a constant value */
191 void aftrans_def(char *tool, char *argv0, char *dflt)
193 char *tmp;
194 char *buf;
196 strcpy(afname, dflt);
198 if (!(tmp = strrchr(argv0, '/')))
199 tmp = argv0; /* no slash?! */
200 else
201 tmp++;
203 if (!(buf = strdup(tmp)))
204 return;
206 if (strlen(tool) >= strlen(tmp)) {
207 free(buf);
208 return;
210 tmp = buf + (strlen(tmp) - strlen(tool));
212 if (strcmp(tmp, tool) != 0) {
213 free(buf);
214 return;
216 *tmp = '\0';
217 if ((tmp = strchr(buf, '_')))
218 *tmp = '\0';
220 afname[0] = '\0';
221 if (aftrans_opt(buf))
222 strcpy(afname, buf);
224 free(buf);
228 /* Check our protocol family table for this family. */
229 struct aftype *get_aftype(const char *name)
231 struct aftype **afp;
233 if (!sVafinit)
234 afinit();
236 afp = aftypes;
237 while (*afp != NULL) {
238 if (!strcmp((*afp)->name, name))
239 return (*afp);
240 afp++;
242 if (index(name, ','))
243 fprintf(stderr, _("Please don't supply more than one address family.\n"));
244 return (NULL);
248 /* Check our protocol family table for this family. */
249 struct aftype *get_afntype(int af)
251 struct aftype **afp;
253 if (!sVafinit)
254 afinit();
256 afp = aftypes;
257 while (*afp != NULL) {
258 if ((*afp)->af == af)
259 return (*afp);
260 afp++;
262 return (NULL);
265 /* Check our protocol family table for this family and return its socket */
266 int get_socket_for_af(int af)
268 struct aftype **afp;
270 if (!sVafinit)
271 afinit();
273 afp = aftypes;
274 while (*afp != NULL) {
275 if ((*afp)->af == af)
276 return (*afp)->fd;
277 afp++;
279 return -1;
282 int aftrans_opt(const char *arg)
284 struct aftrans_t *paft;
285 char *tmp1, *tmp2;
286 char buf[256];
288 safe_strncpy(buf, arg, sizeof(buf));
290 tmp1 = buf;
292 while (tmp1) {
294 tmp2 = index(tmp1, ',');
296 if (tmp2)
297 *(tmp2++) = '\0';
299 paft = aftrans;
300 for (paft = aftrans; paft->alias; paft++) {
301 if (strcmp(tmp1, paft->alias))
302 continue;
303 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
304 fprintf(stderr, _("Too much address family arguments.\n"));
305 return (0);
307 if (paft->flag)
308 (*paft->flag)++;
309 if (afname[0])
310 strcat(afname, ",");
311 strcat(afname, paft->name);
312 break;
314 if (!paft->alias) {
315 fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1);
316 return (1);
318 tmp1 = tmp2;
321 return (0);
324 /* type: 0=all, 1=getroute */
325 void print_aflist(int type) {
326 int count = 0;
327 char * txt;
328 struct aftype **afp;
330 if (!sVafinit)
331 afinit();
333 afp = aftypes;
334 while (*afp != NULL) {
335 if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
336 afp++; continue;
338 if ((count % 3) == 0) fprintf(stderr,count?"\n ":" ");
339 txt = (*afp)->name; if (!txt) txt = "..";
340 fprintf(stderr,"%s (%s) ",txt,(*afp)->title);
341 count++;
342 afp++;
344 fprintf(stderr,"\n");