simple patch from Ajax <ajax@firest0rm.org> (corrected) to print DDP
[oss-qm-packages.git] / lib / x25.c
blob1ffb569059a44693ad84a19a8a2d9f083f91ca8a
1 /*
2 * lib/x25.c This file contains an implementation of the "X.25"
3 * support functions for the NET-3 base distribution.
5 * Version: @(#)x25.c 1.00 08/15/98
7 * Author: Stephane Fillod, <sfillod@charybde.gyptis.frmug.org>
8 * based on ax25.c by:
9 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
10 * Copyright 1993 MicroWalt Corporation
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General
14 * Public License as published by the Free Software
15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version.
18 #include "config.h"
20 #if HAVE_AFX25 || HAVE_HWX25
21 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <linux/x25.h>
25 #include <net/if_arp.h> /* ARPHRD_X25 */
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <termios.h>
33 #include <unistd.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"
42 static char X25_errmsg[128];
45 extern struct aftype x25_aftype;
47 /* is in net/x25.h, not in the public header file linux/x25.h. Why?*/
48 #ifndef X25_ADDR_LEN
49 #define X25_ADDR_LEN 16
50 #endif
53 static char *
54 X25_print(unsigned char *ptr)
56 static char buff[X25_ADDR_LEN+1];
58 strncpy(buff, ptr, X25_ADDR_LEN);
59 buff[X25_ADDR_LEN] = '\0';
60 return(buff);
65 /* Display an X.25 socket address. */
66 static char *
67 X25_sprint(struct sockaddr *sap, int numeric)
69 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
70 return( _("[NONE SET]"));
71 return(X25_print(((struct sockaddr_x25 *)sap)->sx25_addr.x25_addr));
76 * return the sigdigits of the address
78 static int
79 X25_input(int type, char *bufp, struct sockaddr *sap)
81 unsigned char *ptr;
82 char *p;
83 unsigned int sigdigits;
85 sap->sa_family = x25_aftype.af;
86 ptr = ((struct sockaddr_x25 *)sap)->sx25_addr.x25_addr;
89 /* Address the correct length ? */
90 if (strlen(bufp)>18) {
91 strcpy(X25_errmsg, _("Address can't exceed eighteen digits with sigdigits"));
92 #ifdef DEBUG
93 fprintf(stderr, "x25_input(%s): %s !\n", X25_errmsg, orig);
94 #endif
95 errno = EINVAL;
96 return(-1);
100 if ((p = strchr(bufp, '/')) != NULL) {
101 *p = '\0';
102 sigdigits = atoi(p + 1);
103 } else {
104 sigdigits = strlen(bufp);
107 if (strlen(bufp) < 1 || strlen(bufp) > 15 || sigdigits > strlen(bufp)) {
108 *p = '/';
109 strcpy(X25_errmsg, _("Invalid address"));
110 #ifdef DEBUG
111 fprintf(stderr, "x25_input(%s): %s !\n", X25_errmsg, orig);
112 #endif
113 errno = EINVAL;
114 return(-1);
117 strncpy(ptr, bufp, sigdigits+1);
119 /* All done. */
120 #ifdef DEBUG
121 fprintf(stderr, "x25_input(%s): ", orig);
122 for (i = 0; i < sizeof(x25_address); i++)
123 fprintf(stderr, "%02X ", sap->sa_data[i] & 0377);
124 fprintf(stderr, "\n");
125 #endif
127 return sigdigits;
131 /* Display an error message. */
132 static void
133 X25_herror(char *text)
135 if (text == NULL) fprintf(stderr, "%s\n", X25_errmsg);
136 else fprintf(stderr, "%s: %s\n", text, X25_errmsg);
140 static int
141 X25_hinput(char *bufp, struct sockaddr *sap)
143 if (X25_input(0, bufp, sap) < 0) return(-1);
144 sap->sa_family = ARPHRD_X25;
145 return(0);
149 struct hwtype x25_hwtype = {
150 "x25", NULL, /*"CCITT X.25",*/ ARPHRD_X25, X25_ADDR_LEN,
151 X25_print, X25_hinput, NULL
154 struct aftype x25_aftype =
156 "x25", NULL, /*"CCITT X.25", */ AF_X25, X25_ADDR_LEN,
157 X25_print, X25_sprint, X25_input, X25_herror,
158 X25_rprint, X25_rinput, NULL /* getmask */,
160 "/proc/net/x25"
164 #endif /* HAVE_xxX25 */