⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket.cxx

📁 移植到WLIT项目的redboot源代码
💻 CXX
📖 第 1 页 / 共 2 页
字号:
//==========================================================================////      socket.cxx////      Fileio socket operations////==========================================================================//####COPYRIGHTBEGIN####//                                                                          // -------------------------------------------                              // The contents of this file are subject to the Red Hat eCos Public License // Version 1.1 (the "License"); you may not use this file except in         // compliance with the License.  You may obtain a copy of the License at    // http://www.redhat.com/                                                   //                                                                          // Software distributed under the License is distributed on an "AS IS"      // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the // License for the specific language governing rights and limitations under // the License.                                                             //                                                                          // The Original Code is eCos - Embedded Configurable Operating System,      // released September 30, 1998.                                             //                                                                          // The Initial Developer of the Original Code is Red Hat.                   // Portions created by Red Hat are                                          // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.                             // All Rights Reserved.                                                     // -------------------------------------------                              //                                                                          //####COPYRIGHTEND####//==========================================================================//#####DESCRIPTIONBEGIN####//// Author(s):           nickg// Contributors:        nickg// Date:                2000-05-25// Purpose:             Fileio socket operations// Description:         These are the functions that operate on sockets,//                      such as socket(), bind(), accept() etc.//              //              ////####DESCRIPTIONEND####////==========================================================================#include <pkgconf/system.h>#include <pkgconf/hal.h>#include <pkgconf/kernel.h>#include <pkgconf/io_fileio.h>#include <cyg/hal/hal_tables.h>#include <cyg/kernel/ktypes.h>         // base kernel types#include <cyg/infra/cyg_trac.h>        // tracing macros#include <cyg/infra/cyg_ass.h>         // assertion macros#include <stdarg.h>                     // for fcntl()#include <cyg/io/file.h>                // struct iovec#include "fio.h"                       // Private header#include <cyg/kernel/mutex.hxx>        // mutex definitions#ifdef CYGPKG_NET#include <sys/socket.h>                 // struct msghdr#endif//==========================================================================// Forward definitionsstatic void cyg_ns_lock( cyg_nstab_entry *ns );static void cyg_ns_unlock( cyg_nstab_entry *ns );static void cyg_sock_lock( cyg_file *fp );static void cyg_sock_unlock( cyg_file *fp );//==========================================================================// Local entry/return macros#define SOCKET_ENTRY() FILEIO_ENTRY()#define SOCKET_RETURN(err) FILEIO_RETURN(err)#define SOCKET_RETURN_VALUE(val) FILEIO_RETURN_VALUE(val)//==========================================================================// Locking protocols#define LOCK_NS( _n ) cyg_ns_lock( _n )#define UNLOCK_NS( _n ) cyg_ns_unlock( _n )#define LOCK_SOCKET( _fp ) cyg_sock_lock( _fp )#define UNLOCK_SOCKET( _fp ) cyg_sock_unlock( _fp )//==========================================================================// Tables and local variables// Array of network stacks installed__externC cyg_nstab_entry nstab[];CYG_HAL_TABLE_BEGIN( nstab, nstab );// End of array marker__externC cyg_nstab_entry nstab_end;CYG_HAL_TABLE_END( nstab_end, nstab );Cyg_Mutex nstab_lock[CYGNUM_FILEIO_NSTAB_MAX];//==========================================================================// Initialization__externC void cyg_nstab_init(){    cyg_nstab_entry *n;    for( n = &nstab[0]; n != &nstab_end; n++ )    {        // stop if there are more than the configured maximum        if( n-&nstab[0] >= CYGNUM_FILEIO_NSTAB_MAX )            break;        if( n->init( n ) == 0 )        {            n->valid = true;        }    }}//==========================================================================// Socket API calls// -------------------------------------------------------------------------__externC int	socket (int domain, int type, int protocol){    SOCKET_ENTRY();    int err = 0;    int fd;    cyg_file *file;    fd = cyg_fd_alloc(0);    if( fd < 0 )        SOCKET_RETURN(EMFILE);        file = cyg_file_alloc();    if( file == NULL )    {        cyg_fd_free(fd);        SOCKET_RETURN(ENFILE);    }    cyg_nstab_entry *n;    for( n = &nstab[0]; n != &nstab_end; n++ )    {        LOCK_NS( n );                err = n->socket( n, domain, type, protocol, file );        UNLOCK_NS( n );                if( err == 0 ) break;    }        if( err != 0 )    {        cyg_fd_free(fd);        cyg_file_free(file);                SOCKET_RETURN( err );    }    file->f_syncmode = n->syncmode;    file->f_mte = (cyg_mtab_entry *)n;        cyg_fd_assign( fd, file );    SOCKET_RETURN_VALUE(fd);}// -------------------------------------------------------------------------__externC int	accept (int s, struct sockaddr *sa, socklen_t *addrlen){    SOCKET_ENTRY();    int err = 0;    int fd;    cyg_file *fp;    cyg_file *new_fp;    fp = cyg_fp_get( s );    if( fp == NULL )        FILEIO_RETURN(EBADF);    fd = cyg_fd_alloc(0);    if( fd < 0 )    {        cyg_fp_free( fp );        SOCKET_RETURN(EMFILE);    }        new_fp = cyg_file_alloc();    if( new_fp == NULL )    {        cyg_fp_free( fp );        cyg_fd_free(fd);        SOCKET_RETURN(ENFILE);    }    if( fp->f_type == CYG_FILE_TYPE_SOCKET )    {        cyg_sock_ops *ops = (cyg_sock_ops *)fp->f_xops;        LOCK_SOCKET( fp );        err = ops->accept( fp, new_fp, sa, addrlen );        UNLOCK_SOCKET( fp );    }    else err = EBADF;    if( err != 0 )    {        cyg_fp_free( fp );        cyg_fd_free(fd);        cyg_file_free(new_fp);                SOCKET_RETURN( err );    }    new_fp->f_syncmode = fp->f_syncmode;    new_fp->f_mte = fp->f_mte;        cyg_fd_assign( fd, new_fp );    cyg_fp_free( fp );        SOCKET_RETURN_VALUE(fd);}// -------------------------------------------------------------------------__externC int	bind (int s, const struct sockaddr *sa, unsigned int len){    SOCKET_ENTRY();    int ret = 0;    cyg_file *fp;        fp = cyg_fp_get( s );    if( fp == NULL )        FILEIO_RETURN(EBADF);    if( fp->f_type == CYG_FILE_TYPE_SOCKET )    {        cyg_sock_ops *ops = (cyg_sock_ops *)fp->f_xops;        LOCK_SOCKET( fp );        ret = ops->bind( fp, sa, len );        UNLOCK_SOCKET( fp );    }        cyg_fp_free( fp );        SOCKET_RETURN(ret);}// -------------------------------------------------------------------------__externC int	connect (int s, const struct sockaddr *sa, socklen_t len){    SOCKET_ENTRY();    int ret = 0;    cyg_file *fp;        fp = cyg_fp_get( s );    if( fp == NULL )        FILEIO_RETURN(EBADF);    if( fp->f_type == CYG_FILE_TYPE_SOCKET )    {        cyg_sock_ops *ops = (cyg_sock_ops *)fp->f_xops;        LOCK_SOCKET( fp );        ret = ops->connect( fp, sa, len );        UNLOCK_SOCKET( fp );    }        cyg_fp_free( fp );        SOCKET_RETURN(ret);}// -------------------------------------------------------------------------__externC int	getpeername (int s, struct sockaddr *sa, socklen_t *len){    SOCKET_ENTRY();    int ret = 0;    cyg_file *fp;        fp = cyg_fp_get( s );    if( fp == NULL )        FILEIO_RETURN(EBADF);    if( fp->f_type == CYG_FILE_TYPE_SOCKET )    {        cyg_sock_ops *ops = (cyg_sock_ops *)fp->f_xops;        LOCK_SOCKET( fp );        ret = ops->getname( fp, sa, len, 1 );        UNLOCK_SOCKET( fp );    }        cyg_fp_free( fp );        SOCKET_RETURN(ret);}// -------------------------------------------------------------------------__externC int	getsockname (int s, struct sockaddr *sa, socklen_t *len){    SOCKET_ENTRY();    int ret = 0;    cyg_file *fp;        fp = cyg_fp_get( s );    if( fp == NULL )        FILEIO_RETURN(EBADF);    if( fp->f_type == CYG_FILE_TYPE_SOCKET )    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -