tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / pkts.c
blob0905a6338f6ec61c9d9d05f46e6772cad6af9bf2
1 /*
2 * $Id$
4 * Simplified doublebuffered AmigaDOS IO.
5 *
6 * Author: Tomi Ollila <Tomi.Ollila@nsdi.fi>
8 * Copyright (c) 1994 Network Solutions Development Inc. OY
9 * All rights reserved
11 * Created: Thu Sep 15 06:11:55 1994 too
12 * Last modified: Thu Sep 15 08:33:18 1994 too
14 * HISTORY
15 * $Log: pkts.c,v $
16 * Revision 1.1.1.2 2005/12/07 10:50:34 sonic_amiga
17 * First full import into the CVS
19 * Revision 1.1.1.1 2005/11/08 13:51:05 sonic_amiga
20 * Initial partial import upon request
22 * Revision 1.1 1996/01/21 01:26:36 jraja
23 * Initial revision
27 #include <exec/types.h>
28 #include <exec/ports.h>
29 #include <dos/dos.h>
30 #include <dos/dosextens.h>
32 #include <string.h> /* memset() */
33 #include <errno.h>
35 #if __GNUC__
36 #include <inline/exec.h>
37 #include <inline/dos.h>
38 #error ios1 is SAS/C specific
39 #elif __SASC
40 #include <proto/exec.h>
41 #include <proto/dos.h>
42 #include <ios1.h> /* SAS/C, chkufb() */
43 #else
44 #error :foo
45 #endif
47 #include "pkts.h"
49 extern int __io2errno(long);
51 int
52 initPktInfo(struct pktinfo * pkti, int fd, char * buf1, char * buf2, int size)
54 struct DosPacket * pkt;
55 struct MsgPort * port;
56 struct UFB * ufb;
57 struct FileHandle * fh;
59 memset(pkti, 0, sizeof(*pkti));
62 * get AmigaDOS file handle from SAS/C file handle
64 ufb = chkufb(fd);
65 if (ufb == NULL || ufb->ufbfh == NULL) {
66 errno = EBADF;
67 return -1;
69 /* convert DOS file handle to struct FileHandle * */
70 fh = BADDR((BPTR)ufb->ufbfh);
71 pkti->fhType = fh->fh_Type;
73 port = CreateMsgPort();
74 if (port != NULL) {
75 pkti->port = port;
77 pkt = (struct DosPacket *)AllocDosObject(DOS_STDPKT, 0);
78 if (pkt != NULL) {
79 pkti->pkt = pkt;
81 pkti->bufs[0] = buf1;
82 pkti->bufs[1] = buf2;
83 pkti->whichbuf = 0;
85 pkt->dp_Arg1 = fh->fh_Arg1;
88 * Do the initial action. Do nothing if file is NIL:
90 if (pkti->fhType == NULL) {
91 pkt->dp_Res1 = 0; /* keep everyone happy */
92 pkt->dp_Arg3 = 0;
93 AddTail(&port->mp_MsgList, (struct Node *)pkt->dp_Link);
95 else {
96 if (size == 0) {
97 pkt->dp_Type = ACTION_WRITE;
98 AddTail(&port->mp_MsgList, (struct Node *)pkt->dp_Link);
100 else {
101 pkt->dp_Arg2 = (LONG)buf1;
102 pkt->dp_Arg3 = size;
103 pkt->dp_Type = ACTION_READ;
104 SendPkt(pkt, pkti->fhType, port);
107 return 0;
110 DeleteMsgPort(port);
113 errno = ENOMEM;
114 return -1;
118 deInitPktInfo(struct pktinfo * pkti)
120 int rv;
122 * Take the packet out of the port, save the return code
124 WaitPort(pkti->port);
125 rv = pkti->pkt->dp_Res1;
127 * Set errno if error
129 if (rv < 0 ||
130 (pkti->pkt->dp_Type == ACTION_WRITE && rv < pkti->pkt->dp_Arg3)) {
131 errno = __io2errno(_OSERR = pkti->pkt->dp_Res2);
132 rv = -1;
135 pkti->fhType = NULL;
137 FreeDosObject(DOS_STDPKT, pkti->pkt);
138 DeleteMsgPort(pkti->port);
140 memset(pkti, 0, sizeof(*pkti));
142 return rv;
145 #define msgToPkt(msg) ((struct DosPacket *)msg->mn_Node.ln_Name)
148 readPkt(struct pktinfo * pkti, char ** buf)
150 struct DosPacket * pkt;
151 int rv;
154 * If file is NIL:, return 0 to end the reading
156 if (pkti->fhType == NULL)
157 return 0;
159 WaitPort(pkti->port);
160 pkt = msgToPkt(GetMsg(pkti->port));
162 rv = pkt->dp_Res1;
164 if (rv > 0) {
165 *buf = pkti->bufs[pkti->whichbuf];
166 pkti->whichbuf ^= 1;
168 pkt->dp_Arg2 = (LONG)pkti->bufs[pkti->whichbuf];
169 SendPkt(pkt, pkti->fhType, pkti->port);
171 else {
172 if (rv < 0)
173 errno = __io2errno(_OSERR = pkt->dp_Res2);
174 pkt->dp_Res1 = 0; /* do not receive same error again */
175 AddTail(&pkti->port->mp_MsgList, (struct Node *)pkt->dp_Link);
178 return rv;
182 writePkt(struct pktinfo * pkti, char ** buf, int len)
184 struct DosPacket * pkt;
186 WaitPort(pkti->port);
187 pkt = msgToPkt(GetMsg(pkti->port));
189 if (pkt->dp_Res1 < pkt->dp_Arg3) {
190 errno = __io2errno(_OSERR = pkt->dp_Res2);
191 pkt->dp_Res1 = pkt->dp_Arg3 = 0; /* do not receive same error again */
192 AddTail(&pkti->port->mp_MsgList, (struct Node *)pkt->dp_Link);
193 return -1;
196 /* Assert (*buf == pkti->whichbuf); */
198 pkt->dp_Arg2 = (LONG)*buf;
199 pkt->dp_Arg3 = len;
201 if (pkti->fhType != NULL)
202 SendPkt(pkt, pkti->fhType, pkti->port);
203 else {
204 pkt->dp_Res1 = len; /* writing to NIL: always succeeds */
205 AddTail(&pkti->port->mp_MsgList, (struct Node *)pkt->dp_Link);
208 pkti->whichbuf ^= 1;
209 *buf = pkti->bufs[pkti->whichbuf];
211 return len;