<%
' Get form inputs
Dim vlan, ipstart, cidr, description, subnettypeid, cidrarray, ipend
vlan = Trim(Request.Form("vlan"))
ipstart = Trim(Request.Form("ipstart"))
cidr = Trim(Request.Form("cidr"))
description = Trim(Request.Form("description"))
subnettypeid = Trim(Request.Form("subnettypeid"))
' Validate required fields
If vlan = "" Or ipstart = "" Or cidr = "" Or subnettypeid = "" Then
Response.Write("
Error: Required field missing.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Validate VLAN is numeric
If Not IsNumeric(vlan) Then
Response.Write("
Error: VLAN must be numeric.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Basic IP address validation
If Len(ipstart) < 7 Or Len(ipstart) > 15 Then
Response.Write("
Error: Invalid IP address.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Validate subnet type ID
If Not IsNumeric(subnettypeid) Or CLng(subnettypeid) < 1 Then
Response.Write("
Error: Invalid subnet type.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Parse CIDR value (expected format: "cidr,ipend")
If InStr(cidr, ",") = 0 Then
Response.Write("
Error: Invalid CIDR format.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
cidrarray = Split(cidr, ",")
If UBound(cidrarray) < 1 Then
Response.Write("
Error: Invalid CIDR format.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
ipend = Trim(cidrarray(1))
cidr = Trim(cidrarray(0))
' Validate CIDR is numeric
If Not IsNumeric(cidr) Or CInt(cidr) < 0 Or CInt(cidr) > 32 Then
Response.Write("
Error: CIDR must be between 0 and 32.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Validate ipend is numeric
If Not IsNumeric(ipend) Then
Response.Write("
Error: Invalid IP end value.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Validate description length
If Len(description) > 500 Then
Response.Write("
Error: Description too long.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
' Escape quotes
description = Replace(description, "'", "''")
ipstart = Replace(ipstart, "'", "''")
' Verify subnet type exists
Dim checkSQL, rsCheck
checkSQL = "SELECT COUNT(*) as cnt FROM subnettypes WHERE subnettypeid = " & subnettypeid
Set rsCheck = objConn.Execute(checkSQL)
If rsCheck("cnt") = 0 Then
rsCheck.Close
Response.Write("
Error: Subnet type not found.
")
Response.Write("
Go back")
objConn.Close
Response.End
End If
rsCheck.Close
' Insert
' Note: INET_ATON requires the IP address, ipend is added to the result
Dim strSQL
strSQL = "INSERT INTO subnets (vlan, description, cidr, ipstart, ipend, subnettypeid, isactive) " & _
"VALUES (" & vlan & ", '" & description & "', " & cidr & ", INET_ATON('" & ipstart & "'), (INET_ATON('" & ipstart & "') + " & ipend & "), " & subnettypeid & ", 1)"
On Error Resume Next
objConn.Execute strSQL
If Err.Number = 0 Then
objConn.Close
Response.Redirect("./displaysubnets.asp")
Else
Response.Write("
Error: " & Err.Description & "
")
Response.Write("
Go back")
objConn.Close
End If
%>