pktdrvr.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * File.........: pktdrvr.c
  3. *
  4. * Responsible..: Gisle Vanem, giva@bgnett.no
  5. *
  6. * Created......: 26.Sept 1995
  7. *
  8. * Description..: Packet-driver interface for 16/32-bit C :
  9. * Borland C/C++ 3.0+ small/large model
  10. * Watcom C/C++ 11+, DOS4GW flat model
  11. * Metaware HighC 3.1+ and PharLap 386|DosX
  12. * GNU C/C++ 2.7+ and djgpp 2.x extender
  13. *
  14. * References...: PC/TCP Packet driver Specification. rev 1.09
  15. * FTP Software Inc.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <dos.h>
  22. #include "pcap-dos.h"
  23. #include "pcap-int.h"
  24. #include "msdos/pktdrvr.h"
  25. #if (DOSX)
  26. #define NUM_RX_BUF 32 /* # of buffers in Rx FIFO queue */
  27. #else
  28. #define NUM_RX_BUF 10
  29. #endif
  30. #define DIM(x) (sizeof((x)) / sizeof(x[0]))
  31. #define PUTS(s) do { \
  32. if (!pktInfo.quiet) \
  33. pktInfo.error ? \
  34. printf ("%s: %s\n", s, pktInfo.error) : \
  35. printf ("%s\n", pktInfo.error = s); \
  36. } while (0)
  37. #if defined(__HIGHC__)
  38. extern UINT _mwenv;
  39. #elif defined(__DJGPP__)
  40. #include <stddef.h>
  41. #include <dpmi.h>
  42. #include <go32.h>
  43. #include <pc.h>
  44. #include <sys/farptr.h>
  45. #elif defined(__WATCOMC__)
  46. #include <i86.h>
  47. #include <stddef.h>
  48. extern char _Extender;
  49. #else
  50. extern void far PktReceiver (void);
  51. #endif
  52. #if (DOSX & (DJGPP|DOS4GW))
  53. #include <sys/pack_on.h>
  54. struct DPMI_regs {
  55. DWORD r_di;
  56. DWORD r_si;
  57. DWORD r_bp;
  58. DWORD reserved;
  59. DWORD r_bx;
  60. DWORD r_dx;
  61. DWORD r_cx;
  62. DWORD r_ax;
  63. WORD r_flags;
  64. WORD r_es, r_ds, r_fs, r_gs;
  65. WORD r_ip, r_cs, r_sp, r_ss;
  66. };
  67. /* Data located in a real-mode segment. This becomes far at runtime
  68. */
  69. typedef struct { /* must match data/code in pkt_rx1.s */
  70. WORD _rxOutOfs;
  71. WORD _rxInOfs;
  72. DWORD _pktDrop;
  73. BYTE _pktTemp [20];
  74. TX_ELEMENT _pktTxBuf[1];
  75. RX_ELEMENT _pktRxBuf[NUM_RX_BUF];
  76. WORD _dummy[2]; /* screenSeg,newInOffset */
  77. BYTE _fanChars[4];
  78. WORD _fanIndex;
  79. BYTE _PktReceiver[15]; /* starts on a paragraph (16byte) */
  80. } PktRealStub;
  81. #include <sys/pack_off.h>
  82. static BYTE real_stub_array [] = {
  83. #include "pkt_stub.inc" /* generated opcode array */
  84. };
  85. #define rxOutOfs offsetof (PktRealStub,_rxOutOfs)
  86. #define rxInOfs offsetof (PktRealStub,_rxInOfs)
  87. #define PktReceiver offsetof (PktRealStub,_PktReceiver [para_skip])
  88. #define pktDrop offsetof (PktRealStub,_pktDrop)
  89. #define pktTemp offsetof (PktRealStub,_pktTemp)
  90. #define pktTxBuf offsetof (PktRealStub,_pktTxBuf)
  91. #define FIRST_RX_BUF offsetof (PktRealStub,_pktRxBuf [0])
  92. #define LAST_RX_BUF offsetof (PktRealStub,_pktRxBuf [NUM_RX_BUF-1])
  93. #else
  94. extern WORD rxOutOfs; /* offsets into pktRxBuf FIFO queue */
  95. extern WORD rxInOfs;
  96. extern DWORD pktDrop; /* # packets dropped in PktReceiver() */
  97. extern BYTE pktRxEnd; /* marks the end of r-mode code/data */
  98. extern RX_ELEMENT pktRxBuf [NUM_RX_BUF]; /* PktDrvr Rx buffers */
  99. extern TX_ELEMENT pktTxBuf; /* PktDrvr Tx buffer */
  100. extern char pktTemp[20]; /* PktDrvr temp area */
  101. #define FIRST_RX_BUF (WORD) &pktRxBuf [0]
  102. #define LAST_RX_BUF (WORD) &pktRxBuf [NUM_RX_BUF-1]
  103. #endif
  104. #ifdef __BORLANDC__ /* Use Borland's inline functions */
  105. #define memcpy __memcpy__
  106. #define memcmp __memcmp__
  107. #define memset __memset__
  108. #endif
  109. #if (DOSX & PHARLAP)
  110. extern void PktReceiver (void); /* in pkt_rx0.asm */
  111. static int RealCopy (ULONG, ULONG, REALPTR*, FARPTR*, USHORT*);
  112. #undef FP_SEG
  113. #undef FP_OFF
  114. #define FP_OFF(x) ((WORD)(x))
  115. #define FP_SEG(x) ((WORD)(realBase >> 16))
  116. #define DOS_ADDR(s,o) (((DWORD)(s) << 16) + (WORD)(o))
  117. #define r_ax eax
  118. #define r_bx ebx
  119. #define r_dx edx
  120. #define r_cx ecx
  121. #define r_si esi
  122. #define r_di edi
  123. #define r_ds ds
  124. #define r_es es
  125. LOCAL FARPTR protBase;
  126. LOCAL REALPTR realBase;
  127. LOCAL WORD realSeg; /* DOS para-address of allocated area */
  128. LOCAL SWI_REGS reg;
  129. static WORD _far *rxOutOfsFp, *rxInOfsFp;
  130. #elif (DOSX & DJGPP)
  131. static _go32_dpmi_seginfo rm_mem;
  132. static __dpmi_regs reg;
  133. static DWORD realBase;
  134. static int para_skip = 0;
  135. #define DOS_ADDR(s,o) (((WORD)(s) << 4) + (o))
  136. #define r_ax x.ax
  137. #define r_bx x.bx
  138. #define r_dx x.dx
  139. #define r_cx x.cx
  140. #define r_si x.si
  141. #define r_di x.di
  142. #define r_ds x.ds
  143. #define r_es x.es
  144. #elif (DOSX & DOS4GW)
  145. LOCAL struct DPMI_regs reg;
  146. LOCAL WORD rm_base_seg, rm_base_sel;
  147. LOCAL DWORD realBase;
  148. LOCAL int para_skip = 0;
  149. LOCAL DWORD dpmi_get_real_vector (int intr);
  150. LOCAL WORD dpmi_real_malloc (int size, WORD *selector);
  151. LOCAL void dpmi_real_free (WORD selector);
  152. #define DOS_ADDR(s,o) (((DWORD)(s) << 4) + (WORD)(o))
  153. #else /* real-mode Borland etc. */
  154. static struct {
  155. WORD r_ax, r_bx, r_cx, r_dx, r_bp;
  156. WORD r_si, r_di, r_ds, r_es, r_flags;
  157. } reg;
  158. #endif
  159. #ifdef __HIGHC__
  160. #pragma Alias (pktDrop, "_pktDrop")
  161. #pragma Alias (pktRxBuf, "_pktRxBuf")
  162. #pragma Alias (pktTxBuf, "_pktTxBuf")
  163. #pragma Alias (pktTemp, "_pktTemp")
  164. #pragma Alias (rxOutOfs, "_rxOutOfs")
  165. #pragma Alias (rxInOfs, "_rxInOfs")
  166. #pragma Alias (pktRxEnd, "_pktRxEnd")
  167. #pragma Alias (PktReceiver,"_PktReceiver")
  168. #endif
  169. PUBLIC PKT_STAT pktStat; /* statistics for packets */
  170. PUBLIC PKT_INFO pktInfo; /* packet-driver information */
  171. PUBLIC PKT_RX_MODE receiveMode = PDRX_DIRECT;
  172. PUBLIC ETHER myAddress = { 0, 0, 0, 0, 0, 0 };
  173. PUBLIC ETHER ethBroadcast = { 255,255,255,255,255,255 };
  174. LOCAL struct { /* internal statistics */
  175. DWORD tooSmall; /* size < ETH_MIN */
  176. DWORD tooLarge; /* size > ETH_MAX */
  177. DWORD badSync; /* count_1 != count_2 */
  178. DWORD wrongHandle; /* upcall to wrong handle */
  179. } intStat;
  180. /***************************************************************************/
  181. PUBLIC const char *PktGetErrorStr (int errNum)
  182. {
  183. static const char *errStr[] = {
  184. "",
  185. "Invalid handle number",
  186. "No interfaces of specified class found",
  187. "No interfaces of specified type found",
  188. "No interfaces of specified number found",
  189. "Bad packet type specified",
  190. "Interface does not support multicast",
  191. "Packet driver cannot terminate",
  192. "Invalid receiver mode specified",
  193. "Insufficient memory space",
  194. "Type previously accessed, and not released",
  195. "Command out of range, or not implemented",
  196. "Cannot send packet (usually hardware error)",
  197. "Cannot change hardware address ( > 1 handle open)",
  198. "Hardware address has bad length or format",
  199. "Cannot reset interface (more than 1 handle open)",
  200. "Bad Check-sum",
  201. "Bad size",
  202. "Bad sync" ,
  203. "Source hit"
  204. };
  205. if (errNum < 0 || errNum >= DIM(errStr))
  206. return ("Unknown driver error.");
  207. return (errStr [errNum]);
  208. }
  209. /**************************************************************************/
  210. PUBLIC const char *PktGetClassName (WORD class)
  211. {
  212. switch (class)
  213. {
  214. case PD_ETHER:
  215. return ("DIX-Ether");
  216. case PD_PRONET10:
  217. return ("ProNET-10");
  218. case PD_IEEE8025:
  219. return ("IEEE 802.5");
  220. case PD_OMNINET:
  221. return ("OmniNet");
  222. case PD_APPLETALK:
  223. return ("AppleTalk");
  224. case PD_SLIP:
  225. return ("SLIP");
  226. case PD_STARTLAN:
  227. return ("StartLAN");
  228. case PD_ARCNET:
  229. return ("ArcNet");
  230. case PD_AX25:
  231. return ("AX.25");
  232. case PD_KISS:
  233. return ("KISS");
  234. case PD_IEEE8023_2:
  235. return ("IEEE 802.3 w/802.2 hdr");
  236. case PD_FDDI8022:
  237. return ("FDDI w/802.2 hdr");
  238. case PD_X25:
  239. return ("X.25");
  240. case PD_LANstar:
  241. return ("LANstar");
  242. case PD_PPP:
  243. return ("PPP");
  244. default:
  245. return ("unknown");
  246. }
  247. }
  248. /**************************************************************************/
  249. PUBLIC char const *PktRXmodeStr (PKT_RX_MODE mode)
  250. {
  251. static const char *modeStr [] = {
  252. "Receiver turned off",
  253. "Receive only directly addressed packets",
  254. "Receive direct & broadcast packets",
  255. "Receive direct,broadcast and limited multicast packets",
  256. "Receive direct,broadcast and all multicast packets",
  257. "Receive all packets (promiscuouos mode)"
  258. };
  259. if (mode > DIM(modeStr))
  260. return ("??");
  261. return (modeStr [mode-1]);
  262. }
  263. /**************************************************************************/
  264. LOCAL __inline BOOL PktInterrupt (void)
  265. {
  266. BOOL okay;
  267. #if (DOSX & PHARLAP)
  268. _dx_real_int ((UINT)pktInfo.intr, &reg);
  269. okay = ((reg.flags & 1) == 0); /* OK if carry clear */
  270. #elif (DOSX & DJGPP)
  271. __dpmi_int ((int)pktInfo.intr, &reg);
  272. okay = ((reg.x.flags & 1) == 0);
  273. #elif (DOSX & DOS4GW)
  274. union REGS r;
  275. struct SREGS s;
  276. memset (&r, 0, sizeof(r));
  277. segread (&s);
  278. r.w.ax = 0x300;
  279. r.x.ebx = pktInfo.intr;
  280. r.w.cx = 0;
  281. s.es = FP_SEG (&reg);
  282. r.x.edi = FP_OFF (&reg);
  283. reg.r_flags = 0;
  284. reg.r_ss = reg.r_sp = 0; /* DPMI host provides stack */
  285. int386x (0x31, &r, &r, &s);
  286. okay = (!r.w.cflag);
  287. #else
  288. reg.r_flags = 0;
  289. intr (pktInfo.intr, (struct REGPACK*)&reg);
  290. okay = ((reg.r_flags & 1) == 0);
  291. #endif
  292. if (okay)
  293. pktInfo.error = NULL;
  294. else pktInfo.error = PktGetErrorStr (reg.r_dx >> 8);
  295. return (okay);
  296. }
  297. /**************************************************************************/
  298. /*
  299. * Search for packet driver at interrupt 60h through 80h. If ASCIIZ
  300. * string "PKT DRVR" found at offset 3 in the interrupt handler, return
  301. * interrupt number, else return zero in pktInfo.intr
  302. */
  303. PUBLIC BOOL PktSearchDriver (void)
  304. {
  305. BYTE intr = 0x20;
  306. BOOL found = FALSE;
  307. while (!found && intr < 0xFF)
  308. {
  309. static char str[12]; /* 3 + strlen("PKT DRVR") */
  310. static char pktStr[9] = "PKT DRVR"; /* ASCIIZ string at ofs 3 */
  311. DWORD rp; /* in interrupt routine */
  312. #if (DOSX & PHARLAP)
  313. _dx_rmiv_get (intr, &rp);
  314. ReadRealMem (&str, (REALPTR)rp, sizeof(str));
  315. #elif (DOSX & DJGPP)
  316. __dpmi_raddr realAdr;
  317. __dpmi_get_real_mode_interrupt_vector (intr, &realAdr);
  318. rp = (realAdr.segment << 4) + realAdr.offset16;
  319. dosmemget (rp, sizeof(str), &str);
  320. #elif (DOSX & DOS4GW)
  321. rp = dpmi_get_real_vector (intr);
  322. memcpy (&str, (void*)rp, sizeof(str));
  323. #else
  324. _fmemcpy (&str, getvect(intr), sizeof(str));
  325. #endif
  326. found = memcmp (&str[3],&pktStr,sizeof(pktStr)) == 0;
  327. intr++;
  328. }
  329. pktInfo.intr = (found ? intr-1 : 0);
  330. return (found);
  331. }
  332. /**************************************************************************/
  333. static BOOL PktSetAccess (void)
  334. {
  335. reg.r_ax = 0x0200 + pktInfo.class;
  336. reg.r_bx = 0xFFFF;
  337. reg.r_dx = 0;
  338. reg.r_cx = 0;
  339. #if (DOSX & PHARLAP)
  340. reg.ds = 0;
  341. reg.esi = 0;
  342. reg.es = RP_SEG (realBase);
  343. reg.edi = (WORD) &PktReceiver;
  344. #elif (DOSX & DJGPP)
  345. reg.x.ds = 0;
  346. reg.x.si = 0;
  347. reg.x.es = rm_mem.rm_segment;
  348. reg.x.di = PktReceiver;
  349. #elif (DOSX & DOS4GW)
  350. reg.r_ds = 0;
  351. reg.r_si = 0;
  352. reg.r_es = rm_base_seg;
  353. reg.r_di = PktReceiver;
  354. #else
  355. reg.r_ds = 0;
  356. reg.r_si = 0;
  357. reg.r_es = FP_SEG (&PktReceiver);
  358. reg.r_di = FP_OFF (&PktReceiver);
  359. #endif
  360. if (!PktInterrupt())
  361. return (FALSE);
  362. pktInfo.handle = reg.r_ax;
  363. return (TRUE);
  364. }
  365. /**************************************************************************/
  366. PUBLIC BOOL PktReleaseHandle (WORD handle)
  367. {
  368. reg.r_ax = 0x0300;
  369. reg.r_bx = handle;
  370. return PktInterrupt();
  371. }
  372. /**************************************************************************/
  373. PUBLIC BOOL PktTransmit (const void *eth, int len)
  374. {
  375. if (len > ETH_MTU)
  376. return (FALSE);
  377. reg.r_ax = 0x0400; /* Function 4, send pkt */
  378. reg.r_cx = len; /* total size of frame */
  379. #if (DOSX & DJGPP)
  380. dosmemput (eth, len, realBase+pktTxBuf);
  381. reg.x.ds = rm_mem.rm_segment; /* DOS data segment and */
  382. reg.x.si = pktTxBuf; /* DOS offset to buffer */
  383. #elif (DOSX & DOS4GW)
  384. memcpy ((void*)(realBase+pktTxBuf), eth, len);
  385. reg.r_ds = rm_base_seg;
  386. reg.r_si = pktTxBuf;
  387. #elif (DOSX & PHARLAP)
  388. memcpy (&pktTxBuf, eth, len);
  389. reg.r_ds = FP_SEG (&pktTxBuf);
  390. reg.r_si = FP_OFF (&pktTxBuf);
  391. #else
  392. reg.r_ds = FP_SEG (eth);
  393. reg.r_si = FP_OFF (eth);
  394. #endif
  395. return PktInterrupt();
  396. }
  397. /**************************************************************************/
  398. #if (DOSX & (DJGPP|DOS4GW))
  399. LOCAL __inline BOOL CheckElement (RX_ELEMENT *rx)
  400. #else
  401. LOCAL __inline BOOL CheckElement (RX_ELEMENT _far *rx)
  402. #endif
  403. {
  404. WORD count_1, count_2;
  405. /*
  406. * We got an upcall to the same RMCB with wrong handle.
  407. * This can happen if we failed to release handle at program exit
  408. */
  409. if (rx->handle != pktInfo.handle)
  410. {
  411. pktInfo.error = "Wrong handle";
  412. intStat.wrongHandle++;
  413. PktReleaseHandle (rx->handle);
  414. return (FALSE);
  415. }
  416. count_1 = rx->firstCount;
  417. count_2 = rx->secondCount;
  418. if (count_1 != count_2)
  419. {
  420. pktInfo.error = "Bad sync";
  421. intStat.badSync++;
  422. return (FALSE);
  423. }
  424. if (count_1 > ETH_MAX)
  425. {
  426. pktInfo.error = "Large esize";
  427. intStat.tooLarge++;
  428. return (FALSE);
  429. }
  430. #if 0
  431. if (count_1 < ETH_MIN)
  432. {
  433. pktInfo.error = "Small esize";
  434. intStat.tooSmall++;
  435. return (FALSE);
  436. }
  437. #endif
  438. return (TRUE);
  439. }
  440. /**************************************************************************/
  441. PUBLIC BOOL PktTerminHandle (WORD handle)
  442. {
  443. reg.r_ax = 0x0500;
  444. reg.r_bx = handle;
  445. return PktInterrupt();
  446. }
  447. /**************************************************************************/
  448. PUBLIC BOOL PktResetInterface (WORD handle)
  449. {
  450. reg.r_ax = 0x0700;
  451. reg.r_bx = handle;
  452. return PktInterrupt();
  453. }
  454. /**************************************************************************/
  455. PUBLIC BOOL PktSetReceiverMode (PKT_RX_MODE mode)
  456. {
  457. if (pktInfo.class == PD_SLIP || pktInfo.class == PD_PPP)
  458. return (TRUE);
  459. reg.r_ax = 0x1400;
  460. reg.r_bx = pktInfo.handle;
  461. reg.r_cx = (WORD)mode;
  462. if (!PktInterrupt())
  463. return (FALSE);
  464. receiveMode = mode;
  465. return (TRUE);
  466. }
  467. /**************************************************************************/
  468. PUBLIC BOOL PktGetReceiverMode (PKT_RX_MODE *mode)
  469. {
  470. reg.r_ax = 0x1500;
  471. reg.r_bx = pktInfo.handle;
  472. if (!PktInterrupt())
  473. return (FALSE);
  474. *mode = reg.r_ax;
  475. return (TRUE);
  476. }
  477. /**************************************************************************/
  478. static PKT_STAT initialStat; /* statistics at startup */
  479. static BOOL resetStat = FALSE; /* statistics reset ? */
  480. PUBLIC BOOL PktGetStatistics (WORD handle)
  481. {
  482. reg.r_ax = 0x1800;
  483. reg.r_bx = handle;
  484. if (!PktInterrupt())
  485. return (FALSE);
  486. #if (DOSX & PHARLAP)
  487. ReadRealMem (&pktStat, DOS_ADDR(reg.ds,reg.esi), sizeof(pktStat));
  488. #elif (DOSX & DJGPP)
  489. dosmemget (DOS_ADDR(reg.x.ds,reg.x.si), sizeof(pktStat), &pktStat);
  490. #elif (DOSX & DOS4GW)
  491. memcpy (&pktStat, (void*)DOS_ADDR(reg.r_ds,reg.r_si), sizeof(pktStat));
  492. #else
  493. _fmemcpy (&pktStat, MK_FP(reg.r_ds,reg.r_si), sizeof(pktStat));
  494. #endif
  495. return (TRUE);
  496. }
  497. /**************************************************************************/
  498. PUBLIC BOOL PktSessStatistics (WORD handle)
  499. {
  500. if (!PktGetStatistics(pktInfo.handle))
  501. return (FALSE);
  502. if (resetStat)
  503. {
  504. pktStat.inPackets -= initialStat.inPackets;
  505. pktStat.outPackets -= initialStat.outPackets;
  506. pktStat.inBytes -= initialStat.inBytes;
  507. pktStat.outBytes -= initialStat.outBytes;
  508. pktStat.inErrors -= initialStat.inErrors;
  509. pktStat.outErrors -= initialStat.outErrors;
  510. pktStat.outErrors -= initialStat.outErrors;
  511. pktStat.lost -= initialStat.lost;
  512. }
  513. return (TRUE);
  514. }
  515. /**************************************************************************/
  516. PUBLIC BOOL PktResetStatistics (WORD handle)
  517. {
  518. if (!PktGetStatistics(pktInfo.handle))
  519. return (FALSE);
  520. memcpy (&initialStat, &pktStat, sizeof(initialStat));
  521. resetStat = TRUE;
  522. return (TRUE);
  523. }
  524. /**************************************************************************/
  525. PUBLIC BOOL PktGetAddress (ETHER *addr)
  526. {
  527. reg.r_ax = 0x0600;
  528. reg.r_bx = pktInfo.handle;
  529. reg.r_cx = sizeof (*addr);
  530. #if (DOSX & DJGPP)
  531. reg.x.es = rm_mem.rm_segment;
  532. reg.x.di = pktTemp;
  533. #elif (DOSX & DOS4GW)
  534. reg.r_es = rm_base_seg;
  535. reg.r_di = pktTemp;
  536. #else
  537. reg.r_es = FP_SEG (&pktTemp);
  538. reg.r_di = FP_OFF (&pktTemp); /* ES:DI = address for result */
  539. #endif
  540. if (!PktInterrupt())
  541. return (FALSE);
  542. #if (DOSX & PHARLAP)
  543. ReadRealMem (addr, realBase + (WORD)&pktTemp, sizeof(*addr));
  544. #elif (DOSX & DJGPP)
  545. dosmemget (realBase+pktTemp, sizeof(*addr), addr);
  546. #elif (DOSX & DOS4GW)
  547. memcpy (addr, (void*)(realBase+pktTemp), sizeof(*addr));
  548. #else
  549. memcpy ((void*)addr, &pktTemp, sizeof(*addr));
  550. #endif
  551. return (TRUE);
  552. }
  553. /**************************************************************************/
  554. PUBLIC BOOL PktSetAddress (const ETHER *addr)
  555. {
  556. /* copy addr to real-mode scrath area */
  557. #if (DOSX & PHARLAP)
  558. WriteRealMem (realBase + (WORD)&pktTemp, (void*)addr, sizeof(*addr));
  559. #elif (DOSX & DJGPP)
  560. dosmemput (addr, sizeof(*addr), realBase+pktTemp);
  561. #elif (DOSX & DOS4GW)
  562. memcpy ((void*)(realBase+pktTemp), addr, sizeof(*addr));
  563. #else
  564. memcpy (&pktTemp, (void*)addr, sizeof(*addr));
  565. #endif
  566. reg.r_ax = 0x1900;
  567. reg.r_cx = sizeof (*addr); /* address length */
  568. #if (DOSX & DJGPP)
  569. reg.x.es = rm_mem.rm_segment; /* DOS offset to param */
  570. reg.x.di = pktTemp; /* DOS segment to param */
  571. #elif (DOSX & DOS4GW)
  572. reg.r_es = rm_base_seg;
  573. reg.r_di = pktTemp;
  574. #else
  575. reg.r_es = FP_SEG (&pktTemp);
  576. reg.r_di = FP_OFF (&pktTemp);
  577. #endif
  578. return PktInterrupt();
  579. }
  580. /**************************************************************************/
  581. PUBLIC BOOL PktGetDriverInfo (void)
  582. {
  583. pktInfo.majVer = 0;
  584. pktInfo.minVer = 0;
  585. memset (&pktInfo.name, 0, sizeof(pktInfo.name));
  586. reg.r_ax = 0x01FF;
  587. reg.r_bx = 0;
  588. if (!PktInterrupt())
  589. return (FALSE);
  590. pktInfo.number = reg.r_cx & 0xFF;
  591. pktInfo.class = reg.r_cx >> 8;
  592. #if 0
  593. pktInfo.minVer = reg.r_bx % 10;
  594. pktInfo.majVer = reg.r_bx / 10;
  595. #else
  596. pktInfo.majVer = reg.r_bx; // !!
  597. #endif
  598. pktInfo.funcs = reg.r_ax & 0xFF;
  599. pktInfo.type = reg.r_dx & 0xFF;
  600. #if (DOSX & PHARLAP)
  601. ReadRealMem (&pktInfo.name, DOS_ADDR(reg.ds,reg.esi), sizeof(pktInfo.name));
  602. #elif (DOSX & DJGPP)
  603. dosmemget (DOS_ADDR(reg.x.ds,reg.x.si), sizeof(pktInfo.name), &pktInfo.name);
  604. #elif (DOSX & DOS4GW)
  605. memcpy (&pktInfo.name, (void*)DOS_ADDR(reg.r_ds,reg.r_si), sizeof(pktInfo.name));
  606. #else
  607. _fmemcpy (&pktInfo.name, MK_FP(reg.r_ds,reg.r_si), sizeof(pktInfo.name));
  608. #endif
  609. return (TRUE);
  610. }
  611. /**************************************************************************/
  612. PUBLIC BOOL PktGetDriverParam (void)
  613. {
  614. reg.r_ax = 0x0A00;
  615. if (!PktInterrupt())
  616. return (FALSE);
  617. #if (DOSX & PHARLAP)
  618. ReadRealMem (&pktInfo.majVer, DOS_ADDR(reg.es,reg.edi), PKT_PARAM_SIZE);
  619. #elif (DOSX & DJGPP)
  620. dosmemget (DOS_ADDR(reg.x.es,reg.x.di), PKT_PARAM_SIZE, &pktInfo.majVer);
  621. #elif (DOSX & DOS4GW)
  622. memcpy (&pktInfo.majVer, (void*)DOS_ADDR(reg.r_es,reg.r_di), PKT_PARAM_SIZE);
  623. #else
  624. _fmemcpy (&pktInfo.majVer, MK_FP(reg.r_es,reg.r_di), PKT_PARAM_SIZE);
  625. #endif
  626. return (TRUE);
  627. }
  628. /**************************************************************************/
  629. #if (DOSX & PHARLAP)
  630. PUBLIC int PktReceive (BYTE *buf, int max)
  631. {
  632. WORD inOfs = *rxInOfsFp;
  633. WORD outOfs = *rxOutOfsFp;
  634. if (outOfs != inOfs)
  635. {
  636. RX_ELEMENT _far *head = (RX_ELEMENT _far*)(protBase+outOfs);
  637. int size, len = max;
  638. if (CheckElement(head))
  639. {
  640. size = min (head->firstCount, sizeof(RX_ELEMENT));
  641. len = min (size, max);
  642. _fmemcpy (buf, &head->destin, len);
  643. }
  644. else
  645. size = -1;
  646. outOfs += sizeof (RX_ELEMENT);
  647. if (outOfs > LAST_RX_BUF)
  648. outOfs = FIRST_RX_BUF;
  649. *rxOutOfsFp = outOfs;
  650. return (size);
  651. }
  652. return (0);
  653. }
  654. PUBLIC void PktQueueBusy (BOOL busy)
  655. {
  656. *rxOutOfsFp = busy ? (*rxInOfsFp + sizeof(RX_ELEMENT)) : *rxInOfsFp;
  657. if (*rxOutOfsFp > LAST_RX_BUF)
  658. *rxOutOfsFp = FIRST_RX_BUF;
  659. *(DWORD _far*)(protBase + (WORD)&pktDrop) = 0;
  660. }
  661. PUBLIC WORD PktBuffersUsed (void)
  662. {
  663. WORD inOfs = *rxInOfsFp;
  664. WORD outOfs = *rxOutOfsFp;
  665. if (inOfs >= outOfs)
  666. return (inOfs - outOfs) / sizeof(RX_ELEMENT);
  667. return (NUM_RX_BUF - (outOfs - inOfs) / sizeof(RX_ELEMENT));
  668. }
  669. PUBLIC DWORD PktRxDropped (void)
  670. {
  671. return (*(DWORD _far*)(protBase + (WORD)&pktDrop));
  672. }
  673. #elif (DOSX & DJGPP)
  674. PUBLIC int PktReceive (BYTE *buf, int max)
  675. {
  676. WORD ofs = _farpeekw (_dos_ds, realBase+rxOutOfs);
  677. if (ofs != _farpeekw (_dos_ds, realBase+rxInOfs))
  678. {
  679. RX_ELEMENT head;
  680. int size, len = max;
  681. head.firstCount = _farpeekw (_dos_ds, realBase+ofs);
  682. head.secondCount = _farpeekw (_dos_ds, realBase+ofs+2);
  683. head.handle = _farpeekw (_dos_ds, realBase+ofs+4);
  684. if (CheckElement(&head))
  685. {
  686. size = min (head.firstCount, sizeof(RX_ELEMENT));
  687. len = min (size, max);
  688. dosmemget (realBase+ofs+6, len, buf);
  689. }
  690. else
  691. size = -1;
  692. ofs += sizeof (RX_ELEMENT);
  693. if (ofs > LAST_RX_BUF)
  694. _farpokew (_dos_ds, realBase+rxOutOfs, FIRST_RX_BUF);
  695. else _farpokew (_dos_ds, realBase+rxOutOfs, ofs);
  696. return (size);
  697. }
  698. return (0);
  699. }
  700. PUBLIC void PktQueueBusy (BOOL busy)
  701. {
  702. WORD ofs;
  703. disable();
  704. ofs = _farpeekw (_dos_ds, realBase+rxInOfs);
  705. if (busy)
  706. ofs += sizeof (RX_ELEMENT);
  707. if (ofs > LAST_RX_BUF)
  708. _farpokew (_dos_ds, realBase+rxOutOfs, FIRST_RX_BUF);
  709. else _farpokew (_dos_ds, realBase+rxOutOfs, ofs);
  710. _farpokel (_dos_ds, realBase+pktDrop, 0UL);
  711. enable();
  712. }
  713. PUBLIC WORD PktBuffersUsed (void)
  714. {
  715. WORD inOfs, outOfs;
  716. disable();
  717. inOfs = _farpeekw (_dos_ds, realBase+rxInOfs);
  718. outOfs = _farpeekw (_dos_ds, realBase+rxOutOfs);
  719. enable();
  720. if (inOfs >= outOfs)
  721. return (inOfs - outOfs) / sizeof(RX_ELEMENT);
  722. return (NUM_RX_BUF - (outOfs - inOfs) / sizeof(RX_ELEMENT));
  723. }
  724. PUBLIC DWORD PktRxDropped (void)
  725. {
  726. return _farpeekl (_dos_ds, realBase+pktDrop);
  727. }
  728. #elif (DOSX & DOS4GW)
  729. PUBLIC int PktReceive (BYTE *buf, int max)
  730. {
  731. WORD ofs = *(WORD*) (realBase+rxOutOfs);
  732. if (ofs != *(WORD*) (realBase+rxInOfs))
  733. {
  734. RX_ELEMENT head;
  735. int size, len = max;
  736. head.firstCount = *(WORD*) (realBase+ofs);
  737. head.secondCount = *(WORD*) (realBase+ofs+2);
  738. head.handle = *(WORD*) (realBase+ofs+4);
  739. if (CheckElement(&head))
  740. {
  741. size = min (head.firstCount, sizeof(RX_ELEMENT));
  742. len = min (size, max);
  743. memcpy (buf, (const void*)(realBase+ofs+6), len);
  744. }
  745. else
  746. size = -1;
  747. ofs += sizeof (RX_ELEMENT);
  748. if (ofs > LAST_RX_BUF)
  749. *(WORD*) (realBase+rxOutOfs) = FIRST_RX_BUF;
  750. else *(WORD*) (realBase+rxOutOfs) = ofs;
  751. return (size);
  752. }
  753. return (0);
  754. }
  755. PUBLIC void PktQueueBusy (BOOL busy)
  756. {
  757. WORD ofs;
  758. _disable();
  759. ofs = *(WORD*) (realBase+rxInOfs);
  760. if (busy)
  761. ofs += sizeof (RX_ELEMENT);
  762. if (ofs > LAST_RX_BUF)
  763. *(WORD*) (realBase+rxOutOfs) = FIRST_RX_BUF;
  764. else *(WORD*) (realBase+rxOutOfs) = ofs;
  765. *(DWORD*) (realBase+pktDrop) = 0UL;
  766. _enable();
  767. }
  768. PUBLIC WORD PktBuffersUsed (void)
  769. {
  770. WORD inOfs, outOfs;
  771. _disable();
  772. inOfs = *(WORD*) (realBase+rxInOfs);
  773. outOfs = *(WORD*) (realBase+rxOutOfs);
  774. _enable();
  775. if (inOfs >= outOfs)
  776. return (inOfs - outOfs) / sizeof(RX_ELEMENT);
  777. return (NUM_RX_BUF - (outOfs - inOfs) / sizeof(RX_ELEMENT));
  778. }
  779. PUBLIC DWORD PktRxDropped (void)
  780. {
  781. return *(DWORD*) (realBase+pktDrop);
  782. }
  783. #else /* real-mode small/large model */
  784. PUBLIC int PktReceive (BYTE *buf, int max)
  785. {
  786. if (rxOutOfs != rxInOfs)
  787. {
  788. RX_ELEMENT far *head = (RX_ELEMENT far*) MK_FP (_DS,rxOutOfs);
  789. int size, len = max;
  790. if (CheckElement(head))
  791. {
  792. size = min (head->firstCount, sizeof(RX_ELEMENT));
  793. len = min (size, max);
  794. _fmemcpy (buf, &head->destin, len);
  795. }
  796. else
  797. size = -1;
  798. rxOutOfs += sizeof (RX_ELEMENT);
  799. if (rxOutOfs > LAST_RX_BUF)
  800. rxOutOfs = FIRST_RX_BUF;
  801. return (size);
  802. }
  803. return (0);
  804. }
  805. PUBLIC void PktQueueBusy (BOOL busy)
  806. {
  807. rxOutOfs = busy ? (rxInOfs + sizeof(RX_ELEMENT)) : rxInOfs;
  808. if (rxOutOfs > LAST_RX_BUF)
  809. rxOutOfs = FIRST_RX_BUF;
  810. pktDrop = 0L;
  811. }
  812. PUBLIC WORD PktBuffersUsed (void)
  813. {
  814. WORD inOfs = rxInOfs;
  815. WORD outOfs = rxOutOfs;
  816. if (inOfs >= outOfs)
  817. return ((inOfs - outOfs) / sizeof(RX_ELEMENT));
  818. return (NUM_RX_BUF - (outOfs - inOfs) / sizeof(RX_ELEMENT));
  819. }
  820. PUBLIC DWORD PktRxDropped (void)
  821. {
  822. return (pktDrop);
  823. }
  824. #endif
  825. /**************************************************************************/
  826. LOCAL __inline void PktFreeMem (void)
  827. {
  828. #if (DOSX & PHARLAP)
  829. if (realSeg)
  830. {
  831. _dx_real_free (realSeg);
  832. realSeg = 0;
  833. }
  834. #elif (DOSX & DJGPP)
  835. if (rm_mem.rm_segment)
  836. {
  837. unsigned ofs; /* clear the DOS-mem to prevent further upcalls */
  838. for (ofs = 0; ofs < 16 * rm_mem.size / 4; ofs += 4)
  839. _farpokel (_dos_ds, realBase + ofs, 0);
  840. _go32_dpmi_free_dos_memory (&rm_mem);
  841. rm_mem.rm_segment = 0;
  842. }
  843. #elif (DOSX & DOS4GW)
  844. if (rm_base_sel)
  845. {
  846. dpmi_real_free (rm_base_sel);
  847. rm_base_sel = 0;
  848. }
  849. #endif
  850. }
  851. /**************************************************************************/
  852. PUBLIC BOOL PktExitDriver (void)
  853. {
  854. if (pktInfo.handle)
  855. {
  856. if (!PktSetReceiverMode(PDRX_BROADCAST))
  857. PUTS ("Error restoring receiver mode.");
  858. if (!PktReleaseHandle(pktInfo.handle))
  859. PUTS ("Error releasing PKT-DRVR handle.");
  860. PktFreeMem();
  861. pktInfo.handle = 0;
  862. }
  863. if (pcap_pkt_debug >= 1)
  864. printf ("Internal stats: too-small %lu, too-large %lu, bad-sync %lu, "
  865. "wrong-handle %lu\n",
  866. intStat.tooSmall, intStat.tooLarge,
  867. intStat.badSync, intStat.wrongHandle);
  868. return (TRUE);
  869. }
  870. #if (DOSX & (DJGPP|DOS4GW))
  871. static void dump_pkt_stub (void)
  872. {
  873. int i;
  874. fprintf (stderr, "PktReceiver %lu, pkt_stub[PktReceiver] =\n",
  875. PktReceiver);
  876. for (i = 0; i < 15; i++)
  877. fprintf (stderr, "%02X, ", real_stub_array[i+PktReceiver]);
  878. fputs ("\n", stderr);
  879. }
  880. #endif
  881. /*
  882. * Front end initialization routine
  883. */
  884. PUBLIC BOOL PktInitDriver (PKT_RX_MODE mode)
  885. {
  886. PKT_RX_MODE rxMode;
  887. BOOL writeInfo = (pcap_pkt_debug >= 3);
  888. pktInfo.quiet = (pcap_pkt_debug < 3);
  889. #if (DOSX & PHARLAP) && defined(__HIGHC__)
  890. if (_mwenv != 2)
  891. {
  892. fprintf (stderr, "Only Pharlap DOS extender supported.\n");
  893. return (FALSE);
  894. }
  895. #endif
  896. #if (DOSX & PHARLAP) && defined(__WATCOMC__)
  897. if (_Extender != 1)
  898. {
  899. fprintf (stderr, "Only DOS4GW style extenders supported.\n");
  900. return (FALSE);
  901. }
  902. #endif
  903. if (!PktSearchDriver())
  904. {
  905. PUTS ("Packet driver not found.");
  906. PktFreeMem();
  907. return (FALSE);
  908. }
  909. if (!PktGetDriverInfo())
  910. {
  911. PUTS ("Error getting pkt-drvr information.");
  912. PktFreeMem();
  913. return (FALSE);
  914. }
  915. #if (DOSX & PHARLAP)
  916. if (RealCopy((ULONG)&rxOutOfs, (ULONG)&pktRxEnd,
  917. &realBase, &protBase, (USHORT*)&realSeg))
  918. {
  919. rxOutOfsFp = (WORD _far *) (protBase + (WORD) &rxOutOfs);
  920. rxInOfsFp = (WORD _far *) (protBase + (WORD) &rxInOfs);
  921. *rxOutOfsFp = FIRST_RX_BUF;
  922. *rxInOfsFp = FIRST_RX_BUF;
  923. }
  924. else
  925. {
  926. PUTS ("Cannot allocate real-mode stub.");
  927. return (FALSE);
  928. }
  929. #elif (DOSX & (DJGPP|DOS4GW))
  930. if (sizeof(real_stub_array) > 0xFFFF)
  931. {
  932. fprintf (stderr, "`real_stub_array[]' too big.\n");
  933. return (FALSE);
  934. }
  935. #if (DOSX & DJGPP)
  936. rm_mem.size = (sizeof(real_stub_array) + 15) / 16;
  937. if (_go32_dpmi_allocate_dos_memory(&rm_mem) || rm_mem.rm_offset != 0)
  938. {
  939. PUTS ("real-mode init failed.");
  940. return (FALSE);
  941. }
  942. realBase = (rm_mem.rm_segment << 4);
  943. dosmemput (&real_stub_array, sizeof(real_stub_array), realBase);
  944. _farpokel (_dos_ds, realBase+rxOutOfs, FIRST_RX_BUF);
  945. _farpokel (_dos_ds, realBase+rxInOfs, FIRST_RX_BUF);
  946. #elif (DOSX & DOS4GW)
  947. rm_base_seg = dpmi_real_malloc (sizeof(real_stub_array), &rm_base_sel);
  948. if (!rm_base_seg)
  949. {
  950. PUTS ("real-mode init failed.");
  951. return (FALSE);
  952. }
  953. realBase = (rm_base_seg << 4);
  954. memcpy ((void*)realBase, &real_stub_array, sizeof(real_stub_array));
  955. *(WORD*) (realBase+rxOutOfs) = FIRST_RX_BUF;
  956. *(WORD*) (realBase+rxInOfs) = FIRST_RX_BUF;
  957. #endif
  958. {
  959. int pushf = PktReceiver;
  960. while (real_stub_array[pushf++] != 0x9C && /* pushf */
  961. real_stub_array[pushf] != 0xFA) /* cli */
  962. {
  963. if (++para_skip > 16)
  964. {
  965. fprintf (stderr, "Something wrong with `pkt_stub.inc'.\n");
  966. para_skip = 0;
  967. dump_pkt_stub();
  968. return (FALSE);
  969. }
  970. }
  971. if (*(WORD*)(real_stub_array + offsetof(PktRealStub,_dummy)) != 0xB800)
  972. {
  973. fprintf (stderr, "`real_stub_array[]' is misaligned.\n");
  974. return (FALSE);
  975. }
  976. }
  977. if (pcap_pkt_debug > 2)
  978. dump_pkt_stub();
  979. #else
  980. rxOutOfs = FIRST_RX_BUF;
  981. rxInOfs = FIRST_RX_BUF;
  982. #endif
  983. if (!PktSetAccess())
  984. {
  985. PUTS ("Error setting pkt-drvr access.");
  986. PktFreeMem();
  987. return (FALSE);
  988. }
  989. if (!PktGetAddress(&myAddress))
  990. {
  991. PUTS ("Error fetching adapter address.");
  992. PktFreeMem();
  993. return (FALSE);
  994. }
  995. if (!PktSetReceiverMode(mode))
  996. {
  997. PUTS ("Error setting receiver mode.");
  998. PktFreeMem();
  999. return (FALSE);
  1000. }
  1001. if (!PktGetReceiverMode(&rxMode))
  1002. {
  1003. PUTS ("Error getting receiver mode.");
  1004. PktFreeMem();
  1005. return (FALSE);
  1006. }
  1007. if (writeInfo)
  1008. printf ("Pkt-driver information:\n"
  1009. " Version : %d.%d\n"
  1010. " Name : %.15s\n"
  1011. " Class : %u (%s)\n"
  1012. " Type : %u\n"
  1013. " Number : %u\n"
  1014. " Funcs : %u\n"
  1015. " Intr : %Xh\n"
  1016. " Handle : %u\n"
  1017. " Extended : %s\n"
  1018. " Hi-perf : %s\n"
  1019. " RX mode : %s\n"
  1020. " Eth-addr : %02X:%02X:%02X:%02X:%02X:%02X\n",
  1021. pktInfo.majVer, pktInfo.minVer, pktInfo.name,
  1022. pktInfo.class, PktGetClassName(pktInfo.class),
  1023. pktInfo.type, pktInfo.number,
  1024. pktInfo.funcs, pktInfo.intr, pktInfo.handle,
  1025. pktInfo.funcs == 2 || pktInfo.funcs == 6 ? "Yes" : "No",
  1026. pktInfo.funcs == 5 || pktInfo.funcs == 6 ? "Yes" : "No",
  1027. PktRXmodeStr(rxMode),
  1028. myAddress[0], myAddress[1], myAddress[2],
  1029. myAddress[3], myAddress[4], myAddress[5]);
  1030. #if defined(DEBUG) && (DOSX & PHARLAP)
  1031. if (writeInfo)
  1032. {
  1033. DWORD rAdr = realBase + (WORD)&PktReceiver;
  1034. unsigned sel, ofs;
  1035. printf ("\nReceiver at %04X:%04X\n", RP_SEG(rAdr), RP_OFF(rAdr));
  1036. printf ("Realbase = %04X:%04X\n", RP_SEG(realBase),RP_OFF(realBase));
  1037. sel = _FP_SEG (protBase);
  1038. ofs = _FP_OFF (protBase);
  1039. printf ("Protbase = %04X:%08X\n", sel,ofs);
  1040. printf ("RealSeg = %04X\n", realSeg);
  1041. sel = _FP_SEG (rxOutOfsFp);
  1042. ofs = _FP_OFF (rxOutOfsFp);
  1043. printf ("rxOutOfsFp = %04X:%08X\n", sel,ofs);
  1044. sel = _FP_SEG (rxInOfsFp);
  1045. ofs = _FP_OFF (rxInOfsFp);
  1046. printf ("rxInOfsFp = %04X:%08X\n", sel,ofs);
  1047. printf ("Ready: *rxOutOfsFp = %04X *rxInOfsFp = %04X\n",
  1048. *rxOutOfsFp, *rxInOfsFp);
  1049. PktQueueBusy (TRUE);
  1050. printf ("Busy: *rxOutOfsFp = %04X *rxInOfsFp = %04X\n",
  1051. *rxOutOfsFp, *rxInOfsFp);
  1052. }
  1053. #endif
  1054. memset (&pktStat, 0, sizeof(pktStat)); /* clear statistics */
  1055. PktQueueBusy (TRUE);
  1056. return (TRUE);
  1057. }
  1058. /*
  1059. * DPMI functions only for Watcom + DOS4GW extenders
  1060. */
  1061. #if (DOSX & DOS4GW)
  1062. LOCAL DWORD dpmi_get_real_vector (int intr)
  1063. {
  1064. union REGS r;
  1065. r.x.eax = 0x200;
  1066. r.x.ebx = (DWORD) intr;
  1067. int386 (0x31, &r, &r);
  1068. return ((r.w.cx << 4) + r.w.dx);
  1069. }
  1070. LOCAL WORD dpmi_real_malloc (int size, WORD *selector)
  1071. {
  1072. union REGS r;
  1073. r.x.eax = 0x0100; /* DPMI allocate DOS memory */
  1074. r.x.ebx = (size + 15) / 16; /* Number of paragraphs requested */
  1075. int386 (0x31, &r, &r);
  1076. if (r.w.cflag & 1)
  1077. return (0);
  1078. *selector = r.w.dx;
  1079. return (r.w.ax); /* Return segment address */
  1080. }
  1081. LOCAL void dpmi_real_free (WORD selector)
  1082. {
  1083. union REGS r;
  1084. r.x.eax = 0x101; /* DPMI free DOS memory */
  1085. r.x.ebx = selector; /* Selector to free */
  1086. int386 (0x31, &r, &r);
  1087. }
  1088. #endif
  1089. #if defined(DOSX) && (DOSX & PHARLAP)
  1090. /*
  1091. * Description:
  1092. * This routine allocates conventional memory for the specified block
  1093. * of code (which must be within the first 64K of the protected mode
  1094. * program segment) and copies the code to it.
  1095. *
  1096. * The caller should free up the conventional memory block when it
  1097. * is done with the conventional memory.
  1098. *
  1099. * NOTE THIS ROUTINE REQUIRES 386|DOS-EXTENDER 3.0 OR LATER.
  1100. *
  1101. * Calling arguments:
  1102. * start_offs start of real mode code in program segment
  1103. * end_offs 1 byte past end of real mode code in program segment
  1104. * real_basep returned; real mode ptr to use as a base for the
  1105. * real mode code (eg, to get the real mode FAR
  1106. * addr of a function foo(), take
  1107. * real_basep + (ULONG) foo).
  1108. * This pointer is constructed such that
  1109. * offsets within the real mode segment are
  1110. * the same as the link-time offsets in the
  1111. * protected mode program segment
  1112. * prot_basep returned; prot mode ptr to use as a base for getting
  1113. * to the conventional memory, also constructed
  1114. * so that adding the prot mode offset of a
  1115. * function or variable to the base gets you a
  1116. * ptr to the function or variable in the
  1117. * conventional memory block.
  1118. * rmem_adrp returned; real mode para addr of allocated
  1119. * conventional memory block, to be used to free
  1120. * up the conventional memory when done. DO NOT
  1121. * USE THIS TO CONSTRUCT A REAL MODE PTR, USE
  1122. * REAL_BASEP INSTEAD SO THAT OFFSETS WORK OUT
  1123. * CORRECTLY.
  1124. *
  1125. * Returned values:
  1126. * 0 if error
  1127. * 1 if success
  1128. */
  1129. int RealCopy (ULONG start_offs,
  1130. ULONG end_offs,
  1131. REALPTR *real_basep,
  1132. FARPTR *prot_basep,
  1133. USHORT *rmem_adrp)
  1134. {
  1135. ULONG rm_base; /* base real mode para addr for accessing */
  1136. /* allocated conventional memory */
  1137. UCHAR *source; /* source pointer for copy */
  1138. FARPTR destin; /* destination pointer for copy */
  1139. ULONG len; /* number of bytes to copy */
  1140. ULONG temp;
  1141. USHORT stemp;
  1142. /* First check for valid inputs
  1143. */
  1144. if (start_offs >= end_offs || end_offs > 0x10000)
  1145. return (FALSE);
  1146. /* Round start_offs down to a paragraph (16-byte) boundary so we can set up
  1147. * the real mode pointer easily. Round up end_offs to make sure we allocate
  1148. * enough paragraphs
  1149. */
  1150. start_offs &= ~15;
  1151. end_offs = (15 + (end_offs << 4)) >> 4;
  1152. /* Allocate the conventional memory for our real mode code. Remember to
  1153. * round byte count UP to 16-byte paragraph size. We alloc it
  1154. * above the DOS data buffer so both the DOS data buffer and the appl
  1155. * conventional mem block can still be resized.
  1156. *
  1157. * First just try to alloc it; if we can't get it, shrink the appl mem
  1158. * block down to the minimum, try to alloc the memory again, then grow the
  1159. * appl mem block back to the maximum. (Don't try to shrink the DOS data
  1160. * buffer to free conventional memory; it wouldn't be good for this routine
  1161. * to have the possible side effect of making file I/O run slower.)
  1162. */
  1163. len = ((end_offs - start_offs) + 15) >> 4;
  1164. if (_dx_real_above(len, rmem_adrp, &stemp) != _DOSE_NONE)
  1165. {
  1166. if (_dx_cmem_usage(0, 0, &temp, &temp) != _DOSE_NONE)
  1167. return (FALSE);
  1168. if (_dx_real_above(len, rmem_adrp, &stemp) != _DOSE_NONE)
  1169. *rmem_adrp = 0;
  1170. if (_dx_cmem_usage(0, 1, &temp, &temp) != _DOSE_NONE)
  1171. {
  1172. if (*rmem_adrp != 0)
  1173. _dx_real_free (*rmem_adrp);
  1174. return (FALSE);
  1175. }
  1176. if (*rmem_adrp == 0)
  1177. return (FALSE);
  1178. }
  1179. /* Construct real mode & protected mode pointers to access the allocated
  1180. * memory. Note we know start_offs is aligned on a paragraph (16-byte)
  1181. * boundary, because we rounded it down.
  1182. *
  1183. * We make the offsets come out rights by backing off the real mode selector
  1184. * by start_offs.
  1185. */
  1186. rm_base = ((ULONG) *rmem_adrp) - (start_offs >> 4);
  1187. RP_SET (*real_basep, 0, rm_base);
  1188. FP_SET (*prot_basep, rm_base << 4, SS_DOSMEM);
  1189. /* Copy the real mode code/data to the allocated memory
  1190. */
  1191. source = (UCHAR *) start_offs;
  1192. destin = *prot_basep;
  1193. FP_SET (destin, FP_OFF(*prot_basep) + start_offs, FP_SEL(*prot_basep));
  1194. len = end_offs - start_offs;
  1195. WriteFarMem (destin, source, len);
  1196. return (TRUE);
  1197. }
  1198. #endif /* DOSX && (DOSX & PHARLAP) */