marignotti/mwrite.c

99 lines
2.1 KiB
C
Raw Normal View History

2012-05-05 00:11:02 +00:00
#include "marignotti.h"
#include <gno/kerntool.h>
#include <sys/signal.h>
2012-05-12 23:44:10 +00:00
#include <sys/socket.h>
2012-05-05 00:11:02 +00:00
#include <errno.h>
2012-05-12 01:21:29 +00:00
#include "s16debug.h"
2012-05-05 00:11:02 +00:00
#pragma noroot
#pragma optimize 79
2012-05-12 23:44:10 +00:00
// WriteGS, send, or sendto
2012-05-05 05:08:06 +00:00
int mwrite(Entry *e, void *p1, void *p2, void *p3, void *p4, void *p5)
2012-05-05 00:11:02 +00:00
{
Word terr;
Word t;
int xerrno;
char *buffer = (char *)p1;
LongWord nbytes = *(LongWord *)p2;
2012-05-12 23:44:10 +00:00
xsockaddr *addr = (xsockaddr *)p3;
int addrlen = p4 ? *(int *)p4 : 0;
2012-05-05 00:11:02 +00:00
2012-05-12 23:44:10 +00:00
LongWord *outbytes = (LongWord *)p2;
*outbytes = 0;
2012-05-05 00:11:02 +00:00
2012-05-13 23:45:51 +00:00
2012-05-12 01:21:29 +00:00
if (Debug > 0)
{
s16_debug_printf("write nbytes = %ld", nbytes);
2012-05-12 23:44:10 +00:00
if (addr)
{
s16_debug_puts(" to address...");
s16_debug_dump(addr, addrlen);
}
}
if (Debug > 1)
{
s16_debug_puts("");
s16_debug_dump(buffer, (Word)nbytes);
}
2012-05-13 23:45:51 +00:00
if (e->_SHUT_WR) return EPIPE;
2012-05-12 23:44:10 +00:00
if (e->_TYPE == SOCK_DGRAM)
{
// TCPIPSendUDP builds a header
// using the source port, my ip address,
// destination port and destination ip address.
// mconnect should probably just set the destination
// address/port and exit.
// if an address was specified, we would need to
// build the udp header here.
IncBusy();
TCPIPSendUDP(e->ipid, buffer, nbytes);
t = _toolErr;
DecBusy();
if (t) return ENETDOWN;
*outbytes = nbytes;
return 0;
2012-05-12 01:21:29 +00:00
}
2012-05-05 00:11:02 +00:00
// todo -- queue up if pending >= _SNDLOWAT?
// todo -- push?
IncBusy();
2012-05-05 05:08:06 +00:00
terr = TCPIPWriteTCP(e->ipid, buffer, nbytes, 0, 0);
2012-05-05 00:11:02 +00:00
t = _toolErr;
if (t) terr = t;
2012-05-12 23:44:10 +00:00
e->terr = terr;
DecBusy();
2012-05-05 00:11:02 +00:00
if (t || terr == tcperrBadConnection)
return ENOTCONN;
if (terr == tcperrNoResources)
return ENOMEM;
if (terr == tcperrConClosing)
{
if (!e->_NOSIGPIPE)
2012-05-12 23:44:10 +00:00
{
int xerrno;
2012-05-05 00:11:02 +00:00
Kkill(Kgetpid(), SIGPIPE, &xerrno);
2012-05-12 23:44:10 +00:00
}
2012-05-05 00:11:02 +00:00
return EPIPE;
}
2012-05-12 23:44:10 +00:00
*outbytes = nbytes;
2012-05-05 00:11:02 +00:00
return 0;
2016-01-06 00:59:13 +00:00
}