int main( int argc, char **argv) { struct _connection_list *cl; Connection *conn; add_conn( 1 ); for( conn = list_conn( NULL ); conn != NULL; conn = list_conn( conn ) ) { printf( "%d\n", conn->fd ); } add_conn( 2 ); for( conn = list_conn( NULL ); conn != NULL; conn = list_conn( conn ) ) { cl = (struct _connection_list *)conn; printf( "%d\n", conn->fd ); } }
static void accept_conn( int sd) /* main socket with connection request pending */ { int newsock; struct sockaddr_in from; struct sockaddr_un unixfrom; torque_socklen_t fromsize; from.sin_addr.s_addr = 0; from.sin_port = 0; /* update lasttime of main socket */ svr_conn[sd].cn_lasttime = time((time_t *)0); if (svr_conn[sd].cn_socktype == PBS_SOCK_INET) { fromsize = sizeof(from); newsock = accept(sd, (struct sockaddr *) & from, &fromsize); } else { fromsize = sizeof(unixfrom); newsock = accept(sd, (struct sockaddr *) & unixfrom, &fromsize); } if (newsock == -1) { return; } if ((num_connections >= max_connection) || (newsock >= PBS_NET_MAX_CONNECTIONS)) { close(newsock); return; /* too many current connections */ } /* add the new socket to the select set and connection structure */ add_conn( newsock, FromClientDIS, (pbs_net_t)ntohl(from.sin_addr.s_addr), (unsigned int)ntohs(from.sin_port), svr_conn[sd].cn_socktype, read_func[(int)svr_conn[sd].cn_active]); return; } /* END accept_conn() */
static int contact_listener( int l_idx) /* I */ { int sock; char tmpLine[1024]; char EMsg[1024]; char *id = "contact_listener"; /* If this is the first time contacting the scheduler for * this listener set the cmd */ if(listener_conns[l_idx].first_time) listener_command = SCH_SCHEDULE_FIRST; /* connect to the Listener */ sock = client_to_svr(listener_conns[l_idx].address, listener_conns[l_idx].port, 1, EMsg); if (sock < 0) { /* FAILURE */ bad_node_warning(listener_conns[l_idx].address); sprintf(tmpLine, "%s %d - port %d %s", msg_listnr_nocall, l_idx, listener_conns[l_idx].port, EMsg); /* we lost contact with the scheduler. reset*/ listener_conns[l_idx].first_time = 1; log_err(errno, id, tmpLine); return(-1); } listener_conns[l_idx].first_time = 0; add_conn( sock, FromClientDIS, listener_conns[l_idx].address, listener_conns[l_idx].port, PBS_SOCK_INET, process_request); svr_conn[sock].cn_authen = PBS_NET_CONN_FROM_PRIVIL; net_add_close_func(sock, listener_close); /* send command to Listener */ if (put_4byte(sock, listener_command) < 0) { sprintf(tmpLine, "%s %d - port %d", msg_listnr_nocall, l_idx + 1, listener_conns[l_idx].port); log_err(errno, id, tmpLine); close_conn(sock); return(-1); } sprintf(log_buffer, msg_listnr_called, l_idx + 1, (listener_command != SCH_ERROR) ? PSchedCmdType[listener_command] : "ERROR"); log_event( PBSEVENT_SCHED, PBS_EVENTCLASS_SERVER, server_name, log_buffer); return (sock); } /* END contact_listener() */
static int contact_sched( int cmd) /* I */ { int sock; char tmpLine[1024]; char EMsg[1024]; char *id = "contact_sched"; /* connect to the Scheduler */ #if 0 /* don't check if scheduler runs on same node as server */ if (!addr_ok(pbs_scheduler_addr)) { pbs_errno = EHOSTDOWN; return -1; } #endif sock = client_to_svr(pbs_scheduler_addr, pbs_scheduler_port, 1, EMsg); if (sock < 0) { /* FAILURE */ bad_node_warning(pbs_scheduler_addr); #if 0 sprintf(tmpLine, "%s - port %d %s", msg_sched_nocall, pbs_scheduler_port, EMsg); log_ext(errno,id,tmpLine,LOG_ALERT); #endif return(-1); } add_conn( sock, FromClientDIS, pbs_scheduler_addr, pbs_scheduler_port, PBS_SOCK_INET, process_request); svr_conn[sock].cn_authen = PBS_NET_CONN_FROM_PRIVIL; net_add_close_func(sock, scheduler_close); /* send command to Scheduler */ if (put_4byte(sock, cmd) < 0) { sprintf(tmpLine, "%s - port %d", msg_sched_nocall, pbs_scheduler_port); log_ext(errno,id,tmpLine,LOG_ALERT); close_conn(sock); return(-1); } sprintf(log_buffer, msg_sched_called, (cmd != SCH_ERROR) ? PSchedCmdType[cmd] : "ERROR"); log_event( PBSEVENT_SCHED, PBS_EVENTCLASS_SERVER, server_name, log_buffer); return (sock); } /* END contact_sched() */
static long ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct iscsi_target *target = NULL; long err; u32 id; if ((err = get_user(id, (u32 *) arg)) != 0) goto done; if (cmd == DEL_TARGET) { err = target_del(id); goto done; } target = target_lookup_by_id(id); if (cmd == ADD_TARGET) if (target) { err = -EEXIST; eprintk("Target %u already exist!\n", id); goto done; } switch (cmd) { case ADD_TARGET: assert(!target); err = add_target(arg); goto done; } if (!target) { eprintk("can't find the target %u\n", id); err = -EINVAL; goto done; } if ((err = target_lock(target, 1)) < 0) { eprintk("interrupted %ld %d\n", err, cmd); goto done; } switch (cmd) { case ADD_VOLUME: err = add_volume(target, arg); break; case DEL_VOLUME: err = del_volume(target, arg); break; case ADD_SESSION: err = add_session(target, arg); break; case DEL_SESSION: err = del_session(target, arg); break; case GET_SESSION_INFO: err = get_session_info(target, arg); break; case ISCSI_PARAM_SET: err = iscsi_param_config(target, arg, 1); break; case ISCSI_PARAM_GET: err = iscsi_param_config(target, arg, 0); break; case ADD_CONN: err = add_conn(target, arg); break; case DEL_CONN: err = del_conn(target, arg); break; case GET_CONN_INFO: err = get_conn_info(target, arg); break; } if (target) target_unlock(target); done: return err; }
static bool_t ReAttach_m( FlowMan_cl *self, FlowMan_Flow flow, FlowMan_ConnID cid /* IN */, const FlowMan_SAP *lsap /* IN */, const FlowMan_SAP *rsap /* IN */ ) { flowman_st UNUSED *st = self->st; conn_t *c = (conn_t *)cid; flow_t *f = (flow_t *)flow; port_alloc_t *p; switch (lsap->tag) { case FlowMan_PT_TCP: p = st->host->TCPportuse; break; case FlowMan_PT_UDP: p = st->host->UDPportuse; break; default: printf("FlowMan$ReAttach: unknown protocol %d\n", lsap->tag); return False; } /* check the client owns the local port in question */ while(p) { if (p->port == lsap->u.UDP.port) break; p = p->next; } if (!p) { printf("FlowMan$ReAttach: domain %qx attempted to attach " "to unbound port %d\n", st->dom, ntohs(lsap->u.UDP.port)); return False; } /* check client specified the correct local IP address */ if (lsap->u.UDP.addr.a != f->intf->ipaddr) { printf("FlowMan$ReAttach: domain %qx attempted to attach " "with bogus local IP address (%x != %x)\n", st->dom, ntohl(lsap->u.UDP.addr.a), ntohl(f->intf->ipaddr)); return 0; } /* remove old demux for this flow, and put the new one in */ /* XXX TX stuff isn't changed */ /* XXX race between del and add; need to make these atomic. */ del_conn(f, c); /* take a copy of the new connection endpoints */ c->lsap = *lsap; c->rsap = *rsap; add_conn(f, c); return True; }
static FlowMan_ConnID Attach_m( FlowMan_cl *self, FlowMan_Flow flow /* IN */, const FlowMan_SAP *lsap /* IN */, const FlowMan_SAP *rsap /* IN */ ) { flowman_st *st = self->st; flow_t *f = (flow_t *)flow; conn_t *c; port_alloc_t *p; uint8_t proto; switch (lsap->tag) { case FlowMan_PT_TCP: proto = IP_PROTO_TCP; p = st->host->TCPportuse; break; case FlowMan_PT_UDP: proto = IP_PROTO_UDP; p = st->host->UDPportuse; break; default: printf("FlowMan$Attach: unknown protocol %d\n", lsap->tag); return 0; } /* check the client owns the local port in question */ while(p) { if (p->port == lsap->u.UDP.port) break; p = p->next; } if (!p) { printf("FlowMan$Attach: domain %qx attempted to attach " "to unbound port %d\n", st->dom, ntohs(lsap->u.UDP.port)); return 0; } /* check client specified the correct local IP address */ if (lsap->u.UDP.addr.a != f->intf->ipaddr) { printf("FlowMan$Attach: domain %qx attempted to attach " "with bogus local IP address (%x != %x)\n", st->dom, ntohl(lsap->u.UDP.addr.a), ntohl(f->intf->ipaddr)); return 0; } /* create a new conn_t */ c = Heap$Malloc(Pvs(heap), sizeof(*c)); if (!c) { printf("FlowMan$Attach: out of memory\n"); return 0; } /* fill in the new structure */ c->lsap = *lsap; c->rsap = *rsap; c->next = f->conns; f->conns = c; /* if this is the first attach, then create and set the transmit * filter, otherwise ignore the transmit stuff */ /* XXX TX filter only gets set once! */ if (!f->txpf) { f->txpf = LMPFMod$NewTX(f->intf->card->pfmod, f->intf->card->mac, /* src mac */ f->intf->ipaddr, /* src ip */ proto, lsap->u.UDP.port); Netif$SetTxFilter(f->intf->card->netif, f->txhdl, f->txpf); } add_conn(f, c); return (FlowMan_ConnID)c; }
int start_listener_addrinfo( char *host_name, int server_port, void *(*process_meth)(void *)) { struct addrinfo *adr_svr = NULL; struct sockaddr adr_client; struct sockaddr_in *in_addr; struct sockaddr_in svr_address; socklen_t len_inet; int rc = PBSE_NONE; int sockoptval; int new_conn_port = -1; int listen_socket = 0; int total_cntr = 0; unsigned short port_net_byte_order; pthread_attr_t t_attr; char err_msg[MAXPATHLEN]; char log_buf[LOCAL_LOG_BUF_SIZE + 1]; int ret = pbs_getaddrinfo(host_name, NULL, &adr_svr); if (ret != 0) { /* hostname didn't resolve */ snprintf(err_msg, sizeof(err_msg), "Error with getaddrinfo on host name %s. Error code = %d, '%s'.\n", host_name, ret, gai_strerror(ret)); log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, err_msg); rc = PBSE_SOCKET_FAULT; return rc; } port_net_byte_order = htons(server_port); memcpy(&adr_svr->ai_addr->sa_data, &port_net_byte_order, sizeof(unsigned short)); memset(&svr_address, 0, sizeof(svr_address)); svr_address.sin_family = adr_svr->ai_family; svr_address.sin_port = htons(server_port); svr_address.sin_addr.s_addr = htonl(INADDR_ANY); if ((listen_socket = get_listen_socket(adr_svr)) < 0) { /* Can not get socket for listening */ rc = PBSE_SOCKET_FAULT; } else if ((bind(listen_socket, (struct sockaddr *)&svr_address, sizeof(svr_address))) == -1) { /* Can not bind local socket */ rc = PBSE_SOCKET_FAULT; } else if (listen(listen_socket, 256) == -1) { /* Can not listener on local socket */ rc = PBSE_SOCKET_LISTEN; } else if ((rc = pthread_attr_init(&t_attr)) != 0) { /* Can not init thread attribute structure */ rc = PBSE_THREADATTR; } else if ((rc = pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED)) != 0) { /* Can not set thread initial state as detached */ pthread_attr_destroy(&t_attr); } else { // record this so it can be closed by children listening_socket = listen_socket; int exit_loop = FALSE; int retry_tolerance = NUM_ACCEPT_RETRIES; while (1) { long *args = NULL; /* if successfully allocated args will be freed in process_meth */ args = (long *)calloc(3, sizeof(long)); if (args == NULL) { snprintf(log_buf, sizeof(log_buf), "failed to allocate argument space"); log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_REQUEST, __func__, log_buf); /* Let's try to recover */ sleep(5); continue; } len_inet = sizeof(struct sockaddr); if ((new_conn_port = accept(listen_socket, (struct sockaddr *)&adr_client, (socklen_t *)&len_inet)) == -1) { switch (errno) { case EMFILE: case ENFILE: case EINTR: /* transient error, try again */ if (retry_tolerance-- <= 0) { exit_loop = TRUE; snprintf(err_msg, sizeof(err_msg), "Exiting loop because we passed our retry tolerance: %d", errno); } else sleep(1); break; default: snprintf(err_msg, sizeof(err_msg), "error in accept %s - stopping accept loop", strerror(errno)); exit_loop = TRUE; break; } if (exit_loop == TRUE) { if (args) free(args); break; } errno = 0; } else { retry_tolerance = NUM_ACCEPT_RETRIES; sockoptval = 1; setsockopt(new_conn_port, SOL_SOCKET, SO_REUSEADDR, (void *)&sockoptval, sizeof(sockoptval)); in_addr = (struct sockaddr_in *)&adr_client; args[0] = new_conn_port; args[1] = ntohl(in_addr->sin_addr.s_addr); args[2] = htons(in_addr->sin_port); if (debug_mode == TRUE) { process_meth((void *)args); } else { if (new_conn_port == PBS_LOCAL_CONNECTION) { snprintf(log_buf, LOCAL_LOG_BUF_SIZE, "Ignoring local incoming request %d", new_conn_port); log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_REQUEST, __func__, log_buf); } else { /* add_conn is not protocol independent. We need to do some IPv4 stuff here */ add_conn( new_conn_port, FromClientDIS, (pbs_net_t)ntohl(in_addr->sin_addr.s_addr), (unsigned int)htons(in_addr->sin_port), PBS_SOCK_INET, NULL); enqueue_threadpool_request(process_meth, args, request_pool); } } } if (debug_mode == TRUE) { if (total_cntr % 1000 == 0) { printf("Total requests: %d\n", total_cntr); } total_cntr++; } } /* END infinite_loop() */ pthread_attr_destroy(&t_attr); /* all conditions for exiting the loop must populate err_msg */ log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, err_msg); } if (listen_socket != -1) close(listen_socket); return(rc); } /* END start_listener_addrinfo() */
int start_listener_addrinfo( char *host_name, int server_port, void *(*process_meth)(void *)) { struct addrinfo *adr_svr; struct sockaddr adr_client; struct sockaddr_in *in_addr; struct sockaddr_in svr_address; socklen_t len_inet; int rc = PBSE_NONE; int sockoptval; int *new_conn_port = NULL; int listen_socket = 0; int total_cntr = 0; unsigned short port_net_byte_order; pthread_attr_t t_attr; if (!(getaddrinfo(host_name, NULL, NULL, &adr_svr) == 0)) { rc = PBSE_SOCKET_FAULT; } port_net_byte_order = htons(server_port); memcpy(&adr_svr->ai_addr->sa_data, &port_net_byte_order, sizeof(unsigned short)); memset(&svr_address, 0, sizeof(svr_address)); svr_address.sin_family = adr_svr->ai_family; svr_address.sin_port = htons(server_port); svr_address.sin_addr.s_addr = htonl(INADDR_ANY); if ((listen_socket = get_listen_socket(adr_svr)) < 0) { /* Can not get socket for listening */ rc = PBSE_SOCKET_FAULT; } else if ((bind(listen_socket, (struct sockaddr *)&svr_address, sizeof(svr_address))) == -1) { /* Can not bind local socket */ rc = PBSE_SOCKET_FAULT; } else if (listen(listen_socket, 256) == -1) { /* Can not listener on local socket */ rc = PBSE_SOCKET_LISTEN; } else if ((rc = pthread_attr_init(&t_attr)) != 0) { /* Can not init thread attribute structure */ rc = PBSE_THREADATTR; } else if ((rc = pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED)) != 0) { /* Can not set thread initial state as detached */ pthread_attr_destroy(&t_attr); } else { freeaddrinfo(adr_svr); while (1) { len_inet = sizeof(struct sockaddr); if((new_conn_port = (int *)calloc(1, sizeof(int))) == NULL) { printf("Error allocating new connection handle.\n"); break; } if ((*new_conn_port = accept(listen_socket, (struct sockaddr *)&adr_client, (socklen_t *)&len_inet)) == -1) { if (errno == EMFILE) { sleep(1); printf("Temporary pause\n"); } else { printf("error in accept %s\n", strerror(errno)); break; } errno = 0; close(*new_conn_port); free(new_conn_port); new_conn_port = NULL; } else { sockoptval = 1; setsockopt(*new_conn_port, SOL_SOCKET, SO_REUSEADDR, (void *)&sockoptval, sizeof(sockoptval)); if (debug_mode == TRUE) { process_meth((void *)new_conn_port); } else { /* add_conn is not protocol independent. We need to do some IPv4 stuff here */ in_addr = (struct sockaddr_in *)&adr_client; add_conn( *new_conn_port, FromClientDIS, (pbs_net_t)ntohl(in_addr->sin_addr.s_addr), (unsigned int)htons(in_addr->sin_port), PBS_SOCK_INET, NULL); enqueue_threadpool_request(process_meth, new_conn_port); /*pthread_create(&tid, &t_attr, process_meth, (void *)new_conn_port);*/ } } if (debug_mode == TRUE) { if (total_cntr % 1000 == 0) { printf("Total requests: %d\n", total_cntr); } total_cntr++; } } if (new_conn_port != NULL) { free(new_conn_port); } pthread_attr_destroy(&t_attr); log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, "net_srvr", (char *)"Socket close of network listener requested"); } close(listen_socket); return(rc); } /* END start_listener_addrinfo() */
int main (int argc, char const * argv []) { extern struct channel channel; static char const * optv [] = { "ei:qv", "action priority destination rate ttl operand condition [...] [device] [...]\n\n where condition is field operator value", "CoQos Stream Utility", "e\tredirect stderr to stdout", #if defined (WINPCAP) || defined (LIBPCAP) "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]", #else "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]", #endif "q\tquiet mode", "v\tverbose mode", (char const *) (0) }; #include "../plc/plc.c" struct connection connection; struct MMEClassifier * rule = (struct MMEClassifier *)(&connection.rule.CLASSIFIERS); uint16_t * word; uint8_t * byte; signed code; signed c; if (getenv (PLCDEVICE)) { #if defined (WINPCAP) || defined (LIBPCAP) channel.ifindex = atoi (getenv (PLCDEVICE)); #else channel.ifname = strdup (getenv (PLCDEVICE)); #endif } optind = 1; while ((c = getoptv (argc, argv, optv)) != -1) { switch (c) { case 'e': dup2 (STDOUT_FILENO, STDERR_FILENO); break; case 'i': #if defined (WINPCAP) || defined (LIBPCAP) channel.ifindex = atoi (optarg); #else channel.ifname = optarg; #endif break; case 'q': _setbits (channel.flags, CHANNEL_SILENCE); _setbits (plc.flags, PLC_SILENCE); break; case 'v': _setbits (channel.flags, CHANNEL_VERBOSE); _setbits (plc.flags, PLC_VERBOSE); break; default: break; } } argc -= optind; argv += optind; memset (&connection, 0, sizeof (connection)); if ((code = lookup (* argv++, actions, SIZEOF (actions))) == -1) { assist (*--argv, CLASSIFIER_ACTION_NAME, actions, SIZEOF (actions)); } connection.cspec.CONN_CAP = (uint8_t)(code); argc--; if (!argc) { error (1, ECANCELED, "Expected Priority: 0-15"); } connection.cspec.CONN_COQOS_PRIO = (uint8_t)(uintspec (* argv++, 0, 15)); argc--; if (!argc) { error (1, ECANCELED, "Expected Destination MAC Address"); } if (!hexencode (connection.APP_DA, sizeof (connection.APP_DA), synonym (* argv++, devices, SIZEOF (devices)))) { error (1, errno, "Invalid MAC=[%s]", *--argv); } argc--; if (!argc) { error (1, ECANCELED, "Expected Data Rate: 10-9000 (kbps)"); } connection.cspec.CONN_RATE = (uint16_t)(uintspec (* argv++, 1, 9000)); argc--; if (!argc) { error (1, ECANCELED, "Expected TTL: 10000-2000000 (microseconds)"); } connection.cspec.CONN_TTL = (uint32_t)(uintspec (* argv++, 10000, 2000000)); argc--; if ((code = lookup (* argv++, operands, SIZEOF (operands))) == -1) { assist (*--argv, CLASSIFIER_OPERAND_NAME, operands, SIZEOF (operands)); } connection.rule.MOPERAND = (uint8_t)(code); argc--; while ((* argv) && (lookup (* argv, controls, SIZEOF (controls)) == -1)) { if ((code = lookup (* argv++, fields, SIZEOF (fields))) == -1) { assist (*--argv, CLASSIFIER_FIELD_NAME, fields, SIZEOF (fields)); } rule->CR_PID = (uint8_t)(code); argc--; if ((code = lookup (* argv++, operators, SIZEOF (operators))) == -1) { assist (*--argv, CLASSIFIER_OPERATOR_NAME, operators, SIZEOF (operators)); } rule->CR_OPERAND = (uint8_t)(code); argc--; if (!argc || !* argv) { error (1, ENOTSUP, "Have %s '%s' without any value", CLASSIFIER_OPERATOR_NAME, *--argv); } switch (rule->CR_PID) { case FIELD_ETH_SA: case FIELD_ETH_DA: bytespec (* argv++, rule->CR_VALUE, ETHER_ADDR_LEN); break; case FIELD_IPV4_SA: case FIELD_IPV4_DA: ipv4spec (* argv++, rule->CR_VALUE); break; case FIELD_IPV6_SA: case FIELD_IPV6_DA: ipv6spec (* argv++, rule->CR_VALUE); break; case FIELD_VLAN_UP: case FIELD_IPV4_TOS: case FIELD_IPV4_PROT: byte = (uint8_t *)(rule->CR_VALUE); *byte = (uint8_t)(basespec (* argv++, 0, sizeof (* byte))); break; case FIELD_VLAN_ID: case FIELD_TCP_SP: case FIELD_TCP_DP: case FIELD_UDP_SP: case FIELD_UDP_DP: case FIELD_IP_SP: case FIELD_IP_DP: word = (uint16_t *)(rule->CR_VALUE); *word = (uint16_t)(basespec (* argv++, 0, sizeof (* word))); *word = htons (*word); break; case FIELD_ETH_TYPE: word = (uint16_t *)(rule->CR_VALUE); *word = (uint16_t)(basespec (* argv++, 0, sizeof (* word))); *word = htons (*word); break; case FIELD_HPAV_MME: bytespec (* argv++, rule->CR_VALUE, sizeof (uint8_t) + sizeof (uint16_t)); byte = (uint8_t *)(rule->CR_VALUE); HTOBE16 ((uint16_t)(* ++byte)); break; case FIELD_IPV6_TC: case FIELD_IPV6_FL: case FIELD_TCP_ACK: default: error (1, ENOTSUP, "Field '%s' (0x%02X)", argv [-2], rule->CR_PID); break; } connection.rule.NUM_CLASSIFIERS++; if (connection.rule.NUM_CLASSIFIERS > RULE_MAX_CLASSIFIERS) { error (1, ENOTSUP, "More than %d classifiers in rule", RULE_MAX_CLASSIFIERS); } rule++; argc--; } connection.cspec.CSPEC_VERSION = 0x0001; openchannel (&channel); if (!(plc.message = malloc (sizeof (* plc.message)))) { error (1, errno, PLC_NOMEMORY); } if (!argc) { add_conn (&plc, &connection); } while ((argc) && (* argv)) { if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices)))) { error (1, errno, PLC_BAD_MAC, * argv); } add_conn (&plc, &connection); argc--; argv++; } free (plc.message); closechannel (&channel); exit (0); }
int main(int argc, char* argv[]) { if (argc <= 2) { printf("Usage: %s ip_address port_number\n", argv[0]); return 0; } log_globals_init(&g_log); log_init(&g_log, "jhttpserver.log", NULL); log_set_loglevel(&g_log, LOG_DEBUG); const char* ip = argv[1]; int port = atoi(argv[2]); INFO(&g_log, "jhttpserver", "%s : %d", ip, port); add_signal(SIGPIPE, SIG_IGN, TRUE); thread_pool* pool = create_thread_pool(8 ,10000); if (pool == NULL) { printf("create thread pool is failed."); ERROR(&g_log, "jhttpserver", "create thread pool is failed."); return 1; } http_conn* users = (http_conn*)malloc(sizeof(http_conn) * MAX_FD); assert(users); int listen_fd = socket(PF_INET, SOCK_STREAM, 0); struct linger tmp = {1, 0}; setsockopt(listen_fd, SOL_SOCKET, SO_LINGER, &tmp, sizeof(tmp)); int ret = 0; struct sockaddr_in address; bzero(&address, sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(port); inet_pton(AF_INET, ip, &address.sin_addr); ret = bind(listen_fd, (struct sockaddr *)&address, sizeof(address)); assert(ret >= 0); ret = listen(listen_fd, 5); assert(ret >= 0); struct epoll_event events[MAX_EVENT_NUMBER]; epollfd = epoll_create(5); assert(epollfd != -1); add_fd(epollfd, listen_fd, false); while (true) { int number = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1); if ((number < 0) && (errno != EINTR)) { printf("epoll failure\n"); break; } int i=0; for (; i<number; i++) { int sockfd = events[i].data.fd; if (sockfd == listen_fd) { struct sockaddr_in client_address; socklen_t client_addr_len = sizeof(client_address); int conn_fd = accept(listen_fd, (struct sockaddr*)&client_address, &client_addr_len); if (conn_fd < 0) { printf("errno is : %d\n", errno); continue; } if (user_count > MAX_FD) { show_error(conn_fd, "Internal server busy"); continue; } init_new_connect(&users[conn_fd], conn_fd, &client_address); } else if (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) { close_connect(&users[sockfd]); } else if (events[i].events & EPOLLIN) { if (http_conn_read(&users[sockfd])) { add_conn(pool, users + sockfd); } else { close_connect(&users[sockfd]); } } else if (events[i].events & EPOLLOUT) { if (!http_conn_write(&users[sockfd])) { close_connect(&users[sockfd]); } } } } close(epollfd); close(listen_fd); free(users); destroy_thread_pool(pool); return 0; }
static long ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct iscsi_target *target = NULL; long err; u32 id; err = down_interruptible(&ioctl_sem); if (err < 0) return err; if (cmd == GET_MODULE_INFO) { err = get_module_info(arg); goto done; } if (cmd == ADD_TARGET) { err = add_target(arg); goto done; } err = get_user(id, (u32 *) arg); if (err < 0) goto done; /* locking handled in target_del */ if (cmd == DEL_TARGET) { err = target_del(id); goto done; } target = target_lookup_by_id(id); if (!target) { err = -ENOENT; goto done; } err = target_lock(target, 1); if (err < 0) goto done; switch (cmd) { case ADD_VOLUME: err = add_volume(target, arg); break; case DEL_VOLUME: err = del_volume(target, arg); break; case ADD_SESSION: err = add_session(target, arg); break; case DEL_SESSION: err = del_session(target, arg); break; case GET_SESSION_INFO: err = get_session_info(target, arg); break; case ISCSI_PARAM_SET: err = iscsi_param_config(target, arg, 1); break; case ISCSI_PARAM_GET: err = iscsi_param_config(target, arg, 0); break; case ADD_CONN: err = add_conn(target, arg); break; case DEL_CONN: err = del_conn(target, arg); break; case GET_CONN_INFO: err = get_conn_info(target, arg); break; default: eprintk("invalid ioctl cmd %x\n", cmd); err = -EINVAL; } target_unlock(target); done: up(&ioctl_sem); return err; }
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; } } } } } } } } } } }
int init_network( unsigned int port, void (*readfunc)()) { int i; static int initialized = 0; int sock; int MaxNumDescriptors = 0; struct sockaddr_in socname; enum conn_type type; #ifdef ENABLE_UNIX_SOCKETS struct sockaddr_un unsocname; int unixsocket; memset(&unsocname, 0, sizeof(unsocname)); #endif MaxNumDescriptors = get_max_num_descriptors(); memset(&socname, 0, sizeof(socname)); if (initialized == 0) { for (i = 0;i < PBS_NET_MAX_CONNECTIONS;i++) svr_conn[i].cn_active = Idle; /* initialize global "read" socket FD bitmap */ GlobalSocketReadSet = (fd_set *)calloc(1,sizeof(char) * get_fdset_size()); type = Primary; } else if (initialized == 1) { type = Secondary; } else { /* FAILURE */ return(-1); /* too many main connections */ } /* save the routine which should do the reading on connections */ /* accepted from the parent socket */ read_func[initialized++] = readfunc; if (port != 0) { sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { /* FAILURE */ return(-1); } if (MaxNumDescriptors < PBS_NET_MAX_CONNECTIONS) max_connection = MaxNumDescriptors; i = 1; setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i)); i = 1; setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i)); /* name that socket "in three notes" */ socname.sin_port = htons((unsigned short)port); socname.sin_addr.s_addr = INADDR_ANY; socname.sin_family = AF_INET; if (bind(sock, (struct sockaddr *)&socname, sizeof(socname)) < 0) { /* FAILURE */ close(sock); return(-1); } /* record socket in connection structure and select set */ add_conn(sock, type, (pbs_net_t)0, 0, PBS_SOCK_INET, accept_conn); /* start listening for connections */ if (listen(sock, 512) < 0) { /* FAILURE */ return(-1); } } /* END if (port != 0) */ #ifdef ENABLE_UNIX_SOCKETS if (port == 0) { /* setup unix domain socket */ unixsocket = socket(AF_UNIX, SOCK_STREAM, 0); if (unixsocket < 0) { return(-1); } unsocname.sun_family = AF_UNIX; strncpy(unsocname.sun_path, TSOCK_PATH, sizeof(unsocname.sun_path) - 1); unlink(TSOCK_PATH); /* don't care if this fails */ if (bind(unixsocket, (struct sockaddr *)&unsocname, sizeof(unsocname)) < 0) { close(unixsocket); return(-1); } if (chmod(TSOCK_PATH, S_IRUSR | S_IWUSR) != 0) { close(unixsocket); return(-1); } add_conn(unixsocket, type, (pbs_net_t)0, 0, PBS_SOCK_UNIX, accept_conn); if (listen(unixsocket, 512) < 0) { /* FAILURE */ return(-1); } } /* END if (port == 0) */ #endif /* END ENABLE_UNIX_SOCKETS */ if (port != 0) { /* allocate a minute's worth of counter structs */ for (i = 0;i < 60;i++) { nc_list[i].time = 0; nc_list[i].counter = 0; } } return(0); } /* END init_network() */
int iet_ioctl(struct cdev *dev, unsigned long cmd, caddr_t iarg, int fflag, struct thread *td) #endif { struct iscsi_target *target = NULL; long err; u32 id; #ifdef FREEBSD unsigned long arg = (unsigned long)(iarg); /* Avoid make warnings */ #endif err = mutex_lock_interruptible(&ioctl_mutex); if (err != 0) return err; if (cmd == GET_MODULE_INFO) { err = get_module_info(arg); goto done; } if (cmd == ADD_TARGET) { err = add_target(arg); goto done; } err = copy_from_user(&id, (u32 *) (unsigned long)arg, sizeof(u32)); if (err < 0) goto done; /* locking handled in target_del */ if (cmd == DEL_TARGET) { err = target_del(id); goto done; } target = target_lookup_by_id(id); if (!target) { err = -ENOENT; goto done; } err = target_lock(target, 1); if (err < 0) goto done; switch (cmd) { case ADD_VOLUME: err = add_volume(target, arg); break; case DEL_VOLUME: err = del_volume(target, arg); break; case ADD_SESSION: err = add_session(target, arg); break; case DEL_SESSION: err = del_session(target, arg); break; case GET_SESSION_INFO: err = get_session_info(target, arg); break; case ISCSI_PARAM_SET: err = iscsi_param_config(target, arg, 1); break; case ISCSI_PARAM_GET: err = iscsi_param_config(target, arg, 0); break; case ADD_CONN: err = add_conn(target, arg); break; case DEL_CONN: err = del_conn(target, arg); break; case GET_CONN_INFO: err = get_conn_info(target, arg); break; default: eprintk("invalid ioctl cmd %lx\n", (unsigned long)cmd); err = -EINVAL; } target_unlock(target); done: mutex_unlock(&ioctl_mutex); #ifdef FREEBSD if (err < 0) err = -(err); #endif return err; }
int main(int argc, char *argv[]) { fprintf(stderr,"Proxy Start yuruiz\n"); int http_port; char *log_file; int http_listen_socket, http_client_sock; struct sockaddr_in http_addr, cli_addr; socklen_t conn_size; pool conn_pool; if (argc < 7 || argc > 8) { printf(USAGE, argv[0]); return EXIT_FAILURE; } log_file = argv[1]; alpha = atof(argv[2]); http_port = atoi(argv[3]); fake_ip = argv[4]; proxy.dns_ip = argv[5]; proxy.dns_port = argv[6]; if (argc == 8) { www_ip = argv[7]; } loginit(log_file); fprintf(stderr,"-------------------Server Start------------------\n"); if ((http_listen_socket = open_port(http_port, &http_addr)) == -1) { fprintf(stderr,"Open port failed\n"); return EXIT_FAILURE; } // parse fake-ip bzero(&proxy.myaddr, sizeof(struct sockaddr_in)); proxy.myaddr.sin_family = AF_INET; inet_aton(fake_ip, &proxy.myaddr.sin_addr); proxy.myaddr.sin_port = htons(0); init_pool(http_listen_socket, &conn_pool); do { // printf("Pool start\n"); conn_pool.ready_set = conn_pool.read_set; conn_pool.nconn = select(conn_pool.maxfd + 1, &conn_pool.ready_set, NULL, NULL, NULL); conn_size = sizeof(cli_addr); if (FD_ISSET(http_listen_socket, &conn_pool.ready_set)) { fprintf(stderr,"Adding new http connection\n"); if ((http_client_sock = accept(http_listen_socket, (struct sockaddr *) &cli_addr, &conn_size)) == -1) { fprintf(stderr,"Error accepting http connection.\n"); continue; } add_conn(http_client_sock, &conn_pool, &cli_addr); } conn_handle(&conn_pool); } while (1); close_socket(http_listen_socket); return EXIT_SUCCESS; }