60 #include <sys/types.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
69 #define _BSD_SOCKLEN_T_
71 #include <sys/socket.h>
72 #include <sys/ioctl.h>
76 #if defined( HAVE_SYS_SELECT_H )
77 #include <sys/select.h>
79 #if defined( HAVE_SYS_FILIO_H )
80 #include <sys/filio.h>
84 #define psock_errno(s) fprintf(stderr, "RxSOCK Error: %s\n", s)
87 #define sock_errno() WSAGetLastError()
89 #define sock_errno() errno
136 RexxRoutine2(
int, SockAccept,
int, sock, OPTIONAL_RexxObjectPtr, stemSource)
141 nameLen =
sizeof(addr);
143 int rc = (int)accept(sock, (
struct sockaddr *)&addr, &nameLen);
191 int rc = bind(sock, (
struct sockaddr *)&addr,
sizeof(addr));
210 int rc = closesocket(sock);
212 int rc = close(sock);
245 int rc = connect(sock,(
struct sockaddr *)&addr,
sizeof(addr));
260 struct hostent *pHostEnt;
270 addr.s_addr = inet_addr(addrArg);
280 pHostEnt = gethostbyaddr((
char*)&addr,
sizeof(addr), domain);
282 setErrno(context, pHostEnt != NULL);
309 struct hostent *pHostEnt;
314 pHostEnt = gethostbyname(name);
316 setErrno(context, pHostEnt != NULL);
343 if (gethostname(pszBuff,
sizeof(pszBuff)))
347 return context->String(
"0.0.0.0");
350 pHostEnt = gethostbyname(pszBuff);
355 return context->String(
"0.0.0.0");
357 ia.s_addr = (*(
uint32_t *)pHostEnt->h_addr);
358 return context->String(inet_ntoa(ia));
360 #if defined(OPSYS_AIX) || defined(OPSYS_LINUX)
361 #define h_addr h_addr_list[0]
364 struct hostent *pHostEnt;
367 if (gethostname(pszBuff,
sizeof(pszBuff)))
371 return context->String(
"0.0.0.0");
374 pHostEnt = gethostbyname(pszBuff);
379 return context->String(
"0.0.0.0");
381 ia.s_addr = (*(
uint32_t *)pHostEnt->h_addr);
382 return context->String(inet_ntoa(ia));
384 ia.s_addr = htonl(gethostid());
387 return context->String(inet_ntoa(ia));
403 int rc = gethostname(pszBuff,
sizeof(pszBuff));
409 return context->String(pszBuff);
432 nameLen =
sizeof(addr);
433 int rc = getpeername(sock,(
struct sockaddr *)&addr,&nameLen);
469 nameLen =
sizeof(addr);
470 int rc = getsockname(sock,(
struct sockaddr *)&addr,&nameLen);
493 struct linger lingStruct;
501 context->InvalidRoutine();
519 len =
sizeof(lingStruct);
530 int rc = getsockopt(sock,SOL_SOCKET,opt,(
char *)ptr,&len);
541 snprintf(buffer,
sizeof buffer,
"%d %d", lingStruct.l_onoff, lingStruct.l_linger);
547 case SOCK_STREAM: strcpy(buffer,
"STREAM");
break;
548 case SOCK_DGRAM: strcpy(buffer,
"DGRAM");
break;
549 case SOCK_RAW: strcpy(buffer,
"RAW");
break;
550 default: strcpy(buffer,
"UNKNOWN");
555 snprintf(buffer,
sizeof buffer,
"%d", intVal);
559 context->SetContextVariable(var, context->String(buffer));
582 if (!context->Int32(var, &temp))
584 context->InvalidRoutine();
587 dataBuff = (int)temp;
595 len =
sizeof(dataBuff);
606 rc = ioctlsocket(sock,cmd,(u_long *)data);
608 rc = ioctl(sock,cmd,data,len);
619 context->SetContextVariable(context->ObjectToStringValue(var), context->Int32(dataBuff));
636 int rc = listen(sock, backlog);
661 char *flagStr = strdup(flagVal);
664 context->InvalidRoutine();
667 const char *pszWord = strtok(flagStr,
" ");
672 pszWord = strtok(NULL,
" ");
680 pBuffer = (
char *)malloc(dataLen);
683 context->InvalidRoutine();
690 rc = recv(sock, pBuffer, dataLen, flags);
704 context->SetContextVariable(var, context->String(pBuffer, dataLen));
721 RexxRoutine5(
int, SockRecvFrom,
int, sock,
CSTRING, var,
int, dataLen,
RexxObjectPtr, flagArg, OPTIONAL_RexxObjectPtr, stemSource)
733 if (stemSource != NULL)
740 char *flagStr = strdup(context->ObjectToStringValue(flagArg));
742 const char *pszWord = strtok(flagStr,
" ");
753 pszWord = strtok(NULL,
" ");
767 addr_size=
sizeof(addr);
772 char *pBuffer = (
char *)malloc(dataLen);
775 context->InvalidRoutine();
782 int rc = recvfrom(sock,pBuffer,dataLen,flags,(
struct sockaddr *)&addr,&addr_size);
798 context->SetContextVariable(var, context->String(pBuffer, dataLen));
815 RexxRoutine4(
int, SockSelect, OPTIONAL_RexxObjectPtr, array1, OPTIONAL_RexxObjectPtr, array2, OPTIONAL_RexxObjectPtr, array3, OPTIONAL_int, timeout)
817 struct timeval timeOutS;
818 struct timeval *timeOutP;
828 #if defined(OPSYS_LINUX)
829 fd_set rSetS, *rSet = &rSetS;
830 fd_set wSetS, *wSet = &wSetS;
831 fd_set eSetS, *eSet = &eSetS;
833 struct fd_set rSetS, *rSet = &rSetS;
834 struct fd_set wSetS, *wSet = &wSetS;
835 struct fd_set eSetS, *eSet = &eSetS;
853 timeOutS.tv_sec = timeout;
854 timeOutS.tv_usec = 0;
855 timeOutP = &timeOutS;
875 for (i=0; i<rCount; i++)
877 FD_SET(rArray[i],rSet);
879 for (i=0; i<wCount; i++)
881 FD_SET(wArray[i],wSet);
883 for (i=0; i<eCount; i++)
885 FD_SET(eArray[i],eSet);
892 for (i=0; i<rCount; i++)
900 for (i=0; i<wCount; i++)
908 for (i=0; i<eCount; i++)
919 rc = select(max+1,rSet,wSet,eSet,timeOutP);
930 for (i=0; i<rCount; i++)
932 if (FD_ISSET(rArray[i],rSet))
934 rArray[j] = rArray[i];
941 for (i=0; i<wCount; i++)
943 if (FD_ISSET(wArray[i],wSet))
945 wArray[j] = wArray[i];
952 for (i=0; i<eCount; i++)
954 if (FD_ISSET(eArray[i],eSet))
956 eArray[j] = eArray[i];
1013 size_t dataLen = context->StringLength(dataObj);
1014 const char *data = context->StringData(dataObj);
1020 if (flagArg != NULL)
1022 char *flagStr = strdup(flagArg);
1023 if (flagStr == NULL)
1025 context->InvalidRoutine();
1029 const char *pszWord = strtok(flagStr,
" ");
1038 flags |= MSG_DONTROUTE;
1041 pszWord = strtok(NULL,
" ");
1050 int rc = send(sock, data, (
int)dataLen, flags);
1076 size_t dataLen = context->StringLength(dataObj);
1077 const char *data = context->StringData(dataObj);
1090 char *flagStr = strdup(context->ObjectToStringValue(flagsOrStem));
1091 if (flagStr == NULL)
1093 context->InvalidRoutine();
1097 const char *pszWord = strtok(flagStr,
" ");
1102 flags |= MSG_DONTROUTE;
1104 pszWord = strtok(NULL,
" ");
1122 int rc = sendto(sock, data, (
int)dataLen, flags, (
struct sockaddr *)&addr,
sizeof(addr));
1140 struct linger lingStruct;
1150 context->InvalidRoutine();
1167 sscanf(arg,
"%d", &intVal);
1172 len =
sizeof(lingStruct);
1174 sscanf(arg,
"%d %d", &intVal,&intVal2);
1175 lingStruct.l_onoff = (u_short)intVal;
1176 lingStruct.l_linger = (u_short)intVal2;
1183 len =
sizeof(lenVal);
1185 sscanf(arg,
"%d", &lenVal);
1196 int rc = setsockopt(sock,SOL_SOCKET,opt,(
const char *)ptr,len);
1218 int rc = shutdown(sock, how);
1238 WORD wVersionRequested;
1240 wVersionRequested = MAKEWORD( 1, 1 );
1241 int rc = WSAStartup( wVersionRequested, &wsaData );
1272 pszDomain = strdup(domainArg);
1273 pszType = strdup(typeArg);
1274 pszProtocol = strdup(protocolArg);
1286 context->InvalidRoutine();
1295 context->InvalidRoutine();
1300 protocol = IPPROTO_UDP;
1302 protocol = IPPROTO_TCP;
1307 context->InvalidRoutine();
1315 int rc = (int)socket(domain,
type, protocol);
bool resolveStem(RexxObjectPtr source)
#define argumentOmitted(i)
struct _RexxStringObject * RexxStringObject
struct _RexxObjectPtr * RexxObjectPtr
void setErrno(RexxCallContext *context, bool noError)
void intArrayToStem(RexxCallContext *context, RexxObjectPtr stem, int count, int *arr)
void stemToIntArray(RexxCallContext *context, RexxObjectPtr stem, int &count, int *&arr)
struct sockaddr_in sockaddr_in
int stringToSockOpt(const char *pszOptName)
void stemToSockAddr(RexxCallContext *context, StemManager &stem, sockaddr_in *pSockAddr)
void hostEntToStem(RexxCallContext *context, struct hostent *pHostEnt, StemManager &stem)
void sockAddrToStem(RexxCallContext *context, sockaddr_in *pSockAddr, StemManager &stem)
int caselessCompare(const char *op1, const char *op2)
void stripBlanks(char *string)
RexxRoutine0(int, SockSock_Errno)
RexxRoutine2(int, SockAccept, int, sock, OPTIONAL_RexxObjectPtr, stemSource)
RexxRoutine4(int, SockGetSockOpt, int, sock, CSTRING, level, CSTRING, option, CSTRING, var)
RexxRoutine5(int, SockRecvFrom, int, sock, CSTRING, var, int, dataLen, RexxObjectPtr, flagArg, OPTIONAL_RexxObjectPtr, stemSource)
RexxRoutine1(CSTRING, SockPSock_Errno, OPTIONAL_CSTRING, type)
RexxRoutine3(int, SockGetHostByAddr, CSTRING, addrArg, RexxObjectPtr, stemSource, OPTIONAL_int, domain)