Guest User

Untitled

a guest
Feb 8th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.56 KB | None | 0 0
  1. ///NET.C
  2. #include "net.h"
  3. #include "log.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <pthread.h>
  11.  
  12. /*#include <netinet/in.h>
  13. #include <arpa/inet.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <time.h>*/
  20.  
  21. typedef enum { FALSE, TRUE } bool;
  22.  
  23. int netSocket = -1;
  24. int netDomain = -1;
  25. int netType = -1;
  26. int netProtocol = -1;
  27.  
  28. struct sockaddr_in netServerAddr;
  29.  
  30. /* Domain Types:
  31. AF_UNIX, AF_LOCAL: Local
  32. AF_INET: IPv4
  33. AF_INET6: IPv6 */
  34.  
  35. /* Socket Types:
  36. SOCK_STREAM: Reliable two-way */
  37.  
  38. // Creates a Socket
  39. bool NetworkInit(int domain, int type, int protocol) {
  40.     // Check if Socket is already initiated
  41.     if(netSocket >= 0) {LogError("Socket already initiated!","NetInit"); return FALSE;}
  42.  
  43.     // Create Socket
  44.     netSocket = socket(domain, type, protocol);
  45.     netDomain = domain;
  46.     netType = type;
  47.     netProtocol = protocol;
  48.     return (netSocket != -1);
  49. }
  50.  
  51. // Resets the Socket
  52. bool NetworkStop() {
  53.     // Check if Socket is initiated
  54.     if(netSocket < 0) {LogWarning("Can't stop a nonexistant Socket","NetStop"); return FALSE;}
  55.  
  56.     // Shutdown Socket
  57.     shutdown(netSocket, 2);
  58.     close(netSocket);
  59.  
  60.     // Reset Socket data
  61.     netSocket = -1;
  62.     netDomain = -1;
  63.     netType = -1;
  64.     netProtocol = -1;
  65. }
  66.  
  67. // Listen on  Client
  68. void NetworkClientListen() {
  69.     int reading = -1;
  70.     char buffer[256];
  71.     pid_t pid = fork();
  72.     if(pid == 0) {
  73.         while(1) {
  74.             reading = recv(netSocket,buffer,sizeof(buffer)-1,0);
  75.             buffer[reading] = '\0';
  76.             if(reading < 0) {
  77.                 LogError("Could not read","NetClientListen");
  78.             } else {
  79.                 onClientRecieve(buffer);
  80.             }
  81.             usleep(1);
  82.         }
  83.     }
  84. }
  85.  
  86. // Connects the Client
  87. bool NetworkConnect(char* ip, int port) {
  88.     // Check if Socket is initiated
  89.     if(netSocket < 0) {LogError("Socket not yet initiated","NetConnect"); return FALSE;}
  90.  
  91.     // Setup Network for connection
  92.     netServerAddr.sin_family = netDomain;
  93.     netServerAddr.sin_port = htons(port);
  94.     netServerAddr.sin_addr.s_addr = inet_addr(ip);
  95.  
  96.     // Attempt connection
  97.     if(connect(netSocket, (struct sockaddr *)&netServerAddr, sizeof(netServerAddr)) < 0) {
  98.         LogError("Could not connect to Server","NetConnect"); return FALSE;
  99.     }
  100.  
  101.     // Connect
  102.     NetworkClientListen();
  103. }
  104.  
  105. // Listen on Server - Client Thread
  106. void *NetworkServerListenOnClient(void *vconnection) {
  107.     int connection;
  108.     connection = (intptr_t)vconnection;
  109.     int reading = -1;
  110.     char buffer[256];
  111.     while(1) {
  112.         reading = recv(connection,buffer,sizeof(buffer)-1,0);
  113.         buffer[reading] = '\0';
  114.         if(reading < 0) { // Client closed
  115.             LogWarning("Client Disconnected","NetServerListenOnClient");
  116.             return NULL;
  117.         } else {
  118.             onServerRecieve(buffer, connection);
  119.         }
  120.         usleep(1);
  121.     }
  122. }
  123.  
  124. // Listen on Server
  125. void NetworkServerListen() {
  126.     int connection = -1;
  127.     int reading = -1;
  128.     char buffer[256];
  129.     pid_t pid = fork();
  130.     if(pid == 0) {
  131.         while(1) {
  132.             connection = accept(netSocket, (struct sockaddr*)NULL ,NULL);
  133.             if(connection < 0) {
  134.                 LogError("Invalid connection","NetServerListen");
  135.             } else {
  136.                 // The fork way
  137.                 /*pid_t pid2 = fork();
  138.                 if(pid2 == 0) {
  139.                     while(1) {
  140.                         reading = recv(connection,buffer,sizeof(buffer)-1,0);
  141.                         buffer[reading] = '\0';
  142.                         if(reading < 1) { // Client closed
  143.                             LogWarning("Client Disconnected","NetServerListen");
  144.                             exit(0);
  145.                         } else {
  146.                             onServerRecieve(buffer, connection);
  147.                         }
  148.                         usleep(1);
  149.                     }
  150.                 }*/
  151.                 // The PThread way
  152.                 pthread_t idclol;
  153.                 if(pthread_create(&idclol, NULL, NetworkServerListenOnClient, (void *)(intptr_t)connection)) {
  154.                     LogError("Could not create PThread","NetServerListen");
  155.                 }
  156.             }
  157.             usleep(1);
  158.         }
  159.     }
  160. }
  161.  
  162. // Creates a Server
  163. bool NetworkCreate(int port) {
  164.     // Check if Socket is initiated
  165.     if(netSocket < 0) {LogError("Socket not yet initiated","NetCreate"); return FALSE;}
  166.     if(port < 1025) {LogError("Cannot initialize Port number less than 1025","NetCreate"); return FALSE;}
  167.  
  168.     // Setup Network for server creation
  169.     netServerAddr.sin_family = netDomain;
  170.     netServerAddr.sin_port = htons(port);
  171.     netServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  172.  
  173.     // Attempt listen
  174.     bind(netSocket, (struct sockaddr*)&netServerAddr, sizeof(netServerAddr));
  175.     if(listen(netSocket, 3) == -1) {
  176.         LogError("Could not listen on server","NetCreaste"); return FALSE;
  177.     }
  178.  
  179.     // Listen
  180.     NetworkServerListen();
  181. }
  182.  
  183. // Net String
  184. char *NetString(char *id, char *data) {
  185.     char *result;
  186.     result = malloc(strlen(id)+strlen(data)+2);
  187.     if(result) {} else {LogWarning("ERROR","ERRRRR");}
  188.     strcpy(result, id);
  189.     strcat(result, "/");
  190.     strcat(result, data);
  191.     return result;
  192. }
  193.  
  194. // Net Integer
  195. char* NetInt(char *id, int i) {
  196.     char data[100];
  197.     sprintf(data, "%d", i);
  198.     return NetString(id, data);
  199. }
  200.  
  201. // Send from Client to Server
  202. bool ClientSend(char *umsg) {
  203.     char msg[256];
  204.     strcpy(msg, umsg);
  205.     int sent = send(netSocket, msg, strlen(msg)+1, 0);
  206.     if(sent < 0) {
  207.         LogError("Could not send message","ClientSend"); return FALSE;
  208.     }
  209.     LogWarning("Client sent",msg);
  210.     return TRUE;
  211. }
  212.  
  213. // Send from Server to Client
  214. bool ServerSend(char *umsg, int connection) {
  215.     char msg[256];
  216.     strcpy(msg, umsg);
  217.     int sent = send(connection, msg, strlen(msg)+1, 0);
  218.     if(sent < 0) {
  219.         LogError("Could not send message to Client","ServerSend"); return FALSE;
  220.     }
  221.     LogWarning("Server sent",msg);
  222.     return TRUE;
  223. }
  224.  
  225. ///SERVER.C
  226. #include "net.h"
  227. #include <stdio.h>
  228. #include <sys/types.h>
  229. #include <sys/socket.h>
  230.  
  231. int main(void) {
  232.     NetworkInit(AF_INET, SOCK_STREAM, 0);
  233.     NetworkCreate(27015);
  234.     while(1) {
  235.         sleep(1);
  236.     }
  237.     return 0;
  238. }
  239.  
  240. void onServerRecieve(char msg[256], int connection) {
  241.     LogWarning("Server Recieved",msg);
  242.     sleep(2);
  243.     char *r = NetString("ch","aaaaa");
  244.     printf("%s",r);
  245.     printf("%s","\n");
  246.     ServerSend(r,connection);
  247. }
  248.  
  249. // Doesn't happen
  250. void onClientRecieve(char msg[256]) {}
  251.  
  252. ///LOG.C
  253. #include "log.h"
  254. #include <stdio.h>
  255.  
  256. // Colors!
  257. #define COLOR_ADD "\x1b[33m"
  258. #define COLOR_WARNING "\x1b[2m"
  259. #define COLOR_ERROR   "\x1b[91m"
  260. #define COLOR_RESET   "\x1b[0m"
  261.  
  262. void LogError(char* error, char* add) {
  263.     printf(COLOR_ERROR);
  264.     printf("%s", "[ERROR] ");
  265.     printf("%s", error);
  266.     printf("%s", COLOR_ADD);
  267.     printf("%s", " - ");
  268.     printf("%s", add);
  269.     printf(COLOR_RESET);
  270.     printf("%s", "\n");
  271. }
  272.  
  273. void LogWarning(char* warning, char* add) {
  274.     printf(COLOR_WARNING);
  275.     printf("%s", "[WARNING] ");
  276.     printf("%s", warning);
  277.     printf("%s", COLOR_ADD);
  278.     printf("%s", " - ");
  279.     printf("%s", add);
  280.     printf(COLOR_RESET);
  281.     printf("%s", "\n");
  282. }
Advertisement
Add Comment
Please, Sign In to add comment