enable -fno-strict-aliasing until the code base gets a hefty clean up to fix all...
[platform/upstream/net-tools.git] / lib / ib.c
1 /*
2  * lib/ib.c        This file contains an implementation of the "Infiniband"
3  *              support functions.
4  *
5  * Version:     $Id: ib.c,v 1.1 2008/10/03 01:52:03 ecki Exp $
6  *
7  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
8  *              Copyright 1993 MicroWalt Corporation
9  *              Tom Duffy <tduffy@sun.com>
10  *
11  *              This program is free software; you can redistribute it
12  *              and/or  modify it under  the terms of  the GNU General
13  *              Public  License as  published  by  the  Free  Software
14  *              Foundation;  either  version 2 of the License, or  (at
15  *              your option) any later version.
16  */
17 #include "config.h"
18
19 #if HAVE_HWIB
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <net/if_arp.h>
23 #include <linux/if_infiniband.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include "net-support.h"
31 #include "pathnames.h"
32 #include "intl.h"
33 #include "util.h"
34
35 extern struct hwtype ib_hwtype;
36
37
38 /* Display an InfiniBand address in readable format. */
39 static char *pr_ib(unsigned char *ptr)
40 {
41     static char buff[128];
42     char *pos;
43     unsigned int i;
44
45     pos = buff;
46     for (i = 0; i < INFINIBAND_ALEN; i++) {
47         pos += sprintf(pos, "%02X:", (*ptr++ & 0377));
48     }
49     buff[strlen(buff) - 1] = '\0';
50
51     /* snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
52              (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
53              (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
54         );
55     */
56     return (buff);
57 }
58
59
60 /* Input an Infiniband address and convert to binary. */
61 static int in_ib(char *bufp, struct sockaddr *sap)
62 {
63     unsigned char *ptr;
64     char c, *orig;
65     int i;
66     unsigned val;
67
68     sap->sa_family = ib_hwtype.type;
69     ptr = sap->sa_data;
70
71     i = 0;
72     orig = bufp;
73     while ((*bufp != '\0') && (i < INFINIBAND_ALEN)) {
74         val = 0;
75         c = *bufp++;
76         if (isdigit(c))
77             val = c - '0';
78         else if (c >= 'a' && c <= 'f')
79             val = c - 'a' + 10;
80         else if (c >= 'A' && c <= 'F')
81             val = c - 'A' + 10;
82         else {
83 #ifdef DEBUG
84             fprintf(stderr, _("in_ib(%s): invalid infiniband address!\n"), orig);
85 #endif
86             errno = EINVAL;
87             return (-1);
88         }
89         val <<= 4;
90         c = *bufp;
91         if (isdigit(c))
92             val |= c - '0';
93         else if (c >= 'a' && c <= 'f')
94             val |= c - 'a' + 10;
95         else if (c >= 'A' && c <= 'F')
96             val |= c - 'A' + 10;
97         else if (c == ':' || c == 0)
98             val >>= 4;
99         else {
100 #ifdef DEBUG
101             fprintf(stderr, _("in_ib(%s): invalid infiniband address!\n"), orig);
102 #endif
103             errno = EINVAL;
104             return (-1);
105         }
106         if (c != 0)
107             bufp++;
108         *ptr++ = (unsigned char) (val & 0377);
109         i++;
110
111         /* We might get a semicolon here - not required. */
112         if (*bufp == ':') {
113             if (i == INFINIBAND_ALEN) {
114 #ifdef DEBUG
115                 fprintf(stderr, _("in_ib(%s): trailing : ignored!\n"),
116                         orig)
117 #endif
118                     ;           /* nothing */
119             }
120             bufp++;
121         }
122     }
123
124     /* That's it.  Any trailing junk? */
125     if ((i == INFINIBAND_ALEN) && (*bufp != '\0')) {
126 #ifdef DEBUG
127         fprintf(stderr, _("in_ib(%s): trailing junk!\n"), orig);
128         errno = EINVAL;
129         return (-1);
130 #endif
131     }
132 #ifdef DEBUG
133     fprintf(stderr, "in_ib(%s): %s\n", orig, pr_ib(sap->sa_data));
134 #endif
135
136     return (0);
137 }
138
139
140 struct hwtype ib_hwtype =
141 {
142     "infiniband", NULL, ARPHRD_INFINIBAND, INFINIBAND_ALEN,
143     pr_ib, in_ib, NULL
144 };
145
146
147 #endif                          /* HAVE_HWIB */