git subrepo clone https://github.com/IanSeyler/netLib deps/netLib

subrepo:
  subdir:   "deps/netLib"
  merged:   "e2b1801"
upstream:
  origin:   "https://github.com/IanSeyler/netLib"
  branch:   "master"
  commit:   "e2b1801"
git-subrepo:
  version:  "0.3.0"
  origin:   "???"
  commit:   "???"
parent 0fd67cad
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
;
[subrepo]
remote = https://github.com/IanSeyler/netLib
branch = master
commit = e2b1801e29a14c46a35a5caa6d900d79a09c2c31
parent = 0fd67cad7ffabecf0b20a7b6e38fd334a75baca3
cmdver = 0.3.0
netLib Network Communication Library
v0.4 - 2007.08.30
Coded by Ian Seyler ([email protected])
http://bubbai.ath.cx/projects/netlib/
Copyright (c) 2007 Ian Seyler
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
all:
gcc webserver.c netSocket.c -o webserver -DUNIX
clean:
rm -f webserver
/*
bufferclient.c - Module file for Test Buffer Client demo using netLib
v0.4 - 2007.08.30
Coded by Ian Seyler ([email protected])
http://bubbai.ath.cx/projects/netlib/
Copyright (c) 2007 Ian Seyler. All Rights Reserved.
Distributed under the terms of the MIT License.
*/
#include "netSocket.h"
#include "netBuffer.h"
int main(int argc, char *argv[])
{
int tint, tint2, tint3;
unsigned int tval;
unsigned short tval2;
int buffervalint;
short buffervalshort;
char buffervalstring[128];
netSocket *tempsocket; // YOU MUST INITIALIZE ALL SOCKETS BEFORE USING THEM!! (netInitSocket)
netBuffer *tempbuffer; // YOU MUST INITIALIZE ALL BUFFERS BEFORE USING THEM!! (netInitBuffer)
printf("Buffer Client demo for netLib v0.4\n");
if (argc < 2) // make sure we got a destination as an arguement
{
printf("Usage : bufferclient hostname (ex: testclient bubbai.ath.cx)\n");
exit(1);
}
netInit(); // Start the netLib library
tempsocket = netInitSocket(); // initiate the socket
tempbuffer = netInitBuffer(128); // initiate a 128 byte buffer
if (netConnect(tempsocket, argv[1], 8192) == netOK) // if the connection looks good, keep going
{
printf("\n");
for (tint=0; tint<3; tint++) //loop of doing a netRecvBuffer 3 times
{
netBufferClear(tempbuffer);
tint2 = netRecvBuffer(tempsocket, tempbuffer);
if(tint2 > 0) // make sure we got some data
{
printf("\nBUFF: ");
for (tint3=0; tint3<tempbuffer->datalength; tint3++)
printf("%02X", tempbuffer->dataptr[tint3]);
printf("\n");
tval = netBufferRemoveInt32(tempbuffer);
printf("int32: %d\n", tval);
netBufferRemoveString(buffervalstring, tempbuffer);
printf("string: %s\n", buffervalstring);
tval = netBufferRemoveInt32(tempbuffer);
printf("int32: %d\n", tval);
tval2 = netBufferRemoveInt16(tempbuffer);
printf("int16: %d\n", tval2);
printf("int32: %d\n", netBufferRemoveInt32(tempbuffer));
}
else
{
printf("\nNo data?");
// break; // if the netRecvBuffer failed then break out to the netDisconnect
}
}
netDisconnect(tempsocket); // Issue here. We disconnect twice if the server dies early as netRecvBuffer does cleanup on broken connections.
}
netStop(); // Stop the netLib library
printf("\n");
return 0;
}
/*
bufferserver.c - Module file for Test Buffer Server demo using netLib
v0.4 - 2007.08.30
Coded by Ian Seyler ([email protected])
http://bubbai.ath.cx/projects/netlib/
Copyright (c) 2007 Ian Seyler. All Rights Reserved.
Distributed under the terms of the MIT License.
*/
#include "netSocket.h"
#include "netBuffer.h"
#define MAXCONCURRENTCLIENTS 3
int main()
{
int tint, tint3, step = 60, freeclient = -1;
netSocket *clientsocket[MAXCONCURRENTCLIENTS];
netSocket *tempsocket; // YOU MUST INITIALIZE ALL SOCKETS BEFORE USING THEM!!
netSocket *listensocket;
netBuffer *tempbuffer; // YOU MUST INITIALIZE ALL BUFFERS BEFORE USING THEM!!
char *netBuff;
char netBuffData[128];
unsigned char rawdata[4] = {0xBA, 0xBE, 0x0D, 0xAD};
char message[]="Sample test string.";
netBuff = netBuffData;
printf("Buffer Server demo for netLib v0.4\n");
netInit(); // Start the netLib library
for(tint=0; tint<MAXCONCURRENTCLIENTS; tint++)
{
clientsocket[tint] = netInitSocket();
printf(" - Client %d", tint+1);
}
tempsocket = netInitSocket(); // initiate the socket
listensocket = netInitSocket(); // initiate the socket
tempbuffer = netInitBuffer(128); // create a 128 byte buffer
netListen(listensocket, 8192); // Set the server to listen on port 8192
// Main program loop
for (;;)
{
tempbuffer->data[0] = step++; // increment the message
// listen for new connections and accept them if there is room
if (netIsDataPending(listensocket) == 1)
{
for(tint=0; tint<MAXCONCURRENTCLIENTS; tint++) // loop through all the client records and find a free one
{
if (clientsocket[tint]->inuse == 0)
{
freeclient = tint;
break; // get out of the for loop... we have the freeclient token
}
}
if (freeclient != -1) // There is a valid free connection.. use it!
{
netAccept(listensocket, clientsocket[freeclient]);
printf(" --> Client is %d", tint+1);
freeclient = -1; // clear the token
}
else // No more connection.. kick them off
{
printf("\nNo more free connections.");
netAccept(listensocket, tempsocket); // accept and disconnect the socket
// Do something here???
netDisconnect(tempsocket);
}
} // we are done listening here
// Update current clients
for(tint=0; tint<MAXCONCURRENTCLIENTS; tint++)
{
if (clientsocket[tint]->inuse == 1)
{
netBufferClear(tempbuffer);
netBufferAppendInt32(tempbuffer, 16909060);
netBufferAppendString(tempbuffer, message);
netBufferAppendInt32(tempbuffer, 2147483647);
netBufferAppendInt16(tempbuffer, 32768);
netBufferAppendInt32(tempbuffer, 87654321);
netBufferAppendData(tempbuffer, rawdata, 4);
netSendBuffer(clientsocket[tint], tempbuffer); // NetSendBuffer will do the cleaning if there is an error.
printf("\nBUFF: ");
for (tint3=0; tint3<tempbuffer->datalength; tint3++)
printf("%02X", tempbuffer->dataptr[tint3]);
}
}
// done updating the current clients
}
netDisconnect(listensocket);
netStop(); // Stop the netLib library
printf("\n");
return 0;
}
/*
chat.c - Module file for Chat Client using netLib
v0.2 - 2007.08.30
Coded by Ian Seyler ([email protected])
http://bubbai.ath.cx/
Copyright (c) 2007 Ian Seyler. All Rights Reserved.
Distributed under the terms of the MIT License.
*/
#include "netSocket.h"
#include "netBuffer.h"
#include "chat.h"
void printhelp();
int main(int argc, char *argv[])
{
int tint, chatmode, returnval;
char tempstring[100];
char tempmessage[2048];
char nickname[9]; // 8 chars plus a null
char message[MessageSize];
netSocket *serverSocket;
netBuffer *chatBuffer;
printf("Chat v0.2 (A demo using netLib)\n");
printf("Designed and coded by Ian Seyler\n");
if (argc <= 2)
{
printhelp();
exit(1);
}
else if (argc > 3)
{
printf("\nToo many args");
exit(1);
}
if (argv[2][1] == 'l' || argv[2][1] == 'L') // Listen
{
chatmode = 0;
}
else if (argv[2][1] == 's' || argv[2][1] == 'S') // Speak
{
chatmode = 1;
}
else
{
printf("\nInvalid Arguement\n");
exit(1);
}
netInit();
serverSocket = netInitSocket();
chatBuffer = netInitBuffer(MessageSize);
if (chatmode == 0) // Listen mode
{
printf("\n\nListen mode");
printf("\n\nConnecting to chat server...");
if (netConnect(serverSocket, argv[1], ChatPort) == netOK)
{
printf(" OK!\n\n");
netSend(serverSocket, "0", 1); // tell the server we want listen mode
while(1)
{
netBufferClear (chatBuffer);
returnval = netRecvBuffer(serverSocket, chatBuffer);
if (returnval > 0 && chatBuffer->complete == 1)
{
printf("%s\n", chatBuffer->dataptr);
// for (tint=0; tint<chatBuffer->datalength+1; tint++)
// printf("%d ", chatBuffer->data[tint]);
}
else if (returnval < 0)
{
printf("\nDisconnected from chat server.\n");
netStop();
exit(1); // error
}
}
}
else
{
printf("\nCould not connect to server.\n");
netStop();
exit(1);
}
}
else if (chatmode == 1) // Speak mode
{
printf("\n\nSpeak mode");
printf("\n\nEnter your nickname (Max 8 chars): ");
fgets(tempstring, 80, stdin); // only read in 80 chars at a time
fflush(stdin); // flush the rest of the keystrokes that are waiting in stdin
tempstring[strlen(tempstring)-1] = '\0'; // get rid of the new line character
strncpy(nickname, tempstring, 8);
nickname[8] = '\0';
printf("Your nickname is '%s'", nickname);
printf("\n\nConnecting to chat server...");
if (netConnect(serverSocket, argv[1], ChatPort) == netOK)
{
returnval = netSend(serverSocket, "1", 1); // tell the server we want speak mode
returnval = netSend(serverSocket, nickname, 9);
printf(" OK!\n\n");
}
else
{
printf("\nCould not connect to server.\n");
netStop();
exit(1);
}
while(1)
{
printf("Say: ");
fgets(tempstring, 80, stdin); // only read in 80 chars at a time
fflush(stdin); // flush the rest of the keystrokes that are waiting in stdin
tempstring[strlen(tempstring)-1] = '\0'; // get rid of the new line character
if (tempstring[0] == '/' && (tempstring[1] == 'q' || tempstring[1] == 'Q')) // exit the loop
break;
netBufferClear (chatBuffer);
memset(tempmessage, '\0', sizeof(tempmessage));
strncpy(tempmessage, nickname, strlen(nickname));
memset(tempmessage+strlen(nickname), ' ', 8-strlen(nickname)); // pad the nick with spaces
strncat(tempmessage, ": ", 2);
strncat(tempmessage, tempstring, (MessageSize - 10)); // 128 - 10 for nickname
netBufferAppendString (chatBuffer, tempmessage);
if(netSendBuffer(serverSocket, chatBuffer) <= 0)
{
printf("\nDisconnected from chat server.\n");
netStop();
exit(1);
}
}
}
else // No mode was chosen
{
printf("\nProgram error. Mode not set.\n");
netStop();
exit(1);
}
printf("\nDisconnected from chat server.\n");
netDisconnect(serverSocket);
netStop();
return 0;
}
void printhelp()
{
printf("\nUsage: chat hostname [-listen or -speak]\n");
}
/*
chat.h - Global header file for Chat and Chatserver
v0.2 - 2007.08.30
Coded by Ian Seyler ([email protected])
http://bubbai.ath.cx/
Copyright (c) 2007 Ian Seyler. All Rights Reserved.
Distributed under the terms of the MIT License.
*/
#include <stdio.h> // printf
#include <stdlib.h> // malloc
#include <string.h> // memset
#define ChatPort 8192
#define MessageSize 128
#define MaxConcurrentClients 10
/*
chatserver.c - Module file for Chatserver using netLib
v0.2 - 2007.08.30
Coded by Ian Seyler ([email protected])
http://bubbai.ath.cx/
Copyright (c) 2007 Ian Seyler. All Rights Reserved.
Distributed under the terms of the MIT License.
*/
#include "netSocket.h"
#include "netBuffer.h"
#include "chat.h"
int main()
{
int tint, tint2, freeclient = -1, returnval, loopcounter = 0, tval;
netSocket *clientsocket[MaxConcurrentClients];
netBuffer *clientbuffer[MaxConcurrentClients];
int clientsockettype[MaxConcurrentClients];
char clientnick[MaxConcurrentClients][9];
netSocket *tempsocket; // YOU MUST INITIALIZE ALL SOCKETS BEFORE USING THEM!!
netSocket *listensocket;
netBuffer *tempbuffer; // YOU MUST INITIALIZE ALL BUFFERS BEFORE USING THEM!!
char *netBuff;
char netBuffData[MessageSize];
char tempmessage[2048];
netBuff = netBuffData;
printf("Chat Server v0.2 (A demo using netLib)\n");
printf("Designed and coded by Ian Seyler\n");
netInit(); // Start the netLib library
for(tint=0; tint<MaxConcurrentClients; tint++)
{
clientsocket[tint] = netInitSocket();
clientbuffer[tint] = netInitBuffer(MessageSize);
clientsocket[tint]->inuse = 0;
clientsockettype[tint] = -1;
}
tempsocket = netInitSocket(); // initiate the socket
listensocket = netInitSocket(); // initiate the socket
tempbuffer = netInitBuffer(MessageSize); // create a buffer
netListen(listensocket, ChatPort); // Set the server to listen
printf("\n");
// Main program loop
for (;;)
{
// Listen for new connections and accept them if there is room
if (netIsDataPending(listensocket) == 1)
{
printf("\nGot a connection.");
for(tint=0; tint<MaxConcurrentClients; tint++) // loop through all the client records and find a free one
{
if (clientsocket[tint]->inuse == 0)
{
freeclient = tint;
break; // get out of the for loop... we have the freeclient token
}
}
if (freeclient != -1) // There is a valid free connection.. use it!
{
netAccept(listensocket, clientsocket[freeclient]);
printf(" Client is %d", tint+1);
netRecv(clientsocket[freeclient], netBuff, 1);
if (netBuff[0] == '0')
{
printf(" --> Listen mode");
clientsockettype[freeclient] = 0;
}
else if (netBuff[0] == '1')
{
printf(" --> Speak mode");
memset(netBuff, '\0', sizeof(netBuff));
for (tint=0; tint<3; tint++) // loop here three times in case of a slow client connection
{
returnval = netRecv(clientsocket[freeclient], clientnick[freeclient], 9); // get the name
if (returnval == 9)
tint = 1000000; // one meeeellion
}
if (returnval != 9) // the client is corrupt
{
printf(" --> Corrupt client");
netDisconnect(clientsocket[freeclient]);
}
else // we have a good client
{
clientsockettype[freeclient] = 1;
printf(" : %s.", clientnick[freeclient]);
memset(tempmessage, '\0', sizeof(tempmessage));
strcpy(tempmessage, "CHANSERV: ");
strcat(tempmessage, clientnick[freeclient]);
strcat(tempmessage, " has joined the chat.");
netBufferClear (tempbuffer);
netBufferAppendString (tempbuffer, tempmessage);
printf(" Sending global message.");
for(tint=0; tint<MaxConcurrentClients; tint++)
{
if (clientsocket[tint]->inuse == 1 && clientsockettype[tint] == 0)
if (netSendBuffer(clientsocket[tint], tempbuffer) <= 0)
clientsockettype[tint] = -1;
}
}
}
else // The other end is not a chat client.
{
printf(" --> Corrupt client");
netDisconnect(clientsocket[freeclient]);
}
freeclient = -1; // clear the token
}
else // No more connection.. kick them off
{
printf(" No more free connections.");
netAccept(listensocket, tempsocket); // accept and disconnect the socket
netSend(tempsocket, "Server message: No free connections. Please try again later.\0", MessageSize); // send an error message
netDisconnect(tempsocket);
}
}
// we are done listening here
// Update current clients
for(tint=0; tint<MaxConcurrentClients; tint++)
{
if (clientbuffer[tint]->complete == 1)
{
printf(" Sending message to clients.");
for(tint2=0; tint2<MaxConcurrentClients; tint2++)
{
if (clientsocket[tint2]->inuse == 1 && clientsockettype[tint2] == 0)
{
if (netSendBuffer(clientsocket[tint2], clientbuffer[tint]) <= 0)
clientsockettype[tint2] = -1;
}
}
clientbuffer[tint]->complete = 0;
}
}
// done updating the current clients
netBufferClear (tempbuffer);
// Check the speaking clients
for(tint=0; tint<MaxConcurrentClients; tint++)
{
if (clientsocket[tint]->inuse == 1 && clientsockettype[tint] == 1)
{
returnval = netRecvBuffer(clientsocket[tint], clientbuffer[tint]);
if (returnval < 0) // the client has disconnected
{
memset(tempmessage, '\0', sizeof(tempmessage));
strcpy(tempmessage, "CHANSERV: ");
strcat(tempmessage, clientnick[tint]);
strcat(tempmessage, " has left the chat.");
netBufferClear (clientbuffer[tint]);
netBufferAppendString (clientbuffer[tint], tempmessage);
clientbuffer[tint]->complete = 1;
printf("\nMessage received from client %d.", tint+1);
}
else if (returnval > 0) // the client is sending a message
{
if (clientbuffer[tint]->complete == 1) // we have a whole message
printf("\nMessage received from client %d.", tint+1);
}
}
}
// done updating the current clients
}
netDisconnect(listensocket);
netStop(); // Stop the netLib library
printf("\n");
return 0;
}
Change Log for netLib Network Communication Library
---------------------------------------------------
v0.4 - 20070830
-major rework of how netBuffers are used. netBuffers can now store binary information that can be sent between different computer platforms (Endian issues)
-added netBufferAppendInt8, netBufferAppendInt16, netBufferAppendInt32, netBufferAppendString, netBufferAppendData, netBufferRemoveInt8, netBufferRemoveInt16, netBufferRemoveInt32, netBufferRemoveString, netBufferRemoveData, and netBufferClear functions.
-separate source files for netSockets (basic functions like connect, listen, accept, send, receive, etc.) and netBuffers (sendbuffer, recvbuffer, appends, removes)
-Thanks to Brad Fewster for offering tips on a few parts of netLib. "if" statements were altered as well as variable passing to some functions based on his advice
-added netSetOption function
-found the command line option to allow for correct functionality in BeOS BONE
-miscellaneous cleanup of code
v0.3 - 20070626
-fixed bug in netConnect on Unix. setsockint was not declared as a variable
-disable the Nagle algorithm by default in netConnect and netAccept
-changed all library messages to print to stderr instead of stdout