/* * scamper_fd_private * * allocate a private fd for scamper to manage. this fd is not shared amongst * scamper. */ scamper_fd_t *scamper_fd_private(int fd, scamper_fd_cb_t read_cb, void *read_param, scamper_fd_cb_t write_cb, void *write_param) { scamper_fd_t *fdn = NULL; if((fdn = fd_alloc(SCAMPER_FD_TYPE_PRIVATE, fd)) == NULL) { goto err; } if((fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL) goto err; if(read_cb != NULL) { scamper_fd_read_set(fdn, read_cb, read_param); scamper_fd_read_unpause(fdn); } if(write_cb != NULL) { scamper_fd_write_set(fdn, write_cb, write_param); scamper_fd_write_unpause(fdn); } return fdn; err: if(fdn != NULL) fd_free(fdn); return NULL; }
/* * open a file with flags and mode and return file descriptor. */ int open(const char * path, u32_t flags, u32_t mode) { char buf[MAX_PATH]; struct file * fp; int fd; int err; if((fd = fd_alloc(0)) < 0) return -1; if(vfs_path_conv(path, buf) !=0 ) { fd_free(fd); return -1; } if((err = sys_open(buf, flags, mode, &fp)) != 0) { fd_free(fd); return err; } set_fp(fd, fp); return fd; }
// Open a file (or directory). // // Returns: // The file descriptor index on success // -E_BAD_PATH if the path is too long (>= MAXPATHLEN) // < 0 for other errors. int open(const char *path, int mode) { // Find an unused file descriptor page using fd_alloc. // Then send a file-open request to the file server. // Include 'path' and 'omode' in request, // and map the returned file descriptor page // at the appropriate fd address. // FSREQ_OPEN returns 0 on success, < 0 on failure. // // (fd_alloc does not allocate a page, it just returns an // unused fd address. Do you need to allocate a page?) // // Return the file descriptor index. // If any step after fd_alloc fails, use fd_close to free the // file descriptor. // LAB 5: Your code here. struct Fd *fd; int r; if((r = fd_alloc(&fd)) < 0) return r; strcpy(fsipcbuf.open.req_path, path); fsipcbuf.open.req_omode = mode; ipc_send(envs[1].env_id, FSREQ_OPEN, &fsipcbuf, PTE_P | PTE_W | PTE_U); if((r = ipc_recv(NULL, fd, NULL))<0) return r; return fd2num(fd); panic("open not implemented"); }
// Open a file (or directory), // returning the file descriptor index on success, < 0 on failure. int open(const char *path, int mode) { // Find an unused file descriptor page using fd_alloc. // Then send a message to the file server to open a file // using a function in fsipc.c. // (fd_alloc does not allocate a page, it just returns an // unused fd address. Do you need to allocate a page? Look // at fsipc.c if you aren't sure.) // Then map the file data (you may find fmap() helpful). // Return the file descriptor index. // If any step fails, use fd_close to free the file descriptor. // LAB 5: Your code here. int r; struct Fd *fd; if ((r = fd_alloc(&fd)) < 0) return r; if ((r = fsipc_open(path, mode, fd)) < 0){ fd_close(fd, 0); return r; } if ((r = fmap(fd, 0, fd->fd_file.file.f_size) ) < 0){ fd_close(fd, 0); return r; } return fd2num(fd); }
// Open a file (or directory). // // Returns: // The file descriptor index on success // -E_BAD_PATH if the path is too long (>= MAXPATHLEN) // < 0 for other errors. int open(const char *path, int mode) { // Find an unused file descriptor page using fd_alloc. // Then send a file-open request to the file server. // Include 'path' and 'omode' in request, // and map the returned file descriptor page // at the appropriate fd address. // FSREQ_OPEN returns 0 on success, < 0 on failure. // // (fd_alloc does not allocate a page, it just returns an // unused fd address. Do you need to allocate a page?) // // Return the file descriptor index. // If any step after fd_alloc fails, use fd_close to free the // file descriptor. // LAB 5: Your code here. struct Fd* retval; int r = fd_alloc(&retval); if(r<0) return r; fsipcbuf.open.req_omode = mode; strcpy(fsipcbuf.open.req_path, path); r = fsipc(FSREQ_OPEN, retval); if (r<0) { fd_close(retval, 0); return r; } return fd2num(retval); // panic("open not implemented"); }
// Open a file (or directory). // // Returns: // The file descriptor index on success // -E_BAD_PATH if the path is too long (>= MAXPATHLEN) // < 0 for other errors. int open(const char *path, int mode) { // Find an unused file descriptor page using fd_alloc. // Then send a file-open request to the file server. // Include 'path' and 'omode' in request, // and map the returned file descriptor page // at the appropriate fd address. // FSREQ_OPEN returns 0 on success, < 0 on failure. // // (fd_alloc does not allocate a page, it just returns an // unused fd address. Do you need to allocate a page?) // // Return the file descriptor index. // If any step after fd_alloc fails, use fd_close to free the struct Fd *f = NULL; int r = 0; if (!path) return -E_INVAL; if((r=fd_alloc(&f))<0) return r; strcpy(fsipcbuf.open.req_path, path); fsipcbuf.open.req_omode = mode; if ((r=fsipc(FSREQ_OPEN, f))<0) { fd_close(f, 0); return r; } return fd2num(f); }
int accept(int fd, struct sockaddr *addr, int *anamelen) { oskit_error_t rc; oskit_socket_t *newsock; int newfd; if (fd_check_socket(fd)) return -1; fd_access_lock(fd, FD_RDWR); /* Accept a new connection */ rc = oskit_socket_accept(fd_array[fd].socket, (struct oskit_sockaddr *)addr, anamelen, &newsock); fd_access_unlock(fd, FD_RDWR); if (rc) { errno = rc; return -1; } /* Allocate a file descriptor for it */ newfd = fd_alloc((oskit_iunknown_t*)newsock, 0); if (newfd < 0) { oskit_socket_release(newsock); return -1; } fd_lock(newfd); fd_array[newfd].socket = newsock; fd_unlock(newfd); return newfd; }
int pipe(int pfd[2]) { int r; struct Fd *fd0, *fd1; void *va; // allocate the file descriptor table entries if ((r = fd_alloc(&fd0)) < 0 || (r = sys_page_alloc(0, fd0, PTE_P|PTE_W|PTE_U|PTE_SHARE)) < 0) goto err; if ((r = fd_alloc(&fd1)) < 0 || (r = sys_page_alloc(0, fd1, PTE_P|PTE_W|PTE_U|PTE_SHARE)) < 0) goto err1; // allocate the pipe structure as first data page in both va = fd2data(fd0); if ((r = sys_page_alloc(0, va, PTE_P|PTE_W|PTE_U|PTE_SHARE)) < 0) goto err2; if ((r = sys_page_map(0, va, 0, fd2data(fd1), PTE_P|PTE_W|PTE_U|PTE_SHARE)) < 0) goto err3; // set up fd structures fd0->fd_dev_id = devpipe.dev_id; fd0->fd_omode = O_RDONLY; fd1->fd_dev_id = devpipe.dev_id; fd1->fd_omode = O_WRONLY; if (debug) cprintf("[%08x] pipecreate %08x\n", env->env_id, vpt[VPN(va)]); pfd[0] = fd2num(fd0); pfd[1] = fd2num(fd1); return 0; err3: sys_page_unmap(0, va); err2: sys_page_unmap(0, fd1); err1: sys_page_unmap(0, fd0); err: return r; }
scamper_fd_t *scamper_fd_dl(int ifindex) { scamper_fd_t *fdn = NULL, findme; int fd = -1; findme.type = SCAMPER_FD_TYPE_DL; findme.fd_dl_ifindex = ifindex; if((fdn = fd_find(&findme)) != NULL) { scamper_fd_read_unpause(fdn); return fdn; } /* * open the file descriptor for the ifindex, and then allocate a scamper_fd * for the file descriptor */ if((fd = scamper_dl_open(ifindex)) == -1 || (fdn = fd_alloc(SCAMPER_FD_TYPE_DL, fd)) == NULL) { goto err; } /* * record the ifindex for the file descriptor, and then allocate the state * that is maintained with it */ fdn->fd_dl_ifindex = ifindex; /* * 1. add the file descriptor to the splay tree * 2. allocate state for the datalink file descriptor */ if((fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL || (fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL || (fdn->fd_dl_dl = scamper_dl_state_alloc(fdn)) == NULL) { goto err; } /* set the file descriptor up for reading */ fdn->read.cb = scamper_dl_read_cb; fdn->read.param = fdn->fd_dl_dl; fdn->write.cb = NULL; fdn->write.param = NULL; scamper_fd_read_unpause(fdn); return fdn; err: if(fdn != NULL) free(fdn); if(fd != -1) scamper_dl_close(fd); return NULL; }
static scamper_fd_t *fd_tcp(int type, void *addr, uint16_t sport) { scamper_fd_t *fdn, findme; size_t len = 0; int fd = -1; findme.type = type; findme.fd_tcp_addr = addr; findme.fd_tcp_sport = sport; if((fdn = fd_find(&findme)) != NULL) { return fdn; } if(type == SCAMPER_FD_TYPE_TCP4) { fd = scamper_tcp4_open(addr, sport); len = sizeof(struct in_addr); } else if(type == SCAMPER_FD_TYPE_TCP6) { fd = scamper_tcp6_open(addr, sport); len = sizeof(struct in6_addr); } if(fd == -1 || (fdn = fd_alloc(type, fd)) == NULL || (addr != NULL && (fdn->fd_tcp_addr = memdup(addr, len)) == NULL)) { goto err; } fdn->fd_tcp_sport = sport; if((fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL || (fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL) { goto err; } return fdn; err: if(fd != -1) { if(type == SCAMPER_FD_TYPE_TCP4) scamper_tcp4_close(fd); else if(type == SCAMPER_FD_TYPE_TCP6) scamper_tcp6_close(fd); } if(fdn != NULL) fd_free(fdn); return NULL; }
int opencons(void) { int r; struct Fd* fd; if ((r = fd_alloc(&fd)) < 0) return r; if ((r = sys_page_alloc(0, fd, PTE_P|PTE_U|PTE_W|PTE_SHARE)) < 0) return r; fd->fd_dev_id = devcons.dev_id; fd->fd_omode = O_RDWR; return fd2num(fd); }
int fd_open(int uid, const char *path, uint32_t flags) { int fd; if ((fd = fd_alloc()) < 0) return fd; //if ((vfs_file_descriptor[fd].ino = _path_to_inode(0, path)) < 0) // goto fail; //vfs_file_descriptor[fd].pos = 0; //vfs_file_descriptor[fd]. fail: fd_return(fd); return -ENOENT; }
int opencons(void) { int r; struct Fd *fd; if ((r = fd_alloc(&fd)) < 0) return r; if ((r = syscall_mem_alloc(0, (u_int)fd, PTE_V|PTE_R|PTE_LIBRARY)) < 0) return r; fd->fd_dev_id = devcons.dev_id; fd->fd_omode = O_RDWR; return fd2num(fd); }
int sys_open(char *name, unsigned int mode) { struct file *file; int fd = fd_alloc(); if (fd < 0) goto out; file = file_open(name, mode); if (!file) goto out_free_fd; fd_save_file(fd, file); return fd; out_free_fd: fd_free(fd); out: return -1; }
int sys_mkdir(char *path, unsigned int mode) { struct file *file; int fd = fd_alloc(); if (fd < 0) goto out; file = file_mkdir(path, mode); if (!file) goto out_free_fd; fd_save_file(fd, file); return fd; out_free_fd: fd_free(fd); out: return -1; }
void host_ipc_init() { int r; if ((r = fd_alloc(&host_fd)) < 0) panic("Couldn't allocate an fd!"); strcpy(host_fsipcbuf.open.req_path, HOST_FS_FILE); host_fsipcbuf.open.req_omode = O_RDWR; if ((r = host_fsipc(FSREQ_OPEN, host_fd)) < 0) { fd_close(host_fd, 0); panic("Couldn't open host file!"); } }
// Open a file (or directory). // // Returns: // The file descriptor index on success // -E_BAD_PATH if the path is too long (>= MAXPATHLEN) // < 0 for other errors. int open(const char *path, int mode) { // Find an unused file descriptor page using fd_alloc. // Then send a file-open request to the file server. // Include 'path' and 'omode' in request, // and map the returned file descriptor page // at the appropriate fd address. // FSREQ_OPEN returns 0 on success, < 0 on failure. // // (fd_alloc does not allocate a page, it just returns an // unused fd address. Do you need to allocate a page?) // // Return the file descriptor index. // If any step after fd_alloc fails, use fd_close to free the // file descriptor. // LAB 5: Your code here. //panic("open not implemented"); int result; struct Fd *fd_instance; if(strlen(path) >= MAXPATHLEN) { cprintf("open() error: path is too long"); return -E_BAD_PATH; } if((result=fd_alloc(&fd_instance)) < 0) { return result; } strcpy(fsipcbuf.open.req_path, path); fsipcbuf.open.req_omode=mode; if((result=fsipc(FSREQ_OPEN, (void *)fd_instance)) < 0) { fd_close(fd_instance, 0); return result; } else { return fd2num(fd_instance); } }
void host_ipc_init() { int r; if ((r = fd_alloc(&host_fd)) < 0) //if(( host_fd = (struct Fd*)malloc(sizeof(host_fd)))< 0) if((r = sys_page_alloc(0, (void*)host_fd, PTE_P|PTE_W|PTE_U)) < 0) panic("Couldn't allocate an fd!"); strcpy(host_fsipcbuf.open.req_path, HOST_FS_FILE); host_fsipcbuf.open.req_omode = O_RDWR; if ((r = host_fsipc(FSREQ_OPEN, host_fd)) < 0) { fd_close(host_fd, 0); panic("Couldn't open host file!"); } }
int _epoll_create(int ignored) { int i, fd; struct epoll_file *ef = NULL; struct file *f = NULL; /* find epoll_file struct */ for(i = 0; i < EPOLL_MAXFDS; i++) { if(!files[i].file) { ef = &files[i]; break; } } if(!ef) { errno = ENOMEM; return -1; } /* allocate file struct */ f = file_alloc(); if(!f) { errno = ENOMEM; return -1; } /* allocate fd */ fd = fd_alloc(f); if(fd == -1) { file_close(f); errno = EMFILE; return -1; } /* got everything, fill out structures */ f->f_ops = &epoll_operations; f->f_cookie_ptr = (void *)ef; ef->file = f; for(i = 0; i < EPOLL_MAXFDS; i++) { ef->entries[i].fd = -1; } /* return fd */ return fd; }
void host_ipc_init() { int r; if ((r = fd_alloc(&host_fd)) < 0) panic("Couldn't allocate an fd!"); //r = sys_page_alloc(0, &host_fd, PTE_P | PTE_U | PTE_W); //if (r < 0) //{ // panic("Not Valid ALloc"); //} strcpy(host_fsipcbuf.open.req_path, HOST_FS_FILE); host_fsipcbuf.open.req_omode = O_RDWR; if ((r = host_fsipc(FSREQ_OPEN, host_fd)) < 0) { fd_close(host_fd, 0); panic("Couldn't open host file!"); } }
// Open a file (or directory). // // Returns: // The file descriptor index on success // -E_BAD_PATH if the path is too long (>= MAXPATHLEN) // < 0 for other errors. int open(const char *path, int mode) { // Find an unused file descriptor page using fd_alloc. // Then send a file-open request to the file server. // Include 'path' and 'omode' in request, // and map the returned file descriptor page // at the appropriate fd address. // FSREQ_OPEN returns 0 on success, < 0 on failure. // // (fd_alloc does not allocate a page, it just returns an // unused fd address. Do you need to allocate a page?) // // Return the file descriptor index. // If any step after fd_alloc fails, use fd_close to free the // file descriptor. // LAB 5: Your code here. int r; struct Fd *fd; r = fd_alloc(&fd); if (r < 0) return r; // DO NOT ALLOCATE !! //r = sys_page_alloc(0, fd, PTE_U|PTE_W|PTE_P); //if (r < 0) goto out; if (strlen(path) >= MAXPATHLEN) return -E_BAD_PATH; strcpy(fsipcbuf.open.req_path, path); fsipcbuf.open.req_omode = mode; r = fsipc(FSREQ_OPEN, fd); if (r < 0) goto out; return fd2num(fd); out: assert(fd_close(fd, 0) == 0); return r; }
static scamper_fd_t *fd_null(int type) { scamper_fd_t *fdn = NULL, findme; int fd = -1; /* first check if a sharable fd exists for this type */ findme.type = type; if((fdn = fd_find(&findme)) != NULL) { return fdn; } if(type == SCAMPER_FD_TYPE_RTSOCK) fd = scamper_rtsock_open(); else if(type == SCAMPER_FD_TYPE_IFSOCK) fd = socket(AF_INET, SOCK_DGRAM, 0); else if(type == SCAMPER_FD_TYPE_IP4) fd = scamper_ip4_openraw(); if(fd == -1 || (fdn = fd_alloc(type, fd)) == NULL || (fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL || (fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL) { goto err; } return fdn; err: if(fd != -1) { if(type == SCAMPER_FD_TYPE_RTSOCK) scamper_rtsock_close(fd); else if(type == SCAMPER_FD_TYPE_IFSOCK) close(fd); else if(type == SCAMPER_FD_TYPE_IP4) scamper_ip4_close(fd); } if(fdn != NULL) fd_free(fdn); return NULL; }
// Open a file (or directory). // // Returns: // The file descriptor index on success // -E_BAD_PATH if the path is too long (>= MAXPATHLEN) // < 0 for other errors. int open(const char *path, int mode) { // Find an unused file descriptor page using fd_alloc. // Then send a file-open request to the file server. // Include 'path' and 'omode' in request, // and map the returned file descriptor page // at the appropriate fd address. // FSREQ_OPEN returns 0 on success, < 0 on failure. // // (fd_alloc does not allocate a page, it just returns an // unused fd address. Do you need to allocate a page?) // // Return the file descriptor index. // If any step after fd_alloc fails, use fd_close to free the // file descriptor. if (strlen(path) > MAXPATHLEN) return -E_BAD_PATH; struct Fd *fd; int r; if ((r < fd_alloc(&fd)) < 0) return r; strcpy(fsipcbuf.open.req_path, path); fsipcbuf.open.req_omode = mode; if ((r = fsipc(FSREQ_OPEN, fd)) < 0) { fd_close(fd, 0); return r; } return fd2num(fd); panic("open not implemented"); }
/* * open a directory */ void * opendir(const char * name) { char buf[MAX_PATH]; struct dir * dir; struct file * fp; int fd; int err; if((dir = malloc(sizeof(struct dir))) == NULL) return NULL; /* find empty slot for file descriptor. */ if((fd = fd_alloc(0)) < 0) { free(dir); return NULL; } if((err = vfs_path_conv(name, buf)) !=0 ) { free(dir); fd_free(fd); return NULL; } if((err = sys_opendir(buf, &fp)) != 0) { free(dir); fd_free(fd); return NULL; } set_fp(fd, fp); dir->fd = fd; return (void *)dir; }
int main (int argc, char *argv[]) { int cl_sfd,adm_sfd, s,u; int efd; char small_buf[10]; struct epoll_event event; struct epoll_event event2; struct epoll_event *events; struct ev_conn *tmpptr=NULL; struct rlimit limit; long maxfd = MAX_FD; //allocate and check memory limit.rlim_cur = maxfd; limit.rlim_max = maxfd; if ( setrlimit( RLIMIT_NOFILE, &limit ) == -1 ) { perror( "unable to set new soft limit" ); return 1; } /* obtain hard/soft limit */ if ( getrlimit( RLIMIT_NOFILE, &limit ) == -1 ) { perror( "unable to obtain limits" ); return 1; } else { printf( "FD limit = { hard : %d, soft : %d }\n", limit.rlim_max, limit.rlim_cur ); } ev_conn_ptr=(struct ev_conn *) malloc(sizeof(struct ev_conn)*MAX_CLIENTS); if (ev_conn_ptr == NULL) { fprintf(stderr,"malloc can't allocate memory !!!"); return 0; } // //bind on the client port fprintf(stderr,"Binding on the client port...\n"); cl_sfd = create_and_bind (CLIENT_PORT); if (cl_sfd == -1) abort(); s = make_socket_non_blocking (cl_sfd); if (s == -1) abort(); s = listen (cl_sfd, SOMAXCONN); if (s == -1) { perror ("listen"); abort(); } efd = epoll_create1 (0); if (efd == -1) { perror ("epoll_create"); abort(); } fprintf(stderr,"Client sfd %d\n",cl_sfd); event.data.ptr = fd_alloc(cl_sfd);; // event.data.u64 = 0; // event.data.u32 = 0; event.events = EPOLLIN | EPOLLET; s = epoll_ctl (efd, EPOLL_CTL_ADD, cl_sfd, &event); if (s == -1) { perror ("epoll_ctl"); abort(); } //bind on the admin port fprintf(stderr,"Binding on the admin port...\n"); adm_sfd = create_and_bind (ADMIN_PORT); if (adm_sfd == -1) abort(); s = make_socket_non_blocking (adm_sfd); if (s == -1) abort(); s = listen (adm_sfd, SOMAXCONN); if (s == -1) { perror ("listen"); abort(); } fprintf(stderr,"Admin sfd %d\n",adm_sfd); event2.data.ptr = fd_alloc(adm_sfd); // event2.data.u64 = 0; // event2.data.u32 = 0; event2.events = EPOLLIN | EPOLLET; s = epoll_ctl (efd, EPOLL_CTL_ADD, adm_sfd, &event2); if (s == -1) { perror ("epoll_ctl"); abort(); } /* Buffer where events are returned */ events = calloc (MAXEVENTS, sizeof event); /* The event loop */ while (1) { int n, i; n = epoll_wait (efd, events, MAXEVENTS, -1); for (i = 0; i < n; i++) { struct fd *tmpfd=events[i].data.ptr; fprintf(stderr,"wait stopped with %d events fd %d\n",n,tmpfd->sock); //continue; if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { /* An error has occured on this fd, or the socket is not ready for reading (why were we notified then?) */ fprintf (stderr, "epoll error closing the socket or not error maybe\n"); fd_close(tmpfd); continue; } else if (adm_sfd == tmpfd->sock) { fprintf(stderr,"admin connection\n"); /* We have a notification on the listening socket, which means one or more incoming connections. */ while (1) { struct fd *allocfd=NULL; struct sockaddr in_addr; socklen_t in_len; int infd; char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; in_len = sizeof in_addr; infd = accept (adm_sfd, &in_addr, &in_len); if (infd == -1) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { /* We have processed all incoming connections. */ break; } else { perror ("accept"); break; } } //TODO da comentiram dolniq zapis, poneje e izlishen ama sega za debug 6te go ostavq s = getnameinfo (&in_addr, in_len, hbuf, sizeof hbuf, sbuf, sizeof sbuf, NI_NUMERICHOST | NI_NUMERICSERV); if (s == 0) { printf("Accepted Admin connection on descriptor %d " "(host=%s, port=%s)\n", infd, hbuf, sbuf); } /* Make the incoming socket non-blocking and add it to the list of fds to monitor. */ s = make_socket_non_blocking (infd); if (s == -1) abort(); allocfd=fd_alloc(infd); allocfd->is_admin=1; event.data.ptr = allocfd; //event.data.u32 = 1; event.events = EPOLLIN | EPOLLET; s = epoll_ctl (efd, EPOLL_CTL_ADD, infd, &event); if (s == -1) { perror ("epoll_ctl"); abort(); } } continue; } else if (cl_sfd == tmpfd->sock) { fprintf(stderr,"client connection\n"); /* We have a notification on the listening socket, which means one or more incoming connections. */ while (1) { struct sockaddr in_addr; socklen_t in_len; int infd; char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; in_len = sizeof in_addr; infd = accept (cl_sfd, &in_addr, &in_len); if (infd == -1) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { /* We have processed all incoming connections. */ break; } else { perror ("accept"); break; } } //TODO da comentiram dolniq zapis, poneje e izlishen ama sega za debug 6te go ostavq s = getnameinfo (&in_addr, in_len, hbuf, sizeof hbuf, sbuf, sizeof sbuf, NI_NUMERICHOST | NI_NUMERICSERV); if (s == 0) { printf("Accepted Client connection on descriptor %d " "(host=%s, port=%s)\n", infd, hbuf, sbuf); } /* Make the incoming socket non-blocking and add it to the list of fds to monitor. */ s = make_socket_non_blocking (infd); if (s == -1) abort(); event.data.ptr = fd_alloc(infd); event.events = EPOLLIN | EPOLLET; s = epoll_ctl (efd, EPOLL_CTL_ADD, infd, &event); if (s == -1) { perror ("epoll_ctl"); abort(); } } continue; } else { /* We have data on the fd waiting to be read. Read and display it. We must read whatever data is available completely, as we are running in edge-triggered mode and won't get a notification again for the same data. */ int done = 0; fprintf(stderr,"check\n"); if(tmpfd->is_admin)//admin socket { ssize_t count=0; int local_ptr=-1; int buf_i=0; if(tmpfd->buffer==NULL) { tmpfd->buffer=ev_buffer_alloc(BUFFER_SIZE); tmpfd->buffer->len=0; } fprintf(stderr,"buffer ne e null\n"); char *buf=tmpfd->buffer->buf; while (1) { if(tmpfd->buffer->len>=BUFFER_SIZE) { fprintf(stderr,"max buffer size reached\n"); done=1; break; } count = read (tmpfd->sock, buf+tmpfd->buffer->len, (BUFFER_SIZE-tmpfd->buffer->len)); fprintf(stderr,"4ete ot admin\n"); write(0,buf+tmpfd->buffer->len,count); if (count == -1) { /* If errno == EAGAIN, that means we have read all data. So go back to the main loop. */ if (errno != EAGAIN) { perror ("read"); done = 1; } break; } else if (count == 0) { /* End of file. The remote has closed the connection. */ done = 1; break; } tmpfd->buffer->len+=count; fprintf(stderr,"tmpfd->buffer->len e %d\n",tmpfd->buffer->len); /* Write the buffer to standard output */ if(tmpfd->cur_ptr<0) { if(buf[0]=='\n' || buf[0]=='\r') // new key { u=0; while(u<ERROR_LOOP) //TODO to fix this da po4ne da 4isti socketite kogato sa na 80% pulni { fprintf(stderr,"vleze v cikula i vurti u\n"); cur_ptr++; if(cur_ptr==MAX_CLIENTS) { cur_ptr=1; u++; } tmpptr=ev_conn_ptr+cur_ptr; if(!tmpptr->sock && tmpptr->buffer==NULL) { fprintf(stderr,"cur_ptr e %d\n",cur_ptr); sprintf(small_buf, "%d", cur_ptr); //key generation fprintf(stderr,"small_buf e %s\n",small_buf); write(tmpfd->sock,small_buf,strlen(small_buf)); fprintf(stderr,"brum2\n"); //tmpfd->cur_ptr=cur_ptr; //adding the socket to the memory //tmpptr->sock=tmpfd->sock; break; } } if(u==ERROR_LOOP) { fprintf(stderr,"no free fds\n"); write(tmpfd->sock,"ERROR",5); //TODO to send reconnect signals to the clients and to free the all fds abort(); } fprintf(stderr,"key %d\n",cur_ptr); done=1; break; } else { fprintf(stderr,"chete admina kum koi client 6te se vurje\n"); // write (0, buf, count); for(buf_i=0;buf_i<9 && *(buf+buf_i)!='\n' && *(buf+buf_i)!='\r';buf_i++)small_buf[buf_i]=*(buf+buf_i); small_buf[buf_i]='\0'; local_ptr=strtol(small_buf, NULL, 10); fprintf(stderr,"local_ptr se okaza %d\n",local_ptr); tmpfd->buffer->offset=(buf_i+1); if(tmpfd->buffer->offset>tmpfd->buffer->len) { fprintf(stderr,"ERROR , the offset is bigger than the len.This should not happen\n"); return -1; } if(!local_ptr || local_ptr>MAX_CLIENTS) { fprintf(stderr,"ERROR wrong number\n"); write(tmpfd->sock,"ERROR",5); close(tmpfd->sock); tmpfd->sock=0; break; //error } tmpfd->cur_ptr=local_ptr; fprintf(stderr,"DEBUG count %d\n",count); fprintf(stderr,"DEBUG buf_i %d\n",buf_i); } } } if (done) { if((tmpfd->cur_ptr)>=0) { tmpptr=ev_conn_ptr+tmpfd->cur_ptr; //buf[count]='\0';//TODO tova da go vidq dali nqma da otreje posledniq symbol if(tmpptr->sock) { fprintf(stderr,"ima clientski socket za %d i pi6e v nego\n",tmpfd->cur_ptr); s = write (tmpptr->sock, tmpfd->buffer->buf+tmpfd->buffer->offset, tmpfd->buffer->len-tmpfd->buffer->offset); s = write (0, tmpfd->buffer->buf+tmpfd->buffer->offset, tmpfd->buffer->len-tmpfd->buffer->offset); if (s == -1) { perror ("write"); write(tmpfd->sock,"ERROR",5); close(tmpfd->sock); tmpfd->sock=0; //nikov if(tmpptr->sock){close(tmpptr->sock);tmpptr->sock=0;} ev_buffer_free(tmpfd->buffer); tmpfd->buffer=NULL; break; } //closing the client socket if(tmpptr->sock)close(tmpptr->sock); tmpptr->sock=0; ev_buffer_free(tmpfd->buffer); tmpfd->buffer=NULL; //if(tmpptr->buffer!=NULL)ev_buffer_free(tmpptr->buffer); } else { fprintf(stderr,"zapisva v buffer za %d golqm %d,\n",tmpfd->cur_ptr,tmpfd->buffer->len); tmpptr->buffer=tmpfd->buffer; } } printf ("Closed connection on descriptor %d\n", tmpfd->sock); /* Closing the descriptor will make epoll remove it from the set of descriptors which are monitored. */ tmpfd->buffer=NULL; close (tmpfd->sock); } } else // client socket { char buf[BUFFER_SIZE]; long int local_ptr=-1; int buf_i=0; int tmp=0; int i=0; int error=0; ssize_t count; while (1) { ssize_t count; count = read (tmpfd->sock, buf,BUFFER_SIZE); fprintf(stderr,"client count %d\n",count); if (count == -1) { /* If errno == EAGAIN, that means we have read all data. So go back to the main loop. */ if (errno != EAGAIN) { perror ("read"); done = 1; } break; } else if (count == 0) { /* End of file. The remote has closed the connection. */ done = 1; break; } if (strncmp(buf,"GET ",4)) { break; } for(i=0;i<count;i++) { fprintf(stderr,"%c %d\n",buf[i],i); if(buf[i]=='\r' || buf[i]=='\n') { break; } } if(i==count) { fprintf(stderr,"strange error in http protocol\n"); fd_close(tmpfd); break; } fprintf(stderr,"i e %d\n",i); count=i; if(count <5) { fprintf(stderr,"ERROR count out of boundaries\n"); error=1; break; } fprintf(stderr,"buf+4 e %c\n",*(buf+4)); buf_i=findend(buf+4,count); fprintf(stderr,"bif_i e %d\n",buf_i); buf[buf_i+4]='\0'; tmp=buf_i; for(;buf[buf_i+4]!='/' && buf_i>0;buf_i--); if((tmp-(buf_i+1))>9) { fprintf(stderr,"ERROR count out of boundaries 2\n"); error=1; break; } tmp=0; local_ptr = strtol(buf+4+buf_i+1, NULL, 10); fprintf(stderr,"local_ptr e %lld\n",local_ptr); if(local_ptr==0) { fprintf(stderr,"ERROR local_ptr is 0 for some reason\n"); error=1; break; } if(local_ptr>MAX_CLIENTS) { fprintf(stderr,"ERROR local_ptr %ld",local_ptr); error=1; break; } tmpptr=ev_conn_ptr+local_ptr; if(tmpptr->sock) { fprintf(stderr,"ERROR tmpptr->sock %d",tmpptr->sock); error=1; break; } tmpptr->sock=tmpfd->sock; tmpfd->cur_ptr=local_ptr; done=1; } if(error) { s = write (tmpfd->sock, HEADER_BUSY, HEADER_LEN_BUSY); fd_close(tmpfd);tmpfd->sock=0; } else if (done && tmpfd->cur_ptr!=-1) { tmpptr=ev_conn_ptr+tmpfd->cur_ptr; s = write (tmpptr->sock, HEADER, HEADER_LEN); if(tmpptr->buffer!=NULL) { fprintf(stderr,"buffer ne e NULL len e %d\n",tmpptr->buffer->len); fprintf(stderr,"offset\n"); fprintf(stderr,"buffer ne e NULL offset e %d\n",tmpptr->buffer->offset); //for(i=0;i<tmpptr->buffer->len;i++)fprintf(stderr,"%c %d\n",tmpptr->buffer->buf); s = write (tmpptr->sock, tmpptr->buffer->buf+tmpptr->buffer->offset, tmpptr->buffer->len-tmpptr->buffer->offset); write(0,tmpptr->buffer->buf+tmpptr->buffer->offset,tmpptr->buffer->len-tmpptr->buffer->offset); fprintf(stderr,"close\n"); fd_close(tmpfd); tmpptr->sock=0; fprintf(stderr,"sock 0\n"); ev_buffer_free(tmpptr->buffer); fprintf(stderr,"free\n"); tmpptr->buffer=NULL; } } else if(done) { fprintf(stderr,"nqma cur_ptr i e done taka 4e zatvarq conneciqta\n"); fd_close(tmpfd);tmpfd->sock=0; } } } } } free (events); close (adm_sfd); close (cl_sfd); return EXIT_SUCCESS; }
static scamper_fd_t *fd_udp(int type, void *addr, uint16_t sport) { scamper_fd_t *fdn, findme; size_t len = 0; int fd = -1; findme.type = type; findme.fd_udp_addr = addr; findme.fd_udp_sport = sport; if((fdn = fd_find(&findme)) != NULL) return fdn; if(type == SCAMPER_FD_TYPE_UDP4) { fd = scamper_udp4_openraw(addr); len = sizeof(struct in_addr); } else if(type == SCAMPER_FD_TYPE_UDP6) { fd = scamper_udp6_open(addr, sport); len = sizeof(struct in6_addr); } else if(type == SCAMPER_FD_TYPE_UDP4DG) { fd = scamper_udp4_opendgram(addr, sport); len = sizeof(struct in_addr); } if(fd == -1 || (fdn = fd_alloc(type, fd)) == NULL || (addr != NULL && (fdn->fd_udp_addr = memdup(addr, len)) == NULL)) { printerror(errno, strerror, __func__, "could not open socket"); goto err; } fdn->fd_udp_sport = sport; if((fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL) { printerror(errno, strerror, __func__, "could not add socket to tree"); goto err; } if((fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL) { printerror(errno, strerror, __func__, "could not add socket to list"); goto err; } return fdn; err: if(fd != -1) { if(type == SCAMPER_FD_TYPE_UDP4 || type == SCAMPER_FD_TYPE_UDP4DG) scamper_udp4_close(fd); else if(type == SCAMPER_FD_TYPE_UDP6) scamper_udp6_close(fd); } if(fdn != NULL) fd_free(fdn); return NULL; }