#define KERNEL
#define _INTERNAL_FILE
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <stdio.h>
#include <string.h>
#include <proto/socket.h>
#include <sys/syslog.h>
#include <errno.h>
extern int errno;

extern int __tcpioctl(struct file *f, unsigned int request, int inout, int arglen,char *argp);
extern int __tcpread(struct file *ufb, char *buffer, unsigned int length);
extern int __tcpwrite(struct file *ufb, char *buffer, unsigned int length);
extern int __tcpclose(struct file *ufb);
extern int falloc(struct file **, int *);
extern int ix_panic(char *);

int _allocsocket(int sock, int domain) {
    struct file *fp;
    int fd,err;

    if ((err=falloc(&fp, &fd))) {
	char errmsg[512];
	sprintf(errmsg,"falloc failed: %s\n",strerror(err));
	ix_panic(errmsg);
	exit(20);
    }

    fp->f_so = (struct socket *)sock;
    fp->f_stb.st_mode = 0666 | S_IFSOCK; /* not always, but.. */
    fp->f_stb.st_size = 128;	/* sizeof mbuf.. */
    fp->f_stb.st_blksize = 128;

    fp->f_flags  = FREAD|FWRITE;
    fp->f_type	 = (domain == AF_INET) ? DTYPE_SOCKET : DTYPE_USOCKET;
    fp->f_read	 = __tcpread;
    fp->f_write  = __tcpwrite;
    fp->f_ioctl  = __tcpioctl;
    fp->f_close  = __tcpclose;
    fp->f_select = NULL;
    return fd;
}
