OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [route/] [lib/] [ax25.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 745 simons
/*
2
 * lib/ax25.c This file contains an implementation of the "AX.25"
3
 *              support functions.
4
 *
5
 * Version:     $Id: ax25.c,v 1.1 2002-03-17 19:58:53 simons 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 char *AX25_hprint(struct sockaddr *sap)
153
{
154
    static char buf[64];
155
 
156
    if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
157
        return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
158
    return (AX25_print(((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call));
159
}
160
 
161
 
162
static int AX25_hinput(char *bufp, struct sockaddr *sap)
163
{
164
    if (AX25_input(0, bufp, sap) < 0)
165
        return (-1);
166
    sap->sa_family = ARPHRD_AX25;
167
    return (0);
168
}
169
 
170
#if 0
171
/* Set the line discipline of a terminal line. */
172
static int KISS_set_disc(int fd, int disc)
173
{
174
    if (ioctl(fd, TIOCSETD, &disc) < 0) {
175
        fprintf(stderr, "KISS_set_disc(%d): %s\n", disc, strerror(errno));
176
        return (-errno);
177
    }
178
    return (0);
179
}
180
 
181
 
182
/* Start the KISS encapsulation on the file descriptor. */
183
static int KISS_init(int fd)
184
{
185
    if (KISS_set_disc(fd, N_SLIP) < 0)
186
        return (-1);
187
    if (ioctl(fd, SIOCSIFENCAP, 4) < 0)
188
        return (-1);
189
    return (0);
190
}
191
#endif
192
 
193
struct hwtype ax25_hwtype =
194
{
195
    "ax25", NULL, /*"AMPR AX.25", */ ARPHRD_AX25, 7,
196
    AX25_print, AX25_hprint, AX25_hinput, NULL
197
};
198
 
199
struct aftype ax25_aftype =
200
{
201
    "ax25", NULL, /*"AMPR AX.25", */ AF_AX25, 7,
202
    AX25_print, AX25_sprint, AX25_input, AX25_herror,
203
    NULL, NULL, NULL,
204
    -1,
205
    "/proc/net/ax25"
206
};
207
 
208
#endif                          /* HAVE_xxAX25 */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.