enable -fno-strict-aliasing until the code base gets a hefty clean up to fix all...
[platform/upstream/net-tools.git] / lib / ax25.c
1 /*
2  * lib/ax25.c This file contains an implementation of the "AX.25"
3  *              support functions.
4  *
5  * Version:     $Id: ax25.c,v 1.9 1999/09/27 11:00:45 philip Exp $
6  *
7  * NOTE:        I will redo this module as soon as I got the libax25.a
8  *              library sorted out.  This library contains some useful
9  *              and often used address conversion functions, database
10  *              lookup stuff, and more of the like.
11  *
12  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
13  *              Copyright 1993 MicroWalt Corporation
14  *
15  *              This program is free software; you can redistribute it
16  *              and/or  modify it under  the terms of  the GNU General
17  *              Public  License as  published  by  the  Free  Software
18  *              Foundation;  either  version 2 of the License, or  (at
19  *              your option) any later version.
20  */
21 #include "config.h"
22
23 #if HAVE_AFAX25 || HAVE_HWAX25
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
27 #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
28 #include <netax25/ax25.h>
29 #else
30 #include <linux/ax25.h>
31 #endif
32 #include <net/if_arp.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <string.h>
39 #include <termios.h>
40 #include <unistd.h>
41 #include "net-support.h"
42 #include "pathnames.h"
43 #include "intl.h"
44 #include "util.h"
45
46 static char AX25_errmsg[128];
47
48 extern struct aftype ax25_aftype;
49
50 static char *AX25_print(unsigned char *ptr)
51 {
52     static char buff[8];
53     int i;
54
55     for (i = 0; i < 6; i++) {
56         buff[i] = ((ptr[i] & 0377) >> 1);
57         if (buff[i] == ' ')
58             buff[i] = '\0';
59     }
60     buff[6] = '\0';
61     i = ((ptr[6] & 0x1E) >> 1);
62     if (i != 0)
63         sprintf(&buff[strlen(buff)], "-%d", i);
64     return (buff);
65 }
66
67
68 /* Display an AX.25 socket address. */
69 static char *
70  AX25_sprint(struct sockaddr *sap, int numeric)
71 {
72     static char buf[64];
73
74     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
75         return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
76     return (AX25_print(((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call));
77 }
78
79
80 static int AX25_input(int type, char *bufp, struct sockaddr *sap)
81 {
82     unsigned char *ptr;
83     char *orig, c;
84     int i;
85
86     sap->sa_family = ax25_aftype.af;
87     ptr = ((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call;
88
89     /* First, scan and convert the basic callsign. */
90     orig = bufp;
91     i = 0;
92     while ((*bufp != '\0') && (*bufp != '-') && (i < 6)) {
93         c = *bufp++;
94         if (islower(c))
95             c = toupper(c);
96         if (!(isupper(c) || isdigit(c))) {
97             safe_strncpy(AX25_errmsg, _("Invalid callsign"), sizeof(AX25_errmsg));
98 #ifdef DEBUG
99             fprintf(stderr, "ax25_input(%s): %s !\n", AX25_errmsg, orig);
100 #endif
101             errno = EINVAL;
102             return (-1);
103         }
104         *ptr++ = (unsigned char) ((c << 1) & 0xFE);
105         i++;
106     }
107
108     /* Callsign too long? */
109     if ((i == 6) && (*bufp != '-') && (*bufp != '\0')) {
110         strcpy(AX25_errmsg, _("Callsign too long"));
111 #ifdef DEBUG
112         fprintf(stderr, "ax25_input(%s): %s !\n", AX25_errmsg, orig);
113 #endif
114         errno = E2BIG;
115         return (-1);
116     }
117     /* Nope, fill out the address bytes with blanks. */
118     while (i++ < sizeof(ax25_address) - 1) {
119         *ptr++ = (unsigned char) ((' ' << 1) & 0xFE);
120     }
121
122     /* See if we need to add an SSID field. */
123     if (*bufp == '-') {
124         i = atoi(++bufp);
125         *ptr = (unsigned char) ((i << 1) & 0xFE);
126     } else {
127         *ptr = (unsigned char) '\0';
128     }
129
130     /* All done. */
131 #ifdef DEBUG
132     fprintf(stderr, "ax25_input(%s): ", orig);
133     for (i = 0; i < sizeof(ax25_address); i++)
134         fprintf(stderr, "%02X ", sap->sa_data[i] & 0377);
135     fprintf(stderr, "\n");
136 #endif
137
138     return (0);
139 }
140
141
142 /* Display an error message. */
143 static void AX25_herror(char *text)
144 {
145     if (text == NULL)
146         fprintf(stderr, "%s\n", AX25_errmsg);
147     else
148         fprintf(stderr, "%s: %s\n", text, AX25_errmsg);
149 }
150
151
152 static int AX25_hinput(char *bufp, struct sockaddr *sap)
153 {
154     if (AX25_input(0, bufp, sap) < 0)
155         return (-1);
156     sap->sa_family = ARPHRD_AX25;
157     return (0);
158 }
159
160 #if 0
161 /* Set the line discipline of a terminal line. */
162 static int KISS_set_disc(int fd, int disc)
163 {
164     if (ioctl(fd, TIOCSETD, &disc) < 0) {
165         fprintf(stderr, "KISS_set_disc(%d): %s\n", disc, strerror(errno));
166         return (-errno);
167     }
168     return (0);
169 }
170
171
172 /* Start the KISS encapsulation on the file descriptor. */
173 static int KISS_init(int fd)
174 {
175     if (KISS_set_disc(fd, N_SLIP) < 0)
176         return (-1);
177     if (ioctl(fd, SIOCSIFENCAP, 4) < 0)
178         return (-1);
179     return (0);
180 }
181 #endif
182
183 struct hwtype ax25_hwtype =
184 {
185     "ax25", NULL, /*"AMPR AX.25", */ ARPHRD_AX25, 7,
186     AX25_print, AX25_hinput, NULL
187 };
188
189 struct aftype ax25_aftype =
190 {
191     "ax25", NULL, /*"AMPR AX.25", */ AF_AX25, 7,
192     AX25_print, AX25_sprint, AX25_input, AX25_herror,
193     NULL, NULL, NULL,
194     -1,
195     "/proc/net/ax25"
196 };
197
198 #endif                          /* HAVE_xxAX25 */