17 #define FD_SETSIZE 16384 18 #define poll(pfd,nfds,timeout) WSAPoll(pfd,nfds,timeout) 19 #define ioctl ioctlsocket 21 #define closesock myclosesocket 23 #define writesock send 25 #define EINTR WSAEINTR 26 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL 27 #define EWOULDBLOCK WSAEWOULDBLOCK 28 #define ECONNREFUSED WSAECONNREFUSED 29 #define HOST_NOT_FOUND WSAHOST_NOT_FOUND 30 #define NO_DATA WSANO_DATA 36 #define closesock(s) close(s) 37 #define readsock(a,b,c,d) read(a,b,c) 39 #if defined(__linux__) && !defined (darwin) 40 #define writesock(a,b,c,d) send(a,b,c,MSG_NOSIGNAL) 42 #define writesock(a,b,c,d) write(a,b,c) 59 #include <sys/socket.h> 61 #include <netinet/in.h> 62 #include <netinet/tcp.h> 64 #include <sys/ioctl.h> 72 #define MY_FD_ZERO(set) 73 #define MY_FD_SET(fd, set) poll_add(fd) 74 #define MY_FD_CLR(fd, set) 75 #define MY_FD_ISSET(fd, set) poll_test(fd) 77 #define MY_FD_ZERO(set) FD_ZERO(set) 78 #define MY_FD_SET(fd, set) FD_SET(fd, set) 79 #define MY_FD_CLR(fd, set) FD_CLR(fd, set) 80 #define MY_FD_ISSET(fd, set) FD_ISSET(fd, set) 88 #define ushort unsigned short 96 static struct sockaddr_in DIM_sockname;
195 WORD wVersionRequested;
198 static int sock_init_done = 0;
200 if(sock_init_done)
return(1);
201 wVersionRequested = MAKEWORD( 2, 0 );
202 err = WSAStartup( wVersionRequested, &wsaData );
215 if ( LOBYTE( wsaData.wVersion ) != 2 ||
216 HIBYTE( wsaData.wVersion ) != 0 )
225 int myclosesocket(
int path)
228 code = WSAGetLastError();
229 ret = closesocket(path);
230 WSASetLastError(code);
242 void create_io_thread(
void);
244 struct sigaction sig_info;
247 void dummy_io_sig_handler();
268 sigaddset(&
set,SIGALRM);
270 sig_info.sa_mask =
set;
272 sig_info.sa_flags = SA_RESTART;
274 sig_info.sa_flags = 0;
277 if( sigaction(SIGIO, &sig_info, 0) < 0 )
279 perror(
"sigaction(SIGIO)" );
285 sig_info.sa_mask =
set;
287 sig_info.sa_flags = SA_RESTART;
289 sig_info.sa_flags = 0;
292 if( sigaction(SIGPIPE, &sig_info, 0) < 0 ) {
293 perror(
"sigaction(SIGPIPE)" );
304 if( (
DIM_IO_path[0] = (
int)socket(AF_INET, SOCK_STREAM, 0)) == -1 )
310 DIM_sockname.sin_family = PF_INET;
312 DIM_sockname.sin_addr = *((
struct in_addr *) &addr);
313 DIM_sockname.sin_port = htons((
ushort) 2000);
357 int ret = 1, flags = 1;
365 printf(
"Enabling signals on channel 0\n");
383 if( (
DIM_IO_path[0] = (
int)socket(AF_INET, SOCK_STREAM, 0)) == -1 )
391 perror(
"ioctlsocket");
411 ret = ioctl(
Net_conns[conn_id].channel, SIOCSPGRP, &pid );
413 ret = fcntl(
Net_conns[conn_id].channel,F_SETOWN, pid);
418 printf(
"ioctl returned -1\n");
423 ret = ioctl(
Net_conns[conn_id].channel, FIOASYNC, &flags );
427 printf(
"ioctl1 returned -1\n");
432 flags = fcntl(
Net_conns[conn_id].channel,F_GETFD,0);
439 ret = fcntl(
Net_conns[conn_id].channel,F_SETFD, flags | FD_CLOEXEC );
443 printf(
"ioctl2 returned -1\n");
452 int tcpip_get_send_space(
int conn_id)
456 ret = ioctl(
Net_conns[conn_id].channel, TIOCOUTQ, &n_bytes );
460 printf(
"Couln't get send buffer free size, ret = %d\n", ret);
485 static struct pollfd *Pollfds = 0;
486 static int Pollfd_size = 0;
488 static int poll_create()
494 Pollfds =
malloc(Pollfd_size *
sizeof(
struct pollfd));
496 for(i = 0; i < Pollfd_size; i++)
498 Pollfds[
i].events = POLLIN;
505 Pollfds =
malloc(Pollfd_size *
sizeof(
struct pollfd));
507 for(i = 0; i < Pollfd_size; i++)
509 Pollfds[
i].events = POLLIN;
515 static int poll_add(
int fd)
521 static int poll_test(
int fd)
523 if(Pollfds[0].fd == fd)
525 if( (Pollfds[0].revents & POLLIN) || (Pollfds[0].revents & POLLHUP) )
527 Pollfds[0].revents = 0;
575 int index = *conn_id;
578 for( i = index; i < Pollfd_size; i++ )
581 (Pollfds[i].revents & POLLIN) || (Pollfds[i].revents & POLLHUP) ) )
583 Pollfds[
i].revents = 0;
609 #if defined(__linux__) && !defined (darwin) 611 void tcpip_set_keepalive(
int channel,
int tmout )
617 setsockopt(channel, SOL_SOCKET, SO_KEEPALIVE, (
char*)&val,
sizeof(val));
622 setsockopt(channel, IPPROTO_TCP, TCP_KEEPIDLE, (
char*)&val,
sizeof(val));
624 setsockopt(channel, IPPROTO_TCP, TCP_KEEPCNT, (
char*)&val,
sizeof(val));
626 setsockopt(channel, IPPROTO_TCP, TCP_KEEPINTVL, (
char*)&val,
sizeof(val));
641 cur_time =
time(NULL);
654 #if defined(__linux__) && !defined (darwin) 655 tcpip_set_keepalive(
Net_conns[conn_id].channel, timeout);
689 for(i = 0; i < 3; i++)
691 ret = ioctl(
Net_conns[conn_id].channel, FIONREAD, &count );
730 while( size > 0 && count > 0 )
769 struct sockaddr_in other;
772 othersize =
sizeof(other);
773 memset( (
char *) &other, 0, (
size_t)othersize );
775 (
struct sockaddr*)&other, (
unsigned int *)&othersize );
814 int conn_id, ret, selret,
count;
815 struct timeval timeout;
824 selret = poll(Pollfds, Pollfd_size, 0);
826 selret = select(FD_SETSIZE, &rfds, NULL, NULL, &timeout);
868 int conn_id, ret,
count;
887 ret = poll(Pollfds, Pollfd_size, -1);
889 ret = select(FD_SETSIZE, &rfds, NULL, &efds, NULL);
893 printf(
"poll returned %d, errno %d\n", ret, errno);
964 printf(
"START_READ - enable_sig returned -1\n");
978 ptr = (
unsigned char *)node+(
int)strlen(node)+1;
983 if( (ipaddr[0] == 0xff) &&
984 (ipaddr[1] == 0xff) &&
985 (ipaddr[2] == 0xff) &&
986 (ipaddr[3] == 0xff) )
988 errno = ECONNREFUSED;
990 WSASetLastError(errno);
994 if( gethostbyaddr(ipaddr,
sizeof(ipaddr), AF_INET) == (
struct hostent *)0 )
999 ret = WSAGetLastError();
1001 if((ret == HOST_NOT_FOUND) || (ret == NO_DATA))
1019 struct sockaddr_in sockname;
1021 struct hostent *host = 0;
1025 int path, val, ret_code, ret;
1028 unsigned char ipaddr_buff[4];
1029 unsigned char *ipaddr = ipaddr_buff;
1030 int host_number = 0;
1033 if(isdigit(node[0]))
1035 sscanf(node,
"%d.%d.%d.%d",&a, &b, &c, &d);
1036 ipaddr[0] = (
unsigned char)a;
1037 ipaddr[1] = (
unsigned char)b;
1038 ipaddr[2] = (
unsigned char)c;
1039 ipaddr[3] = (
unsigned char)d;
1060 else if( (host = gethostbyname(node)) == (
struct hostent *)0 )
1094 *(strchr(node,
'.')) =
'\0';
1095 host_addr = hostGetByName(node);
1096 printf(
"node %s addr: %x\n",node, host_addr);
1099 if( (path = (
int)socket(AF_INET, SOCK_STREAM, 0)) == -1 )
1107 if ((ret_code = setsockopt(path, IPPROTO_TCP, TCP_NODELAY,
1108 (
char*)&val,
sizeof(val))) == -1 )
1111 printf(
"Couln't set TCP_NODELAY\n");
1118 if ((ret_code = setsockopt(path, SOL_SOCKET, SO_SNDBUF,
1119 (
char*)&val,
sizeof(val))) == -1 )
1122 printf(
"Couln't set SO_SNDBUF\n");
1129 if ((ret_code = setsockopt(path, SOL_SOCKET, SO_RCVBUF,
1130 (
char*)&val,
sizeof(val))) == -1 )
1133 printf(
"Couln't set SO_RCVBUF\n");
1139 #if defined(__linux__) && !defined (darwin) 1141 if ((ret_code = setsockopt(path, IPPROTO_TCP, TCP_SYNCNT,
1142 (
char*)&val,
sizeof(val))) == -1 )
1145 printf(
"Couln't set TCP_SYNCNT\n");
1150 sockname.sin_family = PF_INET;
1153 sockname.sin_addr = *((
struct in_addr *) ipaddr);
1155 sockname.sin_addr = *((
struct in_addr *) host->h_addr);
1158 sockname.sin_addr = *((
struct in_addr *) ipaddr);
1160 sockname.sin_addr = *((
struct in_addr *) &host_addr);
1162 sockname.sin_port = htons((
ushort) port);
1163 while((ret = connect(path, (
struct sockaddr*)&sockname,
sizeof(sockname))) == -1 )
1171 strcpy(
Net_conns[conn_id].node, node );
1172 strcpy(
Net_conns[conn_id].task, task );
1187 struct sockaddr_in sockname;
1188 int path, val, ret_code, ret;
1191 if( (path = (
int)socket(AF_INET, SOCK_STREAM, 0)) == -1 )
1197 if ((ret_code = setsockopt(path, IPPROTO_TCP, TCP_NODELAY,
1198 (
char*)&val,
sizeof(val))) == -1 )
1202 printf(
"Couln't set TCP_NODELAY\n");
1209 if ((ret_code = setsockopt(path, SOL_SOCKET, SO_SNDBUF,
1210 (
void *)&val,
sizeof(val))) == -1 )
1213 printf(
"Couln't set SO_SNDBUF\n");
1232 if ((ret_code = setsockopt(path, SOL_SOCKET, SO_RCVBUF,
1233 (
void *)&val,
sizeof(val))) == -1 )
1236 printf(
"Couln't set SO_RCVBUF\n");
1248 sockname.sin_family = AF_INET;
1249 sockname.sin_addr.s_addr = INADDR_ANY;
1250 sockname.sin_port = htons((
ushort) *port);
1252 errno = EADDRNOTAVAIL;
1255 WSASetLastError(errno);
1259 ret = bind(path, (
struct sockaddr*)&sockname,
sizeof(sockname));
1263 }
while( ret == -1 );
1270 if( setsockopt(path, SOL_SOCKET, SO_REUSEADDR, (
char*)&val,
1271 sizeof(val)) == -1 )
1274 printf(
"Couln't set SO_REUSEADDR\n");
1280 sockname.sin_family = AF_INET;
1281 sockname.sin_addr.s_addr = INADDR_ANY;
1282 sockname.sin_port = htons((
ushort) *port);
1283 if( (ret = bind(path, (
struct sockaddr*) &sockname,
sizeof(sockname))) == -1 )
1297 strcpy(
Net_conns[conn_id].task, task );
1322 printf(
"START_LISTEN - enable_sig returned -1\n");
1341 if ((ret_code = setsockopt(path, IPPROTO_TCP, TCP_NODELAY,
1342 (
char*)&val,
sizeof(val))) == -1 )
1345 printf(
"Couln't set TCP_NODELAY\n");
1351 if ((ret_code = setsockopt(path, SOL_SOCKET, SO_SNDBUF,
1352 (
char*)&val,
sizeof(val))) == -1 )
1355 printf(
"Couln't set SO_SNDBUF\n");
1362 if ((ret_code = setsockopt(path, SOL_SOCKET, SO_RCVBUF,
1363 (
char*)&val,
sizeof(val))) == -1 )
1366 printf(
"Couln't set SO_RCVBUF\n");
1385 strcpy( node,
Net_conns[conn_id].node );
1386 strcpy( task,
Net_conns[conn_id].task );
1410 ret = ioctl(channel, FIONBIO, &flags );
1414 printf(
"ioctl non block returned -1\n");
1424 ret = ioctl(channel, FIONBIO, &flags );
1428 printf(
"ioctl block returned -1\n");
1439 int wrote, ret, selret;
1442 struct pollfd pollitem;
1444 struct timeval timeout;
1458 ret = WSAGetLastError();
1475 pollitem.events = POLLOUT;
1476 pollitem.revents = 0;
1480 timeout.tv_usec = 0;
1482 FD_SET(
Net_conns[conn_id].channel, &wfds);
1483 selret = select(FD_SETSIZE, NULL, &wfds, NULL, &timeout);
1530 #if defined(__linux__) && !defined (darwin) 1531 shutdown(channel, 2);
1547 if(code == EWOULDBLOCK)
1560 my_perror(
"tcpip", code);
1565 int my_perror(
char *
str,
int error)
1570 code = WSAGetLastError();
1573 printf(
"new - %s\n",strerror(code));
1577 case WSAEWOULDBLOCK:
1578 printf(
"Operation would block");
1580 case WSAEINPROGRESS:
1581 printf(
"Operation now in progress");
1584 printf(
"Operation already in progress");
1587 printf(
"Socket operation on non-socket");
1589 case WSAEDESTADDRREQ:
1590 printf(
"Destination address required");
1593 printf(
"Message too long");
1596 printf(
"Protocol wrong type for socket");
1598 case WSAENOPROTOOPT:
1599 printf(
"Protocol not available");
1601 case WSAEPROTONOSUPPORT:
1602 printf(
"Protocol not supported");
1604 case WSAESOCKTNOSUPPORT:
1605 printf(
"Socket type not supported");
1608 printf(
"Operation not supported on transport endpoint");
1610 case WSAEPFNOSUPPORT:
1611 printf(
"Protocol family not supported");
1613 case WSAEAFNOSUPPORT:
1614 printf(
"Address family not supported by protocol");
1617 printf(
"Address already in use");
1619 case WSAEADDRNOTAVAIL:
1620 printf(
"Cannot assign requested address");
1623 printf(
"Network is down");
1625 case WSAENETUNREACH:
1626 printf(
"Network is unreachable");
1629 printf(
"Network dropped connection because of reset");
1631 case WSAECONNABORTED:
1632 printf(
"Software caused connection abort");
1635 printf(
"Connection reset by peer");
1638 printf(
"No buffer space available");
1641 printf(
"Transport endpoint is already connected");
1644 printf(
"Transport endpoint is not connected");
1647 printf(
"Cannot send after transport endpoint shutdown");
1649 case WSAETOOMANYREFS:
1650 printf(
"Too many references: cannot splice");
1653 printf(
"Connection timed out");
1655 case WSAECONNREFUSED:
1656 printf(
"Connection refused");
1659 printf(
"Too many symbolic links encountered");
1661 case WSAENAMETOOLONG:
1662 printf(
"File name too long");
1665 printf(
"Host is down");
1667 case WSAEHOSTUNREACH:
1668 printf(
"No route to host");
1671 printf(
"Directory not empty");
1674 printf(
"Too many users");
1677 printf(
"Quota exceeded");
1680 printf(
"Stale NFS file handle");
1683 printf(
"Object is remote");
1685 case WSAHOST_NOT_FOUND:
1686 printf(
"Host not found");
1689 printf(
"Host not found, or SERVERFAIL");
1691 case WSANO_RECOVERY:
1692 printf(
"Non recoverable errors, FORMERR, REFUSED, NOTIMP");
1695 printf(
"Valid name, no data record of requested type");
1698 printf(
"Unknown error %d",code);
1704 void my_strerror(
int error,
char *msg)
1710 code = WSAGetLastError();
1715 case WSAEWOULDBLOCK:
1716 sprintf(str,
"Operation would block");
1718 case WSAEINPROGRESS:
1719 sprintf(str,
"Operation now in progress");
1722 sprintf(str,
"Operation already in progress");
1725 sprintf(str,
"Socket operation on non-socket");
1727 case WSAEDESTADDRREQ:
1728 sprintf(str,
"Destination address required");
1731 sprintf(str,
"Message too long");
1734 sprintf(str,
"Protocol wrong type for socket");
1736 case WSAENOPROTOOPT:
1737 sprintf(str,
"Protocol not available");
1739 case WSAEPROTONOSUPPORT:
1740 sprintf(str,
"Protocol not supported");
1742 case WSAESOCKTNOSUPPORT:
1743 sprintf(str,
"Socket type not supported");
1746 sprintf(str,
"Operation not supported on transport endpoint");
1748 case WSAEPFNOSUPPORT:
1749 sprintf(str,
"Protocol family not supported");
1751 case WSAEAFNOSUPPORT:
1752 sprintf(str,
"Address family not supported by protocol");
1755 sprintf(str,
"Address already in use");
1757 case WSAEADDRNOTAVAIL:
1758 sprintf(str,
"Cannot assign requested address");
1761 sprintf(str,
"Network is down");
1763 case WSAENETUNREACH:
1764 sprintf(str,
"Network is unreachable");
1767 sprintf(str,
"Network dropped connection because of reset");
1769 case WSAECONNABORTED:
1770 sprintf(str,
"Software caused connection abort");
1773 sprintf(str,
"Connection reset by peer");
1776 sprintf(str,
"No buffer space available");
1779 sprintf(str,
"Transport endpoint is already connected");
1782 sprintf(str,
"Transport endpoint is not connected");
1785 sprintf(str,
"Cannot send after transport endpoint shutdown");
1787 case WSAETOOMANYREFS:
1788 sprintf(str,
"Too many references: cannot splice");
1791 sprintf(str,
"Connection timed out");
1793 case WSAECONNREFUSED:
1794 sprintf(str,
"Connection refused");
1797 sprintf(str,
"Too many symbolic links encountered");
1799 case WSAENAMETOOLONG:
1800 sprintf(str,
"File name too long");
1805 case WSAEHOSTUNREACH:
1806 sprintf(str,
"No route to host");
1809 sprintf(str,
"Directory not empty");
1812 sprintf(str,
"Too many users");
1815 sprintf(str,
"Quota exceeded");
1818 sprintf(str,
"Stale NFS file handle");
1821 sprintf(str,
"Object is remote");
1823 case WSAHOST_NOT_FOUND:
1824 sprintf(str,
"Host not found");
1827 sprintf(str,
"Host not found, or SERVERFAIL");
1829 case WSANO_RECOVERY:
1830 sprintf(str,
"Non recoverable errors, FORMERR, REFUSED, NOTIMP");
1833 sprintf(str,
"Valid name, no data record of requested type");
1836 sprintf(str,
"Unknown error %d",code);
1847 if((errno == 0) && (h_errno == HOST_NOT_FOUND))
1848 strcpy(str,
"Host not found");
1850 strcpy(str, strerror(errno));
1852 my_strerror(code, str);
int dim_get_keepalive_timeout()
int tcpip_start_listen(int conn_id, void(*ast_routine)())
void dna_test_write(int conn_id)
DllExp DIM_NOSHARE int Curr_N_Conns
static void tcpip_test_write(int conn_id)
void io_sig_handler(int num)
int dim_get_listen_backlog()
DllExp DIM_NOSHARE NET_CONNECTION * Net_conns
int dim_get_read_buffer_size()
void dim_usleep(int usecs)
void tcpip_get_error(char *str, int code)
static int get_bytes_to_read(int conn_id)
#define MY_FD_SET(fd, set)
static int DIM_IO_path[2]
#define MY_FD_ISSET(fd, set)
int dim_tcpip_init(int thr_flag)
int tcpip_would_block(int code)
int Tcpip_max_io_data_write
void dim_set_listen_backlog(int size)
int tcpip_write_nowait(int conn_id, char *buffer, int size)
void tcpip_rem_test_write(int conn_id)
int set_blocking(int channel)
int dtq_rem_entry(int queue_id, TIMR_ENT *entry)
static int do_read(int conn_id)
int tcpip_start_read(int conn_id, char *buffer, int size, void(*ast_routine)())
#define writesock(a, b, c, d)
#define readsock(a, b, c, d)
void dim_set_write_timeout(int secs)
int tcpip_close(int conn_id)
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
void dna_report_error(int conn_id, int code, char *routine_name, int severity, int errcode)
static int fds_get_entry(fd_set *fds, int *conn_id)
void dim_set_keepalive_timeout(int secs)
int Tcpip_max_io_data_read
int tcpip_write(int conn_id, char *buffer, int size)
#define MY_FD_CLR(fd, set)
TIMR_ENT * dtq_add_entry(int queue_id, int time, void(*user_routine)(), dim_long tag)
DllExp DIM_NOSHARE DNA_CONNECTION * Dna_conns
static int Keepalive_timeout_set
int tcpip_open_client(int conn_id, char *node, char *task, int port)
int dim_set_write_buffer_size(int size)
static int enable_sig(int conn_id)
void tcpip_get_node_task(int conn_id, char *node, char *task)
static int Write_buffer_size
static int Read_buffer_size
void tcpip_task(void *dummy)
int dim_get_write_timeout()
int tcpip_open_server(int conn_id, char *task, int *port)
static int Listen_backlog
void tcpip_pipe_sig_handler(int num)
int check_node_addr(char *node, unsigned char *ipaddr)
int dim_get_write_buffer_size()
int get_keepalive_tmout()
int tcpip_failure(int code)
sprintf(name1,"NewService%d", i)
static int Write_timeout_set
void do_accept(int conn_id)
static int list_to_fds(fd_set *fds)
void tcpip_set_test_write(int conn_id, int timeout)
int tcpip_open_connection(int conn_id, int path)
int dim_set_read_buffer_size(int size)
void tcpip_report_error(int code)
int set_non_blocking(int channel)