patch from Alex Belits <abelits@phobos.illtel.denver.co.us> to support
[platform/upstream/net-tools.git] / lib / strip.c
1 /*
2  * lib/strip.c  This file contains an implementation of the STRIP
3  *              support functions.
4  *
5  * Version:     strip.c 1.20 1999/04/22 eswierk
6  *
7  * Author:      Stuart Cheshire <cheshire@cs.stanford.edu>
8  *
9  *              This program is free software; you can redistribute it
10  *              and/or  modify it under  the terms of  the GNU General
11  *              Public  License as  published  by  the  Free  Software
12  *              Foundation;  either  version 2 of the License, or  (at
13  *              your option) any later version.
14  */
15 #include "config.h"
16
17 #if HAVE_HWSTRIP
18
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <net/if_arp.h>
23 #include <linux/types.h>
24 #include <linux/if_strip.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <unistd.h>
33 #include "net-support.h"
34 #include "pathnames.h"
35 #include "util.h"
36 #include "intl.h"
37
38
39 extern struct hwtype strip_hwtype;
40
41 static char *
42 pr_strip(unsigned char *ptr)
43 {
44   static char buff[64];
45   if(ptr[1])
46       sprintf(buff, "%02x-%02x%02x-%02x%02x", *(ptr+1), *(ptr+2), *(ptr+3),
47           *(ptr+4), *(ptr+5));
48    else
49       sprintf(buff, "%02x%02x-%02x%02x", *(ptr+2), *(ptr+3), *(ptr+4),
50           *(ptr+5));
51   return buff;
52 }
53
54 static int
55 in_strip(char *bufp, struct sockaddr *sap)
56 {
57   int i,i0;
58   MetricomAddress *haddr = (MetricomAddress *) (sap->sa_data);
59
60
61   sap->sa_family = strip_hwtype.type;
62
63   /* figure out what the device-address should be */
64   i0 = i = (bufp[0] == '*') ? 1 : 0;
65
66   while (bufp[i] && (bufp[i] != '-'))
67     i++;
68
69   if (bufp[i] != '-')
70     return -1;
71
72   if(i-i0 == 2)
73   {
74      haddr->c[1] = strtol(&bufp[i0], 0, 16);
75      i++;
76      if(bufp[i] == 0) return -1;
77   }else{
78      haddr->c[1] = 0;
79      i=i0;
80   }
81   haddr->c[2] = strtol(&bufp[i], 0, 16) >> 8;
82   haddr->c[3] = strtol(&bufp[i], 0, 16) & 0xFF;
83
84   while (bufp[i] && (bufp[i] != '-'))
85     i++;
86
87   if (bufp[i] != '-')
88     return -1;
89
90   haddr->c[4] = strtol(&bufp[i+1], 0, 16) >> 8;
91   haddr->c[5] = strtol(&bufp[i+1], 0, 16) & 0xFF;
92   haddr->c[0] = 0;
93
94   return 0;
95 }
96
97
98
99 /* Start the STRIP encapsulation on the file descriptor. */
100 static int do_strip(int fd)
101         {
102         int disc = N_STRIP;
103         if (ioctl(fd, TIOCSETD, &disc) < 0)
104                 {
105                 fprintf(stderr, "STRIP_set_disc(%d): %s\n", disc, strerror(errno));
106                 return(-errno);
107                 }
108         return(0);
109         }
110
111 struct hwtype strip_hwtype = {
112   "strip", "Metricom Starmode IP", ARPHRD_METRICOM, sizeof(MetricomAddress),
113   pr_strip, in_strip, do_strip, 0
114 };
115
116 #endif  /* HAVE_HWSTRIP */