static int botnet_listen(BOTNET_REC *botnet) { IPADDR addr; int port; g_return_val_if_fail(botnet != NULL, FALSE); if (botnet->port <= 0) return FALSE; port = botnet->port; if (botnet->addr == NULL) botnet->listen_handle = net_listen(NULL, &port); else { net_host2ip(botnet->addr, &addr); botnet->listen_handle = net_listen(&addr, &port); } if (botnet->listen_handle == -1) { g_warning("Couldn't start listening botnet\n"); return FALSE; } botnet->listen_tag = g_input_add(botnet->listen_handle, G_INPUT_READ, (GInputFunction) sig_botnet_listen, botnet); return TRUE; }
/* Start listening for incoming connections */ static GIOChannel *dcc_listen_port(GIOChannel *iface, IPADDR *ip, int port) { if (net_getsockname(iface, ip, NULL) == -1) return NULL; if (IPADDR_IS_V6(ip)) return net_listen(NULL, &port); else return net_listen(&ip4_any, &port); }
/* Start listening for incoming connections */ GIOChannel *dcc_listen(GIOChannel *iface, IPADDR *ip, int *port) { GIOChannel *handle; IPADDR *listen_ip = NULL; const char *dcc_port, *p, *own_ip; int first, last; if (net_getsockname(iface, ip, NULL) == -1) return NULL; /* figure out if we want to listen in IPv4 address or in "any" address, which may mean IPv4+IPv6 or just IPv6 depending on OS. */ own_ip = settings_get_str("dcc_own_ip"); if (*own_ip != '\0') { if (is_ipv4_address(own_ip)) listen_ip = &ip4_any; } else { if (!IPADDR_IS_V6(ip)) listen_ip = &ip4_any; } /* get first port */ dcc_port = settings_get_str("dcc_port"); first = atoi(dcc_port); if (first == 0) { /* random port */ *port = 0; return net_listen(listen_ip, port); } /* get last port */ p = strchr(dcc_port, ' '); if (p == NULL) p = strchr(dcc_port, '-'); dcc_port = p; if (dcc_port == NULL) last = first; else { last = atoi(dcc_port+1); if (last == 0) last = first; } /* use the first available port */ for (*port = first; *port <= last; (*port)++) { handle = net_listen(listen_ip, port); if (handle != NULL) return handle; } return NULL; }
void network_init() { struct sockaddr_in my_name; my_name.sin_family = AF_INET; my_name.sin_port = htons(SERVER_PORT); my_name.sin_addr.s_addr = htonl(INADDR_ANY); net_init(); server_socket = net_socket(AF_INET, SOCK_STREAM, 0); int yes = 1; net_setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); while(net_bind(server_socket, (struct sockaddr*)&my_name, sizeof(my_name)) < 0) { } net_listen(server_socket, 0); struct sockaddr_in client_info; socklen_t ssize = sizeof(client_info); client_socket = net_accept(server_socket, (struct sockaddr*)&client_info, &ssize); network_printf("Hello world!\n"); }
int qnet_tcp_listen(int port, const char *bindaddr) { UNUSED(bindaddr); int fd; struct sockaddr_in sa; if ((fd = create_socket()) < 0) { return -1; } if (set_nonblocking(fd) < 0) { return -1; } memset(&sa,0,sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(port); sa.sin_addr.s_addr = htonl(INADDR_ANY); if (bindaddr && inet_aton(bindaddr, &sa.sin_addr) == 0) { qerror("invalid bind address"); close(fd); return -1; } if (net_listen(fd,(struct sockaddr*)&sa, sizeof(sa)) < 0) { close(fd); return -1; } return fd; }
NSAPI_PUBLIC SYS_NETFD INTnet_create_listener_alt(const char *ipstr, int port, PRBool internal) { SYS_NETFD sd; struct sockaddr_in sa_server; if (PR_TRUE == internal) { // internal listen sockets are always created without SSL sd = INTnet_socket_alt(AF_INET,SOCK_STREAM,IPPROTO_TCP); } else { // otherwise the socket may be SSL sd = net_socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); }; if (sd == SYS_NET_ERRORFD) { return SYS_NET_ERRORFD; }; ZERO((char *) &sa_server, sizeof(sa_server)); sa_server.sin_family=AF_INET; sa_server.sin_addr.s_addr = (ipstr ? inet_addr(ipstr) : htonl(INADDR_ANY)); sa_server.sin_port=htons(port); if(net_bind(sd, (struct sockaddr *) &sa_server,sizeof(sa_server)) < 0) { return SYS_NET_ERRORFD; } net_listen(sd, net_listenqsize); return sd; }
int sockcl_open(dev_file_t *f) { char *p; int port; char server[129]; // open "SOCL:smallbasic.sf.net:80" as #1 // open "SOCL:80" as #2 f->drv_dw[0] = 1; p = strchr(f->name + 5, ':'); if (!p) { port = xstrtol(f->name + 5); f->handle = (int) net_listen(port); } else { *p = '\0'; strcpy(server, f->name + 5); *p = ':'; port = xstrtol(p + 1); f->handle = (int) net_connect(server, port); } if (f->handle <= 0) { f->handle = -1; f->drv_dw[0] = 0; return 0; } return 1; }
int create_stream_sock(char *_role, int control) { if (!strcmp(_role, "initiator") || !strcmp(role, "initiator")) return net_connect(control); else return net_listen(); }
static void * proto_server_event_listen(void *arg) { int fd = Proto_Server.EventListenFD; int connfd; if (net_listen(fd)<0) { exit(-1); } for (;;) { connfd = net_accept(fd); if (connfd < 0) { fprintf(stderr, "Error: EventListen accept failed (%d)\n", errno); } else { int i; fprintf(stderr, "EventListen: connfd=%d -> ", connfd); if (proto_server_record_event_subscriber(connfd, &i) <0) { fprintf(stderr, "oops no space for any more event subscribers\n"); close(connfd); } else { } } } }
static int server_listen(void) { t_addr * curr_laddr; t_addr_data laddr_data; int sock; if (!(server_listen_addrs=addrlist_create(prefs_get_servaddrs(),INADDR_ANY,D2CS_SERVER_PORT))) { eventlog(eventlog_level_error,__FUNCTION__,"error create listening address list"); return -1; } BEGIN_LIST_TRAVERSE_DATA(server_listen_addrs,curr_laddr) { sock=net_listen(addr_get_ip(curr_laddr),addr_get_port(curr_laddr),PSOCK_SOCK_STREAM); if (sock<0) { eventlog(eventlog_level_error,__FUNCTION__,"error listen socket"); return -1; } if (psock_ctl(sock,PSOCK_NONBLOCK)<0) { eventlog(eventlog_level_error,__FUNCTION__,"error set listen socket in non-blocking mode"); } laddr_data.i = sock; addr_set_data(curr_laddr,laddr_data); if (fdwatch_add_fd(sock, fdwatch_type_read, d2cs_server_handle_accept, curr_laddr)<0) { eventlog(eventlog_level_error,__FUNCTION__,"error adding socket %d to fdwatch pool (max sockets?)",sock); psock_close(sock); return -1; } eventlog(eventlog_level_info,__FUNCTION__,"listen on %s", addr_num_to_addr_str(addr_get_ip(curr_laddr),addr_get_port(curr_laddr))); }
static int pi_row_end(void *p, unsigned row) { struct port_info *pi = p; struct domain *dom; assert(pi != NULL); if (!row) /* ignore first row */ return 0; if (!pi->domain || !pi->canonical) return -1; dom = dom_create(pi->domain); if (!dom) return -1; ha_add(dom, pi->canonical); Debug("listening... %s:%s\n", pi->addr, pi->port); if (net_listen(li_server_create, dom, pi->addr, pi->port)) { Error("Unable to listen (%s:%s)\n", pi->addr, pi->port); return -1; } pi_free(pi); return 0; }
static void * proto_server_rpc_listen(void *arg) { int fd = Proto_Server.RPCListenFD; unsigned long connfd; pthread_t tid; if (net_listen(fd) < 0) { fprintf(stderr, "Error: proto_server_rpc_listen listen failed (%d)\n", errno); exit(-1); } for (;;) { connfd = net_accept(fd); if (connfd < 0) { fprintf(stderr, "Error: proto_server_rpc_listen accept failed (%d)\n", errno); } else { pthread_create(&tid, NULL, &proto_server_req_dispatcher, (void *)connfd); } } }
s32 create_server(u16 port) { s32 server = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (server < 0) return -1; set_blocking(server, false); struct sockaddr_in bindAddress; memset(&bindAddress, 0, sizeof(bindAddress)); bindAddress.sin_family = AF_INET; bindAddress.sin_port = htons(port); bindAddress.sin_addr.s_addr = htonl(INADDR_ANY); s32 ret; if ((ret = net_bind(server, (struct sockaddr *)&bindAddress, sizeof(bindAddress))) < 0) { net_close(server); gxprintf("Error binding socket: [%i] %s\n", -ret, strerror(-ret)); return ret; } if ((ret = net_listen(server, 3)) < 0) { net_close(server); gxprintf("Error listening on socket: [%i] %s\n", -ret, strerror(-ret)); return ret; } return server; }
int main(int argc, char **argv) { int listenfd, port=0; long connfd; pthread_t tid; bzero(&globals, sizeof(globals)); if (net_setup_listen_socket(&listenfd,&port)!=1) { fprintf(stderr, "net_setup_listen_socket FAILED!\n"); exit(-1); } printf("listening on port=%d\n", port); if (net_listen(listenfd) < 0) { fprintf(stderr, "Error: server listen failed (%d)\n", errno); exit(-1); } for (;;) { connfd = net_accept(listenfd); if (connfd < 0) { fprintf(stderr, "Error: server accept failed (%d)\n", errno); } else { //EXPLAIN WHAT IS HAPPENING HERE IN YOUR LOG pthread_create(&tid, NULL, &doit, (void *)connfd); } } VPRINTF("Exiting\n"); }
/**************************************************************************** * NetworkWait ***************************************************************************/ int NetworkWait() { if (!checkincomming) return -3; struct sockaddr_in sin; struct sockaddr_in client_address; socklen_t addrlen = sizeof(client_address); //Open socket socket = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (socket == INVALID_SOCKET) { return socket; } sin.sin_family = AF_INET; sin.sin_port = htons(PORT); sin.sin_addr.s_addr = htonl(INADDR_ANY); if (net_bind(socket, (struct sockaddr*)&sin, sizeof(sin)) < 0) { net_close(socket); return -1; } if (net_listen(socket, 3) < 0) { net_close(socket); return -1; } connection = net_accept(socket, (struct sockaddr*)&client_address, &addrlen); sprintf(incommingIP, "%s", inet_ntoa(client_address.sin_addr)); if (connection < 0) { net_close(connection); net_close(socket); return -4; } else { unsigned char haxx[9]; //skip haxx net_read(connection, &haxx, 8); wiiloadVersion[0] = haxx[4]; wiiloadVersion[1] = haxx[5]; net_read(connection, &infilesize, 4); if (haxx[4] > 0 || haxx[5] > 4) { net_read(connection, &uncfilesize, 4); // Compressed protocol, read another 4 bytes } waitforanswer = true; checkincomming = false; networkHalt = true; } return 1; }
void tcp_server_listen(tcp_server_ctx_t* ctx) { net_result_t res; net_listen(&ctx->listen_socket, ctx->port, ctx->backlog, &res); if(res.code != NET_RESULT_OK){ exit(EXIT_FAILURE); } }
void cos_init(void *arg) { int c, accept_fd, ret; long eid; cvect_init_static(&evts); cvect_init_static(&tor_from); cvect_init_static(&tor_to); eid = evt_get(); c = net_create_tcp_connection(cos_spd_id(), cos_get_thd_id(), eid); if (c < 0) BUG(); ret = net_bind(cos_spd_id(), c, 0, 200); if (ret < 0) BUG(); ret = net_listen(cos_spd_id(), c, 255); if (ret < 0) BUG(); accept_fd = c; evt_add(c, eid); while (1) { struct tor_conn tc; int t; long evt; memset(&tc, 0, sizeof(struct tor_conn)); printc("waiting...\n"); evt = evt_grp_wait(cos_spd_id()); t = evt_torrent(evt); if (t > 0) { tc.feid = evt; tc.from = t; if (t == accept_fd) { tc.to = 0; printc("accepting event.\n"); accept_new(accept_fd); } else { tc.to = tor_get_to(t); assert(tc.to > 0); printc("data from net.\n"); from_data_new(&tc); } } else { t *= -1; tc.teid = evt; tc.to = t; tc.from = tor_get_from(t); assert(tc.from > 0); printc("data from torrent.\n"); to_data_new(&tc); } cos_mpd_update(); } }
void net_io_process(int server) { unsigned long nioBufferArray[6] = { 4, 128, 512, 1024, 2048, 4096 }; // in kB int err; if (server) { printf("Listening on %s\n", net_get_bound_addr()); if ((err = net_listen(0, NET_IPV4)) < 0) DPRINTF("Error listening on %s: %s\n", net_get_bound_addr(), strerror(-err)); DPRINTF("Server run complete...\n"); } else { int sock; fprintf(stderr, "Trying connection to %s\n", net_get_connect_addr()); sock = net_connect(NULL, 0, NET_IPV4); if (sock <= 0) fprintf(stderr, "Connection to %s failed: %s\n", net_get_connect_addr(), strerror(-sock)); else { int ii, nioIdx; float tm = 0.0, cpu = 0.0; results->net_res_size = (sizeof(nioBufferArray) / sizeof(nioBufferArray[0])); fprintf(stderr, "Network: Getting %d results, this may take some time\n", results->net_res_size); results->net = (tIOResults *)malloc( results->net_res_size * sizeof(tIOResults) ); nioIdx = 0; for (ii = 0; ii < (sizeof(nioBufferArray) / sizeof(nioBufferArray[0])); ii++) { unsigned long long total = 0, chunk = 0; char size_total[16] = { 0 }, size_chunk[16] = { 0 }, size_thp[16] = { 0 }; total = nioBufSize; chunk = (unsigned long long)nioBufferArray[ii] * (1 << 10); net_write_command(sock, total, chunk, chunk, &tm, &cpu); strncpy(results->net[nioIdx].operation, NET_OP_READ(outType), sizeof(results->net[nioIdx].operation)); results->net[nioIdx].size = total; results->net[nioIdx].throughput = total / tm; results->net[nioIdx].chunk_size = chunk; results->net[nioIdx++].cpu_usage = cpu; io_get_size(total, 0, size_total, 16); io_get_size(chunk, 0, size_chunk, 16); io_get_size_double(total / tm, prec, size_thp, 16); DPRINTF("Network benchmark on %s with buffer size %s: %s/s (CPU: %.*f%%)\n", size_total, size_chunk, size_thp, prec, cpu); } if (net_server_terminate(sock)) DPRINTF("Server socket terminated\n"); } } }
ServerSocket::ServerSocket(int port) { AMJU_CALL_STACK; Bind(port); #ifdef GEKKO net_listen(m_socket, 3); #else listen(m_socket, 3); #endif }
arg_t _listen(void) { struct socket *s = sock_get(fd, NULL); if (s == NULL) return -1; if (s->s_state == SS_UNCONNECTED && sock_autobind(s)) return -1; if (s->s_type != SOCKTYPE_TCP || s->s_state != SS_BOUND) { udata.u_error = EINVAL; return -1; } /* Call the protocol services */ return net_listen(s); }
void doPerformance(const string &arg){ if(arg == ARG_CLIENT){ bool bRet = net_connect(g_IP, TEST_CONNECT_PORT); if(!bRet){ DEBUG_E("client 连接失败。"); } PackerPtr pPacker(new Packer(NULL)); pPacker->setBuffer(g_TestContext_1.c_str(), g_TestContext_1.size()); long times(PERFROMANCE_TIMES); while(times--){ net_sendAll_C(pPacker); usleep(1); } sleep(100); } if(arg == ARG_SERVER){ TimeCounter timeCount; bool bRet = net_listen(g_IP, TEST_LISTEN_PORT); if(!bRet){ DEBUG_E("server 监听失败。"); } PackerPtr pPacker; long times(PERFROMANCE_TIMES); bool bNotFull(true); timeCount.start(); while(bRet && times>0){ if(net_recv_S(pPacker)){ if(g_TestContext_1 == string((char*)pPacker->getBuffer(), pPacker->getBufferSize())){ cout << --times << endl; } else{ bNotFull = false; DEBUG_E("接收数据出错。" << string((char*)pPacker->getBuffer(), pPacker->getBufferSize())); break; } if(!bNotFull){ cout << "有数据丢失" << endl; } } } timeCount.stop(); cout << "发送[" << PERFROMANCE_TIMES << "] use time [" << timeCount.getSecTimeD() << "]s" << endl; } }
int WaitForConnection(int& server_socket) { int addrlen; struct sockaddr_in my_name, peer_name; int status; server_socket = net_socket(AF_INET, SOCK_STREAM, 0); if (server_socket == -1) { printf("Failed to create server socket\n"); } int yes = 1; net_setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); memset(&my_name, 0, sizeof(my_name)); my_name.sin_family = AF_INET; my_name.sin_port = htons(DFF_CONN_PORT); my_name.sin_addr.s_addr = htonl(INADDR_ANY); status = net_bind(server_socket, (struct sockaddr*)&my_name, sizeof(my_name)); if (status == -1) { printf("Failed to bind server socket\n"); } status = net_listen(server_socket, 5); // TODO: Change second parameter.. if (status == -1) { printf("Failed to listen on server socket\n"); } printf("Listening now!\n"); int client_socket = -1; struct sockaddr_in client_info; socklen_t ssize = sizeof(client_info); int new_socket = net_accept(server_socket, (struct sockaddr*)&client_info, &ssize); if (new_socket < 0) { printf("accept failed!\n"); } else { client_socket = new_socket; printf("accept succeeded and returned %d\n", client_socket); } return client_socket; }
static int ht_listen(const char *node, const char *service) { struct addrinfo hints; struct addrinfo *res, *curr; int e; int ret = 0; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_UNSPEC; hints.ai_protocol = 0; hints.ai_socktype = SOCK_STREAM; hints.ai_next = NULL; e = getaddrinfo(node, service, &hints, &res); if (e) { fprintf(stderr, "%s():%s\n", __func__, gai_strerror(e)); return -1; } for (curr = res; curr; curr = curr->ai_next) { int fd; char host[40], serv[16]; /* currently we only support IPv4 and IPv6 */ if (curr->ai_family != AF_INET && curr->ai_family != AF_INET6) continue; getnameinfo(curr->ai_addr, curr->ai_addrlen, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV); if (net_listen(&fd, curr)) { fprintf(stderr, "%s():failed to add %s:%s\n", __func__, host, serv); ret = -1; } else if (li_add(fd)) { close(fd); } else { fprintf(stderr, "%s():listen on %s:%s\n", __func__, host, serv); } } freeaddrinfo(res); return ret; }
static int modify_connection(spdid_t spdid, net_connection_t nc, char *ops, int len) { struct intern_connection *ic; char *prop; int ret = -EINVAL; prop = strstr(ops, "bind"); if (prop) { u32_t ip; u32_t port; int r; NET_LOCK_TAKE(); ic = net_conn_get_internal(nc); //ic = net_verify_tcp_connection(nc, &ret); if (NULL == ic) goto release; r = sscanf(prop, "bind:%x:%d", &ip, &port); if (r != 2) goto release; port &= 0xFFFF; ret = net_bind(spdid, nc, ip, port); NET_LOCK_RELEASE(); } prop = strstr(ops, "listen"); if (prop) { int r; unsigned int q; NET_LOCK_TAKE(); ic = net_conn_get_internal(nc); //ic = net_verify_tcp_connection(nc, &ret); if (NULL == ic) goto release; r = sscanf(prop, "listen:%d", &q); if (r != 1) goto release; printc("net_listen q %d\n", q); ret = net_listen(spdid, nc, q); NET_LOCK_RELEASE(); } done: return ret; release: NET_LOCK_RELEASE(); goto done; }
static void srvlisten_rtmplive_proc(void *pArg) { SRV_LISTENER_CFG_T *pListenCfg = (SRV_LISTENER_CFG_T *) pArg; NETIO_SOCK_T netsocksrv; struct sockaddr_in sa; int rc = 0; char buf[SAFE_INET_NTOA_LEN_MAX]; logutil_tid_add(pthread_self(), pListenCfg->tid_tag); memset(&sa, 0, sizeof(sa)); memset(&netsocksrv, 0, sizeof(netsocksrv)); sa.sin_family = PF_INET; sa.sin_addr = pListenCfg->sain.sin_addr; sa.sin_port = pListenCfg->sain.sin_port; netsocksrv.flags = pListenCfg->netflags; if((NETIOSOCK_FD(netsocksrv) = net_listen(&sa, 5)) == INVALID_SOCKET) { logutil_tid_remove(pthread_self()); return; } pthread_mutex_lock(&pListenCfg->mtx); pListenCfg->pnetsockSrv = &netsocksrv; pthread_mutex_unlock(&pListenCfg->mtx); LOG(X_INFO("rtmp %sserver available at rtmp://%s:%d max:%d"), ((pListenCfg->netflags & NETIO_FLAG_SSL_TLS) ? "(SSL) " : ""), net_inet_ntoa(sa.sin_addr, buf), ntohs(sa.sin_port), pListenCfg->max); // // Service any client connections on the live listening port // rc = srvlisten_loop(pListenCfg, srv_rtmp_proc); pthread_mutex_lock(&pListenCfg->mtx); pListenCfg->pnetsockSrv = NULL; netio_closesocket(&netsocksrv); pthread_mutex_unlock(&pListenCfg->mtx); LOG(X_WARNING("rtmp listener thread exiting with code: %d"), rc); logutil_tid_remove(pthread_self()); return; }
static struct net_connection* start_listening_socket(const char* bind_addr, uint16_t port, int backlog, struct hub_info* hub) { struct net_connection* server; struct sockaddr_storage addr; socklen_t sockaddr_size; int sd, ret; if (ip_convert_address(bind_addr, port, (struct sockaddr*) &addr, &sockaddr_size) == -1) { return 0; } sd = net_socket_create(addr.ss_family, SOCK_STREAM, IPPROTO_TCP); if (sd == -1) { return 0; } if ((net_set_reuseaddress(sd, 1) == -1) || (net_set_nonblocking(sd, 1) == -1)) { net_close(sd); return 0; } ret = net_bind(sd, (struct sockaddr*) &addr, sockaddr_size); if (ret == -1) { LOG_ERROR("hub_start_service(): Unable to bind to TCP local address. errno=%d, str=%s", net_error(), net_error_string(net_error())); net_close(sd); return 0; } ret = net_listen(sd, backlog); if (ret == -1) { LOG_ERROR("hub_start_service(): Unable to listen to socket"); net_close(sd); return 0; } server = net_con_create(); net_con_initialize(server, sd, net_on_accept, hub, NET_EVENT_READ); return server; }
static void add_listen(const char *ircnet, int port) { LISTEN_REC *rec; IPADDR ip4, ip6, *my_ip; if (port <= 0 || *ircnet == '\0') return; /* bind to specific host/ip? */ my_ip = NULL; if (*settings_get_str("irssiproxy_bind") != '\0') { if (net_gethostbyname(settings_get_str("irssiproxy_bind"), &ip4, &ip6) != 0) { printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Proxy: can not resolve '%s' - aborting", settings_get_str("irssiproxy_bind")); return; } my_ip = ip6.family == 0 ? &ip4 : ip4.family == 0 || settings_get_bool("resolve_prefer_ipv6") ? &ip6 : &ip4; } rec = g_new0(LISTEN_REC, 1); rec->ircnet = g_strdup(ircnet); rec->port = port; rec->handle = net_listen(my_ip, &rec->port); if (rec->handle == NULL) { printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Proxy: Listen in port %d failed: %s", rec->port, g_strerror(errno)); g_free(rec->ircnet); g_free(rec); return; } rec->tag = g_input_add(rec->handle, G_INPUT_READ, (GInputFunction) sig_listen, rec); proxy_listens = g_slist_append(proxy_listens, rec); }
static s32 helper_setup_socket( u16 port) { s32 sock; int ret; //u32 clientlen; struct sockaddr_in server; struct sockaddr_in client; //clientlen = sizeof(client); sock = net_socket (AF_INET, SOCK_STREAM, IPPROTO_IP); if (sock == INVALID_SOCKET) { printf( "setup():INVALID_SOCKET\n"); return -1; } memset (&server, 0, sizeof (server)); memset (&client, 0, sizeof (client)); server.sin_family = AF_INET; server.sin_port = htons (port); server.sin_addr.s_addr = INADDR_ANY; ret = net_bind (sock, (struct sockaddr *) &server, sizeof (server)); if ( ret ) { printf( "net_bind():INVALID_SOCKET\n"); net_close( sock); return -1; } if ( (ret = net_listen( sock, 1)) ) { printf( "net_listen():INVALID_SOCKET\n"); net_close( sock); return -1; } #if 0 ret = setNonblocking(sock); printf( "setNonblocking(%d) returns=%d.\n", sock, ret); #endif return sock; }
int main(int argc, const char *argv[]) { database db; db_init(&db); uv_loop_t *loop = uv_default_loop(); uv_tcp_t server = {}; net_listen(&server, &db, loop, 8766); uv_timer_t t; uv_timer_init(loop, &t); // uv_timer_start(&t, timer_cb, 0, 1000); printf("Listening on port 8766\n"); uv_set_process_title("shared"); uv_run(loop); db_free(&db); }
/* command: DCC CHAT */ static void cmd_dcc_chat(const char *data, IRC_SERVER_REC *server) { DCC_REC *dcc; IPADDR own_ip; char *str, host[MAX_IP_LEN]; int port, handle; g_return_if_fail(data != NULL); if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS); dcc = dcc_find_item(DCC_TYPE_CHAT, data, NULL); if (dcc != NULL) { /* found from dcc list - so we're the connecting side.. */ dcc_chat_connect(dcc); return; } /* send dcc chat request */ if (server == NULL || !server->connected) cmd_return_error(CMDERR_NOT_CONNECTED); if (net_getsockname(server->handle, &own_ip, NULL) == -1) cmd_return_error(CMDERR_ERRNO); port = settings_get_int("dcc_port"); handle = net_listen(&own_ip, &port); if (handle == -1) cmd_return_error(CMDERR_ERRNO); dcc = dcc_create(DCC_TYPE_CHAT, handle, data, "chat", server, NULL); dcc->tagread = g_input_add(dcc->handle, G_INPUT_READ, (GInputFunction) dcc_chat_listen, dcc); /* send the request */ dcc_make_address(&own_ip, host); str = g_strdup_printf("PRIVMSG %s :\001DCC CHAT CHAT %s %d\001", data, host, port); irc_send_cmd(server, str); g_free(str); }