115 lines
5.8 KiB
C
115 lines
5.8 KiB
C
/* Private candidate: invoked only on the owned account worker, never in DllMain.
|
|
* The public endpoint set and global-unicast policy match the current owner.
|
|
* This file is not installed and does not establish startup integration. */
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
static int eligible(const unsigned char a[4]) {
|
|
if (a[0]==0 || a[0]==10 || a[0]==127 || a[0]>=224) return 0;
|
|
if (a[0]==100 && a[1]>=64 && a[1]<=127) return 0;
|
|
if (a[0]==169 && a[1]==254) return 0;
|
|
if (a[0]==172 && a[1]>=16 && a[1]<=31) return 0;
|
|
if (a[0]==192 && a[1]==0 && a[2]==0 && a[3]!=9 && a[3]!=10) return 0;
|
|
if (a[0]==192 && a[1]==0 && a[2]==2) return 0;
|
|
if (a[0]==192 && a[1]==168) return 0;
|
|
if (a[0]==198 && (a[1]==18 || a[1]==19)) return 0;
|
|
if (a[0]==198 && a[1]==51 && a[2]==100) return 0;
|
|
if (a[0]==203 && a[1]==0 && a[2]==113) return 0;
|
|
return 1;
|
|
}
|
|
int gw_private_public_ipv4_eligible(const unsigned char a[4]) { return a && eligible(a); }
|
|
/* Local-only fixture: the target and every discovered realm must be the
|
|
* exact loopback host; the public profile policy is unchanged. */
|
|
int gw_private_source_realm_ipv4_eligible(unsigned int profile,const unsigned char a[4]) {
|
|
if (!a) return 0;
|
|
if (profile==4) return a[0]==127 && a[1]==0 && a[2]==0 && a[3]==1;
|
|
return (profile==2 || profile==3) && eligible(a);
|
|
}
|
|
/* Build-bound local endpoints never use DNS or the public-address policy.
|
|
* Keep this parser independent of Winsock so the exact policy is testable. */
|
|
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 5
|
|
#if !defined(GW_SOURCE_LOCAL_AUTH_IPV4) || !defined(GW_SOURCE_LOCAL_REALM_IPV4) || !defined(GW_SOURCE_LOCAL_REALM_PORT)
|
|
#error Local Source profile requires explicit auth and realm endpoints
|
|
#endif
|
|
#if GW_SOURCE_LOCAL_REALM_PORT < 1 || GW_SOURCE_LOCAL_REALM_PORT > 65535
|
|
#error Local Source realm port is out of range
|
|
#endif
|
|
static int local_ipv4(const char *s,unsigned char out[4]) {
|
|
unsigned i,n,v; if(!s||!out)return 0;
|
|
for(i=0;i<4;i++){
|
|
n=0;v=0;if(*s<'0'||*s>'9')return 0;
|
|
if(*s=='0' && s[1]>='0' && s[1]<='9')return 0;
|
|
while(*s>='0' && *s<='9'){if(++n>3)return 0;v=v*10+(unsigned)(*s++-'0');if(v>255)return 0;}
|
|
out[i]=(unsigned char)v;
|
|
if(i<3){if(*s++!='.')return 0;}else if(*s)return 0;
|
|
}
|
|
return out[0]==127 || out[0]==10 || (out[0]==172 && out[1]>=16 && out[1]<=31) || (out[0]==192 && out[1]==168);
|
|
}
|
|
#endif
|
|
int gw_private_source_realm_endpoint_eligible(unsigned int profile,const unsigned char a[4],uint16_t port) {
|
|
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 5
|
|
/* LOCAL: realmd lists several realms (normal, Hardcore, PvP) on the pinned private host, each on its own
|
|
* world port; the host pin is the boundary, so any unprivileged port on it is selectable. */
|
|
if(profile==5){unsigned char pinned[4];unsigned i;if(!a || port<1024 || !local_ipv4(GW_SOURCE_LOCAL_REALM_IPV4,pinned))return 0;
|
|
for(i=0;i<4;i++){if(a[i]!=pinned[i])return 0;}return 1;}
|
|
#else
|
|
(void)port;
|
|
#endif
|
|
return gw_private_source_realm_ipv4_eligible(profile,a);
|
|
}
|
|
static unsigned int octet(char *p, unsigned int x) {
|
|
unsigned int n=0;if(x>=100)p[n++]=(char)('0'+x/100);
|
|
if(x>=10)p[n++]=(char)('0'+(x/10)%10);
|
|
p[n++]=(char)('0'+x%10);return n;
|
|
}
|
|
static void render(const unsigned char a[4],char p[16]) {
|
|
unsigned int n=0,i;for(i=0;i<4;i++){if(i)p[n++]='.';n+=octet(p+n,a[i]);}p[n]=0;
|
|
}
|
|
static int before(const char *a,const char *b) {
|
|
while(*a && *a==*b){++a;++b;}return (unsigned char)*a<(unsigned char)*b;
|
|
}
|
|
/* Consumes all supplied answers; never returns a partial valid subset. */
|
|
int gw_private_select_ipv4(const unsigned char (*answers)[4],size_t count,char out[16]) {
|
|
char best[16]={0},cur[16];size_t i,n=0,j;if(!answers||!out||!count||count>256)return -1;
|
|
for(i=0;i<count;i++) {
|
|
if(!eligible(answers[i]))return -1;
|
|
render(answers[i],cur);for(j=0;cur[j];j++){}n+=j+1;
|
|
if(n>4096)return -1;
|
|
if(!best[0]||before(cur,best))for(j=0;j<16;j++){best[j]=cur[j];if(!cur[j])break;}
|
|
}
|
|
for(j=0;j<16;j++){out[j]=best[j];if(!best[j])break;}return 0;
|
|
}
|
|
#ifdef _WIN32
|
|
#define UNICODE 1
|
|
#define _UNICODE 1
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
#include <windows.h>
|
|
/* Kinds 2 and 3 are private-only. The existing 0/1 numeric ABI is untouched. */
|
|
int gw_private_worker_resolve(unsigned int profile,char out[16]) {
|
|
const WCHAR *host;WSADATA wsa;ADDRINFOEXW hints={0},*res=0,*p;
|
|
unsigned char answers[256][4];size_t count=0;int status,result=-1;
|
|
if(profile==4){ static const char loopback[]="127.0.0.1"; size_t i; if(!out)return -1; for(i=0;i<16;++i)out[i]=i<sizeof(loopback)?loopback[i]:0; return 0; }
|
|
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 5
|
|
if(profile==5){unsigned char parsed[4];size_t i;const char *s=GW_SOURCE_LOCAL_AUTH_IPV4;if(!out||!local_ipv4(s,parsed))return -1;for(i=0;i<16;i++){out[i]=*s?*s++:0;}return 0;}
|
|
#endif
|
|
if(profile==2)host=L"play.octowow.st";else if(profile==3)host=L"praxis.go.ro";else return -1;
|
|
if(WSAStartup(MAKEWORD(2,2),&wsa))return -1;
|
|
hints.ai_family=AF_INET;hints.ai_socktype=SOCK_STREAM;hints.ai_protocol=IPPROTO_TCP;
|
|
status=GetAddrInfoExW(host,L"3724",NS_ALL,0,&hints,&res,0,0,0,0);
|
|
if(status==0){
|
|
for(p=res;p;p=p->ai_next){
|
|
const struct sockaddr_in *sa;
|
|
if(count==256||p->ai_family!=AF_INET||!p->ai_addr||p->ai_addrlen<sizeof(*sa)){count=0;break;}
|
|
sa=(const struct sockaddr_in*)p->ai_addr;
|
|
if(sa->sin_family!=AF_INET||sa->sin_port!=htons(3724)){count=0;break;}
|
|
{const unsigned char *b=(const unsigned char*)&sa->sin_addr;unsigned int i;for(i=0;i<4;i++)answers[count][i]=b[i];}
|
|
++count;
|
|
}
|
|
if(count)result=gw_private_select_ipv4(answers,count,out);
|
|
}
|
|
if(res)FreeAddrInfoExW(res);
|
|
WSACleanup();return result;
|
|
}
|
|
#endif
|