static void setlport(Conv *c) { if(c->restricted == 0 && c->lport == 0) return; so_bind(c->sfd, c->restricted, c->lport); }
/* * pick a local port and set it */ static void setlport(Conv *c) { uchar laddr[IPaddrlen]; ushort p; so_bind(c->sfd, c->restricted, c->laddr, c->lport); if(c->lport == 0 || ipcmp(c->laddr, IPnoaddr) == 0){ so_getsockname(c->sfd, laddr, &p); if(c->lport == 0) c->lport = p; if(ipcmp(c->laddr, IPnoaddr) == 0) memmove(c->laddr, laddr, sizeof laddr); } }
static void test_tcp_server(void) { int re; int sd; int reader = 0; char buf[5]; struct sockaddr_in sa; struct sockaddr_in sa2; socklen_t sa_len; printf("[tcp(server)] start\n"); sd = so_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if ( sd < 0 ) { goto error; } DEBUG_PRINT(("server_task: so_socket = %d(%d, %d)\n", sd, MERCD(sd), SERCD(sd))); bzero(&sa, sizeof sa); sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_ANY); sa.sin_port = htons(12345); re = so_bind(sd, (struct sockaddr*)&sa, sizeof sa); DEBUG_PRINT(("server_task: so_bind = %d(%d, %d)\n", re, MERCD(re), SERCD(re))); if ( re < 0 ) { goto error; } re = so_listen(sd, 5); DEBUG_PRINT(("server_task: so_listen = %d(%d, %d)\n", re, MERCD(re), SERCD(re))); if ( re < 0 ) { goto error; } tk_sig_sem(semid, 1); DEBUG_PRINT(("server_task: server semaphore signaled 1\n")); reader = so_accept(sd, (struct sockaddr*)&sa2, &sa_len); DEBUG_PRINT(("server_task: so_accept = %d(%d, %d)\n", reader, MERCD(reader), SERCD(reader))); if ( reader < 0 ) { goto error; } wait_data(reader); bzero(buf, sizeof buf); re = so_sockatmark(reader); DEBUG_PRINT(("server_task: so_sockatmark = %d(%d, %d)\n", re, MERCD(re), SERCD(re))); if ( re < 0 ) { goto error; } re = so_read(reader, buf, 4); DEBUG_PRINT(("server_task: so_read = %d(%d, %d), buf = %s\n", re, MERCD(re), SERCD(re), buf)); if ( re < 0 || memcmp(buf, "1234", 4) != 0 ) { goto error; } wait_data(reader); bzero(buf, sizeof buf); re = so_sockatmark(reader); DEBUG_PRINT(("server_task: so_sockatmark = %d(%d, %d)\n", re, MERCD(re), SERCD(re))); if ( re < 0 ) { goto error; } re = so_recv(reader, buf, 4, MSG_OOB); DEBUG_PRINT(("server_task: so_recv = %d(%d, %d), buf = %s\n", re, MERCD(re), SERCD(re), buf)); if ( re < 0 || buf[0] != 'a' ) { goto error; } tk_sig_sem(semid2, 1); DEBUG_PRINT(("server_task: server semaphore for break signaled 2\n")); DEBUG_PRINT(("server_task: pre-accept for break\n")); re = so_accept(sd, (struct sockaddr*)&sa2, &sa_len); DEBUG_PRINT(("server_task: so_accept = %d(%d, %d)\n", re, MERCD(re), SERCD(re))); if ( re != EX_INTR ) { goto error; } so_close(reader); so_close(sd); printf("[tcp(server)] OK\n"); return; error: printf("[tcp(server)] FAILED\n"); if ( sd > 0 ) { so_close(sd); } if ( reader > 0 ) { so_close(reader); } tk_del_sem(semid2); return; }
BOOL cg_socket_bind(CgSocket *sock, int bindPort, char *bindAddr, BOOL bindFlag, BOOL reuseFlag) { #if defined(BTRON) || defined(TENGINE) struct sockaddr_in sockaddr; ERR ret; #elif defined(ITRON) T_UDP_CCEP udpccep = { 0, { IPV4_ADDRANY, UDP_PORTANY }, (FP)cg_socket_udp_callback }; T_TCP_CREP tcpcrep = { 0, { IPV4_ADDRANY, 0 } }; T_TCP_CCEP tcpccep = { 0, sock->sendWinBuf, CG_NET_SOCKET_WINDOW_BUFSIZE, sock->recvWinBuf, CG_NET_SOCKET_WINDOW_BUFSIZE, (FP)cg_socket_tcp_callback }; #else struct addrinfo *addrInfo; int ret; #endif cg_log_debug_l4("Entering...\n"); if (bindPort <= 0 /* || bindAddr == NULL*/) return FALSE; #if defined(BTRON) || (defined(TENGINE) && !defined(CG_TENGINE_NET_KASAGO)) if (cg_socket_tosockaddrin(bindAddr, bindPort, &sockaddr, bindFlag) == FALSE) return FALSE; cg_socket_setid(sock, so_socket(PF_INET, cg_socket_getrawtype(sock), 0)); if (sock->id < 0) return FALSE; if (reuseFlag == TRUE) { if (cg_socket_setreuseaddress(sock, TRUE) == FALSE) { cg_socket_close(sock); return FALSE; } } ret = so_bind(sock->id, (SOCKADDR *)&sockaddr, sizeof(struct sockaddr_in)); if (ret < 0) { cg_socket_close(sock); return FALSE; } #elif defined(TENGINE) && defined(CG_TENGINE_NET_KASAGO) if (cg_socket_tosockaddrin(bindAddr, bindPort, &sockaddr, bindFlag) == FALSE) return FALSE; cg_socket_setid(sock, ka_socket( PF_INET, cg_socket_getrawtype(sock), cg_socket_getprototype(sock))); if (sock->id < 0) return FALSE; /* if (cg_socket_setmulticastinterface(sock, bindAddr) == FALSE) return FALSE; */ if (reuseFlag == TRUE) { if (cg_socket_setreuseaddress(sock, TRUE) == FALSE) { cg_socket_close(sock); return FALSE; } } ret = ka_bind(sock->id, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr_in)); if (ret < 0) { cg_socket_close(sock); return FALSE; } #elif defined(ITRON) cg_socket_setid(sock, cg_socket_getavailableid(cg_socket_issocketstream(sock))); if (sock->id < 0) return FALSE; if (cg_socket_issocketstream(sock) == TRUE) { if (bindAddr != NULL) tcpcrep.myaddr.ipaddr = ascii_to_ipaddr(bindAddr); tcpcrep.myaddr.ipaddr = htons(bindPort); if (tcp_cre_rep(sock->id, &tcpcrep) != E_OK) { cg_socket_close(sock); return FALSE; } if (tcp_cre_cep(sock->id, &tcpccep) != E_OK) { cg_socket_close(sock); return FALSE; } } else { if (bindAddr != NULL) udpccep.myaddr.ipaddr = ascii_to_ipaddr(bindAddr); udpccep.myaddr.ipaddr = htons(bindPort); if (udp_cre_cep(sock->id, &udpccep) != E_OK) { cg_socket_close(sock); return FALSE; } } #else if (cg_socket_tosockaddrinfo(cg_socket_getrawtype(sock), bindAddr, bindPort, &addrInfo, bindFlag) == FALSE) return FALSE; cg_socket_setid(sock, socket(addrInfo->ai_family, addrInfo->ai_socktype, 0)); if (sock->id== -1) { cg_socket_close(sock); return FALSE; } if (reuseFlag == TRUE) { if (cg_socket_setreuseaddress(sock, TRUE) == FALSE) { cg_socket_close(sock); return FALSE; } } ret = bind(sock->id, addrInfo->ai_addr, addrInfo->ai_addrlen); freeaddrinfo(addrInfo); #endif #if !defined(ITRON) if (ret != 0) return FALSE; #endif cg_socket_setdirection(sock, CG_NET_SOCKET_SERVER); cg_socket_setaddress(sock, bindAddr); cg_socket_setport(sock, bindPort); cg_log_debug_l4("Leaving...\n"); return TRUE; }