📄 winnet.c
字号:
{
sfree(addr);
}
static Plug sk_tcp_plug(Socket sock, Plug p)
{
Actual_Socket s = (Actual_Socket) sock;
Plug ret = s->plug;
if (p)
s->plug = p;
return ret;
}
static void sk_tcp_flush(Socket s)
{
/*
* We send data to the socket as soon as we can anyway,
* so we don't need to do anything here. :-)
*/
}
static void sk_tcp_close(Socket s);
static int sk_tcp_write(Socket s, const char *data, int len);
static int sk_tcp_write_oob(Socket s, const char *data, int len);
static void sk_tcp_set_private_ptr(Socket s, void *ptr);
static void *sk_tcp_get_private_ptr(Socket s);
static void sk_tcp_set_frozen(Socket s, int is_frozen);
static char *sk_tcp_socket_error(Socket s);
extern char *do_select(SOCKET skt, int startup);
Socket sk_register(void *sock, Plug plug)
{
/*static const struct socket_function_table fn_table = {
sk_tcp_plug,
sk_tcp_close,
sk_tcp_write,
sk_tcp_write_oob,
sk_tcp_flush,
sk_tcp_set_private_ptr,
sk_tcp_get_private_ptr,
sk_tcp_set_frozen,
sk_tcp_socket_error
};
DWORD err;
char *errstr;
Actual_Socket ret;
/*
* Create Socket structure.
*/
/* ret = smalloc(sizeof(struct Socket_tag));
ret->fn = &fn_table;
ret->error = NULL;
ret->plug = plug;
bufchain_init(&ret->output_data);
ret->writable = 1; /* to start with */
/* ret->sending_oob = 0;
ret->frozen = 1;
ret->frozen_readable = 0;
ret->localhost_only = 0; /* unused, but best init anyway */
/* ret->pending_error = 0;
ret->s = (SOCKET)sock;
if (ret->s == INVALID_SOCKET) {
err = WSAGetLastError();
ret->error = winsock_error_string(err);
return (Socket) ret;
}
ret->oobinline = 0;
/* Set up a select mechanism. This could be an AsyncSelect on a
* window, or an EventSelect on an event object. */
/* errstr = do_select(ret->s, 1);
if (errstr) {
ret->error = errstr;
return (Socket) ret;
}
add234(sktree, ret);
return (Socket) ret;*/
return 0;
}
Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
int nodelay, Plug plug)
{
static const struct socket_function_table fn_table = {
sk_tcp_plug,
sk_tcp_close,
sk_tcp_write,
sk_tcp_write_oob,
sk_tcp_flush,
sk_tcp_set_private_ptr,
sk_tcp_get_private_ptr,
sk_tcp_set_frozen,
sk_tcp_socket_error
};
SOCKET s;
#ifdef IPV6
SOCKADDR_IN6 a6;
#endif
char *errstr;
Actual_Socket ret;
/*
* Create Socket structure.
*/
ret = smalloc(sizeof(struct Socket_tag));
ret->fn = &fn_table;
ret->error = NULL;
ret->plug = plug;
bufchain_init(&ret->output_data);
ret->connected = 0; /* to start with */
ret->writable = 0; /* to start with */
ret->sending_oob = 0;
ret->frozen = 0;
ret->frozen_readable = 0;
ret->localhost_only = 0; /* unused, but best init anyway */
ret->pending_error = 0;
ret->connected = 1;
ret->oobinline = oobinline;
s=77;
ret->s = s;
/*
* Open socket.
*/
/*
s = socket(addr->family, SOCK_STREAM, 0);
ret->s = s;
if (s == INVALID_SOCKET) {
err = WSAGetLastError();
ret->error = winsock_error_string(err);
return (Socket) ret;
}
ret->oobinline = oobinline;
if (oobinline) {
BOOL b = TRUE;
setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
}
if (nodelay) {
BOOL b = TRUE;
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
}
*/
/*
* Bind to local address.
*/
// if (privport)
// localport = 1023; /* count from 1023 downwards */
// else
// localport = 0; /* just use port 0 (ie winsock picks) */
/* Loop round trying to bind */
/*while (1) {
int retcode;
#ifdef IPV6
if (addr->family == AF_INET6) {
memset(&a6, 0, sizeof(a6));
a6.sin6_family = AF_INET6;*/
/*a6.sin6_addr = in6addr_any; *//* == 0 */
/* a6.sin6_port = htons(localport);
} else
#endif
{
a.sin_family = AF_INET;
a.sin_addr.s_addr = htonl(INADDR_ANY);
a.sin_port = htons(localport);
}
#ifdef IPV6
retcode = bind(s, (addr->family == AF_INET6 ?
(struct sockaddr *) &a6 :
(struct sockaddr *) &a),
(addr->family ==
AF_INET6 ? sizeof(a6) : sizeof(a)));
#else
retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
#endif
if (retcode != SOCKET_ERROR) {
err = 0;
break; /* done */
/* } else {
err = WSAGetLastError();
if (err != WSAEADDRINUSE) /* failed, for a bad reason */
/* break;
}
if (localport == 0)
break; /* we're only looping once */
/* localport--;
if (localport == 0)
break; /* we might have got to the end */
/* }
if (err) {
ret->error = winsock_error_string(err);
return (Socket) ret;
}
/*
* Connect to remote address.
*/
/*#ifdef IPV6
if (addr->family == AF_INET6) {
memset(&a, 0, sizeof(a));
a6.sin6_family = AF_INET6;
a6.sin6_port = htons((short) port);
a6.sin6_addr =
((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr;
} else
#endif
{
a.sin_family = AF_INET;
a.sin_addr.s_addr = htonl(addr->address);
a.sin_port = htons((short) port);
}
*/
/* Set up a select mechanism. This could be an AsyncSelect on a
* window, or an EventSelect on an event object. */
errstr = do_select(s, 1);
if (errstr) {
ret->error = errstr;
return (Socket) ret;
}
/*
if ((
#ifdef IPV6
connect(s, ((addr->family == AF_INET6) ?
(struct sockaddr *) &a6 : (struct sockaddr *) &a),
(addr->family == AF_INET6) ? sizeof(a6) : sizeof(a))
#else
connect(s, (struct sockaddr *) &a, sizeof(a))
#endif
) == SOCKET_ERROR) {
err = WSAGetLastError();
/*
* We expect a potential EWOULDBLOCK here, because the
* chances are the front end has done a select for
* FD_CONNECT, so that connect() will complete
* asynchronously.
*/
/* if ( err != WSAEWOULDBLOCK ) {
ret->error = winsock_error_string(err);
return (Socket) ret;
}
} else {
/*
* If we _don't_ get EWOULDBLOCK, the connect has completed
* and we should set the socket as writable.
*/
ret->writable = 1;
// }
add234(sktree, ret);
return (Socket) ret;
}
static void sk_tcp_close(Socket sock)
{
extern char *do_select(SOCKET skt, int startup);
Actual_Socket s = (Actual_Socket) sock;
del234(sktree, s);
do_select(s->s, 0);
//closesocket(s->s);
sfree(s);
}
/*
* The function which tries to send on a socket once it's deemed
* writable.
*/
void try_send(Actual_Socket s)
{
while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
int nsent;
DWORD err;
void *data;
int len, urgentflag;
if (s->sending_oob) {
urgentflag = MSG_OOB;
len = s->sending_oob;
data = &s->oobdata;
} else {
urgentflag = 0;
bufchain_prefix(&s->output_data, &data, &len);
}
if (len>20480)
MessageBox(0, "Error! Please get in contact with Tim.Kosse@gmx.de if this happens frequently.\r\nError: WINNET.C->try_send->len>20480", "Critical error!!!", MB_ICONSTOP);
while (TRUE)
{
FzSFtpIpc_SendRequest(SFTP_DATAID_CTS_WRITE, len, data);
while (TRUE)
{
DWORD nID, nDataLength;
char pData[20480];
if (!FzSFtpIpc_ReceiveRequest(&nID, &nDataLength, pData))
cleanup_exit(1);
if (nID==SFTP_DATAID_STC_WRITE)
{
if (nDataLength!=8)
{
FzSFtpIpc_Trace("SFTP_DATAID_STC_WRITE->nDataLength!=4");
cleanup_exit(1);
}
nsent=*(int *)pData;
if (!nsent)
err=*((int *)pData+1);
else
err=0;
if (err)
nsent=-1;
break;
}
}
if (nsent==SOCKET_ERROR && err!=WSAEWOULDBLOCK)
break;
else if (nsent>=0)
break;
while (TRUE)
{
DWORD nID, nDataLength;
char pData[20480];
if (!FzSFtpIpc_ReceiveRequest(&nID, &nDataLength,pData))
cleanup_exit(1);
if (nID==SFTP_DATAID_STC_FDWRITE)
{
if (nDataLength)
{
FzSFtpIpc_Trace("SFTP_DATAID_STC_FDWRITE->nDataLength!=0");
cleanup_exit(1);
}
break;
}
}
}
// nsent = send(s->s, data, len, urgentflag);
noise_ultralight(nsent);
if (nsent <= 0) {
if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) {
/*
* Perfectly normal: we've sent all we can for the moment.
*
* (Some WinSock send() implementations can return
* <0 but leave no sensible error indication -
* WSAGetLastError() is called but returns zero or
* a small number - so we check that case and treat
* it just like WSAEWOULDBLOCK.)
*/
s->writable = FALSE;
return;
} else if (nsent == 0 ||
err == WSAECONNABORTED || err == WSAECONNRESET) {
/*
* If send() returns CONNABORTED or CONNRESET, we
* unfortunately can't just call plug_closing(),
* because it's quite likely that we're currently
* _in_ a call from the code we'd be calling back
* to, so we'd have to make half the SSH code
* reentrant. Instead we flag a pending error on
* the socket, to be dealt with (by calling
* plug_closing()) at some suitable future moment.
*/
s->pending_error = err;
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -