int main( int argc, char **argv ) { Connection conn; conn.fd = open( argv[1], O_RDONLY ); conn.used = 0; conn.buf_ptr = 0; conn.state = CONN_BEGIN; conn.ipp = ipp_new(); conn.http = calloc(1, sizeof( HTTP ) ); process_conn( &conn ); }
int main() { int sock = socket(AF_INET,SOCK_STREAM,0); if(sock==-1) { err("socket"); } struct sockaddr_in listen_addr; bzero(&listen_addr,sizeof(listen_addr)); listen_addr.sin_family = AF_INET; listen_addr.sin_port = htons(PORT); listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); socklen_t len = sizeof(listen_addr); if(bind(sock,(const struct sockaddr*)&listen_addr, len)==-1) { err("bind"); } if(listen(sock,BACKLOG)==-1) { err("listen"); } for(;;) { struct sockaddr_in client_addr; socklen_t len = sizeof(client_addr); int cli_sock; if((cli_sock = accept(sock,(struct sockaddr*)&client_addr,&len))==-1) { err("accept"); } else { char* str_addr = inet_ntoa(client_addr.sin_addr); printf("Accepted a client:%s:%d\n",str_addr,client_addr.sin_port); process_conn(cli_sock); } } }
int connect_ep(char *hostname) { DAT_IA_ADDRESS_PTR remote_addr = (DAT_IA_ADDRESS_PTR)&remote; DAT_EP_ATTR ep_attr; DAT_IA_ATTR ia_attr; DAT_RETURN status; DAT_REGION_DESCRIPTION region; DAT_EVENT event; DAT_COUNT nmore; DAT_LMR_TRIPLET iov; DAT_RMR_TRIPLET *r_iov; DAT_DTO_COOKIE cookie; DAT_CONN_QUAL conn_qual; DAT_BOOLEAN in, out; int i, ii, pdata, ctx; DAT_PROVIDER_ATTR prov_attrs; DAT_DTO_COMPLETION_EVENT_DATA *dto_event = &event.event_data.dto_completion_event_data; status = dat_ia_open(provider, 8, &async_evd, &ia); _OK(status, "dat_ia_open"); memset(&prov_attrs, 0, sizeof(prov_attrs)); status = dat_ia_query(ia, NULL, DAT_IA_FIELD_ALL, &ia_attr, DAT_PROVIDER_FIELD_ALL, &prov_attrs); _OK(status, "dat_ia_query"); print_ia_address(ia_attr.ia_address_ptr); if (ucm && ud_test) { printf("%d UD test over UCM provider not supported\n", getpid()); exit(1); } /* Print provider specific attributes */ for (i = 0; i < prov_attrs.num_provider_specific_attr; i++) { LOGPRINTF(" Provider Specific Attribute[%d] %s=%s\n", i, prov_attrs.provider_specific_attr[i].name, prov_attrs.provider_specific_attr[i].value); /* check for counter support */ status = strcmp(prov_attrs.provider_specific_attr[i].name, "DAT_COUNTERS"); if (!status) counters_ok = 1; } /* make sure provider supports counters */ if ((counters) && (!counters_ok)) { printf("Disable dat_query_counters:" " Provider not built with counters\n"); counters = 0; } status = dat_pz_create(ia, &pz); _OK(status, "dat_pz_create"); status = dat_evd_create(ia, eps * 2, DAT_HANDLE_NULL, DAT_EVD_CR_FLAG, &cr_evd); _OK(status, "dat_evd_create CR"); status = dat_evd_create(ia, eps * 2, DAT_HANDLE_NULL, DAT_EVD_CONNECTION_FLAG, &con_evd); _OK(status, "dat_evd_create CR"); status = dat_evd_create(ia, eps * 10, DAT_HANDLE_NULL, DAT_EVD_DTO_FLAG, &dto_evd); _OK(status, "dat_evd_create DTO"); memset(&ep_attr, 0, sizeof(ep_attr)); if (ud_test) { msg_size += 40; ep_attr.service_type = DAT_IB_SERVICE_TYPE_UD; ep_attr.max_message_size = buf_size; ep_attr.max_rdma_read_in = 0; ep_attr.max_rdma_read_out = 0; } else { ep_attr.service_type = DAT_SERVICE_TYPE_RC; ep_attr.max_rdma_size = 0x10000; ep_attr.max_rdma_read_in = 4; ep_attr.max_rdma_read_out = 4; } ep_attr.qos = 0; ep_attr.recv_completion_flags = 0; ep_attr.max_recv_dtos = eps * 10; ep_attr.max_request_dtos = eps * 10; ep_attr.max_recv_iov = 1; ep_attr.max_request_iov = 1; ep_attr.request_completion_flags = DAT_COMPLETION_DEFAULT_FLAG; ep_attr.ep_transport_specific_count = 0; ep_attr.ep_transport_specific = NULL; ep_attr.ep_provider_specific_count = 0; ep_attr.ep_provider_specific = NULL; for (i = 0; i < eps; i++) { status = dat_ep_create(ia, pz, dto_evd, dto_evd, con_evd, &ep_attr, &ep[i]); _OK(status, "dat_ep_create"); LOGPRINTF(" create_ep[%d]=%p\n", i, ep[i]); } for (i = 0; i < REG_MEM_COUNT * eps; i++) { buf[i] = (DAT_RMR_TRIPLET *) malloc(buf_size); region.for_va = buf[i]; status = dat_lmr_create(ia, DAT_MEM_TYPE_VIRTUAL, region, buf_size, pz, DAT_MEM_PRIV_ALL_FLAG | DAT_IB_MEM_PRIV_REMOTE_ATOMIC, DAT_VA_TYPE_VA, &lmr[i], &lmr_context[i], &rmr_context[i], ®_size[i], ®_addr[i]); _OK(status, "dat_lmr_create"); } /* register atomic return buffer for original data */ atomic_buf = (DAT_UINT64 *) malloc(BUF_SIZE_ATOMIC); region.for_va = atomic_buf; status = dat_lmr_create(ia, DAT_MEM_TYPE_VIRTUAL, region, BUF_SIZE_ATOMIC, pz, DAT_MEM_PRIV_ALL_FLAG | DAT_IB_MEM_PRIV_REMOTE_ATOMIC, DAT_VA_TYPE_VA, &lmr_atomic, &lmr_atomic_context, &rmr_atomic_context, ®_atomic_size, ®_atomic_addr); _OK(status, "dat_lmr_create atomic"); for (ii = 0; ii < eps; ii++) { for (i = RECV_BUF_INDEX; i < REG_MEM_COUNT; i++) { int ep_idx = 0; cookie.as_64 = (ii * REG_MEM_COUNT) + i; iov.lmr_context = lmr_context[(ii * REG_MEM_COUNT) + i]; iov.virtual_address = (DAT_VADDR) (uintptr_t) buf[(ii * REG_MEM_COUNT) + i]; iov.segment_length = buf_size; LOGPRINTF(" post_recv (%p) on ep[%d]=%p\n", buf[(ii * REG_MEM_COUNT) + i], ii, ep[ii]); /* ep[0], unless testing Server and multi EP's */ if (server && multi_eps) { ep_idx = ii; cookie.as_64 = i; } status = dat_ep_post_recv(ep[ep_idx], 1, &iov, cookie, DAT_COMPLETION_DEFAULT_FLAG); _OK(status, "dat_ep_post_recv"); } } /* setup receive buffer to initial string to be overwritten */ strcpy((char *)buf[RCV_RDMA_BUF_INDEX], "blah, blah, blah\n"); /* ud can resolve_ah and connect both ways, same EP */ if (server || (!server && ud_test)) { if (server) { conn_qual = SERVER_ID; strcpy((char *)buf[SND_RDMA_BUF_INDEX], "Server data"); } else { conn_qual = CLIENT_ID; strcpy((char *)buf[SND_RDMA_BUF_INDEX], "Client data"); } status = dat_psp_create(ia, conn_qual, cr_evd, DAT_PSP_CONSUMER_FLAG, &psp); _OK(status, "dat_psp_create"); /* Server always waits for first CR from Client */ if (server) process_cr(0); } /* ud can resolve_ah and connect both ways */ if (!server || (server && ud_test)) { struct addrinfo *target; if (ucm) goto no_resolution; if (getaddrinfo(hostname, NULL, NULL, &target) != 0) { printf("Error getting remote address.\n"); exit(1); } printf("Remote %s Name: %s \n", server ? "Client" : "Server", hostname); printf("Remote %s Net Address: %s\n", server ? "Client" : "Server", inet_ntoa(((struct sockaddr_in *) target->ai_addr)->sin_addr)); strcpy((char *)buf[SND_RDMA_BUF_INDEX], "Client written data"); remote_addr = (DAT_IA_ADDRESS_PTR)target->ai_addr; /* IP */ no_resolution: /* one Client EP, multiple Server EPs, same conn_qual * use private data to select EP on Server */ for (i = 0; i < eps; i++) { /* pdata selects Server EP, * support both muliple Server and single EP's */ if (multi_eps) pdata = hton32(i); else pdata = 0; /* just use first EP */ status = dat_ep_connect(ep[0], remote_addr, (server ? CLIENT_ID : SERVER_ID), CONN_TIMEOUT, 4, (DAT_PVOID) & pdata, 0, DAT_CONNECT_DEFAULT_FLAG); _OK(status, "dat_ep_connect"); } if (!ucm) freeaddrinfo(target); } /* UD: process CR's starting with 2nd on server, 1st for client */ if (ud_test) { for (i = (server ? 1 : 0); i < eps; i++) process_cr(i); } /* RC and UD: process CONN EST events */ for (i = 0; i < eps; i++) process_conn(i); /* UD: CONN EST events for CONN's and CR's */ if (ud_test) { for (i = 0; i < eps; i++) process_conn(i); } printf("Connected! %d endpoints\n", eps); /* * Setup our remote memory and tell the other side about it * Swap to network order. */ r_iov = (DAT_RMR_TRIPLET *) buf[SEND_BUF_INDEX]; r_iov->rmr_context = hton32(rmr_context[RCV_RDMA_BUF_INDEX]); r_iov->virtual_address = hton64((DAT_VADDR) (uintptr_t) buf[RCV_RDMA_BUF_INDEX]); r_iov->segment_length = hton32(buf_size); printf("Send RMR message: r_key_ctx=0x%x,va=" F64x ",len=0x%x\n", hton32(r_iov->rmr_context), hton64(r_iov->virtual_address), hton32(r_iov->segment_length)); send_msg(buf[SEND_BUF_INDEX], sizeof(DAT_RMR_TRIPLET), lmr_context[SEND_BUF_INDEX], cookie, DAT_COMPLETION_SUPPRESS_FLAG); dat_ep_get_status(ep[0], NULL, &in, &out); printf("EP[0] status: posted buffers: Req=%d, Rcv=%d\n", in, out); /* * Wait for their RMR */ for (i = 0, ctx = 0; i < eps; i++, ctx++) { /* expected cookie, recv buf idx in every mem pool */ ctx = (ctx % REG_MEM_COUNT) ? ctx : ctx + RECV_BUF_INDEX; LOGPRINTF("Waiting for remote to send RMR data\n"); status = dat_evd_wait(dto_evd, DTO_TIMEOUT, 1, &event, &nmore); _OK(status, "dat_evd_wait after dat_ep_post_send"); if ((event.event_number != DAT_DTO_COMPLETION_EVENT) && (ud_test && event.event_number != DAT_IB_DTO_EVENT)) { printf("unexpected event waiting for RMR context " "- 0x%x\n", event.event_number); exit(1); } _OK(dto_event->status, "event status for post_recv"); /* careful when checking cookies: * Client - receiving multi messages on a single EP * Server - not receiving on multiple EP's */ if (!server || (server && !multi_eps)) { if (dto_event->transfered_length != msg_size || dto_event->user_cookie.as_64 != ctx) { printf("unexpected event data on recv: len=%d" " cookie=" F64x " expected %d/%d\n", (int)dto_event->transfered_length, dto_event->user_cookie.as_64, msg_size, ctx); exit(1); } /* Server - receiving one message each across many EP's */ } else { if (dto_event->transfered_length != msg_size || dto_event->user_cookie.as_64 != RECV_BUF_INDEX) { printf("unexpected event data on recv: len=%d" "cookie=" F64x " expected %d/%d\n", (int)dto_event->transfered_length, dto_event->user_cookie.as_64, msg_size, RECV_BUF_INDEX); exit(1); } } /* swap RMR,address info to host order */ if (!server || (server && !multi_eps)) r_iov = (DAT_RMR_TRIPLET *) buf[ctx]; else r_iov = (DAT_RMR_TRIPLET *) buf[(i * REG_MEM_COUNT) + RECV_BUF_INDEX]; if (ud_test) r_iov = (DAT_RMR_TRIPLET *) ((char *)r_iov + 40); r_iov->rmr_context = ntoh32(r_iov->rmr_context); r_iov->virtual_address = ntoh64(r_iov->virtual_address); r_iov->segment_length = ntoh32(r_iov->segment_length); printf("Recv RMR message: r_iov(%p):" " r_key_ctx=%x,va=" F64x ",len=0x%x on EP=%p\n", r_iov, r_iov->rmr_context, r_iov->virtual_address, r_iov->segment_length, dto_event->ep_handle); } return (0); }
static void run_loop(struct sigma_dut *dut) { struct sigma_conn conn[MAX_CONNECTIONS]; int i, res, maxfd, can_accept; fd_set rfds; for (i = 0; i < MAX_CONNECTIONS; i++) conn[i].s = -1; for (;;) { FD_ZERO(&rfds); maxfd = -1; can_accept = 0; for (i = 0; i < MAX_CONNECTIONS; i++) { if (conn[i].s >= 0) { FD_SET(conn[i].s, &rfds); if (conn[i].s > maxfd) maxfd = conn[i].s; } else can_accept = 1; } if (can_accept) { FD_SET(dut->s, &rfds); if (dut->s > maxfd) maxfd = dut->s; } sigma_dut_print( DUT_MSG_DEBUG, "Waiting for next " "command (can_accept=%d)", can_accept); res = select(maxfd + 1, &rfds, NULL, NULL, NULL); if (res < 0) { perror("select"); sleep(1); continue; } if (!res) { sigma_dut_print( DUT_MSG_DEBUG, "Nothing ready"); sleep(1); continue; } if (FD_ISSET(dut->s, &rfds)) { for (i = 0; i < MAX_CONNECTIONS; i++) { if (conn[i].s < 0) break; } conn[i].addrlen = sizeof(conn[i].addr); conn[i].s = accept(dut->s, (struct sockaddr *) &conn[i].addr, &conn[i].addrlen); if (conn[i].s < 0) { sigma_dut_print( DUT_MSG_INFO, "accept: %s", strerror(errno)); continue; } sigma_dut_print( DUT_MSG_DEBUG, "Connection %d from %s:%d", i, inet_ntoa(conn[i].addr.sin_addr), ntohs(conn[i].addr.sin_port)); conn[i].pos = 0; } for (i = 0; i < MAX_CONNECTIONS; i++) { //sigma_dut_print( DUT_MSG_DEBUG, "%s : idx : %d, s : %d\n", __func__, i, conn[i].s); if (conn[i].s < 0) continue; if (FD_ISSET(conn[i].s, &rfds)) #if 0 // by bbelief process_conn(dut, &conn[i]); #else process_conn2(dut, &conn[i]); #endif //sigma_dut_print( DUT_MSG_DEBUG, "##-7\n"); } //sigma_dut_print( DUT_MSG_DEBUG, "##-8\n"); } }
int main( int argc, char **argv ) { int num_polled = 1; struct pollfd *polls; struct sockaddr_in incoming; int i, n, pos, len; int new_fd; socklen_t addr_len; Connection *conn; IPP *response; signal( SIGUSR1, quit_handler ); // Temp printer for now if( argc < 2 ) { fprintf(stderr, "Missing argument pointing to location of printer definition file.\n" ); exit(1); } if(!init_printers( argv[1] )) { fprintf( stderr, "Unable to load printer definition file %s\n", argv[1] ); exit(1); } new_fd = StartListening(); polls = malloc( sizeof( struct pollfd ) * MAX_CLIENTS); polls[0].fd = new_fd; polls[0].events = POLLIN; for(;;) { // printf("."); for( conn = list_conn( NULL ), num_polled = 1; conn != NULL; conn = list_conn( conn ) ) { if( conn->state == CONN_BEGIN || conn->state == CONN_PRINTING_READ ) { polls[num_polled].fd = conn->fd; polls[num_polled++].events = POLLIN; } else if( conn->state == CONN_OUTPUT ) { polls[num_polled].fd = conn->fd; polls[num_polled++].events = POLLOUT; } } for( i = 0; i < array_len( printers ); i++ ) { printer = array_get( printers, i ); if( printer->state == PRINTER_PRINTING_WRITE ) { polls[num_polled].fd = printer->fd; polls[num_polled++].events = POLLOUT; } } poll( polls, num_polled, -1 ); // printf("o" ); for( i = 0; i < num_polled; i++ ) { if( polls[i].revents & POLLIN ) { // Read if( i == 0 ) { // Special case.. addr_len = sizeof( struct sockaddr_in ); new_fd = accept( polls[i].fd, (struct sockaddr *)&(incoming), &addr_len ); add_conn( new_fd ); } else { if( ( conn = get_conn( polls[i].fd ) ) ) { process_conn( conn ); } } } else if ( polls[i].revents & POLLNVAL ) { if( ( conn = get_conn( polls[i].fd ) ) ) remove_conn( conn ); } else if ( polls[i].revents & POLLOUT ) { if( ( conn = get_conn( polls[i].fd ) ) ) { if( ( n = write( conn->fd, conn->buffer+conn->buf_ptr, conn->used-conn->buf_ptr ) ) ) { conn->buf_ptr += n; if( conn->buf_ptr == conn->used ) { close( conn->fd ); remove_conn( conn ); } } } else { int e; for( e = 0; e < array_len( printers ); e++ ) { printer = array_get( printers, e ); if( printer->fd == polls[i].fd ) { if( ( n = write( printer->fd, printer->buffer+printer->buf_ptr, printer->used-printer->buf_ptr ) ) == 0 ) { #if 0 // ERROR WRITING TO PRINTER // Simple error support -- close printer fd if( ( conn = get_conn( printer->jobs->job.fd ) ) ) { fprintf( stderr, "Print job error.\n" ); response = ipp_new(); response->response = 0x0504; response->version = 256; // IPP 1.0 response->request_id = conn->ipp->request_id; ipp_add_tag( response, IPP_TAG_OPERATIONS, NULL, NULL, 0, 0 ); ipp_copy_tag( response, conn->ipp, IPP_TAG_OPERATIONS, "attributes-charset" ); ipp_copy_tag( response, conn->ipp, IPP_TAG_OPERATIONS, "attributes-natural-language" ); len = ipp_write( response, NULL, len ); pos = sprintf( conn->buffer, "HTTP/1.1 200 OK\r\nContent-Type: application/ipp\r\nContent-Length: %d\r\n\r\n", len ); ipp_write( response, conn->buffer+pos, len ); conn->used = len + pos; conn->buf_ptr = 0; conn->state = CONN_OUTPUT; ipp_free( response ); } fprintf( stderr, "Marking printer closed.\n" ); close(printer->fd ); printer->state = PRINTER_CLOSED; #endif /* 0 */ } else { printer->buf_ptr += n; if( printer->buf_ptr == printer->used ) { printer->state = PRINTER_PRINTING_WAIT; printer->buf_ptr = printer->used = 0; if( ( conn = get_conn( printer->jobs->job.fd ) ) == NULL ) { printf( "Bad printer state.\n" ); } if( conn->ipp->data_left ) conn->state = CONN_PRINTING_READ; else { int pos, len, val; struct _ipp_jobs *old_job; IPP *response; // Generate a done message response = ipp_new(); response->response = 0x0000; // Success response->request_id = conn->ipp->request_id; response->version = 256; ipp_add_tag( response, IPP_TAG_OPERATIONS, NULL, NULL, 0, 0 ); ipp_copy_tag( response, conn->ipp, IPP_TAG_OPERATIONS, "attributes-charset" ); ipp_copy_tag( response, conn->ipp, IPP_TAG_OPERATIONS, "attributes-natural-language" ); /* 2004/11/8 : Added to make XP printer utility report success status*/ ipp_add_tag( response, IPP_TAG_TEXT_WO_LANG, "status-message", "successful-ok", strlen( "successful-ok" ), 0 ); ipp_add_tag( response, IPP_TAG_JOBS, NULL, NULL, 0, 0 ); val = printer->jobs->job.id; ipp_add_tag( response, IPP_TAG_INTEGERS, "job-id", &val, 4, 0 ); val = 9; ipp_add_tag( response, IPP_TAG_ENUM, "job-state", &val, 4, 0 ); /* 9 = COMPLETED */ // Done len = ipp_write( response, NULL, len ); pos = sprintf( conn->buffer, "HTTP/1.1 200 OK\r\nContent-Type: application/ipp\r\nContent-Length: %d\r\n\r\n", len ); ipp_write( response, conn->buffer + pos, len ); conn->used = len + pos; conn->buf_ptr = 0; conn->state = CONN_OUTPUT; /* 2004/11/8: added to free response */ ipp_free( response ); // The JOB being is printed is ALWAYS the first one old_job = printer->jobs; printer->jobs = old_job->next; free( old_job ); // Free the job printf( "Going to next job...\n" ); if( printer->jobs ) { if( ( conn = get_conn( printer->jobs->job.fd ) ) ) { if( conn->buf_ptr < conn->used ) { memcpy( printer->buffer, conn->buffer + conn->buf_ptr, conn->used - conn->buf_ptr ); printer->used = conn->used - conn->buf_ptr; printer->buf_ptr = 0; printer->state = PRINTER_PRINTING_WRITE; conn->state = CONN_PRINTING_WAIT; conn->ipp->data_left -= conn->used - conn->buf_ptr; } else { if( conn->ipp->data_left ) { printer->state = PRINTER_PRINTING_WAIT; conn->state = CONN_PRINTING_READ; } } } else { printf( "Unable to get connection for FD - %d.\n", printer->jobs->job.fd ); } } else { /* printer->state = PRINTER_OPEN; */ /* 2004/11/5 : put back into closed state so that kernel can remove lp0 when printer unplugged */ close(printer->fd); printer->fd = 0; printer->state = PRINTER_CLOSED; } } } } } } } } } } }
static void run_loop(struct sigma_dut *dut) { struct sigma_conn conn[MAX_CONNECTIONS]; int i, res, maxfd, can_accept; fd_set rfds; memset(&conn, 0, sizeof(conn)); for (i = 0; i < MAX_CONNECTIONS; i++) conn[i].s = -1; #ifdef __linux__ signal(SIGINT, handle_term); signal(SIGTERM, handle_term); signal(SIGPIPE, SIG_IGN); #endif /* __linux__ */ while (!stop_loop) { FD_ZERO(&rfds); maxfd = -1; can_accept = 0; for (i = 0; i < MAX_CONNECTIONS; i++) { if (conn[i].s >= 0) { FD_SET(conn[i].s, &rfds); if (conn[i].s > maxfd) maxfd = conn[i].s; } else if (!conn[i].waiting_completion) can_accept = 1; } if (can_accept) { FD_SET(dut->s, &rfds); if (dut->s > maxfd) maxfd = dut->s; } sigma_dut_print(dut, DUT_MSG_DEBUG, "Waiting for next " "command (can_accept=%d)", can_accept); res = select(maxfd + 1, &rfds, NULL, NULL, NULL); if (res < 0) { perror("select"); if (!stop_loop) sleep(1); continue; } if (!res) { sigma_dut_print(dut, DUT_MSG_DEBUG, "Nothing ready"); sleep(1); continue; } if (FD_ISSET(dut->s, &rfds)) { for (i = 0; i < MAX_CONNECTIONS; i++) { if (conn[i].s < 0 && !conn[i].waiting_completion) break; } if (i == MAX_CONNECTIONS) { /* * This cannot really happen since can_accept * would not be set to one. */ sigma_dut_print(dut, DUT_MSG_DEBUG, "No room for new connection"); continue; } conn[i].addrlen = sizeof(conn[i].addr); conn[i].s = accept(dut->s, (struct sockaddr *) &conn[i].addr, &conn[i].addrlen); if (conn[i].s < 0) { sigma_dut_print(dut, DUT_MSG_INFO, "accept: %s", strerror(errno)); continue; } sigma_dut_print(dut, DUT_MSG_DEBUG, "Connection %d from %s:%d", i, inet_ntoa(conn[i].addr.sin_addr), ntohs(conn[i].addr.sin_port)); conn[i].pos = 0; } for (i = 0; i < MAX_CONNECTIONS; i++) { if (conn[i].s < 0) continue; if (FD_ISSET(conn[i].s, &rfds)) process_conn(dut, &conn[i]); } } }