📄 socket.c
字号:
pTmp = FragGetBufParams( hFrag, 0, &validsize, &offset );
pTmp += offset;
*ppbuf = pTmp;
if( !validsize )
{
FragFree( hFrag );
hFrag = 0;
}
}
// Return the frag
*phFrag = hFrag;
llExit();
return((int)validsize);
recvnc_error:
pfd->pfdt->error = error;
llExit();
return( SOCKET_ERROR );
}
//--------------------------------------------------------------------
// recvncfrom() (USER FUNCTION)
//
// Receive data from a socket without copy with "from" name info
//--------------------------------------------------------------------
int recvncfrom( int s, void **ppbuf, int flags,
PSA pName, int *plen, HANDLE *phFrag )
{
FILEDESC *pfd;
int error = 0;
uint offset,validsize;
UINT8 *pTmp;
HANDLE hFrag = 0;
llEnter();
// Verify Index
if( !(pfd = fdint_verify(s, FDTYPE_ANY)) )
{
llExit();
return( SOCKET_ERROR );
}
// Validate Sizes and Pointers
if( !ppbuf || !phFrag || !pName || !plen || *plen<sizeof( SA ) )
{
error = EINVAL;
goto recvnc_error;
}
// Receive data - works only with socket type
if( pfd->fdType == FDTYPE_SOCKET )
error = SockRecvNC(pfd->hSock, flags, pName, &hFrag);
else
error = EINVAL;
if( error )
goto recvnc_error;
// Get size from return frag
if( !hFrag )
validsize = 0;
else
{
pTmp = FragGetBufParams( hFrag, 0, &validsize, &offset );
pTmp += offset;
*ppbuf = pTmp;
if( !validsize )
{
FragFree( hFrag );
hFrag = 0;
}
}
// Return the frag
*phFrag = hFrag;
// Return Addr size
*plen = sizeof( SA );
llExit();
return((int)validsize);
recvnc_error:
pfd->pfdt->error = error;
llExit();
return( SOCKET_ERROR );
}
//--------------------------------------------------------------------
// recvncfree() (USER FUNCTION)
//
// Free a receive data buffer from recvnc() or recvncfrom()
//--------------------------------------------------------------------
void recvncfree( HANDLE hFrag )
{
llEnter();
if( hFrag )
FragFree( hFrag );
llExit();
}
//--------------------------------------------------------------------
// send() (USER FUNCTION)
//
// Send data to a socket
//--------------------------------------------------------------------
int send( int s, void *pbuf, int size, int flags )
{
FILEDESC *pfd;
int error = 0;
INT32 txsize;
llEnter();
// Verify Index
if( !(pfd = fdint_verify(s, FDTYPE_ANY)) )
{
llExit();
return( SOCKET_ERROR );
}
// Send data - use Socket or Pipe send based on type
if( pfd->fdType == FDTYPE_SOCKET )
error = SockSend( pfd->hSock, pbuf, (INT32)size, flags, &txsize );
else
error = PipeSend( pfd->hSock, pbuf, (INT32)size, flags, &txsize );
if( error )
{
pfd->pfdt->error = error;
llExit();
return( SOCKET_ERROR );
}
llExit();
return((int)txsize);
}
//--------------------------------------------------------------------
// sendto() (USER FUNCTION)
//
// Send data to a specific socket destination
//--------------------------------------------------------------------
int sendto( int s, void *pbuf, int size, int flags, PSA pName, int len )
{
FILEDESC *pfd;
int error = 0;
INT32 txsize;
llEnter();
// Verify Index
if( !(pfd = fdint_verify(s, FDTYPE_SOCKET)) )
{
llExit();
return( SOCKET_ERROR );
}
// Verify Address Size
if( len != sizeof( SA ) )
{
error = EINVAL;
goto sendto_error;
}
// Connect
if( (error = SockConnect( pfd->hSock, pName )) )
goto sendto_error;
// Send
error = SockSend( pfd->hSock, pbuf, (INT32)size, flags, &txsize );
// Disconnect
SockDisconnect( pfd->hSock );
// Record any error condition
if( error )
goto sendto_error;
llExit();
return((int)txsize);
sendto_error:
pfd->pfdt->error = error;
llExit();
return( SOCKET_ERROR );
}
//--------------------------------------------------------------------
// shutdown()
//
// Shutdown a socket connection
//--------------------------------------------------------------------
int shutdown( int s, int how )
{
FILEDESC *pfd;
int error = 0;
llEnter();
// Verify Index
if( !(pfd = fdint_verify(s, FDTYPE_SOCKET)) )
{
llExit();
return( SOCKET_ERROR );
}
// Shutdown the socket
error = SockShutdown( pfd->hSock, how );
// Check error condition
if( error )
{
pfd->pfdt->error = error;
llExit();
return( SOCKET_ERROR );
}
llExit();
return(0);
}
//--------------------------------------------------------------------
// socket()
//
// Create a socket
//--------------------------------------------------------------------
int socket( int domain, int type, int protocol )
{
FDTABLE *pfdt;
FILEDESC *pfd;
int error = 0;
llEnter();
// Verify Task
if( !(pfdt = fdint_getfdt( 0 )) )
{
llExit();
return( INVALID_SOCKET );
}
// Get a new file descriptor
if( (error=fdint_new(pfdt, &pfd, FDTYPE_SOCKET)) )
goto socket_error;
// Create a new socket
if( (error=SockNew( domain, type, protocol, pfd, &pfd->hSock )) )
{
// Abort the new fd and set error
fdint_free( pfd );
goto socket_error;
}
llExit();
// Return the file descriptor table index
return( pfd->fd );
socket_error:
pfdt->error = error;
llExit();
return( INVALID_SOCKET );
}
//--------------------------------------------------------------------
// socketpair()
//
// Create a two socket communucations channel
//--------------------------------------------------------------------
int socketpair( int domain, int type, int protocol, int s[2] )
{
(void) domain;
(void) type;
(void) protocol;
(void) s;
return( SOCKET_ERROR );
}
//--------------------------------------------------------------------
// getsockopt() (USER FUNCTION)
//
// Read a socket option value
//--------------------------------------------------------------------
int getsockopt( int s, int level, int op, void *pbuf, int *pbufsize )
{
FILEDESC *pfd;
int error = 0;
llEnter();
// Verify Index
if( !(pfd = fdint_verify(s, FDTYPE_SOCKET)) )
{
llExit();
return( SOCKET_ERROR );
}
// Get Option
error = SockGet( pfd->hSock, level, op, pbuf, pbufsize );
// Check error condition
if( error )
{
pfd->pfdt->error = error;
llExit();
return( SOCKET_ERROR );
}
llExit();
return(0);
}
//--------------------------------------------------------------------
// setsockopt() (USER FUNCTION)
//
// Write a socket option value
//--------------------------------------------------------------------
int setsockopt( int s, int level, int op, void *pbuf, int bufsize )
{
FILEDESC *pfd;
int error = 0;
llEnter();
// Verify Index
if( !(pfd = fdint_verify(s, FDTYPE_SOCKET)) )
{
llExit();
return( SOCKET_ERROR );
}
// Set Option
error = SockSet( pfd->hSock, level, op, pbuf, bufsize );
// Check error condition
if( error )
{
pfd->pfdt->error = error;
llExit();
return( SOCKET_ERROR );
}
llExit();
return(0);
}
//--------------------------------------------------------------------
// pipe()
//
// Create a pipe
//
// Returns 0 on success, else -1
//--------------------------------------------------------------------
int pipe( int *pfdret1, int *pfdret2 )
{
FDTABLE *pfdt;
FILEDESC *pfd1=0,*pfd2=0;
int error = 0;
llEnter();
// Verify Task
if( !(pfdt = fdint_getfdt( 0 )) )
{
llExit();
return( -1 );
}
// Verify the fd pointers
if( !pfdret1 || !pfdret2 )
{
error = EINVAL;
goto pipe_error;
}
// Get a new file descriptors
if( (error=fdint_new(pfdt, &pfd1, FDTYPE_PIPE)) )
goto pipe_error;
if( (error=fdint_new(pfdt, &pfd2, FDTYPE_PIPE)) )
goto pipe_error;
// Create a new pipe
if( (error=PipeNew( pfd1, pfd2, &pfd1->hSock, &pfd2->hSock )) )
goto pipe_error;
llExit();
// Return the file descriptor table indicies
*pfdret1 = pfd1->fd;
*pfdret2 = pfd2->fd;
return(0);
pipe_error:
if( pfd1 )
fdint_free( pfd1 );
if( pfd2 )
fdint_free( pfd2 );
pfdt->error = error;
llExit();
return( -1 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -