Example #1
0
        } // class SocketStream

        // Create a new socket.
        public static int __syscall_socket(int domain, int type)
        {
            AddressFamily family;
            SocketType    socketType;
            ProtocolType  protocol;

            // Convert the parametrs into their C# equivalents.
            if (domain == 2 /*AF_INET*/)
            {
                family = AddressFamily.InterNetwork;
            }
            else if (domain == 10 /*AF_INET6*/)
            {
                family = AddressFamily.InterNetworkV6;
            }
            else
            {
                return(-22);                                    /* EINVAL */
            }
            if (type == 1 /*SOCK_STREAM*/)
            {
                socketType = SocketType.Stream;
                protocol   = ProtocolType.Tcp;
            }
            else if (type == 2 /*SOCK_DGRAM*/)
            {
                socketType = SocketType.Dgram;
                protocol   = ProtocolType.Udp;
            }
            else
            {
                return(-22);                                    /* EINVAL */
            }

            // Create the socket.
            Socket socket;

            try
            {
                socket = new Socket(family, socketType, protocol);
            }
            catch (SocketException)
            {
                return(-22);                                    /* EINVAL */
            }

            // Wrap the socket within a stream.
            Stream stream = new SocketStream(socket);

            // Associate the stream with a file descriptor.
            int fd = FileTable.AllocFD();

            if (fd == -1)
            {
                stream.Close();
                return(fd);
            }
            FileTable.SetFileDescriptor(fd, stream);
            return(fd);
        }
Example #2
0
        // Open a file descriptor, based on a file.
        public static int __syscall_open(IntPtr path, int mode, int access)
        {
            int    fd;
            Stream stream = null;

            // Reserve a slot in the file descriptor table.
            fd = FileTable.AllocFD();
            if (fd == -1)
            {
                return(-24);                                    /* EMFILE */
            }

            // Open the file stream.
            try
            {
                String name = Marshal.PtrToStringAnsi(path);
                stream = new FileStream
                             (name, (FileMode)mode, (FileAccess)access);
            }
            catch (SecurityException)
            {
                return(-13);                                    /* EACCES */
            }
            catch (FileNotFoundException)
            {
                return(-2);                                     /* ENOENT */
            }
            catch (DirectoryNotFoundException)
            {
                return(-20);                                    /* ENOTDIR */
            }
            catch (PathTooLongException)
            {
                return(-36);                                    /* ENAMETOOLONG */
            }
            catch (UnauthorizedAccessException)
            {
                return(-13);                                    /* EACCES */
            }
            catch (IOException)
            {
                // Could be anything, but EEXIST is most likely.
                return(-17);                                    /* EEXIST */
            }
            finally
            {
                if (stream == null)
                {
                    FileTable.ReleaseFD(fd);
                }
            }

            // Assign the stream to the "fd" slot in the file
            // descriptor table.
            FileTable.SetFileDescriptor(fd, stream);
            return(fd);
        }
Example #3
0
        // Wrap a socket that was just accepted with a file descriptor.
        public static int __syscall_wrap_accept(Socket socket)
        {
            // Wrap the socket within a stream.
            SocketStream stream = new SocketStream(socket);

            stream.Connected = true;

            // Associate the stream with a file descriptor.
            int fd = FileTable.AllocFD();

            if (fd == -1)
            {
                stream.Close();
                return(fd);
            }
            FileTable.SetFileDescriptor(fd, stream);
            return(fd);
        }