Rework socket handling again. Rather than
[platform/upstream/net-tools.git] / lib / rose.c
1 /*
2  * lib/rose.c   This file contains an implementation of the "ROSE"
3  *              support functions for the NET-2 base distribution.
4  *
5  * Version:     @(#)rose.c      1.00    03/08/97
6  *
7  * Author:      Terry Dawson, VK2KTJ, <terry@perf.no.itg.telstra.com.au>
8  *              based on ax25.c by:
9  *              Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
10  *              Copyright 1993 MicroWalt Corporation
11  *
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.
17  */
18 #include "config.h"
19
20 #if HAVE_AFROSE || HAVE_HWROSE
21 #include <features.h>
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <net/if_arp.h> /* ARPHRD_ROSE */
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 #include "intl.h"
37
38 #if __GLIBC__ >= 2
39 #include <netrose/rose.h>
40 #endif
41
42 static char ROSE_errmsg[128];
43
44 extern struct aftype rose_aftype;
45
46 static char *
47 ROSE_print(unsigned char *ptr)
48 {
49   static char buff[12];
50
51   snprintf(buff,sizeof(buff),"%02x%02x%02x%02x%02x",ptr[0],ptr[1],ptr[2],ptr[3],ptr[4]);
52   buff[10] = '\0';
53   return(buff);
54 }
55
56 /* Display a ROSE socket address. */
57 static char *
58 ROSE_sprint(struct sockaddr *sap, int numeric)
59 {
60   if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
61     return _("[NONE SET]");
62
63   return(ROSE_print(((struct sockaddr_rose *)sap)->srose_addr.rose_addr));
64 }
65
66
67 static int
68 ROSE_input(int type, char *bufp, struct sockaddr *sap)
69 {
70   char *ptr;
71   int i,o;
72
73   sap->sa_family = rose_aftype.af;
74   ptr = ((struct sockaddr_rose *)sap)->srose_addr.rose_addr;
75
76   /* Node address the correct length ? */
77   if (strlen(bufp)!=10) {
78         strcpy(ROSE_errmsg, _("Node address must be ten digits"));
79 #ifdef DEBUG
80         fprintf(stderr, "rose_input(%s): %s !\n", ROSE_errmsg, orig);
81 #endif
82         errno = EINVAL;
83         return(-1);
84   }
85
86   /* Ok, lets set it */
87   for (i=0, o=0; i<5; i++) {                                   
88     o=i*2;                                                     
89     ptr[i]=(((bufp[o]-'0')<<4) | (bufp[o+1]-'0'));
90   }                               
91
92   /* All done. */
93 #ifdef DEBUG
94   fprintf(stderr, "rose_input(%s): ", orig);
95   for (i = 0; i < sizeof(rose_address); i++)
96         fprintf(stderr, "%02X ", sap->sa_data[i] & 0377);
97   fprintf(stderr, "\n");
98 #endif
99
100   return(0);
101 }
102
103
104 /* Display an error message. */
105 static void
106 ROSE_herror(char *text)
107 {
108   if (text == NULL) fprintf(stderr, "%s\n", ROSE_errmsg);
109     else fprintf(stderr, "%s: %s\n", text, ROSE_errmsg);
110 }
111
112
113 static char *
114 ROSE_hprint(struct sockaddr *sap)
115 {
116   if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
117     return _("[NONE SET]");
118
119   return(ROSE_print(((struct sockaddr_rose *)sap)->srose_addr.rose_addr));
120 }
121
122
123 static int
124 ROSE_hinput(char *bufp, struct sockaddr *sap)
125 {
126   if (ROSE_input(0, bufp, sap) < 0) return(-1);
127   sap->sa_family = ARPHRD_ROSE;
128   return(0);
129 }
130
131 struct hwtype rose_hwtype = {
132   "rose",       NULL, /*"AMPR ROSE",*/          ARPHRD_ROSE,    10,
133   ROSE_print,   ROSE_hprint,            ROSE_hinput,    NULL
134 };
135
136 struct aftype rose_aftype = {
137   "rose",       NULL, /*"AMPR ROSE",*/          AF_ROSE,        10,
138   ROSE_print,   ROSE_sprint,            ROSE_input,     ROSE_herror,    
139   NULL,         NULL,           NULL,
140   -1,
141   "/proc/net/rose"
142 };
143
144 #endif  /* HAVE_xxROSE */
145