source: abuse/branches/pd/macabuse/imlib/port/dos4gw/ipxtest.c @ 636

Last change on this file since 636 was 49, checked in by Sam Hocevar, 15 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1#include "doscall.c"
2#include <dos.h>
3
4
5#define JC_SEG(x) (((long)x)>>4)
6#define JC_OFF(x) (((long)x)&0xf)
7#define MKPTR(x)  ((   (((unsigned long)x)&0xffff0000)>>12)+  (((unsigned long)x)&0xffff))
8#define MAX_PACKETS 10
9#define jmalloc(x,str) malloc(x)
10#define jfree(x)       free(x)
11
12#pragma pack (1)
13
14static rminfo rm;
15#include <stdlib.h>
16
17struct ECBStructure
18{
19  ushort Link[2];                /* offset-segment */
20  ushort ESRAddress[2];          /* offset-segment */
21  uchar  InUseFlag;
22  uchar  CompletionCode;
23  ushort ECBSocket;              /* high-low */
24  uchar  IPXWorkspace[4];        /* N/A */
25  uchar  DriverWorkspace[12];    /* N/A */
26  uchar  ImmediateAddress[6];    /* high-low */
27  ushort FragmentCount;          /* low-high */
28  ushort fAddress[2];            /* offset-segment */
29  ushort fSize;                  /* low-high */
30} ;
31
32struct IPXPacketStructure
33{
34  ushort PacketCheckSum;         /* high-low */
35  ushort PacketLength;           /* high-low */
36  uchar  PacketTransportControl;
37  uchar  PacketType;
38
39  uchar  dNetwork[4];            /* high-low */
40  uchar  dNode[6];               /* high-low */
41  ushort dSocket;                /* high-low */
42
43  uchar  sNetwork[4];            /* high-low */
44  uchar  sNode[6];               /* high-low */
45  ushort sSocket;                /* high-low */
46};
47
48
49struct JC_ipx_packet
50{
51  ECBStructure ecb;
52  IPXPacketStructure ipx;
53  ulong time_stamp;
54  ushort verify_stamp;       // should be 0cdc  "crack dot com", all others ignored
55  uchar buffer[512];
56} ;
57
58
59int ipx_init()
60{
61  memset(&rm,0,sizeof(rm));
62  rm.eax=0x7a00;
63  RM_intr(0x2f,&rm);
64  return (rm.eax&0xff)==0xff;
65}
66
67
68
69uchar *ipx_get_local_address()                // same format as above (be sure to jfree this)
70{
71  uchar *addr=(uchar *)jmalloc(10,"IPX address");  // normal memory return
72  uchar *low_addr=(uchar *)alloc_low_memory(10);
73  memset(&rm,0,sizeof(rm));
74  rm.ebx=9;
75  rm.esi=JC_OFF(low_addr);
76  rm.es=JC_SEG(low_addr);
77  RM_intr(0x7a,&rm);
78
79  memcpy(addr,low_addr,10);
80  free_low_memory(low_addr);
81  return addr;
82}
83
84
85int ipx_listen(ECBStructure *ecb)
86{
87  memset(&rm,0,sizeof(rm));
88  rm.esi=JC_OFF(ecb);
89  rm.es=JC_SEG(ecb);
90
91  rm.ebx=4;
92  RM_intr(0x7a,&rm);
93
94  if (rm.eax&0xff)
95    return -1;
96  return 0;
97}
98
99#define htons(x) ((((x)>>8)&0xff)|  (((x)&0xff)<<8))
100
101JC_ipx_packet *ipx_socket(int port)
102{
103  memset(&rm,0,sizeof(rm));
104  rm.ebx=0;                      // Open socket function
105  rm.eax=0;                      // close on program exit
106  rm.edx=htons(port);                      // dynamically allocate socket number
107  RM_intr(0x7a,&rm);
108  if (rm.eax&0xff)
109  {
110    printf("Unable to created ipx socket on port %d\n",port);
111    return NULL;
112  }
113  printf("opened socket %x\n",htons(port));
114
115  printf("created socket %x\n",rm.edx&0xffff);
116  int fd=(ushort)htons((rm.edx&0xffff));
117
118
119
120  JC_ipx_packet *pk=(JC_ipx_packet *)alloc_low_memory(sizeof(JC_ipx_packet)*MAX_PACKETS);
121  if (!pk)
122  {
123    printf("unable to allocate low memory for packets\n");
124    exit(0);
125  }
126  memset(pk,0,sizeof(JC_ipx_packet)*MAX_PACKETS);
127
128
129  // setup an outgoing packet structure
130  pk[0].ecb.ECBSocket = htons(fd);
131  pk[0].ecb.FragmentCount = 1;
132  pk[0].ecb.fAddress[0] = JC_OFF(&pk[0].ipx);
133  pk[0].ecb.fAddress[1] = JC_SEG(&pk[0].ipx);
134
135//  pk[0].ipx.PacketCheckSum=0xffff;
136 
137  uchar *my_addr=ipx_get_local_address();
138  memcpy(pk[0].ipx.sNetwork,my_addr,10);
139  pk[0].ipx.sSocket=htons(fd);
140
141  memcpy(pk[0].ipx.dNetwork,my_addr,4);
142  memset(pk[0].ipx.dNode,0xff,6);
143  memset(pk[0].ecb.ImmediateAddress,0xff,6);
144  pk[0].ipx.dSocket=htons(fd);
145
146  jfree(my_addr);
147
148 
149  for (int i=1;i<MAX_PACKETS;i++)
150  {
151    // setup incoming packet structure
152    pk[i].ecb.InUseFlag = 0x1d;
153    pk[i].ecb.ECBSocket = htons(fd);
154    pk[i].ecb.FragmentCount = 1;
155    pk[i].ecb.fAddress[0] = JC_OFF(&pk[i].ipx);
156    pk[i].ecb.fAddress[1] = JC_SEG(&pk[i].ipx);
157    pk[i].ecb.fSize = sizeof(JC_ipx_packet)-sizeof(ECBStructure);
158    if (ipx_listen(&pk[i].ecb)<0)
159      printf("listen failed on packet %d\n",i);
160    else
161      printf("listen success on packet %d\n",i);
162  } 
163
164  printf("listen to socket %x\n",htons(fd));
165  return pk;
166}
167
168int ipx_ready_to_read(JC_ipx_packet *pk)
169{
170  int i;
171  for (i=1 ; i<MAX_PACKETS ; i++)
172  {
173    if (!pk[i].ecb.InUseFlag)
174    {
175      printf("<saw a packet>\n");
176      if (ipx_listen(&pk[i].ecb)<0)
177      { printf("failed to re-listen\n"); }
178
179      return 1;   
180    }
181  }
182  return 0;
183}
184
185void ipx_close(JC_ipx_packet *pk)
186{
187  memset(&rm,0,sizeof(rm));
188  rm.ebx=1;
189  rm.edx=pk[0].ecb.ECBSocket;
190  printf("close socket %x\n",pk[0].ecb.ECBSocket);
191  RM_intr(0x7a,&rm);
192}
193
194
195void free_up_memory() { ; }
196
197
198int ipx_send(JC_ipx_packet *pk)
199{
200  pk[0].ecb.fSize = sizeof(pk[0].ipx)+10;
201
202  unsigned char *a=(unsigned char *)pk[0].ipx.dNetwork;
203  printf("send : %x%x%x%x-%x%x%x%x%x%x (%x)\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],pk[0].ipx.dSocket);
204
205  memset(&rm,0,sizeof(rm));
206  rm.ebx=3;                      // send packet function
207  rm.esi=JC_OFF(&pk[0]);
208  rm.es=JC_SEG(&pk[0]);
209
210  RM_intr(0x7a,&rm);
211  return 1;
212}
213
214main()
215{
216  if (!ipx_init())
217    printf("IPX not installed\n");
218  else
219  {
220    JC_ipx_packet *sock=ipx_socket(0x869b);
221    if (sock==NULL)
222      printf("unable to open socket\n");
223    else
224    {
225      int i=0;
226      while (i<5)
227      {
228        while (!ipx_ready_to_read(sock))
229        {
230          fprintf(stderr,".");
231          sleep(1);
232          ipx_send(sock);
233          fprintf(stderr,"\\");
234        }
235
236        i++;     
237        printf("saw a packet\n");
238      }
239      ipx_close(sock);
240
241    }                       
242  }
243}
Note: See TracBrowser for help on using the repository browser.