ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count) { size_t total=0; struct sf_parms hdtrl; /* Set up the header/trailer struct params. */ if (header) { hdtrl.header_data = header->data; hdtrl.header_length = header->length; } else { hdtrl.header_data = NULL; hdtrl.header_length = 0; } hdtrl.trailer_data = NULL; hdtrl.trailer_length = 0; hdtrl.file_descriptor = fromfd; hdtrl.file_offset = offset; hdtrl.file_bytes = count; while ( hdtrl.file_bytes + hdtrl.header_length ) { ssize_t ret; /* Return Value There are three possible return values from send_file: Value Description -1 an error has occurred, errno contains the error code. 0 the command has completed successfully. 1 the command was completed partially, some data has been transmitted but the command has to return for some reason, for example, the command was interrupted by signals. */ do { ret = send_file(&tofd, &hdtrl, 0); } while ( (ret == 1) || (ret == -1 && errno == EINTR) ); if ( ret == -1 ) return -1; } return count + header->length; }
/* * will be called for each file * no return -- errors will be sent by subsequent functions */ int respond(const int fd, const char* file) { resp_hdrs hdrs; init_resp_headers(&hdrs); int file_fd; off_t file_size; if (file == NULL) { fprintf(stderr, "file not specified\n"); hdrs.status_code = 404; } else { file_fd = f_open(file); if (file_fd == -1) { fprintf(stderr, "file not found\n"); hdrs.status_code = 404; } else { hdrs.status_code = 200; } file_size = f_size(file_fd); if (file_size == -1) { fprintf(stderr, "file size error\n"); hdrs.status_code = 404; } } if (file != NULL) { hdrs.content_length = file_size; hdrs.last_modified = f_last_mod(file_fd); send_headers(fd, &hdrs); } if (hdrs.status_code != 404) { send_file(fd, file_fd, file_size); } else { char* msg404 = "<html><head><title>404 Not Found</head></title><body><p>404 Not " "Found: The requested resource could not be found!</p></body></html>"; send_msg(fd, msg404); } return 0; }
void *child_thread(void* pf) { pfact p = (pfact)pf; int rfd; while(1) { pthread_mutex_lock(&p->que.mutex); if(p->que.size == 0) { pthread_cond_wait(&p->cond,&p->que.mutex); } que_deque(&p->que,&rfd); pthread_mutex_unlock(&p->que.mutex); send_file(rfd); printf("%d send successful!\n",rfd); } }
/* Generates and sends an error document */ void send_errorpage(request *r) { char *page = NULL; size_t len = 0; int fildes; char file[decimal_length(int) + 5 + 1];/* an int, .html, \0 */ /* Look for a ready-made error document */ snprintf(file, 9, "%d.html", r->status); if((fildes = open(file, O_RDONLY)) != -1) {/* file exists and is readable */ close(fildes); /* set up the request structure */ free(r->file); r->file = strdup(file); file_stuff(r); send_file(r); return; } /* Generate page */ add_text(&page, &len, "<html><head><title>%d %s</title></head>\n", r->status, status_reason[r->status]); add_text(&page, &len, "<body><h1>%d %s</h1>\n", r->status, status_reason[r->status]); if(page_text[r->status]) add_text(&page, &len, page_text[r->status]); else add_text(&page, &len, "See " "<a href=\"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html\">" "this</a> for more information."); add_text(&page, &len, "\n</body></html>\n"); /* And now send the page */ free(r->content_type); r->content_type = strdup("text/html"); r->encoding = IDENTITY; r->content_length = len; send_headers(r); send_str(r->fd, "\r\n"); if(r->meth != HEAD) send(r->fd, page, len, 0); free(page); }
void* send_func(void*p) { int fd = (int)p; int end = 0xffffffff; int i; pthread_t id = pthread_self(); printf("ready send %d\n", (int)id); for(i=0; i<max_files; i++) { send_file(fd, files[i]); } send(fd, &end, sizeof(end), 0); printf("end send %d\n", (int)id); close(fd); }
/** send command and display result */ static int go_cmd(SSL* ssl, int quiet, int argc, char* argv[]) { char pre[10]; const char* space=" "; const char* newline="\n"; int was_error = 0, first_line = 1; int r, i; char buf[1024]; snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION); if(SSL_write(ssl, pre, (int)strlen(pre)) <= 0) ssl_err("could not SSL_write"); for(i=0; i<argc; i++) { if(SSL_write(ssl, space, (int)strlen(space)) <= 0) ssl_err("could not SSL_write"); if(SSL_write(ssl, argv[i], (int)strlen(argv[i])) <= 0) ssl_err("could not SSL_write"); } if(SSL_write(ssl, newline, (int)strlen(newline)) <= 0) ssl_err("could not SSL_write"); if(argc == 1 && strcmp(argv[0], "load_cache") == 0) { send_file(ssl, stdin, buf, sizeof(buf)); } while(1) { ERR_clear_error(); if((r = SSL_read(ssl, buf, (int)sizeof(buf)-1)) <= 0) { if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) { /* EOF */ break; } ssl_err("could not SSL_read"); } buf[r] = 0; if(first_line && strncmp(buf, "error", 5) == 0) { printf("%s", buf); was_error = 1; } else if (!quiet) printf("%s", buf); first_line = 0; } return was_error; }
static int found_regular_file(struct asfd *asfd, FF_PKT *ff_pkt, struct conf *conf, char *fname, bool top_level) { boffset_t sizeleft; sizeleft=ff_pkt->statp.st_size; // If the user specified a minimum or maximum file size, obey it. if(conf->min_file_size && sizeleft<(boffset_t)conf->min_file_size) return 0; if(conf->max_file_size && sizeleft>(boffset_t)conf->max_file_size) return 0; ff_pkt->type=FT_REG; return send_file(asfd, ff_pkt, top_level, conf); }
void* handle_request(void* arg) { pfactory_t pf=(pfactory_t)arg; pque_t pq=&pf->s_que; pnode_t pget; while(1) { pthread_mutex_lock(&pq->q_mutex); if(pq->size == 0) { pthread_cond_wait(&pf->cond,&pq->q_mutex); } que_get(pq,&pget); pthread_mutex_unlock(&pq->q_mutex); send_file(pget->sockfd); free(pget); } }
int port_handler(int work_sockfd, int listenfd, int port, char* fileName, int retr_stor) { int connfd; char buffer[8192]; int recv_len, write_len; FILE *fp; if (retr_stor == 0) { char *tmp_str = waiting_for(work_sockfd, ""); char num[256]; strcpy(num, split(tmp_str, " ", 2)[0]); if (strcmp(num, "150") != 0) { close(listenfd); return 1; } } if ((connfd = accept(listenfd, NULL, NULL)) == -1) { printf("Error accept(): %s(%d)\n", strerror(errno), errno); waiting_for(work_sockfd, ""); close(listenfd); return 1; } if (retr_stor == 0) { if (recv_file(work_sockfd, connfd, fileName) == 1) { close(connfd); close(listenfd); return 1; } } else { if (send_file(work_sockfd, connfd, fileName) == 1) { close(connfd); close(listenfd); return 1; } } close(connfd); close(listenfd); return STATUS_OK; }
void* thread_handle(void* arg)//子线程函数流程,先初始化,再启动,一般这两个是分开的 { while(1)//让每个线程可以启动 { pfactory pf=(pfactory)arg; printf("the start_flag is %d\n",pf->start_flag); pque_t pq=&pf->fd_que; pnode pcur;//因为中间没有对pcur进行判断,所以不用先初始化为null if(factory_que_empty(pq))//子线程判断队列是否为空,如果为空,则不去get。为空返回1,不为空返回0 { pthread_cond_wait(&pf->cond,&pq->mutex);//wait之前得先加锁,wait时解锁,wait后又加锁, //为空等待对应的条件变量 pthread_mutex_unlock(&pq->mutex); } factory_que_get(pq,&pcur);//等待结束又可以get send_file(pcur->new_fd);//将描述符发过去就可以向客户端发文件了 free(pcur);//发过去之后将链表中该节点free掉,因为空间是在主线程中申请的,所以要在主线程中free } }
int main(int argc, char *argv[]) { char *pname; char *host; char *port; char *AEtitle; char **file_list; int ifile, num_files; Acr_File *afpin, *afpout; /* Check arguments */ pname = argv[0]; if (argc < 5) { (void) fprintf(stderr, "Usage: %s host port AEtitle files ...\n", pname); return EXIT_FAILURE; } host = argv[1]; port = argv[2]; AEtitle = argv[3]; file_list = &argv[4]; num_files = argc - 4; /* Make dicom connection */ if (!acr_open_dicom_connection(host, port, AEtitle, "test", ACR_MR_IMAGE_STORAGE_UID, ACR_IMPLICIT_VR_LITTLE_END_UID, &afpin, &afpout)) { return EXIT_FAILURE; } /* Loop over the input files, sending them one at a time */ for (ifile = 0; ifile < num_files; ifile++) { (void) printf("Sending file %s\n", file_list[ifile]); if (!send_file(afpin, afpout, file_list[ifile])) { break; } } /* Release the association */ acr_close_dicom_connection(afpin, afpout); return EXIT_SUCCESS; }
static errval_t do_send_file(struct in_addr *addr, int port, char *path) { assert(addr != NULL); assert(path != NULL); errval_t err; debug_printf("send file %s to %s:%d\n", path, inet_ntoa(*addr), port); static struct ip_addr ip; ip.addr = addr->s_addr; // XXX // debug_printf("ready to connect\n"); struct tcp_pcb *pcb; pcb = connect(&ip, port); if (pcb == NULL) { assert(pcb != NULL); // return SOME_ERR; } // debug_printf("connected\n"); // debug_printf("start sending.\n"); wait_cond = true; err = send_file(pcb, path); if (err_is_fail(err)) { return err; } wait_for_condition(); debug_printf("send finished.\n"); // debug_printf("closing connection.\n"); // close_connection(pcb); debug_printf("connection closed.\n"); return SYS_ERR_OK; }
int file_mode(){ printf("[file_mode]> "); memset(inputbuf, 0, INPUT_BUFSIZE); setbuf(stdin, NULL); fgets(inputbuf, INPUT_BUFSIZE, stdin); setbuf(stdin, NULL); char *friend_name; char *trans_location; friend_name = strtok(inputbuf, ":"); trans_location = strtok(NULL, "\n"); if (friend_name != NULL && trans_location != NULL) { send_file(friend_name, trans_location); return TRUE; } printf("%s", "file mode usage\t<$name><:><$location>\n"); return FALSE; }
void my_put(int sock, char buff[1024], char *path) { char **args; int i; i = 0; args = totab(buff, ' '); if (getSize(args) == 2) { while (args[1][i] != '\0') { if (args[1][i] == '\n') args[1][i] = '\0'; i++; } send_file(sock, buff, args[1], path); } else send_error(sock, "Usage : get <file>"); }
int my_get(t_cli **cli) { char **args; int i; i = 0; args = totab((*cli)->buff, ' '); if (getSize(args) == 2) { while (args[1][i] != '\0') { if (args[1][i] == '\n') args[1][i] = '\0'; i++; } return (send_file(cli, args[1])); } else return (send_error((*cli)->cs, "Usage : get <file>")); }
static err_t ftpd_datasent(void *arg, struct tcp_pcb *pcb, u16_t len) { struct ftpd_datastate *fsd = arg; switch (fsd->msgfs->state) { case FTPD_LIST: send_next_directory(fsd, pcb, 0); break; case FTPD_NLST: send_next_directory(fsd, pcb, 1); break; case FTPD_RETR: send_file(fsd, pcb); break; default: break; } return ERR_OK; }
int execute(int client_fd, char *filename) { int fd; strncpy(p,filename,MAXFILENAME-strlen(buffer2)-1); char *args[2] = {buffer2,NULL}; char *tmp; if( (fd = open(buffer2, O_RDONLY)) < 0 ) { send_status(client_fd, NOT_FOUND); send_file(client_fd,"404.html",1); return; } tmp = get_direc(buffer2); chdir(buffer2); *tmp = '/'; send_status(client_fd,OK); dup2(client_fd,STDOUT_FILENO); close(client_fd); execvp(args[0],args); return 0; }
// Last checks before actually processing the file system entry. int send_file_w(struct asfd *asfd, FF_PKT *ff, bool top_level, struct conf **confs) { if(!file_is_included(confs, ff->fname, top_level)) return 0; // Doing the file size match here also catches hard links. if(S_ISREG(ff->statp.st_mode) && !file_size_match(ff, confs)) return 0; /* * Handle hard linked files * Maintain a list of hard linked files already backed up. This * allows us to ensure that the data of each file gets backed * up only once. */ if(ff->statp.st_nlink > 1 && (S_ISREG(ff->statp.st_mode) || S_ISCHR(ff->statp.st_mode) || S_ISBLK(ff->statp.st_mode) || S_ISFIFO(ff->statp.st_mode) || S_ISSOCK(ff->statp.st_mode))) { struct f_link *lp; struct f_link **bucket=NULL; if((lp=linkhash_search(&ff->statp, &bucket))) { if(!strcmp(lp->name, ff->fname)) return 0; ff->link=lp->name; /* Handle link, file already saved */ ff->type=FT_LNK_H; } else { if(linkhash_add(ff->fname, &ff->statp, bucket)) return -1; } } return send_file(asfd, ff, confs); }
// the operation of GET file command void command_get(struct packet* shp, struct packet* data, int sfd_client, char* lpwd) { int x; FILE* f = fopen(shp->buffer, "rb"); shp->type = INFO; shp->comid = GET; strcpy(shp->buffer, f ? "OK: File found; processing..." : "ER: Error opening file."); printpacket(shp, HP); data = htonp(shp); if(( x = send(sfd_client, data, size_packet, 0)) != size_packet) er("send()", x); if(f) { shp->type = DATA; //send_file(shp, data, sfd_client, f); send_file(shp, data, sfd_server_data, f); fclose(f); } send_EOT(shp, data, sfd_server_data); //send_EOT(shp, data, sfd_client); }
void *thrd_main(void *arg) { //int len = ((struct tftpd_t *)arg)->req_len; TFTP_REQ req = ((struct tftpd_t *)arg)->req; struct sockaddr_in peer = ((struct tftpd_t *)arg)->peer; u_int16_t opcode = ntohs(*(u_int16_t*)&req); free(arg); char fname[256]; switch(opcode) { case TFTP_READ: sprintf(fname, "%s/%s", tftp_root, req.arg); send_file(fname, &peer); break; case TFTP_WRITE: break; } return NULL; }
void get_file(t_ftp *ftp, char **args) { int fd; if (args[1] == NULL) { send_message("Get command needs an argument\n", ftp); printf("Get command needs an argument\n"); } else if ((fd = open_fd(ftp, args[1])) != -1) { send_message("...", ftp); printf("Transfer starting...\n"); send_file(ftp, fd); } else { send_message("Fail\n", ftp); printf("Transfer failed\n"); } }
static bool upload(const char* addr, const char* filepath) { acl::socket_stream conn; if (conn.open(addr, 30, 30) == false) { printf("connect %s error %s\r\n", addr, acl::last_serror()); return false; } if (handshake(conn) == false) return false; acl::websocket ws(conn); if (send_file(ws, filepath) == false) return false; if (read_reply(ws) == false) return false; return true; }
int interactive(usb_dev_handle* device) { int len; char* commandBuffer = NULL; do { do { usleep(100000); len = get_response(device); if(len > 0) { fwrite(response_buffer, 1, len, stdout); fflush(stdout); } } while(len > 0 && strnstr(response_buffer, "] ", len) == NULL); if(len < 0) break; ProcessCommand: if(commandBuffer != NULL) free(commandBuffer); commandBuffer = readline(NULL); if(commandBuffer && *commandBuffer) { add_history(commandBuffer); } if(strncmp(commandBuffer, "sendfile ", sizeof("sendfile ") - 1) == 0) { char* file = commandBuffer + sizeof("sendfile ") - 1; send_file(device, file); goto ProcessCommand; } else { send_command(device, commandBuffer); send_command(device, "\n"); } } while(1); return 0; }
int UploadFile(int server_fd,char* filename) { char response_type[20]; char response_argu[20]; int server_recv_fd; FILE* file_to_send; if(((file_to_send = fopen(filename,"r")) == NULL)){ printf("the file you want to upload can not open \n"); return -1; } memset(buffer,'\0',BUF_SIZE); //clear the buffer before use sprintf(buffer,"request;uploadfile;%s;%s|",current_directory,filename); if(send(server_fd,buffer,strlen(buffer),0)==-1) printf("GetConnection send package error! \n"); memset(buffer,'\0',BUF_SIZE); //clear the buffer before use if(recv(server_fd,buffer,BUF_SIZE,0) > 0){ response_analysis(buffer,response_type,response_argu); if(strncmp(response_type,"ack",3) == 0){ printf("now begin to upload\n"); server_recv_fd = get_data_connection(); //get data channel connection to server send_file(server_recv_fd,file_to_send); fclose(file_to_send); close(server_recv_fd); //close data channel connecion to server return 1; } else{ printf("the file existed \n"); return 0; } } else{ printf("server has been shutdown\n"); return -1; } }
int server_response(int fd) { int len; char *resp; len = 0; while (!len && ioctl(fd, FIONREAD, &len) >= 0) usleep(3000); if ((resp = malloc(sizeof(char) * (len + 1))) == NULL) { printf("Unable to malloc response\n"); return (-1); } len = read(fd, resp, len); resp[len] = '\0'; printf("%s", resp); if (strncmp(resp, "777 : ", 6) == 0) receive_file(fd, &resp[6]); else if (strncmp(resp, "778 : ", 6) == 0) send_file(fd, &resp[6]); free(resp); return (0); }
int main(int argc, char** argv) { int listenfd; int connfd; FILE* fp; set_options(argc, argv); /* Open the file early, so we may fail early. * To note that if no file is specified, stdin will be read instead */ if (strcmp(g_filename, "")) { if ((fp = fopen(g_filename, "r")) == NULL) { printerr("File not found"); return 1; } } else { fp = stdin; } /* Start listening */ listenfd = startup_server(g_port); if (listenfd < 0) return abs(listenfd); /* Start waiting for connections */ connfd = waitconn(listenfd); if (connfd < 0) return abs(connfd); /* Send stuff */ send_headers(connfd); send_file(connfd, fp); /* Cleanup */ fclose(fp); close(connfd); close(listenfd); return 0; }
int main(int argc, char** argv) { int fd, part_size, file_id; int k = 5; struct file_info* file_info; printf("%10d\n", k); sleep(2); if(argc!=4) { usage(argv[0]); return EXIT_FAILURE; } if(sethandler(SIG_IGN,SIGPIPE)) ERR("Seting SIGPIPE:"); if(sethandler(sigint_handler,SIGINT)) ERR("Seting SIGINT:"); fd = connect_socket(argv[1], atoi(argv[2])); send_message(fd, "UPL", ""); part_size = wait_for_system_readiness(fd); file_info = get_file_info(fd, argv[3], part_size); if(( file_id = send_file_info(fd, argv[3], part_size, file_info)) < 0) ERR("write"); file_info -> file_id = file_id; printf("File ID: %d\n\n\n\\n\n\n\n\n\n\n\\n\n\n\n\n\n\\n\n\n\n\n", file_info -> file_id); send_file(fd, argv[3], file_info, part_size); printf("File ID: %d\n\n\n\\n\n\n\n\n\n\n\\n\n\n\n\n\n\\n\n\n\n\n", file_info -> file_id); if(TEMP_FAILURE_RETRY(close(fd))<0) ERR("close"); free(file_info); return 0; }
int process_LIST(struct session* ses, const char* data) { if (open_data_connection(ses) != -1) { // listing directory via `ls` command piped to temporary file char cmd[2*MAX_PATH]; snprintf (cmd, 2*MAX_PATH, "ls %s \"%s\" | tail -n+2 >%s", LIST_LS_PARAMS, ses->current_dir, LIST_LS_OUTFILE); if (system(cmd) != -1) { respond (ses, 150, "Here comes the directory listing."); if (send_file(ses, LIST_LS_OUTFILE) != -1) { respond (ses, 226, "Directory send OK."); close_data_connection (ses); unlink (LIST_LS_OUTFILE); return 0; } } } respond (ses, 550, "Directory listing failed."); return 0; }
static void handle_request(int net_cmd_sock) { while(1) { int net_data_sock; char cmd[CMD_LEN]; char buf[128]; char *p; int n = 0; n = recv(net_cmd_sock, cmd, 3, MSG_WAITALL); cmd[3] = '\0'; printf("rec %d byte, msg: %s\n", n, cmd); if (n > 0) { if (0 == strcmp(cmd, "PRT")) { n = read(net_cmd_sock, buf, 128); //不用recv ,继续read buf[n] = '\0'; char *port = buf + 1; //去除1个空格 ack_succ(net_cmd_sock); net_data_sock = connect_data_channel(atoi(port)); } if (0 == strcmp(cmd, "BYE")) { close_svr(net_cmd_sock, net_data_sock); } if (0 == strcmp(cmd, "DIR")) { send_dir_info(net_cmd_sock, net_data_sock); } if (0 == strcmp(cmd, "GET")) { n = read(net_cmd_sock, buf, 128); //不用recv ,继续read buf[n] = '\0'; char *filename = buf + 1; //去除1个空格 p = filename; send_file(filename, net_data_sock, net_cmd_sock); } } else { break; } } }
int explain(char *buf,int acc_sock) { char text[BUFSIZ]; char aaa[BUFSIZ]; char type[BUFSIZ]; struct stat info; int length; char *tmp; char *inputstring; sscanf(buf,"%s%s",aaa,text); //取得HTTP头前两个字符串 if(strcmp(text,"/") == 0){ strcpy(text,"/index.html"); } if(strcmp(aaa,"GET") == 0){ //GET方法 if((tmp = strchr(text,'?')) != NULL){ length = strlen(text) - strlen(tmp); //对GET方法请求的参数进行解析 strncpy(text,text,length); text[length] = '\0'; } if(lstat(text+1,&info)== -1){ char *head = "HTTP/1.1 404 NOT FOUND\r\n\r\n"; //请求的文件不存在 send(acc_sock,head,strlen(head),0); } send_file(text+1,acc_sock); }else if(strcmp(aaa,"POST") == 0){ //POST方法 bzero(type,sizeof(type)); seperate(buf,type); //对POST方法请求的参数进行解析 send_file2(text+1,acc_sock,type); } }