home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NET-TOOL.1 / NET-TOOL / net-tools / lib / ax25.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-06  |  4.4 KB  |  196 lines

  1. /*
  2.  * NET-2    This file contains an implementation of the "AX.25"
  3.  *        support functions for the NET-2 base distribution.
  4.  *
  5.  * Version:    @(#)ax25.c    1.20    12/16/93
  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. #include <linux/ax25.h>
  28. #include <linux/if_arp.h>    /* ARPHRD_AX25 */
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <errno.h>
  33. #include <fcntl.h>
  34. #include <string.h>
  35. #include <termios.h>
  36. #include <unistd.h>
  37. #include "support.h"
  38. #include "pathnames.h"
  39.  
  40.  
  41. static char AX25_errmsg[128];
  42.  
  43.  
  44. extern struct aftype ax25_aftype;
  45.  
  46.  
  47. static char *
  48. AX25_print(unsigned char *ptr)
  49. {
  50.   static char buff[8];
  51.   int i;
  52.  
  53.   for (i = 0; i < 6; i++) {
  54.     buff[i] = ((ptr[i] & 0377) >> 1);
  55.     if (buff[i] == ' ') buff[i] = '\0';
  56.   }
  57.   buff[6] = '\0';
  58.   i = ((ptr[6] & 0x1E) >> 1);
  59.   if (i != 0) sprintf(&buff[strlen(buff)], "-%d", i);
  60.   return(buff);
  61. }
  62.  
  63.  
  64. /* Display an AX.25 socket address. */
  65. static char *
  66. AX25_sprint(struct sockaddr *sap, int numeric)
  67. {
  68.   if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return("[NONE SET]");
  69.   return(AX25_print(((struct sockaddr_ax25 *)sap)->sax25_call.ax25_call));
  70. }
  71.  
  72.  
  73. static int
  74. AX25_input(char *bufp, struct sockaddr *sap)
  75. {
  76.   unsigned char *ptr;
  77.   char *orig, c;
  78.   int i;
  79.  
  80.   sap->sa_family = ax25_aftype.af;
  81.   ptr = ((struct sockaddr_ax25 *)sap)->sax25_call.ax25_call;
  82.  
  83.   /* First, scan and convert the basic callsign. */
  84.   orig = bufp;
  85.   i = 0;
  86.   while((*bufp != '\0') && (*bufp != '-') && (i < 6)) {
  87.     c = *bufp++;
  88.     if (islower(c)) c = toupper(c);
  89.     if (! (isupper(c) || isdigit(c))) {
  90.         strcpy(AX25_errmsg, "Invalid callsign");
  91. #ifdef DEBUG
  92.         fprintf(stderr, "ax25_input(%s): %s !\n", AX25_errmsg, orig);
  93. #endif
  94.         errno = EINVAL;
  95.         return(-1);
  96.     }
  97.     *ptr++ = (unsigned char) ((c << 1) & 0xFE);
  98.     i++;
  99.   }
  100.  
  101.   /* Callsign too long? */
  102.   if ((i == 6) && (*bufp != '-') && (*bufp != '\0')) {
  103.     strcpy(AX25_errmsg, "Callsign too long");
  104. #ifdef DEBUG
  105.     fprintf(stderr, "ax25_input(%s): %s !\n", AX25_errmsg, orig);
  106. #endif
  107.     errno = E2BIG;
  108.     return(-1);
  109.   }
  110.  
  111.   /* Nope, fill out the address bytes with blanks. */
  112.   while (i++ < sizeof(ax25_address)-1) {
  113.     *ptr++ = (unsigned char) ((' ' << 1) & 0xFE);
  114.   }
  115.  
  116.   /* See if we need to add an SSID field. */
  117.   if (*bufp == '-') {
  118.     i = atoi(++bufp);
  119.     *ptr = (unsigned char) ((i << 1) & 0xFE);
  120.   } else {
  121.     *ptr = (unsigned char) '\0';
  122.   }
  123.  
  124.   /* All done. */
  125. #ifdef DEBUG
  126.   fprintf(stderr, "ax25_input(%s): ", orig);
  127.   for (i = 0; i < sizeof(ax25_address); i++)
  128.     fprintf(stderr, "%02X ", sap->sa_data[i] & 0377);
  129.   fprintf(stderr, "\n");
  130. #endif
  131.  
  132.   return(0);
  133. }
  134.  
  135.  
  136. /* Display an error message. */
  137. static void
  138. AX25_herror(char *text)
  139. {
  140.   if (text == NULL) fprintf(stderr, "%s\n", AX25_errmsg);
  141.     else fprintf(stderr, "%s: %s\n", text, AX25_errmsg);
  142. }
  143.  
  144.  
  145. static char *
  146. AX25_hprint(struct sockaddr *sap)
  147. {
  148.   if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return("[NONE SET]");
  149.   return(AX25_print(((struct sockaddr_ax25 *)sap)->sax25_call.ax25_call));
  150. }
  151.  
  152.  
  153. static int
  154. AX25_hinput(char *bufp, struct sockaddr *sap)
  155. {
  156.   if (AX25_input(bufp, sap) < 0) return(-1);
  157.   sap->sa_family = ARPHRD_AX25;
  158.   return(0);
  159. }
  160.  
  161.  
  162. /* Set the line discipline of a terminal line. */
  163. static int
  164. KISS_set_disc(int fd, int disc)
  165. {
  166.   if (ioctl(fd, TIOCSETD, &disc) < 0) {
  167.     fprintf(stderr, "KISS_set_disc(%d): %s\n", disc, strerror(errno));
  168.     return(-errno);
  169.   }
  170.   return(0);
  171. }
  172.  
  173.  
  174. /* Start the KISS encapsulation on the file descriptor. */
  175. static int
  176. KISS_init(int fd)
  177. {
  178.   if (KISS_set_disc(fd, N_SLIP) < 0) return(-1);
  179.   if (ioctl(fd, SIOCSIFENCAP, 4) <0) return(-1);
  180.   return(0);
  181. }
  182.  
  183.  
  184. struct hwtype ax25_hwtype = {
  185.   "ax25",    "AMPR AX.25",        ARPHRD_AX25,    7,
  186.   AX25_print,    AX25_hprint,        AX25_hinput,    KISS_init
  187. };
  188.  
  189. struct aftype ax25_aftype = {
  190.   "ax25",    "AMPR AX.25",        AF_AX25,    7,
  191.   AX25_print,    AX25_sprint,        AX25_input,    AX25_herror
  192. };
  193.  
  194.  
  195. #endif    /* HAVE_xxAX25 */
  196.