• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 743

30 Mar 2025 12:27AM UTC coverage: 93.641% (+0.1%) from 93.516%
743

push

appveyor

lduchosal
Chore: last cleanups

1561 of 1667 relevant lines covered (93.64%)

820053.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.73
/src/ConsoleApplication/Program.cs
1
// <copyright file="Program.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using Collections.Generic;
8
using Diagnostics;
9
using IO;
10
using Numerics;
11
using Reflection;
12
using Gnu.Getopt;
13

14
/// <summary>
15
/// Console app for IPNetwork.
16
/// </summary>
17
public static class Program
18
{
19
    private static readonly Dictionary<int, ArgParsed> Args = new ();
1✔
20

21
    private static readonly ArgParsed[] ArgsList =
1✔
22
    [
1✔
23
        new ArgParsed('i', (ac, _) => { ac.IPNetwork = true; }),
3✔
24
        new ArgParsed('n', (ac, _) => { ac.Network = true; }),
6✔
25
        new ArgParsed('m', (ac, _) => { ac.Netmask = true; }),
6✔
26
        new ArgParsed('c', (ac, _) => { ac.Cidr = true; }),
6✔
27
        new ArgParsed('b', (ac, _) => { ac.Broadcast = true; }),
6✔
28
        new ArgParsed('f', (ac, _) => { ac.FirstUsable = true; }),
6✔
29
        new ArgParsed('l', (ac, _) => { ac.LastUsable = true; }),
6✔
30
        new ArgParsed('u', (ac, _) => { ac.Usable = true; }),
6✔
31
        new ArgParsed('t', (ac, _) => { ac.Total = true; }),
×
32
        new ArgParsed('w', (ac, _) => { ac.Action = ActionEnum.Supernet; }),
3✔
33
        new ArgParsed('W', (ac, _) => { ac.Action = ActionEnum.WideSupernet; }),
×
34
        new ArgParsed('h', (ac, _) => { ac.Action = ActionEnum.Usage; }),
×
35
        new ArgParsed('x', (ac, _) => { ac.Action = ActionEnum.ListIPAddress; }),
3✔
36
        new ArgParsed('?', (_, _) => { }),
4✔
37
        new ArgParsed('D', (ac, _) => { ac.CidrParse = CidrParseEnum.Default; }),
×
38
        new ArgParsed('d', (ac, arg) =>
1✔
39
        {
2✔
40
            if (!IPNetwork2.TryParseCidr(arg, Sockets.AddressFamily.InterNetwork, out byte? cidr))
2✔
41
            {
×
42
                Console.WriteLine("Invalid cidr {0}", cidr);
×
43
                ac.Action = ActionEnum.Usage;
×
44
                return;
×
45
            }
1✔
46

1✔
47
            ac.CidrParse = CidrParseEnum.Value;
2✔
48
            ac.CidrParsed = (byte)cidr!;
2✔
49
        }),
2✔
50
        new ArgParsed('s', (ac, arg) =>
1✔
51
        {
1✔
52
            if (!IPNetwork2.TryParseCidr(arg, Sockets.AddressFamily.InterNetwork, out byte? cidr))
1✔
53
            {
×
54
                Console.WriteLine("Invalid cidr {0}", cidr);
×
55
                ac.Action = ActionEnum.Usage;
×
56
                return;
×
57
            }
1✔
58

1✔
59
            ac.Action = ActionEnum.Subnet;
1✔
60
            ac.SubnetCidr = (byte)cidr!;
1✔
61
        }),
1✔
62
        new ArgParsed('C', (ac, arg) =>
1✔
63
        {
1✔
64
            if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
1✔
65
            {
×
66
                Console.WriteLine("Unable to parse ipnetwork {0}", arg);
×
67
                ac.Action = ActionEnum.Usage;
×
68
                return;
×
69
            }
1✔
70

1✔
71
            ac.Action = ActionEnum.ContainNetwork;
1✔
72
            ac.ContainNetwork = ipnetwork;
1✔
73
        }),
1✔
74
        new ArgParsed('o', (ac, arg) =>
1✔
75
        {
1✔
76
            if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
1✔
77
            {
×
78
                Console.WriteLine("Unable to parse ipnetwork {0}", arg);
×
79
                ac.Action = ActionEnum.Usage;
×
80
                return;
×
81
            }
1✔
82

1✔
83
            ac.Action = ActionEnum.OverlapNetwork;
1✔
84
            ac.OverlapNetwork = ipnetwork;
1✔
85
        }),
1✔
86
        new ArgParsed('S', (ac, arg) =>
1✔
87
        {
1✔
88
            if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
1✔
89
            {
×
90
                Console.WriteLine("Unable to parse ipnetwork {0}", arg);
×
91
                ac.Action = ActionEnum.Usage;
×
92
                return;
×
93
            }
1✔
94

1✔
95
            ac.Action = ActionEnum.SubtractNetwork;
1✔
96
            ac.SubtractNetwork = ipnetwork;
1✔
97
        })
1✔
98
    ];
1✔
99

100
    /// <summary>
101
    /// Program entry point.
102
    /// </summary>
103
    /// <param name="args">program arguments.</param>
104
    public static void Main(string[] args)
105
    {
11✔
106
        ProgramContext ac = ParseArgs(args);
11✔
107

108
        switch (ac.Action)
11✔
109
        {
110
            case ActionEnum.Subnet:
111
                SubnetNetworks(ac);
1✔
112
                break;
1✔
113
            case ActionEnum.Supernet:
114
                SupernetNetworks(ac);
1✔
115
                break;
1✔
116
            case ActionEnum.WideSupernet:
117
                WideSupernetNetworks(ac);
×
118
                break;
×
119
            case ActionEnum.PrintNetworks:
120
                PrintNetworks(ac);
5✔
121
                break;
5✔
122
            case ActionEnum.ContainNetwork:
123
                ContainNetwork(ac);
1✔
124
                break;
1✔
125
            case ActionEnum.OverlapNetwork:
126
                OverlapNetwork(ac);
1✔
127
                break;
1✔
128
            case ActionEnum.ListIPAddress:
129
                ListIPAddress(ac);
×
130
                break;
×
131
            case ActionEnum.Usage:
132
            case ActionEnum.SubtractNetwork:
133
            default:
134
                Usage();
2✔
135
                break;
2✔
136
        }
137
    }
11✔
138

139
    private static void ListIPAddress(ProgramContext ac)
140
    {
×
141
        foreach (IPNetwork2 ipnetwork in ac.Networks)
×
142
        {
×
143
            foreach (IPAddress ipaddress in ipnetwork.ListIPAddress())
×
144
            {
×
145
                Console.WriteLine("{0}", ipaddress.ToString());
×
146
            }
×
147
        }
×
148
    }
×
149

150
    private static void ContainNetwork(ProgramContext ac)
151
    {
1✔
152
        foreach (IPNetwork2 ipnetwork in ac.Networks)
5✔
153
        {
1✔
154
            bool contain = ac.ContainNetwork.Contains(ipnetwork);
1✔
155
            Console.WriteLine("{0} contains {1} : {2}", ac.ContainNetwork, ipnetwork, contain);
1✔
156
        }
1✔
157
    }
1✔
158

159
    private static void OverlapNetwork(ProgramContext ac)
160
    {
1✔
161
        foreach (IPNetwork2 ipnetwork in ac.Networks)
5✔
162
        {
1✔
163
            bool overlap = ac.OverlapNetwork.Overlap(ipnetwork);
1✔
164
            Console.WriteLine("{0} overlaps {1} : {2}", ac.OverlapNetwork, ipnetwork, overlap);
1✔
165
        }
1✔
166
    }
1✔
167

168
    private static void WideSupernetNetworks(ProgramContext ac)
169
    {
×
170
        if (!IPNetwork2.TryWideSubnet(ac.Networks, out IPNetwork2 widesubnet))
×
171
        {
×
172
            Console.WriteLine("Unable to wide subnet these networks");
×
173
        }
×
174

175
        PrintNetwork(ac, widesubnet);
×
176
    }
×
177

178
    private static void SupernetNetworks(ProgramContext ac)
179
    {
1✔
180
        if (!IPNetwork2.TrySupernet(ac.Networks, out IPNetwork2[] supernet))
1✔
181
        {
×
182
            Console.WriteLine("Unable to supernet these networks");
×
183
        }
×
184

185
        PrintNetworks(ac, supernet, supernet.Length);
1✔
186
    }
1✔
187

188
    private static void PrintNetworks(ProgramContext ac, IEnumerable<IPNetwork2> ipnetworks, BigInteger networkLength)
189
    {
2✔
190
        int i = 0;
2✔
191
        foreach (IPNetwork2 ipn in ipnetworks)
524✔
192
        {
259✔
193
            i++;
259✔
194
            PrintNetwork(ac, ipn);
259✔
195
            PrintSeparator(networkLength, i);
259✔
196
        }
259✔
197
    }
2✔
198

199
    private static void SubnetNetworks(ProgramContext ac)
200
    {
1✔
201
        BigInteger i = 0;
1✔
202
        foreach (IPNetwork2 ipnetwork in ac.Networks)
5✔
203
        {
1✔
204
            i++;
1✔
205
            int networkLength = ac.Networks.Length;
1✔
206
            if (!ipnetwork.TrySubnet(ac.SubnetCidr, out IPNetworkCollection ipnetworks))
1✔
207
            {
×
208
                Console.WriteLine("Unable to subnet ipnetwork {0} into cidr {1}", ipnetwork, ac.SubnetCidr);
×
209
                PrintSeparator(networkLength, i);
×
210
                continue;
×
211
            }
212

213
            PrintNetworks(ac, ipnetworks, ipnetworks.Count);
1✔
214
            PrintSeparator(networkLength, i);
1✔
215
        }
1✔
216
    }
1✔
217

218
    // private static void PrintSeparator(Array network, int index) {
219
    //    if (network.Length > 1 && index != network.Length) {
220
    //        Console.WriteLine("--");
221
    //    }
222
    // }
223
    private static void PrintSeparator(BigInteger max, BigInteger index)
224
    {
270✔
225
        if (max > 1 && index != max)
270✔
226
        {
262✔
227
            Console.WriteLine("--");
262✔
228
        }
262✔
229
    }
270✔
230

231
    private static void PrintNetworks(ProgramContext ac)
232
    {
5✔
233
        int i = 0;
5✔
234
        foreach (IPNetwork2 ipnetwork in ac.Networks)
35✔
235
        {
10✔
236
            i++;
10✔
237
            PrintNetwork(ac, ipnetwork);
10✔
238
            PrintSeparator(ac.Networks.Length, i);
10✔
239
        }
10✔
240
    }
5✔
241

242
    private static void PrintNetwork(ProgramContext ac, IPNetwork2 ipn)
243
    {
269✔
244
        using var sw = new StringWriter();
269✔
245
        if (ac.IPNetwork)
269✔
246
        {
267✔
247
            sw.WriteLine("IPNetwork   : {0}", ipn);
267✔
248
        }
267✔
249

250
        if (ac.Network)
269✔
251
        {
263✔
252
            sw.WriteLine("Network     : {0}", ipn.Network.ToString());
263✔
253
        }
263✔
254

255
        if (ac.Netmask)
269✔
256
        {
263✔
257
            sw.WriteLine("Netmask     : {0}", ipn.Netmask.ToString());
263✔
258
        }
263✔
259

260
        if (ac.Cidr)
269✔
261
        {
263✔
262
            sw.WriteLine("Cidr        : {0}", ipn.Cidr);
263✔
263
        }
263✔
264

265
        if (ac.Broadcast)
269✔
266
        {
263✔
267
            sw.WriteLine("Broadcast   : {0}", ipn.Broadcast.ToString());
263✔
268
        }
263✔
269

270
        if (ac.FirstUsable)
269✔
271
        {
263✔
272
            sw.WriteLine("FirstUsable : {0}", ipn.FirstUsable.ToString());
263✔
273
        }
263✔
274

275
        if (ac.LastUsable)
269✔
276
        {
263✔
277
            sw.WriteLine("LastUsable  : {0}", ipn.LastUsable.ToString());
263✔
278
        }
263✔
279

280
        if (ac.Usable)
269✔
281
        {
263✔
282
            sw.WriteLine("Usable      : {0}", ipn.Usable);
263✔
283
        }
263✔
284

285
        if (ac.Total)
269✔
286
        {
261✔
287
            sw.WriteLine("Total       : {0}", ipn.Total);
261✔
288
        }
261✔
289

290
        Console.Write(sw.ToString());
269✔
291
    }
538✔
292

293
    static Program()
294
    {
1✔
295
        foreach (ArgParsed ap in ArgsList)
43✔
296
        {
20✔
297
            Args.Add(ap.Arg, ap);
20✔
298
        }
20✔
299
    }
1✔
300

301
    private static ProgramContext ParseArgs(string[] args)
302
    {
11✔
303
        int c;
304
        var g = new Getopt("ipnetwork", args, "inmcbfltud:Dhs:wWxC:o:S:");
11✔
305
        var ac = new ProgramContext();
11✔
306

307
        while ((c = g.getopt()) != -1)
36✔
308
        {
25✔
309
            string optArg = g.Optarg;
25✔
310
            Args[c].Run(ac, optArg);
25✔
311
        }
25✔
312

313
        var ipnetworks = new List<string>();
11✔
314
        for (int i = g.Optind; i < args.Length; i++)
72✔
315
        {
25✔
316
            if (!string.IsNullOrEmpty(args[i]))
25✔
317
            {
25✔
318
                ipnetworks.Add(args[i]);
25✔
319
            }
25✔
320
        }
25✔
321

322
        ac.NetworksString = ipnetworks.ToArray();
11✔
323
        ParseIPNetworks(ac);
11✔
324

325
        if (ac.Networks.Length == 0)
11✔
326
        {
1✔
327
            Console.WriteLine("Provide at least one ipnetwork");
1✔
328
            ac.Action = ActionEnum.Usage;
1✔
329
        }
1✔
330

331
        if (ac.Action == ActionEnum.Supernet
11✔
332
            && ipnetworks.Count < 2)
11✔
333
        {
×
334
            Console.WriteLine("Supernet action required at least two ipnetworks");
×
335
            ac.Action = ActionEnum.Usage;
×
336
        }
×
337

338
        if (ac.Action == ActionEnum.WideSupernet
11✔
339
            && ipnetworks.Count < 2)
11✔
340
        {
×
341
            Console.WriteLine("WideSupernet action required at least two ipnetworks");
×
342
            ac.Action = ActionEnum.Usage;
×
343
        }
×
344

345
        if (PrintNoValue(ac))
11✔
346
        {
8✔
347
            PrintAll(ac);
8✔
348
        }
8✔
349

350
        if (g.Optind == 0)
11✔
351
        {
3✔
352
            PrintAll(ac);
3✔
353
        }
3✔
354

355
        return ac;
11✔
356
    }
11✔
357

358
    private static void ParseIPNetworks(ProgramContext ac)
359
    {
11✔
360
        var ipnetworks = new List<IPNetwork2>();
11✔
361
        foreach (string ips in ac.NetworksString)
83✔
362
        {
25✔
363
            if (!TryParseIPNetwork(ips, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
25✔
364
            {
1✔
365
                Console.WriteLine("Unable to parse ipnetwork {0}", ips);
1✔
366
                continue;
1✔
367
            }
368

369
            ipnetworks.Add(ipnetwork);
24✔
370
        }
24✔
371

372
        ac.Networks = ipnetworks.ToArray();
11✔
373
    }
11✔
374

375
    private static bool TryParseIPNetwork(string ip, CidrParseEnum cidrParseEnum, byte cidr, out IPNetwork2 ipn)
376
    {
28✔
377
        IPNetwork2 ipnetwork = null;
28✔
378
        switch (cidrParseEnum)
28✔
379
        {
380
            case CidrParseEnum.Default when !IPNetwork2.TryParse(ip, out ipnetwork):
×
381
                ipn = null;
×
382
                return false;
×
383
            case CidrParseEnum.Value:
384
            {
28✔
385
                if (!IPNetwork2.TryParse(ip, cidr, out ipnetwork))
28✔
386
                {
22✔
387
                    if (!IPNetwork2.TryParse(ip, out ipnetwork))
22✔
388
                    {
1✔
389
                        ipn = null;
1✔
390
                        return false;
1✔
391
                    }
392
                }
21✔
393

394
                break;
27✔
395
            }
396
        }
397

398
        ipn = ipnetwork;
27✔
399
        return true;
27✔
400
    }
28✔
401

402
    private static bool PrintNoValue(ProgramContext ac)
403
    {
11✔
404
        ArgumentNullException.ThrowIfNull(ac);
11✔
405

406
        return ac.IPNetwork == false
11✔
407
               && ac.Network == false
11✔
408
               && ac.Netmask == false
11✔
409
               && ac.Cidr == false
11✔
410
               && ac.Broadcast == false
11✔
411
               && ac.FirstUsable == false
11✔
412
               && ac.LastUsable == false
11✔
413
               && ac.Total == false
11✔
414
               && ac.Usable == false;
11✔
415
    }
11✔
416

417
    private static void PrintAll(ProgramContext ac)
418
    {
11✔
419
        ArgumentNullException.ThrowIfNull(ac);
11✔
420

421
        ac.IPNetwork = true;
11✔
422
        ac.Network = true;
11✔
423
        ac.Netmask = true;
11✔
424
        ac.Cidr = true;
11✔
425
        ac.Broadcast = true;
11✔
426
        ac.FirstUsable = true;
11✔
427
        ac.LastUsable = true;
11✔
428
        ac.Usable = true;
11✔
429
        ac.Total = true;
11✔
430
    }
11✔
431

432
    private static void Usage()
433
    {
2✔
434
        Assembly assembly = Assembly.GetEntryAssembly()
2✔
435
                            ?? Assembly.GetExecutingAssembly()
2✔
436
            ;
2✔
437
        var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
2✔
438
        string version = fvi.FileVersion;
2✔
439

440
        Console.WriteLine(
2✔
441
            "Usage: ipnetwork [-inmcbflu] [-d cidr|-D] [-h|-s cidr|-S|-w|-W|-x|-C network|-o network] networks ...");
2✔
442
        Console.WriteLine("Version: {0}", version);
2✔
443
        Console.WriteLine();
2✔
444
        Console.WriteLine("Print options");
2✔
445
        Console.WriteLine("\t-i : network");
2✔
446
        Console.WriteLine("\t-n : network address");
2✔
447
        Console.WriteLine("\t-m : netmask");
2✔
448
        Console.WriteLine("\t-c : cidr");
2✔
449
        Console.WriteLine("\t-b : broadcast");
2✔
450
        Console.WriteLine("\t-f : first usable ip address");
2✔
451
        Console.WriteLine("\t-l : last usable ip address");
2✔
452
        Console.WriteLine("\t-u : number of usable ip addresses");
2✔
453
        Console.WriteLine("\t-t : total number of ip addresses");
2✔
454
        Console.WriteLine();
2✔
455
        Console.WriteLine("Parse options");
2✔
456
        Console.WriteLine("\t-d cidr : use cidr if not provided (default /32)");
2✔
457
        Console.WriteLine("\t-D      : IPv4 only - use default cidr (ClassA/8, ClassB/16, ClassC/24)");
2✔
458
        Console.WriteLine();
2✔
459
        Console.WriteLine("Actions");
2✔
460
        Console.WriteLine("\t-h         : help message");
2✔
461
        Console.WriteLine("\t-s cidr    : split network into cidr subnets");
2✔
462
        Console.WriteLine("\t-w         : supernet networks into smallest possible subnets");
2✔
463
        Console.WriteLine("\t-W         : supernet networks into one single subnet");
2✔
464
        Console.WriteLine("\t-x         : list all ipaddresses in networks");
2✔
465
        Console.WriteLine("\t-C network : network contain networks");
2✔
466
        Console.WriteLine("\t-o network : network overlap networks");
2✔
467
        Console.WriteLine("\t-S network : subtract network from subnet");
2✔
468
        Console.WriteLine(string.Empty);
2✔
469
        Console.WriteLine("networks  : one or more network addresses ");
2✔
470
        Console.WriteLine(
2✔
471
            "            (1.2.3.4 10.0.0.0/8 10.0.0.0/255.0.0.0 2001:db8::/32 2001:db8:1:2:3:4:5:6/128 )");
2✔
472
    }
2✔
473
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc