#include #include #include "../../basext.h" #include #include #include #include #include #include #include #define MAXDATASIZE 256 besVERSION_NEGOTIATE return (int)INTERFACE_VERSION; besEND besSUB_START printf ("Simple Socket module version 1 loaded.\n"); printf ("Released december 27, 2001.\n"); besEND besSUB_FINISH besEND besFUNCTION (__ss_connect) VARIABLE ptr; char* arg0; long arg1; int sockfd; struct hostent *he; struct sockaddr_in their_addr; // connector's address information if(besARGNR<2) return EX_ERROR_TOO_FEW_ARGUMENTS; if(besARGNR>2) return EX_ERROR_TOO_MANY_ARGUMENTS; ptr = besARGUMENT(1); besDEREFERENCE(ptr); arg0 = besCONVERT2ZCHAR(besCONVERT2STRING(ptr),arg0); ptr = besARGUMENT(2); besDEREFERENCE(ptr); arg1 = besGETLONGVALUE(ptr); sockfd = 0; if ((he=gethostbyname(arg0)) == NULL) { // get the host info printf ("\nSpecified host does not exist!\n"); sockfd = -1; } else if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("\nCannot connect to socket!\n"); sockfd = -2; } else { their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(arg1); // short, network byte order their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { printf("Unable to connect to specified host!\n"); sockfd = -3; } else printf("Socket connected.\n"); } besALLOC_RETURN_LONG; LONGVALUE(besRETURNVALUE) = sockfd; besFREE (arg0); besEND besFUNCTION (__ss_disconnect) VARIABLE ptr; long arg0; if(besARGNR<1) return EX_ERROR_TOO_FEW_ARGUMENTS; if(besARGNR>1) return EX_ERROR_TOO_MANY_ARGUMENTS; ptr = besARGUMENT(1); besDEREFERENCE(ptr); arg0 = besGETLONGVALUE(ptr); close(arg0); printf("Socket disconnected.\n"); besRETURNVALUE = NULL; besEND besFUNCTION (__ss_check) VARIABLE ptr; long arg0; fd_set read_available; struct timeval tv; int returnvalue; if(besARGNR<1) return EX_ERROR_TOO_FEW_ARGUMENTS; if(besARGNR>1) return EX_ERROR_TOO_MANY_ARGUMENTS; ptr = besARGUMENT(1); besDEREFERENCE(ptr); arg0 = besGETLONGVALUE(ptr); returnvalue = 0; FD_ZERO(&read_available); FD_SET(arg0, &read_available); tv.tv_sec = 0; tv.tv_usec = 0; select(arg0+1, &read_available, NULL, NULL, &tv); if (FD_ISSET(arg0, &read_available)){ printf("There is new data.\n"); returnvalue = 1; } besALLOC_RETURN_LONG; LONGVALUE(besRETURNVALUE) = returnvalue; besEND besFUNCTION (__ss_read) VARIABLE ptr; long arg0; int numbytes, i; char buf[MAXDATASIZE]; char* retstr; if(besARGNR<1) return EX_ERROR_TOO_FEW_ARGUMENTS; if(besARGNR>1) return EX_ERROR_TOO_MANY_ARGUMENTS; ptr = besARGUMENT(1); besDEREFERENCE(ptr); arg0 = besGETLONGVALUE(ptr); numbytes = recv(arg0, buf, MAXDATASIZE-1, 0); if (numbytes == -1) { printf("Error reading from socket!\n"); retstr = "SIMPLE SOCKET ERROR 1"; } else if (numbytes == 0){ printf("Remote side has closed connection!\n"); retstr = "SIMPLE SOCKET ERROR 2"; } else { buf[numbytes-1] = '\n'; buf[numbytes] = '\0'; retstr = (char*)buf; printf("Data received.\n"); } besALLOC_RETURN_STRING(strlen(retstr)); memcpy(STRINGVALUE(besRETURNVALUE),retstr,strlen(retstr)); besEND besFUNCTION (__ss_write) VARIABLE ptr; long arg0, bytes_sent; char* arg1; if(besARGNR<2) return EX_ERROR_TOO_FEW_ARGUMENTS; if(besARGNR>2) return EX_ERROR_TOO_MANY_ARGUMENTS; ptr = besARGUMENT(1); besDEREFERENCE(ptr); arg0 = besGETLONGVALUE(ptr); ptr = besARGUMENT(2); besDEREFERENCE(ptr); arg1 = besCONVERT2ZCHAR(besCONVERT2STRING(ptr),arg1); bytes_sent = send(arg0, arg1, strlen(arg1), 0); if (bytes_sent3) return EX_ERROR_TOO_MANY_ARGUMENTS; ptr = besARGUMENT(1); besDEREFERENCE(ptr); arg0 = besCONVERT2ZCHAR(besCONVERT2STRING(ptr),arg0); ptr = besARGUMENT(2); besDEREFERENCE(ptr); arg1 = besGETLONGVALUE(ptr); ptr = besARGUMENT(3); besDEREFERENCE(ptr); arg2 = besGETLONGVALUE(ptr); sockfd = 0; if ((he=gethostbyname(arg0)) == NULL) { // get the host info printf ("\nSpecified host does not exist!\n"); sockfd = -1; } else if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("\nCannot connect to socket!\n"); sockfd = -2; } else if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { printf("\nCannot configure socket!\n"); sockfd = -3; } else { my_addr.sin_family = AF_INET; my_addr.sin_port = htons(arg1); // Fill in specified port my_addr.sin_addr = *((struct in_addr *)he->h_addr); // Fill in specified IP address memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { printf("Unable to bind the specified socket address!\n"); sockfd = -4; } else if (listen(sockfd, arg2) == -1) { printf("Unable to listen to the specified socket address!\n"); sockfd = -5; } else printf("Server started.\n"); } besALLOC_RETURN_LONG; LONGVALUE(besRETURNVALUE) = sockfd; besFREE (arg0); besEND besFUNCTION (__ss_accept) VARIABLE ptr; long arg0; int sin_size, new_fd; struct sockaddr_in their_addr; // connector's address information if(besARGNR<1) return EX_ERROR_TOO_FEW_ARGUMENTS; if(besARGNR>1) return EX_ERROR_TOO_MANY_ARGUMENTS; ptr = besARGUMENT(1); besDEREFERENCE(ptr); arg0 = besGETLONGVALUE(ptr); sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(arg0, (struct sockaddr *)&their_addr, &sin_size)) == -1) { printf("No connection made!\n"); new_fd = -1; } printf("Got connection from %s\n", inet_ntoa(their_addr.sin_addr)); besALLOC_RETURN_LONG; LONGVALUE(besRETURNVALUE) = new_fd; besEND