Files
mars_nwe_pcab/clients/windows/ndis3/legacy_bridge.c

107 lines
9.2 KiB
C

/* SPDX-License-Identifier: GPL-2.0-or-later
* CRT-free x86 Win32 transport for MARS NDIS3. Winsock 1.1 + WinPcap 3.1
* Packet.dll OID API. Not a packet sniffer; writes only to verified virtual NIC.
* Historical Windows kernel/driver compatibility not established by PE build.
*/
#include "../client_core.h"
#include "legacy_core.h"
#include <string.h>
typedef unsigned long U32;
typedef unsigned short U16;
typedef unsigned char U8;
#define I __declspec(dllimport)
#define S __stdcall
I void S ExitProcess(U32);
I void* S GetStdHandle(U32);
I int S WriteFile(void*,const void*,U32,U32*,void*);
I char* S GetCommandLineA(void);
I U32 S GetFullPathNameA(const char*,U32,char*,char**);
I U32 S GetPrivateProfileStringA(const char*,const char*,const char*,char*,U32,const char*);
I unsigned S GetSystemDirectoryA(char*,unsigned);
I void* S LoadLibraryA(const char*);
I void* S GetProcAddress(void*,const char*);
I int S FreeLibrary(void*);
I U32 S GetTickCount(void);
I void S Sleep(U32);
I int S SetConsoleCtrlHandler(int (S *)(U32),int);
I void* S CreateMutexA(void*,int,const char*);
I int S CloseHandle(void*);
I U32 S GetLastError(void);
I int S WSAStartup(U16,void*);
I int S WSACleanup(void);
I int S WSAGetLastError(void);
I U32 S socket(int,int,int);
I int S closesocket(U32);
I int S setsockopt(U32,int,int,const char*,int);
I int S bind(U32,const void*,int);
I int S getsockname(U32,void*,int*);
I int S ioctlsocket(U32,long,U32*);
I int S sendto(U32,const char*,int,int,const void*,int);
I int S recvfrom(U32,char*,int,int,void*,int*);
/* No CRT; volatile copy/clear prevents optimizer replacement by recursive calls. */
void *memcpy(void*d,const void*s,size_t n){volatile U8*a=d;const U8*b=s;while(n--)*a++=*b++;return d;}
void *memset(void*d,int v,size_t n){volatile U8*a=d;while(n--)*a++=(U8)v;return d;}
int memcmp(const void*a,const void*b,size_t n){const U8*x=a,*y=b;while(n--){if(*x!=*y)return (int)*x-(int)*y;x++;y++;}return 0;}
size_t strlen(const char*s){size_t n=0;while(s[n])n++;return n;}
static int eq(const char*a,const char*b){size_t n=strlen(a);return n==strlen(b)&&!memcmp(a,b,n);}
struct ipxaddr {U16 family;U8 net[4],node[6];U16 port;};
struct oid {U32 oid,length;U8 data[MARS_OID_FRAME_SIZE];};
typedef void* (__cdecl *openfn)(char*);
typedef void (__cdecl *closefn)(void*);
typedef U8 (__cdecl *requestfn)(void*,U8,struct oid*);
typedef U8 (__cdecl *namesfn)(char*,U32*);
typedef char* (__cdecl *versionfn)(void);
typedef char addr_check[sizeof(struct ipxaddr)==14?1:-1];
typedef char oid_check[offsetof(struct oid,data)==8?1:-1];
static struct client_core core;
static struct ipxaddr peer;
static U32 sock=0xffffffffUL;
static int wsa,linked;
static volatile int stopping;
static void *console,*dll,*adapter,*mutex;
static closefn closep;static openfn openp;static requestfn requestp;static namesfn namesp;static versionfn versionp;
static union {U32 align;U8 bytes[512];} wsadata;
static char cmd[2048],names[32768],path[260];
static U8 frame[PKT_FRAME],packet[546],gw[4];
static struct oid request;
static void text(const char*s){U32 w;WriteFile(console,s,(U32)strlen(s),&w,0);}
static void number(U32 v){char b[11];unsigned i=10;b[i]=0;do{b[--i]=(char)('0'+v%10);v/=10;}while(v);text(b+i);}
static U32 netnum(const U8*n){return ((U32)n[0]<<24)|((U32)n[1]<<16)|((U32)n[2]<<8)|n[3];}
static U16 swap(U16 x){return (U16)((x<<8)|(x>>8));}
/* Simple bounded command line: quoted path or adapter name, no embedded quotes. */
static int args(char**v,int cap){const char*p=GetCommandLineA();char*w=cmd;int n=0,q;while(*p){while(*p==' '||*p=='\t')p++;if(!*p)break;if(n==cap)return -1;v[n++]=w;q=0;while(*p){if(*p=='"'){q=!q;p++;continue;}if(!q&&(*p==' '||*p=='\t'))break;if(w>=cmd+sizeof cmd-1)return -1;*w++=*p++;}if(q||w>=cmd+sizeof cmd)return -1;*w++=0;}return n;}
static int ip4(const char*s,U8*out){unsigned i,v,n;for(i=0;i<4;i++){v=0;n=0;while(*s>='0'&&*s<='9'){v=v*10+(*s++-'0');if(++n>3||v>255)return -1;}if(!n)return -1;out[i]=(U8)v;if(i<3&&*s++!='.')return -1;}return *s?-1:0;}
static int config(const char*file,const char*node){char n[32],m[32],p[16],ip[32],*last;U8 net[4],mac[6],server[6];U16 port;U32 l=GetFullPathNameA(file,sizeof path,path,&last);if(!l||l>=sizeof path)return -1;
GetPrivateProfileStringA("gateway","network","",n,sizeof n,path);GetPrivateProfileStringA("gateway","server","",m,sizeof m,path);GetPrivateProfileStringA("gateway","port","6181",p,sizeof p,path);GetPrivateProfileStringA("gateway","ip","",ip,sizeof ip,path);
port=eq(p,"6181")?PKT_TRAIL:eq(p,"6180")?PKT_PLAIN:0;if(!port||client_hex(n,net,4)||client_hex(m,server,6)||client_hex(node,mac,6)||ip4(ip,gw))return -1;
return client_init(&core,netnum(net),mac,server,port);
}
static int load_packet(void){unsigned n=GetSystemDirectoryA(path,sizeof path);const char*version;if(!n||n>=sizeof path-12)return -1;memcpy(path+n,"\\Packet.dll",12);dll=LoadLibraryA(path);if(!dll)return -1;
openp=(openfn)GetProcAddress(dll,"PacketOpenAdapter");closep=(closefn)GetProcAddress(dll,"PacketCloseAdapter");requestp=(requestfn)GetProcAddress(dll,"PacketRequest");namesp=(namesfn)GetProcAddress(dll,"PacketGetAdapterNames");versionp=(versionfn)GetProcAddress(dll,"PacketGetVersion");if(!openp||!closep||!requestp||!namesp||!versionp)return -1;
version=versionp();if(!version||strlen(version)<3||memcmp(version,"3.1",3))return -1;return 0;
}
static int oid(U32 id,int set,U8*d,U32 n){if(!adapter||n>sizeof request.data)return -1;memset(&request,0,sizeof request);request.oid=id;request.length=n;if(set)memcpy(request.data,d,n);if(!requestp(adapter,(U8)set,&request)||request.length!=n)return -1;if(!set)memcpy(d,request.data,n);return 0;}
static int list(void){U32 n=sizeof names;char*p,*q,*end;if(load_packet()||!namesp(names,&n)||!n||n>sizeof names)return -1;end=names+n;for(p=names;p<end&&*p;p=q+1){for(q=p;q<end&&*q;q++){ }if(q==end)return -1;text(p);text("\r\n");}return 0;}
static int attach(char*name){U8 ident[8],mac[6],up=1;if(load_packet()||strlen(name)>1023)return -1;adapter=openp(name);if(!adapter)return -1;
if(oid(MARS_OID_IDENT,0,ident,8)||memcmp(ident,MARS_IDENT,8)||oid(0x01010102UL,0,mac,6)||memcmp(mac,core.node,6)){text("Not the requested MARS virtual adapter / MAC. No MARS tunnel enabled.\r\n");return -1;}
if(oid(MARS_OID_LINK,1,&up,1))return -1;linked=1;return 0;}
static int ipx_open(void){struct ipxaddr a,b;int t=4,len=sizeof b;U32 nb=1;if(WSAStartup(0x0101,wsadata.bytes))return -1;wsa=1;sock=socket(6,2,1000);if(sock==0xffffffffUL)return -1;memset(&a,0,sizeof a);a.family=6;a.net[0]=(U8)(core.net>>24);a.net[1]=(U8)(core.net>>16);a.net[2]=(U8)(core.net>>8);a.net[3]=(U8)core.net;memcpy(a.node,core.node,6);a.port=swap(core.port);
if(setsockopt(sock,1000,0x4000,(const char*)&t,sizeof t)||bind(sock,&a,sizeof a)||getsockname(sock,&b,&len)||len!=(int)sizeof b||memcmp(&a,&b,sizeof a)||ioctlsocket(sock,0x8004667eUL,&nb))return -1;
memcpy(&peer,&a,sizeof peer);memcpy(peer.node,core.server,6);return 0;}
static int emit(void*unused,const U8*p,size_t n){(void)unused;return sendto(sock,(const char*)p,(int)n,0,&peer,sizeof peer)==(int)n?0:-1;}
static int S signal_stop(U32 code){if(code<=2||code==5||code==6){stopping=1;return 1;}return 0;}
static int run(void){U8 b[MARS_OID_FRAME_SIZE];int i,n,l,err;U32 last=GetTickCount();struct ipxaddr src;
client_probe(&core,gw,frame);if(client_tx(&core,frame,60,emit,0))text("Initial discovery probe failed.\r\n");
text("Bridge active; renew DHCP on virtual adapter. Ctrl+C stops.\r\n");
while(!stopping){for(i=0;i<32&&!stopping;i++){if(oid(MARS_OID_POP,0,b,sizeof b))return -1;n=b[0]|((unsigned)b[1]<<8);if(!n)break;if(n<14||n>PKT_FRAME)return -1;client_tx(&core,b+2,(size_t)n,emit,0);}
for(i=0;i<32&&!stopping;i++){l=sizeof src;n=recvfrom(sock,(char*)packet,sizeof packet,0,&src,&l);if(n<0){err=WSAGetLastError();if(err==10035)break;if(err==10040)continue;return -1;}if(l!=(int)sizeof src||src.family!=6)continue;n=client_rx(&core,netnum(src.net),src.node,swap(src.port),packet,(size_t)n,(time_t)(GetTickCount()/1000UL),frame);if(n>0){memset(b,0,sizeof b);b[0]=(U8)n;b[1]=(U8)(n>>8);memcpy(b+2,frame,n);if(oid(MARS_OID_PUSH,1,b,sizeof b))return -1;}}
if(GetTickCount()-last>=10000UL){text("TX ");number(core.tx);text(" RX ");number(core.received);text(" dropped ");number(core.dropped);text("\r\n");last=GetTickCount();}Sleep(10);}
return 0;}
void start(void){char*v[6];int n,rc=1;U8 down=0;console=GetStdHandle((U32)-11);n=args(v,6);
if(n==2&&eq(v[1],"--list")){rc=list()!=0;goto done;}
if(n==4&&eq(v[1],"--discover")){if(!config(v[3],v[2])&&!ipx_open()){client_probe(&core,gw,frame);rc=client_tx(&core,frame,60,emit,0)!=0;if(!rc)text("Probe sent. Check the server gateway devices.\r\n");}goto done;}
if(n!=6||!eq(v[1],"--run")||!eq(v[5],"--confirm-virtual")){text("Usage: MARSNET --list | --discover NODE GATEWAY.INI | --run ADAPTER NODE GATEWAY.INI --confirm-virtual\r\n");goto done;}
mutex=CreateMutexA(0,0,"MARSNET-IPXPKT-ONE-INSTANCE");if(!mutex||GetLastError()==183)goto done;
if(config(v[4],v[3])||ipx_open()||!SetConsoleCtrlHandler(signal_stop,1)||attach(v[2]))goto done;rc=run()!=0;
done:if(rc)text("MARSNET failed. Check IPX binding, Packet.dll 3.1, adapter and GATEWAY.INI.\r\n");if(linked)oid(MARS_OID_LINK,1,&down,1);if(adapter&&closep)closep(adapter);if(dll)FreeLibrary(dll);if(sock!=0xffffffffUL)closesocket(sock);if(wsa)WSACleanup();if(mutex)CloseHandle(mutex);ExitProcess((U32)rc);}