Add STRIP 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
46   sprintf(buff, "%02x%02x-%02x%02x", *(ptr+2), *(ptr+3), *(ptr+4),
47           *(ptr+5));
48   return buff;
49 }
50
51 static int
52 in_strip(char *bufp, struct sockaddr *sap)
53 {
54   int i;
55   MetricomAddress *haddr = (MetricomAddress *) (sap->sa_data);
56
57
58   sap->sa_family = strip_hwtype.type;
59
60   /* figure out what the device-address should be */
61   i = (bufp[0] == '*') ? 1 : 0;
62   haddr->c[2] = strtol(&bufp[i], 0, 16) >> 8;
63   haddr->c[3] = strtol(&bufp[i], 0, 16) & 0xFF;
64
65   while (bufp[i] && (bufp[i] != '-'))
66     i++;
67
68   if (bufp[i] != '-')
69     return -1;
70
71   haddr->c[4] = strtol(&bufp[i+1], 0, 16) >> 8;
72   haddr->c[5] = strtol(&bufp[i+1], 0, 16) & 0xFF;
73   haddr->c[0] = 0;
74   haddr->c[1] = 0;
75
76   return 0;
77 }
78
79
80
81 /* Start the STRIP encapsulation on the file descriptor. */
82 static int do_strip(int fd)
83         {
84         int disc = N_STRIP;
85         if (ioctl(fd, TIOCSETD, &disc) < 0)
86                 {
87                 fprintf(stderr, "STRIP_set_disc(%d): %s\n", disc, strerror(errno));
88                 return(-errno);
89                 }
90         return(0);
91         }
92
93 struct hwtype strip_hwtype = {
94   "strip", "Metricom Starmode IP", ARPHRD_METRICOM, sizeof(MetricomAddress),
95   pr_strip, in_strip, do_strip, 0
96 };
97
98 #endif  /* HAVE_HWSTRIP */