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

Subversion Repositories or1k

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 745 simons
/*
2
 * lib/netrom.c       This file contains an implementation of the "NET/ROM"
3
 *              support functions for the NET-2 base distribution.
4
 *
5
 * Version:     $Id: netrom.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
 * Changes:
16
 * 980701 {1.21} Arnaldo Carvalho de Melo - GNU gettext instead of catgets,
17
 *                                          strncpy instead of strcpy for
18
 *                                          i18n strings
19
 *
20
 *              This program is free software; you can redistribute it
21
 *              and/or  modify it under  the terms of  the GNU General
22
 *              Public  License as  published  by  the  Free  Software
23
 *              Foundation;  either  version 2 of the License, or  (at
24
 *              your option) any later version.
25
 */
26
#include "config.h"
27
 
28
#if HAVE_AFNETROM || HAVE_HWNETROM
29
#include <sys/types.h>
30
#include <sys/ioctl.h>
31
#include <sys/socket.h>
32
#include <net/if_arp.h>
33
#if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
34
#include <netax25/ax25.h>
35
#else
36
#include <linux/ax25.h>
37
#endif
38
#include <stdlib.h>
39
#include <stdio.h>
40
#include <ctype.h>
41
#include <errno.h>
42
#include <fcntl.h>
43
#include <string.h>
44
#include <termios.h>
45
#include <unistd.h>
46
#include "net-support.h"
47
#include "pathnames.h"
48
#include "intl.h"
49
#include "util.h"
50
 
51
static char netrom_errmsg[128];
52
 
53
extern struct aftype netrom_aftype;
54
 
55
static char *NETROM_print(unsigned char *ptr)
56
{
57
    static char buff[8];
58
    int i;
59
 
60
    for (i = 0; i < 6; i++) {
61
        buff[i] = ((ptr[i] & 0377) >> 1);
62
        if (buff[i] == ' ')
63
            buff[i] = '\0';
64
    }
65
    buff[6] = '\0';
66
    i = ((ptr[6] & 0x1E) >> 1);
67
    if (i != 0)
68
        sprintf(&buff[strlen(buff)], "-%d", i);
69
    return (buff);
70
}
71
 
72
 
73
/* Display an AX.25 socket address. */
74
static char *NETROM_sprint(struct sockaddr *sap, int numeric)
75
{
76
    char buf[64];
77
    if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
78
        return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
79
    return (NETROM_print(((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call));
80
}
81
 
82
 
83
static int NETROM_input(int type, char *bufp, struct sockaddr *sap)
84
{
85
    unsigned char *ptr;
86
    char *orig, c;
87
    unsigned int i;
88
 
89
    sap->sa_family = netrom_aftype.af;
90
    ptr = ((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call;
91
 
92
    /* First, scan and convert the basic callsign. */
93
    orig = bufp;
94
    i = 0;
95
    while ((*bufp != '\0') && (*bufp != '-') && (i < 6)) {
96
        c = *bufp++;
97
        if (islower(c))
98
            c = toupper(c);
99
        if (!(isupper(c) || isdigit(c))) {
100
            safe_strncpy(netrom_errmsg, _("Invalid callsign"), sizeof(netrom_errmsg));
101
#ifdef DEBUG
102
            fprintf(stderr, "netrom_input(%s): %s !\n", netrom_errmsg, orig);
103
#endif
104
            errno = EINVAL;
105
            return (-1);
106
        }
107
        *ptr++ = (unsigned char) ((c << 1) & 0xFE);
108
        i++;
109
    }
110
 
111
    /* Callsign too long? */
112
    if ((i == 6) && (*bufp != '-') && (*bufp != '\0')) {
113
        safe_strncpy(netrom_errmsg, _("Callsign too long"), sizeof(netrom_errmsg));
114
#ifdef DEBUG
115
        fprintf(stderr, "netrom_input(%s): %s !\n", netrom_errmsg, orig);
116
#endif
117
        errno = E2BIG;
118
        return (-1);
119
    }
120
    /* Nope, fill out the address bytes with blanks. */
121
    while (i++ < sizeof(ax25_address) - 1) {
122
        *ptr++ = (unsigned char) ((' ' << 1) & 0xFE);
123
    }
124
 
125
    /* See if we need to add an SSID field. */
126
    if (*bufp == '-') {
127
        i = atoi(++bufp);
128
        *ptr = (unsigned char) ((i << 1) & 0xFE);
129
    } else {
130
        *ptr = (unsigned char) '\0';
131
    }
132
 
133
    /* All done. */
134
#ifdef DEBUG
135
    fprintf(stderr, "netrom_input(%s): ", orig);
136
    for (i = 0; i < sizeof(ax25_address); i++)
137
        fprintf(stderr, "%02X ", sap->sa_data[i] & 0377);
138
    fprintf(stderr, "\n");
139
#endif
140
 
141
    return (0);
142
}
143
 
144
 
145
/* Display an error message. */
146
static void NETROM_herror(char *text)
147
{
148
    if (text == NULL)
149
        fprintf(stderr, "%s\n", netrom_errmsg);
150
    else
151
        fprintf(stderr, "%s: %s\n", text, netrom_errmsg);
152
}
153
 
154
 
155
static char *NETROM_hprint(struct sockaddr *sap)
156
{
157
    if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
158
        return ("[NONE SET]");
159
    return (NETROM_print(((struct sockaddr_ax25 *) sap)->sax25_call.ax25_call));
160
}
161
 
162
 
163
static int NETROM_hinput(char *bufp, struct sockaddr *sap)
164
{
165
    if (NETROM_input(0, bufp, sap) < 0)
166
        return (-1);
167
    sap->sa_family = ARPHRD_NETROM;
168
    return (0);
169
}
170
 
171
#if 0
172
/* Set the line discipline of a terminal line. */
173
static int KISS_set_disc(int fd, int disc)
174
{
175
    if (ioctl(fd, TIOCSETD, &disc) < 0) {
176
        fprintf(stderr, "KISS_set_disc(%d): %s\n", disc, strerror(errno));
177
        return (-errno);
178
    }
179
    return (0);
180
}
181
 
182
 
183
/* Start the KISS encapsulation on the file descriptor. */
184
static int KISS_init(int fd)
185
{
186
    if (KISS_set_disc(fd, N_SLIP) < 0)
187
        return (-1);
188
    if (ioctl(fd, SIOCSIFENCAP, 4) < 0)
189
        return (-1);
190
    return (0);
191
}
192
#endif
193
 
194
struct hwtype netrom_hwtype =
195
{
196
    "netrom", NULL, /* "AMPR NET/ROM", */ ARPHRD_NETROM, 7,
197
    NETROM_print, NETROM_hprint, NETROM_hinput, NULL
198
};
199
 
200
struct aftype netrom_aftype =
201
{
202
    "netrom", NULL, /* "AMPR NET/ROM", */ AF_NETROM, 7,
203
    NETROM_print, NETROM_sprint, NETROM_input, NETROM_herror,
204
    NULL, NULL, NULL,
205
    -1,
206
    "/proc/net/nr"
207
};
208
 
209
#endif                          /* HAVE_AFNETROM */

powered by: WebSVN 2.1.0

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