A broadband city can swap its home routers for ones with a CLAT (ip6Go / ip6Stop, from the city panel's Home broadband). Over 30 to 180 days an even share of the homes moves each day, paid as the routers go in ($4 a home), waiting while cash is short; starting needs only the first day's share. New homes in a swapping or finished city get the new router at the same price. The panel says how many addresses it frees and what it does to the monthly IPv4 bill. ip4Use splits homes into public, behind CGNAT and IPv6-only: the NAT64 pool (ceil(IPv6-only homes / NAT64 ratio), 64/128/256, default 128) comes right after business blocks, before dual-stack homes. NAT64 gateways cost the same per Gbps of IPv4 as CGNAT boxes; NAT64 refunds sit under CGNAT's at the same ratio and never reach zero. The IPv4 share of home traffic is now a curve: 0.55 on day 0, two points less a year, floor 0.2. Out-of-space advice names the sharing left to stretch. Everything is under rule ip4; old saves load unchanged (no field means dual stack).
108 lines
11 KiB
JavaScript
108 lines
11 KiB
JavaScript
/* ---------- Address space (#150): IPv6 is free for everyone, IPv4 ran out years ago ----------
|
||
The network holds IPv4 blocks in real CIDR sizes (/24 to /16): its first allocation from the RIR, blocks bought on
|
||
the transfer market and blocks leased in by the month. Owned blocks can be leased out (a broker finds the tenant) or
|
||
sold, once two years have passed since they last moved (the RIRs' holding period, which also stops flipping).
|
||
Business customers on IP service take a public block each, sized by kind and port (a /29 for a small DIA port, a /23
|
||
for a big hosting company); broadband homes take what is left, and the homes that do not fit share addresses behind
|
||
CGNAT at the player's ratio. CGNAT costs a little per Gbps it carries (IPv6 goes around the box) and some gamers want
|
||
their money back. An offer that needs more than is free, with every home already behind CGNAT, cannot be signed, and a
|
||
customer renewing bigger stays at its size when the bigger block is not free (clGrow).
|
||
Nothing is stored per customer: what each one needs follows from its kind and size, so growing customers and old saves
|
||
need no new fields. On in On-call and BGP at 3 a.m. (rule ip4), off in the gentler presets, sandbox and puzzles;
|
||
scenarios only when they opt in (S.scn.rules.ip4). No random draws: the market price is a function of the day.
|
||
The first allocation is a /21: one transit's worth of broadband levels off at about 1,300 homes in every start city,
|
||
so the first months fit with room for a few customers (gameplay rule F1). */
|
||
// IP4_BLK: the most blocks a network holds; buying and leasing stop there, and a save keeps that many.
|
||
const IP4_START=21,IP4_MIN=16,IP4_MAX=24,IP4_HOLD=730,IP4_NAT=[8,16,32,64],IP4_NAT0=16,IP4_BLK=256;
|
||
// Public addresses per Gbps of busy-hour broadband (a home each), and the part of it CGNAT still carries on day 0: the rest
|
||
// is IPv6. The world moves on, two points a year to a floor that never goes (ip4V4).
|
||
const IP4_BB=10,IP4_V4=0.55,IP4_V4_YR=0.02,IP4_V4_MIN=0.2,IP4_CGN=2;
|
||
// IPv6-only homes (464XLAT, 34-ipv6-only.js) have no address of their own: their IPv4 shares a NAT64 pool, at a wider ratio
|
||
// than CGNAT can take. Only what still needs IPv4 notices (consoles, IPv4-only VPNs): a share of the bill comes back, well
|
||
// under CGNAT's at the same ratio, and shrinking as the world goes IPv6.
|
||
const IP4_N64=[64,128,256],IP4_N640=128,IP4_ANNOY64={64:0.006,128:0.01,256:0.016};
|
||
// The market: dollars per address, flat for half a year, then +3% a year on a slow four-year swing. Leases are by the
|
||
// month (a broker keeps a cut of what yours earn), sales pay 92% of the price after the broker's fee.
|
||
const IP4_PX=38,IP4_FREE=180,IP4_YR=0.03,IP4_IN=0.5,IP4_OUT=0.4,IP4_SELL=0.92;
|
||
// Of the bill of a home behind CGNAT, the share that comes back as refunds (gamers, mostly): more homes per address, more
|
||
// grumbling. Homes squeezed past the ratio (port exhaustion) are worse.
|
||
const IP4_ANNOY={8:0.01,16:0.015,32:0.025,64:0.04},IP4_SQZ=0.15;
|
||
// Per kind of customer: addresses per Gbps of port, and the smallest block it takes. Circuits, waves and fiber carry no
|
||
// addresses of yours; downstream ISPs and universities bring their own (universities sit on legacy /16s); orbit
|
||
// terminals sit behind the satellite operator's own CGNAT.
|
||
const IP4_BIZ={cgame:[0.5,256],dia:[2,8],fabip:[2,8],game:[2,16],gpu:[0.5,64],host:[4,64],metav:[0.25,256],scrub:[2,8],stream:[1,32]};
|
||
const ip4Sz=len=>2**(32-len),ip4LenOk=len=>Number.isInteger(len)&&len>=IP4_MIN&&len<=IP4_MAX;
|
||
const ip4Cidr=n=>'/'+(32-Math.ceil(Math.log2(Math.max(1,n)))),ip4N=n=>Math.round(n).toLocaleString();
|
||
function ip4On(){return !!S&&!!S.ip4&&rule('ip4')===true&&(S.scn?scnRule('ip4'):S.mode==='normal');}
|
||
function ip4Need(b){const k=b&&IP4_BIZ[b.type];if(!k||!(b.bw>0))return 0;return Math.min(4096,2**Math.ceil(Math.log2(Math.max(k[1],b.bw*k[0]))));}
|
||
/* Who uses what. T: usable addresses (owned and leased in, without what is leased out); B: business customers; H: homes,
|
||
of them H6 IPv6-only (Hd dual stack); p6: the NAT64 pool; cg: dual-stack homes behind CGNAT; over: the share of homes
|
||
that do not fit even at the ratios; need: the least the homes can live with; free: what a new customer can have with
|
||
every dual-stack home behind CGNAT. The NAT64 pool comes right after business customers: its homes gave their own
|
||
addresses up already and cannot fall back on anything, while dual-stack homes still can (behind CGNAT). */
|
||
function ip4Use(s=S){const I=s.ip4,r=I.nat,r6=I.n64||IP4_N640;let own=0,inn=0,out=0,B=0,H=0,H6=0;
|
||
for(const b of I.b){const n=ip4Sz(b.len);if(b.k==='in')inn+=n;else if(b.out)out+=n;else own+=n;}
|
||
for(const b of s.biz)B+=ip4Need(b);
|
||
for(const c in s.pops){const p=s.pops[c];if(p.bb){const h=p.bb.d*IP4_BB;H+=h;if(p.bb.v6)H6+=h*p.bb.v6;}}
|
||
H=Math.ceil(H);H6=Math.min(H,Math.ceil(H6));const Hd=H-H6,T=own+inn,A=Math.max(0,T-B),p6=Math.ceil(H6/r6),min=Math.ceil(Hd/r),need=p6+min,A2=A-p6;let cg=0,over=0;
|
||
if(A2<Hd){if(A2>=min)cg=Math.min(Hd,Math.ceil((Hd-A2)*r/(r-1)));else{cg=Hd;over=need?1-A/need:0;}}
|
||
return {T,own,inn,out,B,H,H6,Hd,p6,cg,over,need,free:T-B-need,r,r6};}
|
||
const ip4Fits=o=>!ip4On()||ip4Need(o)<=Math.max(0,ip4Use().free);
|
||
// Which sharing is left to stretch when space runs out, for the advice: CGNAT (0, no IPv6-only homes), the NAT64 pool alone
|
||
// (1, every home IPv6-only) or both (2).
|
||
const ip4Pools=u=>!u.H6?0:u.Hd?2:1;
|
||
// A customer renewing at bw: does the bigger block it needs fit what is free?
|
||
const ip4GrowOk=(b,bw)=>!ip4On()||ip4Need({type:b.type,bw})-ip4Need(b)<=Math.max(0,ip4Use().free);
|
||
const ip4Full=()=>S.ip4.b.length>=IP4_BLK;
|
||
// What you announce, as the AS box shows it: your blocks by prefix length, without the ones leased out to others.
|
||
const ip4Pfx=()=>{const c={};for(const b of S.ip4.b)if(!b.out)c[b.len]=(c[b.len]||0)+1;return Object.keys(c).sort((a,b)=>a-b).map(l=>`${c[l]} × /${l}`).join(', ');};
|
||
// Would giving this block up (selling, leasing it out, handing it back) still leave room for everyone?
|
||
const ip4Spare=b=>ip4Use().free>=ip4Sz(b.len);
|
||
const ip4Held=b=>b.k!=='in'&&S.day-b.d<IP4_HOLD;
|
||
function ip4Px(d=S.day){if(!rule('market')||d<=IP4_FREE)return IP4_PX;const y=(d-IP4_FREE)/365;return IP4_PX*(1+IP4_YR*y)*(1+0.12*Math.sin(2*Math.PI*y/4));}
|
||
// Bigger blocks are a little cheaper per address, as on the real market.
|
||
const ip4Cost=len=>Math.round(ip4Sz(len)*ip4Px()*(1-0.015*(IP4_MAX-len))/100)*100;
|
||
const ip4Value=len=>Math.round(ip4Cost(len)*IP4_SELL/100)*100;
|
||
const ip4InMo=len=>Math.round(ip4Sz(len)*IP4_IN*ip4Px()/IP4_PX),ip4OutMo=len=>Math.round(ip4Sz(len)*IP4_OUT*ip4Px()/IP4_PX);
|
||
// The IPv4 share of home traffic on day d: 55% on day 0 (so nothing changes at the start), two points less a year, never
|
||
// under a fifth (old consoles, old VPNs, old everything).
|
||
const ip4V4=(d=S.day)=>Math.max(IP4_V4_MIN,IP4_V4-IP4_V4_YR*Math.max(0,d)/365.2425);
|
||
/* The month's IPv4 bill: leases in and out, CGNAT boxes and NAT64 gateways (the same price per Gbps of IPv4 they carry),
|
||
refunds (eye: what broadband earns a month). NAT64 refunds shrink with the IPv4 share: in an IPv6-only home only what
|
||
still needs IPv4 meets the NAT at all. s: the network as ip4Use() takes it (a city panel asks about one that might be). */
|
||
function ip4Bill(eye,s=S){const u=ip4Use(s),v4=ip4V4();let lin=0,lout=0;for(const b of s.ip4.b){if(b.k==='in')lin+=b.mo||0;else if(b.out)lout+=b.mo||0;}
|
||
const cgS=u.H?u.cg/u.H:0,v6S=u.H?u.H6/u.H:0;
|
||
return {u,lin,lout,v4,box:u.cg/IP4_BB*v4*IP4_CGN,box64:u.H6/IP4_BB*v4*IP4_CGN,refund:eye*(cgS*IP4_ANNOY[u.r]+v6S*IP4_ANNOY64[u.r6]*v4/IP4_V4+u.over*IP4_SQZ)};}
|
||
// computeMoney: only what is there, so a network with nothing to pay keeps its money rows as they were.
|
||
function ip4Money(C,V){if(!ip4On())return;const b=ip4Bill(V.eyeballs||0),c=Math.round(b.lin+b.box+b.box64+b.refund);if(c>0)C.ip4=c;if(b.lout>0)V.ip4=b.lout;}
|
||
/* A new network gets its /21 from the RIR. An older save gets what it already uses registered to it and a quarter more to
|
||
grow into (about a month for a big, fast-growing network), rounded up to /24s in real CIDR blocks (a /21 at the
|
||
least), so nothing changes on load; those blocks move like any other two years on.
|
||
Their ids are fixed (rir0, rir1…), not uid(): a new game draws no id, so every id after it stays as it was. */
|
||
function ip4Grant(s){const d=s.day||0,b=[],add=len=>b.push({id:'rir'+b.length,len,k:'rir',d});let need=0;
|
||
for(const x of s.biz||[])need+=ip4Need(x);
|
||
for(const c in s.pops||{}){const p=s.pops[c];if(p&&p.bb&&p.bb.d>0)need+=p.bb.d*IP4_BB;}
|
||
need=Math.max(ip4Sz(IP4_START),Math.ceil(need*1.25/256)*256);
|
||
while(need>=ip4Sz(IP4_MIN)&&b.length<200){add(IP4_MIN);need-=ip4Sz(IP4_MIN);}
|
||
for(let len=IP4_MIN+1;len<=IP4_MAX;len++)if(need>=ip4Sz(len)){add(len);need-=ip4Sz(len);}
|
||
return {b,nat:IP4_NAT0,n64:IP4_N640,w:0};}
|
||
const ip4New=()=>ip4Grant({});
|
||
function ip4Fix(s){const I=s.ip4&&typeof s.ip4==='object'&&!Array.isArray(s.ip4)?s.ip4:null;if(!I){s.ip4=ip4Grant(s);return;}
|
||
const day=Math.max(0,num(s.day,0)),mo=v=>Math.round(Math.min(1e8,Math.max(0,num(v,0))));
|
||
const b=(Array.isArray(I.b)?I.b:[]).filter(x=>x&&typeof x==='object'&&ip4LenOk(x.len)&&['rir','buy','in'].includes(x.k)).slice(0,IP4_BLK).map(x=>{
|
||
const o={id:typeof x.id==='string'&&x.id&&x.id.length<=40?x.id:uid(),len:x.len,k:x.k,d:int(x.d,0,day,0)};
|
||
if(x.k==='in')o.mo=mo(x.mo);else if(x.out===true){o.out=true;o.mo=mo(x.mo);}return o;});
|
||
s.ip4={b,nat:IP4_NAT.includes(I.nat)?I.nat:IP4_NAT0,n64:IP4_N64.includes(I.n64)?I.n64:IP4_N640,w:int(I.w,0,3,0)};}
|
||
/* Once a day: say so when homes go behind CGNAT (a percent of them at least), and when even CGNAT cannot fit them all
|
||
(w: what was said). Each rearms only with room to spare again, so homes hovering at the limit hear it once. */
|
||
function ip4Day(d){if(!ip4On())return;ip6Day(d);const I=S.ip4,u=ip4Use(),A=u.T-u.B,go=[{l:tr('ip4.section'),act:'ip4Open'}];
|
||
if(u.cg>=Math.max(1,u.H*0.01)){if(!(I.w&1)){I.w|=1;toast(tr('ip4.toast_cgnat',{p:pct(u.cg/u.H)}),'',go);}}else if(A-u.p6>=u.Hd*1.05)I.w&=~1;
|
||
if(u.over>0){if(!(I.w&2)){I.w|=2;const k=ip4Pools(u);toast(k===1?tr('ip4.toast_out_v6',{r:u.r6}):k?tr('ip4.toast_out_both',{r:u.r,r6:u.r6}):tr('ip4.toast_out',{r:u.r}),'bad',go);sfx('err');}}
|
||
else if(A>=u.need*1.1)I.w&=~2;}
|
||
// Your customers need the addresses: what to stretch or get instead (ip4Busy, the bank's block rows).
|
||
function ip4InUse(u=ip4Use()){const k=ip4Pools(u);return k===1?tr('ip4.in_use_v6'):k?tr('ip4.in_use_both'):tr('ip4.in_use');}
|
||
// Renewals that wanted a bigger block than is free (clGrow, [customer, bandwidth]): they stay as they are, and hear why.
|
||
function ip4News(l=[]){for(const [b,bw] of l.slice(0,3))toast(tr('ip4.no_grow',{name:esc(b.name),city:esc(clWhere(b)),bw:fmtG(bw)}),'',[{l:tr('ip4.section'),act:'ip4Open'}]);}
|
||
// The line under an offer that cannot be signed for want of addresses.
|
||
function ip4Why(o){if(ip4Fits(o))return '';const n=ip4Need(o);
|
||
return `<div class="why">${tr('ip4.offer_needs',{p:ip4Cidr(n),n:ip4N(n),free:ip4N(Math.max(0,ip4Use().free))})} ${btn(tr('ip4.section'),'ip4Open',{},'ghost sm')}</div>`;}
|