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

lduchosal / ipnetwork / 738

29 Mar 2025 10:31PM UTC coverage: 93.524% (+0.2%) from 93.279%
738

push

appveyor

web-flow
feat/code-quality (#350)

* Chore: file scoped namespace

* Feat: sign assembly
Fix: documentation on public methodes

* Fix: documentation and warnings

* Chore: split IPNetwork2 into multiple partial classes

* Chore: split IPNetwork2

* Chore: document partial classes

* Fix: order methodes

* Chore: documentd

* Chore: cleanup

1603 of 1714 relevant lines covered (93.52%)

825934.22 hits per line

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

82.37
/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 System.Collections.Generic;
8
using System.Diagnostics;
9
using System.IO;
10
using System.Numerics;
11
using System.Reflection;
12
using Gnu.Getopt;
13

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

21
    private static readonly ArgParsed[] ArgsList = new[]
1✔
22
    {
1✔
23
        new ArgParsed('i', (ac, arg) => { ac.IPNetwork = true; }),
3✔
24
        new ArgParsed('n', (ac, arg) => { ac.Network = true; }),
6✔
25
        new ArgParsed('m', (ac, arg) => { ac.Netmask = true; }),
6✔
26
        new ArgParsed('c', (ac, arg) => { ac.Cidr = true; }),
6✔
27
        new ArgParsed('b', (ac, arg) => { ac.Broadcast = true; }),
6✔
28
        new ArgParsed('f', (ac, arg) => { ac.FirstUsable = true; }),
6✔
29
        new ArgParsed('l', (ac, arg) => { ac.LastUsable = true; }),
6✔
30
        new ArgParsed('u', (ac, arg) => { ac.Usable = true; }),
6✔
31
        new ArgParsed('t', (ac, arg) => { ac.Total = true; }),
×
32
        new ArgParsed('w', (ac, arg) => { ac.Action = ActionEnum.Supernet; }),
3✔
33
        new ArgParsed('W', (ac, arg) => { ac.Action = ActionEnum.WideSupernet; }),
×
34
        new ArgParsed('h', (ac, arg) => { ac.Action = ActionEnum.Usage; }),
×
35
        new ArgParsed('x', (ac, arg) => { ac.Action = ActionEnum.ListIPAddress; }),
3✔
36
        new ArgParsed('?', (ac, arg) => { }),
4✔
37
        new ArgParsed('D', (ac, arg) => { 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 (!Program.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 (!Program.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 (!Program.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 = Program.ParseArgs(args);
11✔
107

108
        if (ac.Action == ActionEnum.Subnet)
11✔
109
        {
1✔
110
            Program.SubnetNetworks(ac);
1✔
111
        }
1✔
112
        else if (ac.Action == ActionEnum.Supernet)
10✔
113
        {
1✔
114
            Program.SupernetNetworks(ac);
1✔
115
        }
1✔
116
        else if (ac.Action == ActionEnum.WideSupernet)
9✔
117
        {
×
118
            Program.WideSupernetNetworks(ac);
×
119
        }
×
120
        else if (ac.Action == ActionEnum.PrintNetworks)
9✔
121
        {
5✔
122
            Program.PrintNetworks(ac);
5✔
123
        }
5✔
124
        else if (ac.Action == ActionEnum.ContainNetwork)
4✔
125
        {
1✔
126
            Program.ContainNetwork(ac);
1✔
127
        }
1✔
128
        else if (ac.Action == ActionEnum.OverlapNetwork)
3✔
129
        {
1✔
130
            Program.OverlapNetwork(ac);
1✔
131
        }
1✔
132
        else if (ac.Action == ActionEnum.ListIPAddress)
2✔
133
        {
×
134
            Program.ListIPAddress(ac);
×
135
        }
×
136
        else
137
        {
2✔
138
            Program.Usage();
2✔
139
        }
2✔
140
    }
11✔
141

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

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

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

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

178
        Program.PrintNetwork(ac, widesubnet);
×
179
    }
×
180

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

188
        Program.PrintNetworks(ac, supernet, supernet.Length);
1✔
189
    }
1✔
190

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

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

216
            Program.PrintNetworks(ac, ipnetworks, ipnetworks.Count);
1✔
217
            Program.PrintSeparator(networkLength, i);
1✔
218
        }
1✔
219
    }
1✔
220

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

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

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

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

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

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

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

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

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

284
            if (ac.Usable)
269✔
285
            {
263✔
286
                sw.WriteLine("Usable      : {0}", ipn.Usable);
263✔
287
            }
263✔
288

289
            if (ac.Total)
269✔
290
            {
261✔
291
                sw.WriteLine("Total       : {0}", ipn.Total);
261✔
292
            }
261✔
293

294
            Console.Write(sw.ToString());
269✔
295
        }
269✔
296
    }
269✔
297

298
    static Program()
299
    {
1✔
300
        foreach (ArgParsed ap in Program.ArgsList)
43✔
301
        {
20✔
302
            Program.Args.Add(ap.Arg, ap);
20✔
303
        }
20✔
304
    }
1✔
305

306
    private static ProgramContext ParseArgs(string[] args)
307
    {
11✔
308
        int c;
309
        var g = new Getopt("ipnetwork", args, "inmcbfltud:Dhs:wWxC:o:S:");
11✔
310
        var ac = new ProgramContext();
11✔
311

312
        while ((c = g.getopt()) != -1)
36✔
313
        {
25✔
314
            string optArg = g.Optarg;
25✔
315
            Program.Args[c].Run(ac, optArg);
25✔
316
        }
25✔
317

318
        var ipnetworks = new List<string>();
11✔
319
        for (int i = g.Optind; i < args.Length; i++)
72✔
320
        {
25✔
321
            if (!string.IsNullOrEmpty(args[i]))
25✔
322
            {
25✔
323
                ipnetworks.Add(args[i]);
25✔
324
            }
25✔
325
        }
25✔
326

327
        ac.NetworksString = ipnetworks.ToArray();
11✔
328
        Program.ParseIPNetworks(ac);
11✔
329

330
        if (ac.Networks.Length == 0)
11✔
331
        {
1✔
332
            Console.WriteLine("Provide at least one ipnetwork");
1✔
333
            ac.Action = ActionEnum.Usage;
1✔
334
        }
1✔
335

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

343
        if (ac.Action == ActionEnum.WideSupernet
11✔
344
            && ipnetworks.Count < 2)
11✔
345
        {
×
346
            Console.WriteLine("WideSupernet action required at least two ipnetworks");
×
347
            ac.Action = ActionEnum.Usage;
×
348
        }
×
349

350
        if (Program.PrintNoValue(ac))
11✔
351
        {
8✔
352
            Program.PrintAll(ac);
8✔
353
        }
8✔
354

355
        if (g.Optind == 0)
11✔
356
        {
3✔
357
            Program.PrintAll(ac);
3✔
358
        }
3✔
359

360
        return ac;
11✔
361
    }
11✔
362

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

374
            ipnetworks.Add(ipnetwork);
24✔
375
        }
24✔
376

377
        ac.Networks = ipnetworks.ToArray();
11✔
378
    }
11✔
379

380
    private static bool TryParseIPNetwork(string ip, CidrParseEnum cidrParseEnum, byte cidr, out IPNetwork2 ipn)
381
    {
28✔
382
        IPNetwork2 ipnetwork = null;
28✔
383
        if (cidrParseEnum == CidrParseEnum.Default)
28✔
384
        {
×
385
            if (!IPNetwork2.TryParse(ip, out ipnetwork))
×
386
            {
×
387
                ipn = null;
×
388
                return false;
×
389
            }
390
        }
×
391
        else if (cidrParseEnum == CidrParseEnum.Value)
28✔
392
        {
28✔
393
            if (!IPNetwork2.TryParse(ip, cidr, out ipnetwork))
28✔
394
            {
22✔
395
                if (!IPNetwork2.TryParse(ip, out ipnetwork))
22✔
396
                {
1✔
397
                    ipn = null;
1✔
398
                    return false;
1✔
399
                }
400
            }
21✔
401
        }
27✔
402

403
        ipn = ipnetwork;
27✔
404
        return true;
27✔
405
    }
28✔
406

407
    private static bool PrintNoValue(ProgramContext ac)
408
    {
11✔
409
        ArgumentNullException.ThrowIfNull(ac);
11✔
410

411
        return ac.IPNetwork == false
11✔
412
               && ac.Network == false
11✔
413
               && ac.Netmask == false
11✔
414
               && ac.Cidr == false
11✔
415
               && ac.Broadcast == false
11✔
416
               && ac.FirstUsable == false
11✔
417
               && ac.LastUsable == false
11✔
418
               && ac.Total == false
11✔
419
               && ac.Usable == false;
11✔
420
    }
11✔
421

422
    private static void PrintAll(ProgramContext ac)
423
    {
11✔
424
        ArgumentNullException.ThrowIfNull(ac);
11✔
425

426
        ac.IPNetwork = true;
11✔
427
        ac.Network = true;
11✔
428
        ac.Netmask = true;
11✔
429
        ac.Cidr = true;
11✔
430
        ac.Broadcast = true;
11✔
431
        ac.FirstUsable = true;
11✔
432
        ac.LastUsable = true;
11✔
433
        ac.Usable = true;
11✔
434
        ac.Total = true;
11✔
435
    }
11✔
436

437
    private static void Usage()
438
    {
2✔
439
        Assembly assembly = Assembly.GetEntryAssembly()
2✔
440
                            ?? Assembly.GetExecutingAssembly()
2✔
441
                            ?? Assembly.GetCallingAssembly()
2✔
442
            ;
2✔
443
        var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
2✔
444
        string version = fvi.FileVersion;
2✔
445

446
        Console.WriteLine(
2✔
447
            "Usage: ipnetwork [-inmcbflu] [-d cidr|-D] [-h|-s cidr|-S|-w|-W|-x|-C network|-o network] networks ...");
2✔
448
        Console.WriteLine("Version: {0}", version);
2✔
449
        Console.WriteLine();
2✔
450
        Console.WriteLine("Print options");
2✔
451
        Console.WriteLine("\t-i : network");
2✔
452
        Console.WriteLine("\t-n : network address");
2✔
453
        Console.WriteLine("\t-m : netmask");
2✔
454
        Console.WriteLine("\t-c : cidr");
2✔
455
        Console.WriteLine("\t-b : broadcast");
2✔
456
        Console.WriteLine("\t-f : first usable ip address");
2✔
457
        Console.WriteLine("\t-l : last usable ip address");
2✔
458
        Console.WriteLine("\t-u : number of usable ip addresses");
2✔
459
        Console.WriteLine("\t-t : total number of ip addresses");
2✔
460
        Console.WriteLine();
2✔
461
        Console.WriteLine("Parse options");
2✔
462
        Console.WriteLine("\t-d cidr : use cidr if not provided (default /32)");
2✔
463
        Console.WriteLine("\t-D      : IPv4 only - use default cidr (ClassA/8, ClassB/16, ClassC/24)");
2✔
464
        Console.WriteLine();
2✔
465
        Console.WriteLine("Actions");
2✔
466
        Console.WriteLine("\t-h         : help message");
2✔
467
        Console.WriteLine("\t-s cidr    : split network into cidr subnets");
2✔
468
        Console.WriteLine("\t-w         : supernet networks into smallest possible subnets");
2✔
469
        Console.WriteLine("\t-W         : supernet networks into one single subnet");
2✔
470
        Console.WriteLine("\t-x         : list all ipaddresses in networks");
2✔
471
        Console.WriteLine("\t-C network : network contain networks");
2✔
472
        Console.WriteLine("\t-o network : network overlap networks");
2✔
473
        Console.WriteLine("\t-S network : subtract network from subnet");
2✔
474
        Console.WriteLine(string.Empty);
2✔
475
        Console.WriteLine("networks  : one or more network addresses ");
2✔
476
        Console.WriteLine(
2✔
477
            "            (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✔
478
    }
2✔
479
}
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