daily pastebin goal
89%
SHARE
TWEET

Untitled

a guest Jan 24th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "JC_socketWrapper.h"
  2.  
  3. //#define JC_DEBUG
  4.  
  5. const static int JC_MAX_MESSAGE_SIZE = 536870900; // 1/2 gibibyte
  6.  
  7. //IPv4 TCP
  8. int createServerSocket(int portNumber, int *outputServerSocket) {
  9.     int temp;  
  10.  
  11.     *outputServerSocket = socket(AF_INET, SOCK_STREAM, 0);
  12.     if (*outputServerSocket < 0) { perror("tcp server socket() failed."); return -1;}
  13.    
  14.     struct sockaddr_in serverAddress;
  15.     memset(&serverAddress, 0, sizeof(serverAddress));
  16.     serverAddress.sin_family = AF_INET;
  17.     serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  18.     serverAddress.sin_port = htons(portNumber);
  19.  
  20.     temp = bind(*outputServerSocket, (struct sockaddr*) &serverAddress, sizeof(serverAddress));
  21.     if (temp < 0) { perror("tcp bind() failed."); return -1; }
  22.  
  23.     temp = listen(*outputServerSocket, LISTENQ);
  24.     if (temp < 0) { perror("listen() failed."); return -1; }
  25.    
  26.     return 0;
  27. }
  28.  
  29. int acceptClient(int serverSocketFd, int *clientSocket) {
  30.     ATTEMPT_ACCEPT:
  31.     //                  V &output client address, and &socklen_t (sizeof(addr))
  32.     *clientSocket = accept(serverSocketFd, (struct sockaddr*) NULL, NULL);
  33.     if (*clientSocket < 0) {
  34.         if (errno == EINTR) goto ATTEMPT_ACCEPT;
  35.         perror("accept() failed. "); return -1;
  36.     }
  37.  
  38.     return 0;
  39. }
  40.  
  41. int connectToAddress(char *ipAddress, int portNumber, int *outputConnectedSocket) {
  42.     int temp;
  43.  
  44.     // create socket
  45.     *outputConnectedSocket = socket(AF_INET, SOCK_STREAM, 0);
  46.     if (*outputConnectedSocket < 0) { perror("connecting tcp socket() failed. "); return -1; }
  47.  
  48.     // initialize serverAddress
  49.     struct sockaddr_in serverAddress;
  50.     memset(&serverAddress, 0, sizeof(serverAddress));
  51.     serverAddress.sin_family = AF_INET;
  52.     serverAddress.sin_port = htons(portNumber);
  53.    
  54.     temp = inet_pton(AF_INET, ipAddress, &serverAddress.sin_addr); //presentation to numeric
  55.     if (temp <= 0) { perror("inet_pton() failed. "); return -1; }
  56.  
  57.     // connect to server
  58.     temp = connect(*outputConnectedSocket, (struct sockaddr*) &serverAddress, sizeof(serverAddress));
  59.     if (temp < 0) { perror("connect() failed. "); return -1; }
  60.  
  61.     return 0;
  62. }
  63.  
  64. int sendStringToSocket(int socketFd, const unsigned char *buffer, int len) {
  65.     #ifdef JC_DEBUG
  66.         //printf("sendStringToSocket buffer: %s\n", buffer);
  67.         printf("sendStringToSocket len: %d\n", len);
  68.     #endif
  69.  
  70.     if(writeInt32ToSocket(socketFd, len)) return -1;
  71.     if(writeToSocket(socketFd, buffer, len)) return -1;
  72.     return 0;
  73. }
  74.  
  75. // malloc(size of incoming string) - must be freed
  76. // can return 1 on connection closed
  77. int recvStringFromSocket(int socketFd, unsigned char **buffer, int *length) {
  78.     #ifdef JC_DEBUG
  79.         *length = 0;
  80.     #endif 
  81.  
  82.     int temp;  
  83.     temp = readInt32FromSocket(socketFd, length);
  84.     if (temp) {
  85.         if (temp != 1) printf("readInt32FromSocket() returning error value: %d\n", temp);
  86.     #ifdef JC_DEBUG
  87.         else printf("readInt32FromSocket() returning error value: %d\n (0 is tcp disconnect)", temp);
  88.     #endif
  89.  
  90.         return temp;
  91.     }
  92.     else if (*length > JC_MAX_MESSAGE_SIZE) {
  93.         printf("Warning: recvStringFromSocket() tried to read a message larger than MAX_MESSAGE_SIZE\n");
  94.         return -1;
  95.     }
  96.     *buffer = malloc(*length);
  97.     temp = readFromSocket(socketFd, *buffer, *length);
  98.     if (temp) {
  99.         free(*buffer);
  100.         *buffer = 0;
  101.         return temp;
  102.     }
  103.  
  104.     #ifdef JC_DEBUG
  105.         //printf("recvStringFromSocket buffer: %s\n", *buffer);
  106.         printf("recvStringFromSocket len: %d\n", *length);
  107.     #endif
  108.  
  109.     return 0;
  110. }
  111.  
  112. int closeSocket(int *socketFd) {
  113.     int temp = close(*socketFd);
  114.     if (temp < 0) { perror("close() failed. "); return -1; }
  115.    
  116.     *socketFd = 0;
  117.  
  118.     return 0;
  119. }
  120.  
  121. int shutdownSocket(int socketFd, int option) {
  122.     int temp = shutdown(socketFd, option);
  123.     if (temp < 0) { perror("shutdown() failed"); return -1; }
  124.  
  125.     // This lead to bugs given that it wasn't obvious I was changing the value
  126.     // Although it "should" be from the pointer value its too easy to overlook
  127.     //*socketFd = 0;
  128.  
  129.     return 0;
  130. }
  131.  
  132. int readFromSocket(int socketFd, unsigned char *buffer, int bytesLeftToRead) {
  133.     int bytesJustRead;
  134.  
  135.     while (bytesLeftToRead) {
  136.         bytesJustRead = read(socketFd, buffer, bytesLeftToRead);
  137.        
  138.         if (!bytesJustRead) {
  139.             // Tcp connection closed.
  140.             return 1;
  141.         }
  142.         else if (bytesJustRead < 0) {
  143.             if (errno == EINTR) bytesJustRead = 0;
  144.             else { perror("read() failed. "); return -1; }
  145.         }
  146.  
  147.         buffer += bytesJustRead;
  148.         bytesLeftToRead -= bytesJustRead;
  149.     }
  150.  
  151.     return 0;
  152. }
  153.  
  154. int writeToSocket(int socketFd, const unsigned char *buffer, int bytesToWrite) {
  155.     int bytesJustWritten;
  156.  
  157.     while (bytesToWrite) {
  158.         bytesJustWritten = write(socketFd, buffer, bytesToWrite);
  159.        
  160.         if (bytesJustWritten < 1) {
  161.             if (errno == EINTR && bytesJustWritten < 0) bytesJustWritten = 0;
  162.             else { perror("write() failed. "); return -1; }
  163.         }
  164.  
  165.         buffer += bytesJustWritten;
  166.         bytesToWrite -= bytesJustWritten;
  167.     }
  168.  
  169.     return 0;
  170. }
  171.  
  172. int writeInt32ToSocket(int socketFd, int32_t numberToWrite) {
  173.     union {
  174.         unsigned char asChar[sizeof(int32_t)];
  175.         int32_t asInt32;
  176.     } result;
  177.     result.asInt32 = htonl(numberToWrite);
  178.     return writeToSocket(socketFd, (const char *) result.asChar, sizeof(int32_t));
  179. }
  180.  
  181. int readInt32FromSocket(int socketFd, int32_t *numberRead) {
  182.     union {
  183.         unsigned char asChar[sizeof(int32_t)];
  184.         int32_t asInt32;
  185.     } result;
  186.     int temp = readFromSocket(socketFd, result.asChar, sizeof(int32_t));
  187.     *numberRead = ntohl(result.asInt32);
  188.  
  189.     return temp;
  190. }
  191.  
  192. // UDP
  193. int bindUDPPort(int portNumber, int *outputSocket) {
  194.     *outputSocket = socket(AF_INET, SOCK_DGRAM, 0);
  195.     if (*outputSocket < 0) { perror("UDP socket() failed. "); return -1; }
  196.  
  197.     struct sockaddr_in serverAddress;
  198.     memset(&serverAddress, 0, sizeof(serverAddress));
  199.     serverAddress.sin_family = AF_INET;
  200.     serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  201.     serverAddress.sin_port = htons(portNumber);
  202.  
  203.     int temp = bind(*outputSocket, (struct sockaddr*) &serverAddress, sizeof(serverAddress));
  204.     if (temp < 0) { perror("udp bind() failed."); return -1; }
  205.  
  206.     return 0;
  207. }
  208.  
  209. // Linux Stuff
  210. int setSignalHandler(int signalType, void (*handler) (int)) {
  211.     int temp;
  212.  
  213.     struct sigaction action;
  214.     action.sa_handler = handler;
  215.     action.sa_flags = 0;
  216.     sigemptyset(&action.sa_mask);
  217.  
  218.     temp = sigaction(signalType, &action, NULL);
  219.     if (temp < 0) {perror("sigaction() failed. "); return -1; }
  220. }
  221.  
  222. void basic_SIGCHLD_Handler(int signo) {
  223.     int pid, status;
  224.     // -1 is first child to terminate, WNOHANG means don't block if theres no children to wait for
  225.     do {
  226.         pid = waitpid(-1, &status, WNOHANG);
  227.         //if (pid != -1) printf("Child %d terminated, returning %d\n", pid, status);
  228.     } while (pid > 0);
  229.     return;
  230. }
  231.  
  232. int wrappedSelect(int *numberSocketsReady, int maxfd, fd_set *setForSelect) {
  233.     SELECT_ATTEMPT:
  234.     *numberSocketsReady = select(maxfd + 1, setForSelect, NULL, NULL, NULL);
  235.     if (*numberSocketsReady < 0) {
  236.         if (errno == EINTR) goto SELECT_ATTEMPT;
  237.         perror("select() failed. ");
  238.         return -1;
  239.     }  
  240. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top