/// <summary> /// Update an existing VLAN. /// </summary> /// <param name="vlan"> /// The VLAN to update. /// </param> /// <param name="cancellationToken"> /// An optional cancellation token that can be used to cancel the request. /// </param> /// <returns> /// The API response from CloudControl. /// </returns> public async Task <ApiResponseV2> EditVlan(Vlan vlan, CancellationToken cancellationToken = default(CancellationToken)) { if (vlan == null) { throw new ArgumentNullException(nameof(vlan)); } Guid organizationId = await GetOrganizationId(); HttpRequest request = Requests.Network.EditVlan.WithTemplateParameter("organizationId", organizationId); HttpResponseMessage response = await _httpClient.PostAsJsonAsync(request, new EditVlan { Id = vlan.Id, Name = vlan.Name, Description = vlan.Description }, cancellationToken ); using (response) { return(await response.ReadContentAsApiResponseV2()); } }
public ActionResult UpdateVlan(Vlan form) { Vlan vlan = db.Vlans.Where(v => v.id == form.id).First(); vlan.name = form.name; db.SaveChanges(); return(RedirectToAction("Index", "Vlans")); }
internal Network(CiscoTelePresenceCodec codec, int indexer) : base(codec, indexer) { _cdp = new Cdp(this, "CDP"); _dns = new Dns(this, "DNS"); _ethernet = new Ethernet(this, "Ethernet"); _ipv4 = new Ipv4(this, "IPV4"); _ipv6 = new Ipv6(this, "IPV6"); _vlan = new Vlan(this, "VLAN"); }
public bool isTrunk() { if (Vlan != null) { return(Vlan.Equals("trunk")); } else { return(false); } }
public new EthernetPort this[string name] { get { if (!_switchPortInfos.ContainsKey(name)) { _switchPortInfos[name] = new SwitchPortInfo { Mode = AccessMode.ACCESS, Vlan = Vlan.Get(1).Get() }; } return(base[name]); } }
public void CreateVlanIfNotExist(int id, string name) { var result = db.Vlans.Where(v => v.vlanId == id); if (result.Count() == 1) { Console.WriteLine($"VLAN {id} found. Name: {name}"); } else { Console.WriteLine($"VLAN {id} not found in DB. Creating Vlan..."); var vlan = new Vlan(id, name); db.Vlans.Add(vlan); db.SaveChanges(); } }
public void SetPort(string port, AccessMode mode, Option <ushort> vlan) { Log.Info(Hostname, $"Setting port {port} to {mode.ToString().ToLower()} mode"); if (mode == AccessMode.TRUNK) { if (vlan != null) { throw new Exception("Can't set VLAN for a trunking port!"); } } if (vlan != null) { Log.Debug(Hostname, $"Accessing VLAN {vlan.Get()} on port {port}"); } else { vlan = Vlan.Get(1); } _switchPortInfos[port] = new SwitchPortInfo { Mode = mode, Vlan = vlan.Get() }; }
/// <summary> /// Asynchronously perform Cmdlet processing. /// </summary> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> that can be used to cancel Cmdlet processing. /// </param> /// <returns> /// A <see cref="Task"/> representing the asynchronous operation. /// </returns> protected override async Task ProcessRecordAsync(CancellationToken cancellationToken) { CloudControlClient client = GetClient(); Vlan vlan; switch (ParameterSetName) { case "From VLAN": { vlan = VLAN; break; } case "By Id": { vlan = await client.GetVlan(Id, cancellationToken); if (vlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(Id) ); return; } break; } default: { ThrowTerminatingError( Errors.UnrecognizedParameterSet(this) ); return; } } if (!ShouldProcess(target: $"VLAN '{vlan.Id}' ('{vlan.Name}') in '{vlan.NetworkDomain.Name}'", action: "Edit")) { return; } ApiResponseV2 editResponse = await client.EditVlan(vlan, cancellationToken); if (!editResponse.IsSuccess()) { WriteError( Errors.CloudControlApi(client, editResponse) ); return; } Vlan updatedVlan = await client.GetVlan(vlan.Id, cancellationToken); if (updatedVlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(vlan.Id) ); return; } WriteObject(updatedVlan); }
/// <summary> /// Asynchronously perform Cmdlet processing. /// </summary> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> that can be used to cancel Cmdlet processing. /// </param> /// <returns> /// A <see cref="Task"/> representing the asynchronous operation. /// </returns> protected override async Task ProcessRecordAsync(CancellationToken cancellationToken) { CloudControlClient client = GetClient(); Vlan vlan; switch (ParameterSetName) { case "From VLAN": { vlan = VLAN; break; } case "By Id": { vlan = await client.GetVlan(Id, cancellationToken); if (vlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(Id) ); return; } break; } default: { ThrowTerminatingError( Errors.UnrecognizedParameterSet(this) ); return; } } IPAddress baseAddress = IPAddress.Parse(vlan.PrivateIPv4Range.Address); IPAddress existingStartAddress, existingEndAddress; baseAddress.CalculateIPv4NetworkAddresses(vlan.PrivateIPv4Range.PrefixSize, out existingStartAddress, out existingEndAddress ); string existingNetwork = $"{baseAddress}/{vlan.PrivateIPv4Range.PrefixSize} ({existingStartAddress}-{existingEndAddress})"; IPAddress targetStartAddress, targetEndAddress; baseAddress.CalculateIPv4NetworkAddresses(IPv4PrefixSize, out targetStartAddress, out targetEndAddress ); string targetNetwork = $"{baseAddress}/{IPv4PrefixSize} ({targetStartAddress}-{targetEndAddress})"; if (IPv4PrefixSize >= vlan.PrivateIPv4Range.PrefixSize) { InvalidParameter(nameof(IPv4PrefixSize), $"Cannot expand VLAN network from {existingNetwork} to {targetNetwork}. To expand the VLAN's IPv4 network, reduce its IPv4 prefix size." ); return; } WriteVerbose( $"Expanding VLAN '{vlan.Name}' (in network domain '{vlan.NetworkDomain.Name}') from {existingNetwork} to {targetNetwork}." ); if (!ShouldProcess(target: $"'{vlan.Name}' ('{vlan.Id}') in '{vlan.NetworkDomain.Name}' (from {existingNetwork} to {targetNetwork}).", action: "Expand")) { return; } WriteVerbose("Initiating expansion of VLAN..."); ApiResponseV2 editResponse = await client.ExpandVlan(vlan.Id, IPv4PrefixSize, cancellationToken); if (!editResponse.IsSuccess()) { WriteError( Errors.CloudControlApi(client, editResponse) ); return; } WriteVerbose("VLAN expansion initiated."); Vlan updatedVlan = await client.GetVlan(vlan.Id, cancellationToken); if (updatedVlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(vlan.Id) ); return; } WriteObject(updatedVlan); }
/// <summary> /// Asynchronously perform Cmdlet processing. /// </summary> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> that can be used to cancel Cmdlet processing. /// </param> /// <returns> /// A <see cref="Task"/> representing the asynchronous operation. /// </returns> protected override async Task ProcessRecordAsync(CancellationToken cancellationToken) { CloudControlClient client = GetClient(); switch (ParameterSetName) { case "From network domain": { NetworkDomainId = NetworkDomain.Id; break; } case "From network domain Id": { NetworkDomain = await client.GetNetworkDomain(NetworkDomainId, cancellationToken); if (NetworkDomain == null) { WriteError( Errors.ResourceNotFoundById <NetworkDomain>(NetworkDomainId) ); return; } break; } default: { ThrowTerminatingError( Errors.UnrecognizedParameterSet(this) ); return; } } if (!ShouldProcess(target: $"VLAN '{Name}' in '{NetworkDomain.Name}'", action: "Create")) { return; } WriteVerbose( $"Deploy VLAN '{Name}' in network domain '{NetworkDomain.Name}' ({NetworkDomain.DatacenterId})." ); WriteVerbose("Initiating deployment of VLAN..."); Guid vlanId = await client.CreateVlan(Name, Description, NetworkDomainId, IPv4BaseAddress, IPv4PrefixSize, GatewayAddressing, cancellationToken); WriteVerbose($"Deployment initiated for VLAN '{vlanId}'."); Vlan vlan = await client.GetVlan(vlanId, cancellationToken); if (vlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(vlanId) ); return; } WriteObject(vlan); }
protected virtual async Task Main() { Log.SetLevel(Log.Level.TRACE, Log.Groups.SHOW); Vlan.Register(1, "DEFAULT"); Global.SetDeviceAutoStartup(true); //Global.SetPortAutoInit(true); /* * PC1 --- eth0/1 * | * | fa0/1 * SW1 * | fa0/2 * | * | fa0/1 * SW2 * | fa0/1 * | * PC2 --- eth0/1 */ Log.Group("Initialize devices"); var pc1 = new EthernetDevice("pc1"); var pc2 = new EthernetDevice("pc2"); var sw1 = new EthernetSwitch("sw1"); var sw2 = new EthernetSwitch("sw2"); Log.Group("Initialize ports"); //pc ports pc1[ETH01].Init(); pc2[ETH01].Init(); //Connection from sw1 to pc1 sw1[FA01].Init(); //Connection from sw2 to pc2 sw2[FA01].Init(); //Connection from sw1 to sw2 sw1[FA02].Init(); sw2[FA02].Init(); Log.Group("Connect ports"); //Connect the pcs to the switches pc1[ETH01].ConnectTo(sw1[FA01]); pc2[ETH01].ConnectTo(sw2[FA01]); //Connect the switches to each other sw1[FA02].ConnectTo(sw2[FA02]); Log.Group("Set switchport modes"); //Set the ports from pc to switch to access vlan 1 sw1.SetPort(FA01, EthernetSwitch.AccessMode.ACCESS, Vlan.Get(1)); sw2.SetPort(FA01, EthernetSwitch.AccessMode.ACCESS, Vlan.Get(1)); //Set the ports from switch to switch to trunk sw1.SetPort(FA02, EthernetSwitch.AccessMode.TRUNK, null); sw2.SetPort(FA02, EthernetSwitch.AccessMode.TRUNK, null); //Log.Group("Current state"); //Log.PrintState(); //Learn MAC Addresses Log.Group("Learn MAC Addresses"); /* * The API can be used with constructors (like this) */ pc1[ETH01].SendSync(new EthernetFrame(Constants.ETHERNET_BROADCAST_PORT, pc1[ETH01], Vlan.Get(1), new RawPacket(new byte[100]))); //Wait for all sending operations to be finished (you don't HAVE to wait...I just prefer doing so, cause the log is more readable) //This is necessary cause even tho you send this frame synchronously, all the connected devices create new tasks for incoming frames await Global.WaitForOperationsFinished(); /* * Or like this (with a static methods and a scapy-esque construction method) */ pc2[ETH01].SendSync(Ethernet(Constants.ETHERNET_BROADCAST_ADDRESS, pc2[ETH01]) < -/*Yes...this is indeed valid C#*/ Dot1Q(Vlan.Get(1)) < -RawPacket(new byte[100])); await Global.WaitForOperationsFinished(); Log.Group("Send Ethernet frame over learned ports"); pc1[ETH01].SendAsync(Ethernet(pc2[ETH01], pc1[ETH01]) | Dot1Q(Vlan.Get(1)) | RawPacket(new byte[100])); await Global.WaitForOperationsFinished(); pc1[ETH01].Disconnect(); pc2[ETH01].Disconnect(); pc1.Shutdown(); pc2.Shutdown(); Log.PrintState(); //Console.ReadKey(); }
public EthernetSwitch(string name) : base(name, null) { Log.Info(Hostname, "Initializing switch..."); OnReceive = (frame, port) => { if (frame.Data.Header.EtherType <= 1500) { //Ok...so apparently, an Ethernet 2 frame is constructed the same way a normal Ethernet Frame is //The only difference is in the Type field //In Ethernet 2 this is 2 bytes which, in decimal, is >= 1536 //And in Ethernet <= 1500 //We also expect this frame to be untagged //https://networkengineering.stackexchange.com/questions/5300/what-is-the-difference-between-ethernet-ii-and-802-3-ethernet //TODO: Handle STP BPDU return; } //If an untagged frame comes in, tag it if (!(frame.Data.Payload is Dot1QPDU)) { var type = frame.Data.Header.EtherType; var payload = frame.Data.Payload; var dot1q = new Dot1QPDU { Header = new Dot1QHeader { Type = type, VlanID = _switchPortInfos[port.Name].Vlan == 0 ? Vlan.Get(1).Get() : _switchPortInfos[port.Name].Vlan }, Payload = payload }; frame.Data.Payload = dot1q; frame.Data.Header.EtherType = 0x8100; frame.Data.FCS = Util.GetFCS(frame); } lock (MACTable) { if (!(MACTable.Any(a => a.Key == ((Dot1QPDU)frame.Data.Payload).Header.VlanID) && MACTable .Where(a => a.Key == ((Dot1QPDU)frame.Data.Payload).Header.VlanID) .Select(a => a.Value).FirstOr(new Dictionary <MACAddress, string>()) .Any(a => a.Key == frame.Data.Header.Src))) { Log.Warn(Hostname, $"Unknown MAC Address {frame.Data.Header.Src} for VLAN {((Dot1QPDU) frame.Data.Payload).Header.VlanID.ToMACAddressString()}"); Log.Debug(Hostname, $"Adding MAC Address {frame.Data.Header.Src} to MAC Address table for VLAN {((Dot1QPDU) frame.Data.Payload).Header.VlanID.ToMACAddressString()}..."); if (MACTable.All(a => a.Key != ((Dot1QPDU)frame.Data.Payload).Header.VlanID)) { MACTable[((Dot1QPDU)frame.Data.Payload).Header.VlanID] = new Dictionary <MACAddress, string>(); } var id = MACTable.Where(a => a.Key == ((Dot1QPDU)frame.Data.Payload).Header.VlanID).Select(a => a.Key).First(); MACTable[id].Add(frame.Data.Header.Src, port.Name); } } var dstPort = string.Empty; if (frame.Data.Header.Dst != Constants.ETHERNET_BROADCAST_ADDRESS.Get()) { dstPort = MACTable.Where(a => a.Key == ((Dot1QPDU)frame.Data.Payload).Header.VlanID && a.Value.Any(b => b.Key == frame.Data.Header.Dst)).Select(a => a.Value.Where(b => b.Key == frame.Data.Header.Dst).Select(b => b.Value).FirstOr(null)).FirstOr(null); } //Flooding if (string.IsNullOrEmpty(dstPort)) { //Send to all ports except for source port //Send to all access ports in the same VLAN //Send to all trunk ports var dstPorts = _switchPortInfos.Where(a => a.Key != port.Name && (a.Value.Vlan == ((Dot1QPDU)frame.Data.Payload).Header.VlanID || a.Value.Mode == AccessMode.TRUNK)).Select(a => a.Key).ToList(); if (!dstPorts.Any()) { Log.Error(Hostname, "Can't send a frame to any possible port (Possible VLAN mismatch)! Dropping!"); return; } foreach (var s in dstPorts) { base[s].Send(frame, true); } } else { base[dstPort].Send(frame, true); } }; PostConstruct(); }
/// <summary> /// Asynchronously perform Cmdlet processing. /// </summary> /// <param name="cancellationToken"> /// A <see cref="CancellationToken"/> that can be used to cancel Cmdlet processing. /// </param> /// <returns> /// A <see cref="Task"/> representing the asynchronous operation. /// </returns> protected override async Task ProcessRecordAsync(CancellationToken cancellationToken) { CloudControlClient client = GetClient(); switch (ParameterSetName) { case "By network domain": { Paging paging = GetPagingConfiguration(); Vlans vlans = await client.ListVlans(NetworkDomainId, paging, cancellationToken); WriteObject(vlans.Items, enumerateCollection: true ); break; } case "By Id": { Vlan vlan = await client.GetVlan(Id, cancellationToken); if (vlan == null) { WriteError( Errors.ResourceNotFoundById <Vlan>(Id) ); } else { WriteObject(vlan); } break; } case "By name": { Vlan vlan = await client.GetVlanByName(Name, NetworkDomainId); if (vlan == null) { WriteError( Errors.ResourceNotFoundByName <Vlan>(Name, message: $"No network domain named '{Name}' was found in network domain '{NetworkDomainId}'." ) ); } else { WriteObject(vlan); } break; } default: { ThrowTerminatingError( Errors.UnrecognizedParameterSet(this) ); return; } } }
public ActionResult CreateVlan(Vlan vlan) { db.Vlans.Add(vlan); db.SaveChanges(); return(RedirectToAction("Index", "Vlans")); }