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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
|
/* Copyright 2007-2010 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <assert.h> /* assert */
#include <errno.h> /* errno */
#include <limits.h> /* ULLONG_MAX */
#include <netdb.h> /* getservbyname, getaddrinfo */
#include <stdlib.h> /* strtoull, etc. */
#include <sys/types.h> /* getaddrinfo */
#include <sys/socket.h> /* getaddrinfo, AF_ */
#include <net/ethernet.h> /* ETH_ALEN */
#include <net/if.h> /* IFNAMSIZ */
#include <netinet/in.h> /* IPPROTO_ */
#include <libipset/debug.h> /* D() */
#include <libipset/data.h> /* IPSET_OPT_* */
#include <libipset/icmp.h> /* name_to_icmp */
#include <libipset/icmpv6.h> /* name_to_icmpv6 */
#include <libipset/pfxlen.h> /* prefixlen_netmask_map */
#include <libipset/session.h> /* ipset_err */
#include <libipset/types.h> /* ipset_type_get */
#include <libipset/utils.h> /* string utilities */
#include <libipset/parse.h> /* prototypes */
#ifndef ULLONG_MAX
#define ULLONG_MAX 18446744073709551615ULL
#endif
/* Parse input data */
#define cidr_separator(str) ipset_strchr(str, IPSET_CIDR_SEPARATOR)
#define range_separator(str) ipset_strchr(str, IPSET_RANGE_SEPARATOR)
#define elem_separator(str) ipset_strchr(str, IPSET_ELEM_SEPARATOR)
#define name_separator(str) ipset_strchr(str, IPSET_NAME_SEPARATOR)
#define proto_separator(str) ipset_strchr(str, IPSET_PROTO_SEPARATOR)
#define syntax_err(fmt, args...) \
ipset_err(session, "Syntax error: " fmt , ## args)
static char *
ipset_strchr(const char *str, const char *sep)
{
char *match;
assert(str);
assert(sep);
for (; *sep != '\0'; sep++)
if ((match = strchr(str, sep[0])) != NULL
&& str[0] != sep[0]
&& str[strlen(str)-1] != sep[0])
return match;
return NULL;
}
/*
* Parser functions, shamelessly taken from iptables.c, ip6tables.c
* and parser.c from libnetfilter_conntrack.
*/
/*
* Parse numbers
*/
static int
string_to_number_ll(struct ipset_session *session,
const char *str,
unsigned long long min,
unsigned long long max,
unsigned long long *ret)
{
unsigned long long number = 0;
char *end;
/* Handle hex, octal, etc. */
errno = 0;
number = strtoull(str, &end, 0);
if (*end == '\0' && end != str && errno != ERANGE) {
/* we parsed a number, let's see if we want this */
if (min <= number && (!max || number <= max)) {
*ret = number;
return 0;
} else
errno = ERANGE;
}
if (errno == ERANGE && max)
return syntax_err("'%s' is out of range %llu-%llu",
str, min, max);
else if (errno == ERANGE)
return syntax_err("'%s' is out of range %llu-%llu",
str, min, ULLONG_MAX);
else
return syntax_err("'%s' is invalid as number", str);
}
static int
string_to_u8(struct ipset_session *session,
const char *str, uint8_t *ret)
{
int err;
unsigned long long num = 0;
err = string_to_number_ll(session, str, 0, 255, &num);
*ret = num;
return err;
}
static int
string_to_cidr(struct ipset_session *session,
const char *str, uint8_t min, uint8_t max, uint8_t *ret)
{
int err = string_to_u8(session, str, ret);
if (!err && (*ret < min || *ret > max))
return syntax_err("'%s' is out of range %u-%u",
str, min, max);
return err;
}
static int
string_to_u16(struct ipset_session *session,
const char *str, uint16_t *ret)
{
int err;
unsigned long long num = 0;
err = string_to_number_ll(session, str, 0, USHRT_MAX, &num);
*ret = num;
return err;
}
static int
string_to_u32(struct ipset_session *session,
const char *str, uint32_t *ret)
{
int err;
unsigned long long num = 0;
err = string_to_number_ll(session, str, 0, UINT_MAX, &num);
*ret = num;
return err;
}
/**
* ipset_parse_ether - parse ethernet address
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an ethernet address. The parsed ethernet
* address is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_ether(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
unsigned int i = 0;
unsigned char ether[ETH_ALEN];
assert(session);
assert(opt == IPSET_OPT_ETHER);
assert(str);
if (strlen(str) != ETH_ALEN * 3 - 1)
goto error;
for (i = 0; i < ETH_ALEN; i++) {
long number;
char *end;
number = strtol(str + i * 3, &end, 16);
if (end == str + i * 3 + 2
&& (*end == ':' || *end == '\0')
&& number >= 0 && number <= 255)
ether[i] = number;
else
goto error;
}
return ipset_session_data_set(session, opt, ether);
error:
return syntax_err("cannot parse '%s' as ethernet address", str);
}
/*
* Parse TCP service names or port numbers
*/
static int
parse_portname(struct ipset_session *session, const char *str,
uint16_t *port, const char *proto)
{
struct servent *service;
if ((service = getservbyname(str, proto)) != NULL) {
*port = ntohs((uint16_t) service->s_port);
return 0;
}
return syntax_err("cannot parse '%s' as a %s port", str, proto);
}
/**
* ipset_parse_single_port - parse a single port number or name
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
* @proto: protocol
*
* Parse string as a single port number or name. The parsed port
* number is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_port(struct ipset_session *session,
enum ipset_opt opt, const char *str,
const char *proto)
{
uint16_t port;
int err;
assert(session);
assert(opt == IPSET_OPT_PORT || opt == IPSET_OPT_PORT_TO);
assert(str);
if ((err = string_to_u16(session, str, &port)) == 0
|| (err = parse_portname(session, str, &port, proto)) == 0)
err = ipset_session_data_set(session, opt, &port);
if (!err)
/* No error, so reset false error messages! */
ipset_session_report_reset(session);
return err;
}
/**
* ipset_parse_tcpudp_port - parse TCP/UDP port name, number, or range of them
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
* @proto: TCP|UDP
*
* Parse string as a TCP/UDP port name or number or range of them
* separated by a dash. The parsed port numbers are stored
* in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_tcpudp_port(struct ipset_session *session,
enum ipset_opt opt, const char *str, const char *proto)
{
char *a, *saved, *tmp;
int err = 0;
assert(session);
assert(opt == IPSET_OPT_PORT);
assert(str);
saved = tmp = strdup(str);
if (tmp == NULL)
return ipset_err(session,
"Cannot allocate memory to duplicate %s.",
str);
a = range_separator(tmp);
if (a != NULL) {
/* port-port */
*a++ = '\0';
err = ipset_parse_port(session, IPSET_OPT_PORT_TO, a, proto);
if (err)
goto error;
}
err = ipset_parse_port(session, opt, tmp, proto);
error:
free(saved);
return err;
}
/**
* ipset_parse_tcp_port - parse TCP port name, number, or range of them
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a TCP port name or number or range of them
* separated by a dash. The parsed port numbers are stored
* in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_tcp_port(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
return ipset_parse_tcpudp_port(session, opt, str, "TCP");
}
/**
* ipset_parse_single_tcp_port - parse TCP port name or number
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a single TCP port name or number.
* The parsed port number is stored
* in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_single_tcp_port(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(opt == IPSET_OPT_PORT || opt == IPSET_OPT_PORT_TO);
assert(str);
return ipset_parse_port(session, opt, str, "TCP");
}
/**
* ipset_parse_proto - parse protocol name
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a protocol name.
* The parsed protocol is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_proto(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
const struct protoent *protoent;
uint8_t proto = 0;
assert(session);
assert(opt == IPSET_OPT_PROTO);
assert(str);
protoent = getprotobyname(strcasecmp(str, "icmpv6") == 0
? "ipv6-icmp" : str);
if (protoent == NULL)
return syntax_err("cannot parse '%s' "
"as a protocol name", str);
proto = protoent->p_proto;
if (!proto)
return syntax_err("Unsupported protocol '%s'", str);
return ipset_session_data_set(session, opt, &proto);
}
/* Parse ICMP and ICMPv6 type/code */
static int
parse_icmp_typecode(struct ipset_session *session,
enum ipset_opt opt, const char *str,
const char *family)
{
uint16_t typecode;
uint8_t type, code;
char *a, *saved, *tmp;
int err;
saved = tmp = strdup(str);
if (tmp == NULL)
return ipset_err(session,
"Cannot allocate memory to duplicate %s.",
str);
a = cidr_separator(tmp);
if (a == NULL) {
free(saved);
return ipset_err(session,
"Cannot parse %s as an %s type/code.", str, family);
}
*a++ = '\0';
if ((err = string_to_u8(session, a, &type)) != 0
|| (err = string_to_u8(session, tmp, &code)) != 0)
goto error;
typecode = (type << 8) | code;
err = ipset_session_data_set(session, opt, &typecode);
error:
free(saved);
return err;
}
/**
* ipset_parse_icmp - parse an ICMP name or type/code
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an ICMP name or type/code numbers.
* The parsed ICMP type/code is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_icmp(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
uint16_t typecode;
assert(session);
assert(opt == IPSET_OPT_PORT);
assert(str);
if (name_to_icmp(str, &typecode) < 0)
return parse_icmp_typecode(session, opt, str, "ICMP");
return ipset_session_data_set(session, opt, &typecode);
}
/**
* ipset_parse_icmpv6 - parse an ICMPv6 name or type/code
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an ICMPv6 name or type/code numbers.
* The parsed ICMPv6 type/code is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_icmpv6(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
uint16_t typecode;
assert(session);
assert(opt == IPSET_OPT_PORT);
assert(str);
if (name_to_icmpv6(str, &typecode) < 0)
return parse_icmp_typecode(session, opt, str, "ICMPv6");
return ipset_session_data_set(session, opt, &typecode);
}
/**
* ipset_parse_proto_port - parse (optional) protocol and a single port
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a protocol and port, separated by a colon.
* The protocol part is optional.
* The parsed protocol and port numbers are stored in the data
* blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_proto_port(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
struct ipset_data *data;
char *a, *saved, *tmp;
const char *proto;
uint8_t p = IPPROTO_TCP;
int err = 0;
assert(session);
assert(opt == IPSET_OPT_PORT);
assert(str);
data = ipset_session_data(session);
saved = tmp = strdup(str);
if (tmp == NULL)
return ipset_err(session,
"Cannot allocate memory to duplicate %s.",
str);
a = proto_separator(tmp);
if (a != NULL) {
uint8_t family = ipset_data_family(data);
/* proto:port */
*a++ = '\0';
err = ipset_parse_proto(session, IPSET_OPT_PROTO, tmp);
if (err)
goto error;
p = *(const uint8_t *) ipset_data_get(data, IPSET_OPT_PROTO);
switch (p) {
case IPPROTO_TCP:
case IPPROTO_SCTP:
case IPPROTO_UDP:
case IPPROTO_UDPLITE:
proto = tmp;
tmp = a;
goto parse_port;
case IPPROTO_ICMP:
if (family != AF_INET) {
syntax_err("Protocol ICMP can be used with family INET only");
goto error;
}
err = ipset_parse_icmp(session, opt, a);
break;
case IPPROTO_ICMPV6:
if (family != AF_INET6) {
syntax_err("Protocol ICMPv6 can be used with family INET6 only");
goto error;
}
err = ipset_parse_icmpv6(session, opt, a);
break;
default:
if (!STREQ(a, "0")) {
syntax_err("Protocol %s can be used with pseudo port value 0 only.");
goto error;
}
ipset_data_flags_set(data, IPSET_FLAG(opt));
}
goto error;
} else {
proto = "TCP";
err = ipset_data_set(data, IPSET_OPT_PROTO, &p);
if (err)
goto error;
}
parse_port:
err = ipset_parse_tcpudp_port(session, opt, tmp, proto);
error:
free(saved);
return err;
}
/**
* ipset_parse_family - parse INET|INET6 family names
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an INET|INET6 family name.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_family(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
struct ipset_data *data;
uint8_t family;
assert(session);
assert(opt == IPSET_OPT_FAMILY);
assert(str);
data = ipset_session_data(session);
if (ipset_data_flags_test(data, IPSET_FLAG(IPSET_OPT_FAMILY)))
syntax_err("protocol family may not be specified "
"multiple times");
if (STREQ(str, "inet") || STREQ(str, "ipv4") || STREQ(str, "-4"))
family = AF_INET;
else if (STREQ(str, "inet6") || STREQ(str, "ipv6") || STREQ(str, "-6"))
family = AF_INET6;
else if (STREQ(str, "any") || STREQ(str, "unspec"))
family = AF_UNSPEC;
else
return syntax_err("unknown INET family %s", str);
return ipset_data_set(data, opt, &family);
}
/*
* Parse IPv4/IPv6 addresses, networks and ranges.
* We resolve hostnames but just the first IP address is used.
*/
static struct addrinfo *
call_getaddrinfo(struct ipset_session *session, const char *str,
uint8_t family)
{
struct addrinfo hints;
struct addrinfo *res;
int err;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = family;
hints.ai_socktype = SOCK_RAW;
hints.ai_protocol = 0;
hints.ai_next = NULL;
if ((err = getaddrinfo(str, NULL, &hints, &res)) != 0) {
syntax_err("cannot resolve '%s' to an %s address: %s",
str, family == AF_INET6 ? "IPv6" : "IPv4",
gai_strerror(err));
return NULL;
} else
return res;
}
static int
get_addrinfo(struct ipset_session *session,
enum ipset_opt opt,
const char *str,
struct addrinfo **info,
uint8_t family)
{
struct addrinfo *i;
size_t addrlen = family == AF_INET ? sizeof(struct sockaddr_in)
: sizeof(struct sockaddr_in6);
int found, err = 0;
if ((*info = call_getaddrinfo(session, str, family)) == NULL) {
syntax_err("cannot parse %s: resolving to %s address failed",
str, family == AF_INET ? "IPv4" : "IPv6");
return EINVAL;
}
for (i = *info, found = 0; i != NULL; i = i->ai_next) {
if (i->ai_family != family || i->ai_addrlen != addrlen)
continue;
if (found == 0) {
if (family == AF_INET) {
/* Workaround: direct cast increases required alignment on Sparc */
const struct sockaddr_in *saddr = (void *)i->ai_addr;
err = ipset_session_data_set(session, opt, &saddr->sin_addr);
} else {
/* Workaround: direct cast increases required alignment on Sparc */
const struct sockaddr_in6 *saddr = (void *)i->ai_addr;
err = ipset_session_data_set(session, opt, &saddr->sin6_addr);
}
} else if (found == 1) {
ipset_warn(session,
"%s resolves to multiple addresses: "
"using only the first one returned "
"by the resolver",
str);
}
found++;
}
if (found == 0)
return syntax_err("cannot parse %s: "
"%s address could not be resolved",
str, family == AF_INET ? "IPv4" : "IPv6");
return err;
}
static int
parse_ipaddr(struct ipset_session *session,
enum ipset_opt opt, const char *str,
uint8_t family)
{
uint8_t m = family == AF_INET ? 32 : 128;
int aerr = EINVAL, err = 0, range = 0;
char *saved = strdup(str);
char *a, *tmp = saved;
struct addrinfo *info;
enum ipset_opt copt, opt2;
if (opt == IPSET_OPT_IP) {
copt = IPSET_OPT_CIDR;
opt2 = IPSET_OPT_IP_TO;
} else {
copt = IPSET_OPT_CIDR2;
opt2 = IPSET_OPT_IP2_TO;
}
if (tmp == NULL)
return ipset_err(session,
"Cannot allocate memory to duplicate %s.",
str);
if ((a = cidr_separator(tmp)) != NULL) {
/* IP/mask */
*a++ = '\0';
if ((err = string_to_cidr(session, a, 0, m, &m)) != 0
|| (err = ipset_session_data_set(session, copt, &m)) != 0)
goto out;
} else if ((a = range_separator(tmp)) != NULL) {
/* IP-IP */
*a++ = '\0';
D("range %s", a);
range++;
}
if ((aerr = get_addrinfo(session, opt, tmp, &info, family)) != 0
|| !range)
goto out;
freeaddrinfo(info);
aerr = get_addrinfo(session, opt2, a, &info, family);
out:
if (aerr != EINVAL)
/* getaddrinfo not failed */
freeaddrinfo(info);
else if (aerr)
err = -1;
free(saved);
return err;
}
enum ipaddr_type {
IPADDR_ANY,
IPADDR_PLAIN,
IPADDR_NET,
IPADDR_RANGE,
};
static inline bool
cidr_hostaddr(const char *str, uint8_t family)
{
char *a = cidr_separator(str);
return family == AF_INET ? STREQ(a, "/32") : STREQ(a, "/128");
}
static int
parse_ip(struct ipset_session *session,
enum ipset_opt opt, const char *str, enum ipaddr_type addrtype)
{
struct ipset_data *data = ipset_session_data(session);
uint8_t family = ipset_data_family(data);
if (family == AF_UNSPEC) {
family = AF_INET;
ipset_data_set(data, IPSET_OPT_FAMILY, &family);
}
switch (addrtype) {
case IPADDR_PLAIN:
if (range_separator(str)
|| (cidr_separator(str) && !cidr_hostaddr(str, family)))
return syntax_err("plain IP address must be supplied: %s",
str);
break;
case IPADDR_NET:
if (!cidr_separator(str) || range_separator(str))
return syntax_err("IP/netblock must be supplied: %s",
str);
break;
case IPADDR_RANGE:
if (!range_separator(str) || cidr_separator(str))
return syntax_err("IP-IP range must supplied: %s",
str);
break;
case IPADDR_ANY:
default:
break;
}
return parse_ipaddr(session, opt, str, family);
}
/**
* ipset_parse_ip - parse IPv4|IPv6 address, range or netblock
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 address or address range
* or netblock. Hostnames are resolved. If family is not set
* yet in the data blob, INET is assumed.
* The values are stored in the data blob of the session.
*
* FIXME: if the hostname resolves to multiple addresses,
* the first one is used only.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_ip(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(opt == IPSET_OPT_IP || opt == IPSET_OPT_IP2);
assert(str);
return parse_ip(session, opt, str, IPADDR_ANY);
}
/**
* ipset_parse_single_ip - parse a single IPv4|IPv6 address
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 address or hostname. If family
* is not set yet in the data blob, INET is assumed.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_single_ip(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(opt == IPSET_OPT_IP
|| opt == IPSET_OPT_IP_TO
|| opt == IPSET_OPT_IP2);
assert(str);
return parse_ip(session, opt, str, IPADDR_PLAIN);
}
/**
* ipset_parse_net - parse IPv4|IPv6 address/cidr
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 address/cidr pattern. If family
* is not set yet in the data blob, INET is assumed.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_net(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(opt == IPSET_OPT_IP || opt == IPSET_OPT_IP2);
assert(str);
return parse_ip(session, opt, str, IPADDR_NET);
}
/**
* ipset_parse_range - parse IPv4|IPv6 ranges
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 range separated by a dash. If family
* is not set yet in the data blob, INET is assumed.
* The values are stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_range(struct ipset_session *session,
enum ipset_opt opt ASSERT_UNUSED, const char *str)
{
assert(session);
assert(opt == IPSET_OPT_IP || opt == IPSET_OPT_IP2);
assert(str);
return parse_ip(session, IPSET_OPT_IP, str, IPADDR_RANGE);
}
/**
* ipset_parse_netrange - parse IPv4|IPv6 address/cidr or range
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 address/cidr pattern or a range
* of addresses separated by a dash. If family is not set yet in
* the data blob, INET is assumed.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_netrange(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(opt == IPSET_OPT_IP || opt == IPSET_OPT_IP2);
assert(str);
if (!(range_separator(str) || cidr_separator(str)))
return syntax_err("IP/cidr or IP-IP range must be specified: %s",
str);
return parse_ip(session, opt, str, IPADDR_ANY);
}
/**
* ipset_parse_iprange - parse IPv4|IPv6 address or range
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 address pattern or a range
* of addresses separated by a dash. If family is not set yet in
* the data blob, INET is assumed.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_iprange(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(opt == IPSET_OPT_IP || opt == IPSET_OPT_IP2);
assert(str);
if (cidr_separator(str))
return syntax_err("IP address or IP-IP range must be specified: %s",
str);
return parse_ip(session, opt, str, IPADDR_ANY);
}
/**
* ipset_parse_ipnet - parse IPv4|IPv6 address or address/cidr pattern
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 address or address/cidr pattern.
* If family is not set yet in the data blob, INET is assumed.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_ipnet(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(opt == IPSET_OPT_IP || opt == IPSET_OPT_IP2);
assert(str);
if (range_separator(str))
return syntax_err("IP address or IP/cidr must be specified: %s",
str);
return parse_ip(session, opt, str, IPADDR_ANY);
}
/**
* ipset_parse_ip4_single6 - parse IPv4 address, range or netblock or IPv6 address
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4 address or address range
* or netblock or and IPv6 address. Hostnames are resolved. If family
* is not set yet in the data blob, INET is assumed.
* The values are stored in the data blob of the session.
*
* FIXME: if the hostname resolves to multiple addresses,
* the first one is used only.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_ip4_single6(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
struct ipset_data *data;
uint8_t family;
assert(session);
assert(opt == IPSET_OPT_IP || opt == IPSET_OPT_IP2);
assert(str);
data = ipset_session_data(session);
family = ipset_data_family(data);
if (family == AF_UNSPEC) {
family = AF_INET;
ipset_data_set(data, IPSET_OPT_FAMILY, &family);
}
return family == AF_INET ? ipset_parse_ip(session, opt, str)
: ipset_parse_single_ip(session, opt, str);
}
/**
* ipset_parse_ip4_net6 - parse IPv4|IPv6 address or address/cidr pattern
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 address or address/cidr pattern. For IPv4,
* address range is valid too.
* If family is not set yet in the data blob, INET is assumed.
* The values are stored in the data blob of the session.
*
* FIXME: if the hostname resolves to multiple addresses,
* the first one is used only.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_ip4_net6(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
struct ipset_data *data;
uint8_t family;
assert(session);
assert(opt == IPSET_OPT_IP || opt == IPSET_OPT_IP2);
assert(str);
data = ipset_session_data(session);
family = ipset_data_family(data);
if (family == AF_UNSPEC) {
family = AF_INET;
ipset_data_set(data, IPSET_OPT_FAMILY, &family);
}
return family == AF_INET ? parse_ip(session, opt, str, IPADDR_ANY)
: ipset_parse_ipnet(session, opt, str);
}
/**
* ipset_parse_iptimeout - parse IPv4|IPv6 address and timeout
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an IPv4|IPv6 address and timeout parameter.
* If family is not set yet in the data blob, INET is assumed.
* The value is stored in the data blob of the session.
*
* Compatibility parser.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_iptimeout(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
char *tmp, *saved, *a;
int err;
assert(session);
assert(opt == IPSET_OPT_IP);
assert(str);
/* IP,timeout */
if (ipset_data_flags_test(ipset_session_data(session),
IPSET_FLAG(IPSET_OPT_TIMEOUT)))
return syntax_err("mixed syntax, timeout already specified");
tmp = saved = strdup(str);
if (saved == NULL)
return ipset_err(session,
"Cannot allocate memory to duplicate %s.",
str);
a = elem_separator(tmp);
if (a == NULL) {
free(saved);
return syntax_err("Missing separator from %s", str);
}
*a++ = '\0';
err = parse_ip(session, opt, tmp, IPADDR_ANY);
if (!err)
err = ipset_parse_uint32(session, IPSET_OPT_TIMEOUT, a);
free(saved);
return err;
}
#define check_setname(str, saved) \
do { \
if (strlen(str) > IPSET_MAXNAMELEN - 1) { \
if (saved != NULL) \
free(saved); \
return syntax_err("setname '%s' is longer than %u characters", \
str, IPSET_MAXNAMELEN - 1); \
} \
} while (0)
/**
* ipset_parse_name_compat - parse setname as element
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a setname or a setname element to add to a set.
* The pattern "setname,before|after,setname" is recognized and
* parsed.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_name_compat(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
char *saved;
char *a = NULL, *b = NULL, *tmp;
int err, before = 0;
const char *sep = IPSET_ELEM_SEPARATOR;
struct ipset_data *data;
assert(session);
assert(opt == IPSET_OPT_NAME);
assert(str);
data = ipset_session_data(session);
if (ipset_data_flags_test(data, IPSET_FLAG(IPSET_OPT_NAMEREF)))
syntax_err("mixed syntax, before|after option already used");
tmp = saved = strdup(str);
if (saved == NULL)
return ipset_err(session,
"Cannot allocate memory to duplicate %s.",
str);
if ((a = elem_separator(tmp)) != NULL) {
/* setname,[before|after,setname */
*a++ = '\0';
if ((b = elem_separator(a)) != NULL)
*b++ = '\0';
if (b == NULL
|| !(STREQ(a, "before") || STREQ(a, "after"))) {
err = ipset_err(session, "you must specify elements "
"as setname%s[before|after]%ssetname",
sep, sep);
goto out;
}
before = STREQ(a, "before");
}
check_setname(tmp, saved);
if ((err = ipset_data_set(data, opt, tmp)) != 0 || b == NULL)
goto out;
check_setname(b, saved);
if ((err = ipset_data_set(data,
IPSET_OPT_NAMEREF, b)) != 0)
goto out;
if (before)
err = ipset_data_set(data, IPSET_OPT_BEFORE, &before);
out:
free(saved);
return err;
}
/**
* ipset_parse_setname - parse string as a setname
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a setname.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_setname(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(opt == IPSET_SETNAME
|| opt == IPSET_OPT_NAME
|| opt == IPSET_OPT_SETNAME2);
assert(str);
check_setname(str, NULL);
return ipset_session_data_set(session, opt, str);
}
/**
* ipset_parse_before - parse string as "before" reference setname
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a "before" reference setname for list:set
* type of sets. The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_before(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
struct ipset_data *data;
assert(session);
assert(opt == IPSET_OPT_NAMEREF);
assert(str);
data = ipset_session_data(session);
if (ipset_data_flags_test(data, IPSET_FLAG(IPSET_OPT_NAMEREF)))
syntax_err("mixed syntax, before|after option already used");
check_setname(str, NULL);
ipset_data_set(data, IPSET_OPT_BEFORE, str);
return ipset_data_set(data, opt, str);
}
/**
* ipset_parse_after - parse string as "after" reference setname
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a "after" reference setname for list:set
* type of sets. The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_after(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
struct ipset_data *data;
assert(session);
assert(opt == IPSET_OPT_NAMEREF);
assert(str);
data = ipset_session_data(session);
if (ipset_data_flags_test(data, IPSET_FLAG(IPSET_OPT_NAMEREF)))
syntax_err("mixed syntax, before|after option already used");
check_setname(str, NULL);
return ipset_data_set(data, opt, str);
}
/**
* ipset_parse_uint32 - parse string as an unsigned integer
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an unsigned integer number.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_uint32(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
uint32_t value;
int err;
assert(session);
assert(str);
if ((err = string_to_u32(session, str, &value)) == 0)
return ipset_session_data_set(session, opt, &value);
return err;
}
/**
* ipset_parse_uint8 - parse string as an unsigned short integer
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an unsigned short integer number.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_uint8(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
uint8_t value;
int err;
assert(session);
assert(str);
if ((err = string_to_u8(session, str, &value)) == 0)
return ipset_session_data_set(session, opt, &value);
return err;
}
/**
* ipset_parse_netmask - parse string as a CIDR netmask value
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a CIDR netmask value, depending on family type.
* If family is not set yet, INET is assumed.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_netmask(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
uint8_t family, cidr;
struct ipset_data *data;
int err = 0;
assert(session);
assert(opt == IPSET_OPT_NETMASK);
assert(str);
data = ipset_session_data(session);
family = ipset_data_family(data);
if (family == AF_UNSPEC) {
family = AF_INET;
ipset_data_set(data, IPSET_OPT_FAMILY, &family);
}
err = string_to_cidr(session, str,
family == AF_INET ? 1 : 4,
family == AF_INET ? 31 : 124,
&cidr);
if (err)
return syntax_err("netmask is out of the inclusive range "
"of %u-%u",
family == AF_INET ? 1 : 4,
family == AF_INET ? 31 : 124);
return ipset_data_set(data, opt, &cidr);
}
/**
* ipset_parse_flag - "parse" option flags
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse option flags :-)
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_flag(struct ipset_session *session,
enum ipset_opt opt, const char *str UNUSED)
{
assert(session);
return ipset_session_data_set(session, opt, NULL);
}
/**
* ipset_parse_type - parse ipset type name
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse ipset module type: supports both old and new formats.
* The type name is looked up and the type found is stored
* in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_typename(struct ipset_session *session,
enum ipset_opt opt ASSERT_UNUSED, const char *str)
{
const struct ipset_type *type;
const char *typename;
assert(session);
assert(opt == IPSET_OPT_TYPENAME);
assert(str);
if (strlen(str) > IPSET_MAXNAMELEN - 1)
return syntax_err("typename '%s' is longer than %u characters",
str, IPSET_MAXNAMELEN - 1);
/* Find the corresponding type */
typename = ipset_typename_resolve(str);
if (typename == NULL)
return syntax_err("typename '%s' is unkown", str);
ipset_session_data_set(session, IPSET_OPT_TYPENAME, typename);
type = ipset_type_get(session, IPSET_CMD_CREATE);
if (type == NULL)
return -1;
return ipset_session_data_set(session, IPSET_OPT_TYPE, type);
}
/**
* ipset_parse_iface - parse string as an interface name
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as an interface name, optionally with 'physdev:' prefix.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_iface(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
struct ipset_data *data;
int offset = 0, err = 0;
assert(session);
assert(opt == IPSET_OPT_IFACE);
assert(str);
data = ipset_session_data(session);
if (STREQ(str, "physdev:")) {
offset = 8;
err = ipset_data_set(data, IPSET_OPT_PHYSDEV, str);
}
if (strlen(str + offset) > IFNAMSIZ - 1)
return syntax_err("interface name '%s' is longer than %u characters",
str + offset, IFNAMSIZ - 1);
return ipset_data_set(data, opt, str + offset);
}
/**
* ipset_parse_output - parse output format name
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse output format names and set session mode.
* The value is stored in the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_output(struct ipset_session *session,
int opt UNUSED, const char *str)
{
assert(session);
assert(str);
if (STREQ(str, "plain"))
return ipset_session_output(session, IPSET_LIST_PLAIN);
else if (STREQ(str, "xml"))
return ipset_session_output(session, IPSET_LIST_XML);
else if (STREQ(str, "save"))
return ipset_session_output(session, IPSET_LIST_SAVE);
return syntax_err("unkown output mode '%s'", str);
}
/**
* ipset_parse_ignored - "parse" ignored option
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Ignore deprecated options. A single warning is generated
* for every ignored opton.
*
* Returns 0.
*/
int
ipset_parse_ignored(struct ipset_session *session,
enum ipset_opt opt, const char *str)
{
assert(session);
assert(str);
if (!ipset_data_ignored(ipset_session_data(session), opt))
ipset_warn(session,
"Option %s is ignored. Please upgrade your syntax.", str);
return 0;
}
/**
* ipset_call_parser - call a parser function
* @session: session structure
* @parsefn: parser function
* @optstr: option name
* @opt: option kind of the data
* @str: string to parse
*
* Wrapper to call the parser functions so that ignored options
* are handled properly.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_call_parser(struct ipset_session *session,
const struct ipset_arg *arg,
const char *str)
{
if (ipset_data_flags_test(ipset_session_data(session),
IPSET_FLAG(arg->opt)))
syntax_err("%s already specified", arg->name[0]);
return arg->parse(session, arg->opt, str);
}
#define parse_elem(s, t, d, str) \
do { \
if (!(t)->elem[d].parse) \
goto internal; \
ret = (t)->elem[d].parse(s, (t)->elem[d].opt, str); \
if (ret) \
goto out; \
} while (0)
#define elem_syntax_err(fmt, args...) \
do { \
free(saved); \
return syntax_err(fmt , ## args);\
} while (0)
/**
* ipset_parse_elem - parse ADT elem, depending on settype
* @session: session structure
* @opt: option kind of the data
* @str: string to parse
*
* Parse string as a (multipart) element according to the settype.
* The value is stored in the data blob of the session.
*
* Returns 0 on success or a negative error code.
*/
int
ipset_parse_elem(struct ipset_session *session,
enum ipset_opt optional, const char *str)
{
const struct ipset_type *type;
char *a = NULL, *b = NULL, *tmp, *saved;
int ret;
assert(session);
assert(str);
type = ipset_session_data_get(session, IPSET_OPT_TYPE);
if (!type)
return ipset_err(session,
"Internal error: set type is unknown!");
saved = tmp = strdup(str);
if (tmp == NULL)
return ipset_err(session,
"Cannot allocate memory to duplicate %s.",
str);
a = elem_separator(tmp);
if (type->dimension > IPSET_DIM_ONE) {
if (a != NULL) {
/* elem,elem */
*a++ = '\0';
} else if (!optional)
elem_syntax_err("Second element is missing from %s.",
str);
} else if (a != NULL) {
if (type->compat_parse_elem) {
ret = type->compat_parse_elem(session,
type->elem[IPSET_DIM_ONE].opt,
saved);
goto out;
}
elem_syntax_err("Elem separator in %s, "
"but settype %s supports none.",
str, type->name);
}
if (a)
b = elem_separator(a);
if (type->dimension > IPSET_DIM_TWO) {
if (b != NULL) {
/* elem,elem,elem */
*b++ = '\0';
} else if (!optional)
elem_syntax_err("Third element is missing from %s.",
str);
} else if (b != NULL)
elem_syntax_err("Two elem separators in %s, "
"but settype %s supports one.",
str, type->name);
if (b != NULL && elem_separator(b))
elem_syntax_err("Three elem separators in %s, "
"but settype %s supports two.",
str, type->name);
D("parse elem part one: %s", tmp);
parse_elem(session, type, IPSET_DIM_ONE, tmp);
if (type->dimension > IPSET_DIM_ONE && a != NULL) {
D("parse elem part two: %s", a);
parse_elem(session, type, IPSET_DIM_TWO, a);
}
if (type->dimension > IPSET_DIM_TWO && b != NULL)
parse_elem(session, type, IPSET_DIM_THREE, b);
goto out;
internal:
ret = ipset_err(session,
"Internal error: missing parser function for %s",
type->name);
out:
free(saved);
return ret;
}
|