This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnetmap_parse.y
160 lines (141 loc) · 5.75 KB
/
netmap_parse.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* This file is part of iouyap, a program to bridge IOU with
* network interfaces.
*
* Copyright (C) 2013, 2014 James E. Carpenter
*
* iouyap is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iouyap is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "netmap.h"
#include "iouyap.h"
extern int yylineno;
extern int yylex();
void yyerror(const char *msg);
unsigned int param_error = 0;
unsigned int errors_found = 0;
#define RANGE_CHECK(item, value, item_min, item_max) \
do { \
if (value < item_min || value > item_max) { \
log_fmt("invalid %s value (%d) (%d <= %s <= %d)\n", \
item, value, item_min, item, item_max); \
param_error = 1; \
errors_found++; \
} \
} while (0)
%}
// token types
%union {
int ival;
char *sval;
char pval[64];
}
%token <ival> INT
%token <sval> ADDRESS
%token ENDSEG
%token ENDNODE
%type <pval> params
%type <pval> host
%destructor { free($$); } <sval>
%%
netmap
: netmap segment_line
| segment_line
;
segment_line
: segment ENDSEG { end_segment(); }
;
segment
: segment node
| node
;
node
: INT params host ENDNODE {
RANGE_CHECK("ID", $1, ID_MIN, ID_MAX);
iou_node_tmp->ids.id = $1;
if (param_error) {
log_fmt("^^ NETMAP line %d: ignoring node %d%s%s\n",
yylineno, $1, $2, $3);
param_error = 0;
memset(iou_node_tmp, 0, sizeof(*iou_node_tmp));
} else {
add_node();
}
}
| INT error ENDNODE { log_fmt("^^ NETMAP line %d: ignoring node id %d\n",
yylineno, $1); }
;
params
: ':' INT {
sprintf($$, ":%d", $2);
RANGE_CHECK("PORT", $2, PORT_MIN, PORT_MAX);
iou_node_tmp->port = unpack_port ($2);
}
| ':' INT '/' INT {
sprintf($$, ":%d/%d", $2, $4);
RANGE_CHECK("BAY", $2, BAY_MIN, BAY_MAX);
RANGE_CHECK("UNIT", $4, UNIT_MIN, UNIT_MAX);
iou_node_tmp->port.bay = $2;
iou_node_tmp->port.unit = $4;
}
| ':' INT '/' INT '/' INT {
sprintf($$, ":%d/%d/%d", $2, $4, $6);
RANGE_CHECK("SUBID", $2, SUBID_MIN, SUBID_MAX);
RANGE_CHECK("BAY", $4, BAY_MIN, BAY_MAX);
RANGE_CHECK("UNIT", $6, UNIT_MIN, UNIT_MAX);
iou_node_tmp->ids.subid_present = 1;
iou_node_tmp->ids.subid = $2;
iou_node_tmp->port.bay = $4;
iou_node_tmp->port.unit = $6;
}
;
host
: /* empty */ {
iou_node_tmp->addr.s_addr = INADDR_NONE;
$$[0] = '\0';
}
| '@' ADDRESS {
struct hostent *host;
sprintf($$, "@%s", $2);
host = gethostbyname ($2);
if (host == NULL)
{
log_fmt ("gethostbyname(%s) failed: ", $2);
herror ("");
param_error = 1;
errors_found++;
}
else
{
iou_node_tmp->addr = *(struct in_addr *)host->h_addr_list[0];
if (yap_verbose >= LOG_NOISY)
log_fmt ("gethostbyname(%s) = %s = %s\n", $2, host->h_name,
inet_ntoa (iou_node_tmp->addr));
}
free($2);
}
;
%%
/* for syntax errors */
void yyerror(const char *msg) {
log_msg(msg);
errors_found++;
}