Merge branch 'krueger' into network
This commit is contained in:
commit
7d63b27105
19 changed files with 1495 additions and 874 deletions
1306
drivers/net/mmnif.c
1306
drivers/net/mmnif.c
File diff suppressed because it is too large
Load diff
|
@ -1,4 +1,4 @@
|
|||
C_source := main.c tasks.c syscall.c tests.c echo.c ping.c init.c
|
||||
C_source := main.c tasks.c syscall.c tests.c echo.c ping.c init.c server.c client.c shell.c
|
||||
MODULE := kernel
|
||||
|
||||
include $(TOPDIR)/Makefile.inc
|
||||
|
|
120
kernel/client.c
Normal file
120
kernel/client.c
Normal file
|
@ -0,0 +1,120 @@
|
|||
#include "client.h"
|
||||
|
||||
#include "../drivers/net/util.h"
|
||||
|
||||
#define SOCKET_ERROR -1
|
||||
|
||||
int cli_sendBuffer(Client* cli,void* pBuffer, unsigned int bufferlen)
|
||||
{
|
||||
int iResult;
|
||||
ClientEventArgs e;
|
||||
//abfragen ob client existiert!! wichtig
|
||||
if (cli->sSocket != SOCKET_ERROR)
|
||||
{
|
||||
iResult= send(cli->sSocket,(char*)pBuffer,bufferlen,0);
|
||||
if (cli->_OnWrite != 0)
|
||||
{
|
||||
e.dwLen = iResult;
|
||||
e.pBuffer = pBuffer;
|
||||
cli->_OnWrite(&e);
|
||||
}
|
||||
return iResult;
|
||||
}
|
||||
else
|
||||
return -3;
|
||||
}
|
||||
|
||||
int cli_ConnectTo(Client* cli,char * pAdresse,unsigned short Port,int webAdresse)
|
||||
{
|
||||
ClientEventArgs e;
|
||||
cli->sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Einen Socket erstellen
|
||||
cli->wPort=Port;
|
||||
cli->adAddr.sin_port = htons(cli->wPort);
|
||||
|
||||
if (cli->sSocket == SOCKET_ERROR)
|
||||
return -2;
|
||||
|
||||
if (webAdresse) //Fall es sich um eine Internet Adresse Handelt
|
||||
return -1;
|
||||
else //Fall es sich um eine LAN Adresse im 127.0.0.1 Stil handelt
|
||||
cli->adAddr.sin_addr.s_addr = inet_addr(pAdresse);
|
||||
|
||||
if (connect(cli->sSocket,(const struct sockaddr*)&cli->adAddr, sizeof(cli->adAddr))==0)
|
||||
{
|
||||
|
||||
create_kernel_task(&cli->bThread,cli_WaitForPacket,cli);
|
||||
|
||||
if (cli->_OnConnect != 0)
|
||||
{
|
||||
e.dwLen = 0;
|
||||
e.pBuffer = 0;
|
||||
cli->_OnConnect(&e);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
};
|
||||
|
||||
int cli_DisconnectFrom(Client* cli)
|
||||
{
|
||||
shutdown(cli->sSocket,2);
|
||||
closesocket(cli->sSocket);
|
||||
cli->sSocket = SOCKET_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cli_WaitForPacket(Client* cli)
|
||||
{
|
||||
int iResult=1;
|
||||
char pTmpBuffer[DEF_BUFFERSIZE+1];
|
||||
ClientEventArgs e;
|
||||
|
||||
while (iResult > 0)
|
||||
{
|
||||
iResult = recv(cli->sSocket ,pTmpBuffer, DEF_BUFFERSIZE, 0);
|
||||
if (iResult > 0)
|
||||
{
|
||||
pTmpBuffer[iResult]='\0';
|
||||
if (cli->_OnRead != 0)
|
||||
{
|
||||
e.dwLen = iResult;
|
||||
e.pBuffer = pTmpBuffer;
|
||||
cli->_OnRead( &e);
|
||||
}
|
||||
}
|
||||
else //Verbindung geschlossen
|
||||
{
|
||||
cli->sSocket=SOCKET_ERROR;
|
||||
if (cli->_OnDisconnect != 0)
|
||||
{
|
||||
e.dwLen = 0;
|
||||
e.pBuffer = 0;
|
||||
cli->_OnDisconnect( &e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int cli_init(Client* cli)
|
||||
{
|
||||
|
||||
cli->_OnConnect = 0;
|
||||
cli->_OnWrite = 0;
|
||||
cli->_OnRead = 0;
|
||||
cli->_OnDisconnect = 0;
|
||||
|
||||
cli->adAddr.sin_addr.s_addr = INADDR_ANY; // IP'S die der Client annehmen soll
|
||||
cli->adAddr.sin_family = AF_INET; // AdressFamilie (TCP/IP)
|
||||
// adAddr.sin_port = htons(iPort); // Port auf dem der Client erreichbar seien soll
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int cli_destroy(Client* cli)
|
||||
{
|
||||
closesocket(cli->sSocket);;
|
||||
return NULL;
|
||||
}
|
56
kernel/client.h
Normal file
56
kernel/client.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
#ifndef __CLIENT__
|
||||
#define __CLIENT__
|
||||
|
||||
#define DEF_BUFFERSIZE 2048 // Buffergröße für ein Packet
|
||||
|
||||
#ifndef LWIP_SOCKET
|
||||
#include <lwip/sockets.h>
|
||||
#endif
|
||||
|
||||
#ifndef SOCKET
|
||||
#define SOCKET int
|
||||
#endif
|
||||
|
||||
|
||||
#include <metalsvm/tasks.h>
|
||||
|
||||
|
||||
typedef struct _ClientEventArgs
|
||||
{
|
||||
unsigned int dwLen;
|
||||
void* pBuffer;
|
||||
} ClientEventArgs;
|
||||
|
||||
|
||||
typedef void (*ClientEventHandler)(ClientEventArgs*);
|
||||
|
||||
|
||||
typedef struct _Client
|
||||
{
|
||||
//Connection Handling
|
||||
SOCKET sSocket;
|
||||
|
||||
struct sockaddr_in adAddr;
|
||||
|
||||
//Output Interfacte
|
||||
unsigned short wPort;
|
||||
|
||||
tid_t bThread;
|
||||
|
||||
// Eventhandling
|
||||
ClientEventHandler _OnConnect;
|
||||
ClientEventHandler _OnDisconnect;
|
||||
ClientEventHandler _OnRead;
|
||||
ClientEventHandler _OnWrite;
|
||||
} Client;
|
||||
|
||||
int cli_sendBuffer(Client* cli,void* pBuffer, unsigned int bufferlen);
|
||||
int cli_ConnectTo(Client* cli,char * pAdresse,unsigned short Port,int webAdresse);
|
||||
int cli_DisconnectFrom(Client* cli);
|
||||
void cli_WaitForPacket(Client* cli);
|
||||
|
||||
cli_init(Client* cli);
|
||||
cli_destroy(Client* cli);
|
||||
|
||||
|
||||
#endif
|
|
@ -157,7 +157,7 @@ int network_init(void)
|
|||
|
||||
// start echo and ping server
|
||||
echo_init();
|
||||
ping_init();
|
||||
//ping_init();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
|
199
kernel/server.c
Normal file
199
kernel/server.c
Normal file
|
@ -0,0 +1,199 @@
|
|||
#include "server.h"
|
||||
|
||||
typedef int UINT_PTR;
|
||||
#define SOCKET_ERROR -1
|
||||
|
||||
|
||||
int srv_sendBuffer(Server* srv, unsigned int cli,void* pBuffer, unsigned int dwLen)
|
||||
{
|
||||
int result;
|
||||
ServerEventArgs e;
|
||||
//abfragen ob client existiert!! wichtig
|
||||
if (srv->sConnections[cli] != SOCKET_ERROR)
|
||||
{
|
||||
result= send(srv->sConnections[cli],(char*)pBuffer,dwLen,0);
|
||||
if (srv->_OnWrite!=0)
|
||||
{
|
||||
e.ClientID = cli;
|
||||
e.dwLen = dwLen;
|
||||
e.pBuffer = pBuffer;
|
||||
srv->_OnWrite(&e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
void srv_DisconnectClient(Server* srv, unsigned int cli)
|
||||
{
|
||||
ServerEventArgs e;
|
||||
closesocket(srv->sConnections[cli]);
|
||||
srv->sConnections[cli]=SOCKET_ERROR;
|
||||
srv->dwConnections--;
|
||||
if (srv->_OnDisconnect!=0)
|
||||
{
|
||||
e.ClientID = cli;
|
||||
e.dwLen = 0;
|
||||
e.pBuffer = 0;
|
||||
srv->_OnDisconnect(&e);
|
||||
}
|
||||
|
||||
memset(&srv->ConnectionsAddr[cli],0,sizeof(struct sockaddr_in));
|
||||
}
|
||||
void* srv_WaitForConnection(Server* srv)
|
||||
{
|
||||
SOCKET tmpClient;
|
||||
struct sockaddr_in tmpAddr;
|
||||
int tmpAddrLen;
|
||||
ServerEventArgs e;
|
||||
ServerThreadArgs* t;
|
||||
unsigned int i;
|
||||
|
||||
|
||||
do
|
||||
{
|
||||
if (srv->dwConnections < srv->dwMaximumConnections)
|
||||
{
|
||||
tmpAddrLen = sizeof(struct sockaddr);
|
||||
if ((tmpClient=accept(srv->sSocket,(struct sockaddr*)&tmpAddr,&tmpAddrLen)) != SOCKET_ERROR)
|
||||
{
|
||||
for(i=0;i<srv->dwMaximumConnections;i++)
|
||||
{
|
||||
if (srv->sConnections[i] == SOCKET_ERROR)
|
||||
{
|
||||
srv->sConnections[i] = tmpClient;
|
||||
srv->ConnectionsAddr[i] = tmpAddr;
|
||||
srv->dwConnections++;
|
||||
|
||||
if(srv->_OnConnect!=0)
|
||||
{
|
||||
e.ClientID = i;
|
||||
e.dwLen = 0;
|
||||
e.pBuffer =0;
|
||||
srv->_OnConnect(&e);
|
||||
}
|
||||
|
||||
t = (ServerThreadArgs*) kmalloc(sizeof(ServerThreadArgs));
|
||||
t->ID = i;
|
||||
t->srv = srv;
|
||||
create_kernel_task(&srv->bThreads[i],srv_WaitForPacket,t);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}while(1);
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
void* srv_WaitForPacket(ServerThreadArgs* t)
|
||||
{
|
||||
int iResult=1;
|
||||
char pTmpBuffer[DEF_BUFFERSIZE+1];
|
||||
ServerEventArgs e;
|
||||
Server* srv = t->srv;
|
||||
int cli = t->ID;
|
||||
|
||||
while (iResult > 0)
|
||||
{
|
||||
iResult = recv(srv->sConnections[cli] ,pTmpBuffer, DEF_BUFFERSIZE, 0);
|
||||
if (iResult > 0)
|
||||
{
|
||||
pTmpBuffer[iResult]='\0';
|
||||
if(srv->_OnRead!=0)
|
||||
{
|
||||
e.ClientID = cli;
|
||||
e.dwLen = iResult;
|
||||
e.pBuffer = pTmpBuffer;
|
||||
srv->_OnRead(&e);
|
||||
}
|
||||
}
|
||||
else //Verbindung geschlossen
|
||||
{
|
||||
closesocket(srv->sConnections[cli]);
|
||||
srv->sConnections[cli]=SOCKET_ERROR;
|
||||
if(srv->_OnDisconnect!=0)
|
||||
{
|
||||
e.ClientID = cli;
|
||||
e.dwLen = 0;
|
||||
e.pBuffer = 0;
|
||||
srv->_OnDisconnect(&e);
|
||||
}
|
||||
memset(&srv->ConnectionsAddr[cli],0,sizeof(struct sockaddr_in));
|
||||
srv->dwConnections--;
|
||||
iResult=-1;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int server_init(Server* srv, unsigned short Port, unsigned int dwMaxConnections)
|
||||
{
|
||||
struct sockaddr_in tmpAddr;
|
||||
int tmpAddrLen;
|
||||
ServerThreadArgs t;
|
||||
tmpAddrLen = sizeof(struct sockaddr);
|
||||
|
||||
// Unregister Events
|
||||
srv->_OnConnect=0;
|
||||
srv->_OnRead=0;
|
||||
srv->_OnDisconnect=0;
|
||||
srv->_OnWrite=0;
|
||||
|
||||
// Allocate needed Memory
|
||||
srv->sConnections=(SOCKET*)kmalloc(sizeof(SOCKET)*dwMaxConnections);
|
||||
srv->ConnectionsAddr =(struct sockaddr_in*) kmalloc(sizeof(struct sockaddr_in)*dwMaxConnections);
|
||||
srv->bThreads = (tid_t*)kmalloc(sizeof(tid_t)*dwMaxConnections);
|
||||
|
||||
if (!srv->sConnections || !srv->ConnectionsAddr || !srv->bThreads)
|
||||
{
|
||||
kprintf("low on mem -%d- -%d- -%d-",srv->sConnections ,srv->ConnectionsAddr,srv->bThreads);
|
||||
return -1;
|
||||
}
|
||||
|
||||
srv->dwMaximumConnections=dwMaxConnections;
|
||||
|
||||
memset(srv->sConnections,0xFF,sizeof(SOCKET)*dwMaxConnections);
|
||||
memset(srv->ConnectionsAddr,0x00,sizeof(struct sockaddr_in)*dwMaxConnections);
|
||||
|
||||
srv->dwConnections=0;
|
||||
srv->wPort = Port;
|
||||
|
||||
srv->adAddr.sin_addr.s_addr = INADDR_ANY; // IP'S die der Server annehmen soll
|
||||
srv->adAddr.sin_family = AF_INET; // AdressFamilie (TCP/IP)
|
||||
srv->adAddr.sin_port = htons(srv->wPort); // Port auf dem der server erreichbar seien soll
|
||||
|
||||
srv->sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Einen Socket erstellen
|
||||
|
||||
bind( srv->sSocket,(const struct sockaddr *) &srv->adAddr, sizeof(srv->adAddr)); // Der Server an die Adresse binden;
|
||||
listen(srv->sSocket,srv->dwMaximumConnections); // Den Server in listenig State versetzen
|
||||
|
||||
create_kernel_task(&srv->bThread_listen,srv_WaitForConnection,srv);
|
||||
// sConnections[0] = accept(sSocket,(struct sockaddr*)&tmpAddr,&tmpAddrLen);
|
||||
// t.ID = 0;
|
||||
// bthread_create(&bThreads[0],NULL,(start_routine) srv_WaitForPacket,&t);
|
||||
}
|
||||
|
||||
int server_destroy(Server* srv)
|
||||
{
|
||||
unsigned int i;
|
||||
for (i=0;i<srv->dwMaximumConnections;i++)
|
||||
{
|
||||
if (srv->sConnections[i] != SOCKET_ERROR)closesocket(srv->sConnections[i]);
|
||||
//bthread_terminate(&srv->bThreads[i],0x00);
|
||||
}
|
||||
closesocket(srv->sSocket);
|
||||
//bthread_terminate(&srv->bThread_listen,0x00);
|
||||
|
||||
// free(srv->sConnections);
|
||||
// free(srv->ConnectionsAddr);
|
||||
// free(srv->bThreads);
|
||||
|
||||
}
|
67
kernel/server.h
Normal file
67
kernel/server.h
Normal file
|
@ -0,0 +1,67 @@
|
|||
#ifndef __SERVER__
|
||||
#define __SERVER__
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
#include <system/threads/bthread.h>
|
||||
#else
|
||||
#include <metalsvm/tasks.h>
|
||||
#endif
|
||||
|
||||
#ifndef LWIP_SOCKET
|
||||
#include <lwip/sockets.h>
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct _ServerEventArgs
|
||||
{
|
||||
unsigned int ClientID;
|
||||
unsigned int dwLen;
|
||||
void* pBuffer;
|
||||
} ServerEventArgs;
|
||||
|
||||
#define DEF_BUFFERSIZE 2048
|
||||
|
||||
typedef void (*ServerEventHandler)(ServerEventArgs*);
|
||||
|
||||
#ifndef SOCKET
|
||||
#define SOCKET int
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct _Server
|
||||
{
|
||||
SOCKET sSocket;
|
||||
unsigned int* sConnections;
|
||||
struct sockaddr_in adAddr;
|
||||
unsigned short wPort;
|
||||
unsigned int dwConnections;
|
||||
unsigned int dwMaximumConnections;
|
||||
|
||||
struct sockaddr_in* ConnectionsAddr;
|
||||
|
||||
tid_t bThread_listen;
|
||||
tid_t* bThreads;
|
||||
|
||||
ServerEventHandler _OnConnect;
|
||||
ServerEventHandler _OnDisconnect;
|
||||
ServerEventHandler _OnRead;
|
||||
ServerEventHandler _OnWrite;
|
||||
} Server;
|
||||
|
||||
typedef struct _ServerThreadArgs
|
||||
{
|
||||
Server* srv;
|
||||
unsigned int ID;
|
||||
} ServerThreadArgs;
|
||||
|
||||
|
||||
int srv_sendBuffer(Server* srv,unsigned int cli,void* pBuffer, unsigned int dwLen);
|
||||
void srv_DisconnectClient(Server* srv,unsigned int cli);
|
||||
void* srv_WaitForConnection(Server* srv);
|
||||
void* srv_WaitForPacket(ServerThreadArgs* t);
|
||||
|
||||
int server_init(Server* srv,unsigned short Port, unsigned int dwMaxConnections);
|
||||
int server_destroy(Server* srv);
|
||||
|
||||
#endif
|
76
kernel/shell.c
Normal file
76
kernel/shell.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
#include "shell.h"
|
||||
|
||||
#include "server.h"
|
||||
#include "client.h"
|
||||
|
||||
#include <metalsvm/time.h>
|
||||
|
||||
static Server srv;
|
||||
static int emac_id = -1;
|
||||
|
||||
static Client cli;
|
||||
|
||||
static int iamsrv = 0;
|
||||
|
||||
char shellbuffer[512];
|
||||
|
||||
|
||||
void shelldebugprint(char* x)
|
||||
{
|
||||
kprintf("debugprinting : %s",x);
|
||||
if (iamsrv)
|
||||
srv_sendBuffer(&srv,emac_id,x,strlen(x));
|
||||
else
|
||||
cli_sendBuffer(&cli,x,strlen(x));
|
||||
}
|
||||
|
||||
void shell_on_connect(ServerEventArgs* e)
|
||||
{
|
||||
kprintf("connection from %s:%d",inet_ntoa(srv.ConnectionsAddr[e->ClientID].sin_addr),ntohs(srv.ConnectionsAddr[e->ClientID].sin_port));
|
||||
kprintf("connehx: 0x%.8X",srv.ConnectionsAddr[e->ClientID].sin_addr);
|
||||
// if (srv.ConnectionsAddr[e->ClientID].sin_addr.s_addr > 0xC0A80031 || srv.ConnectionsAddr[e->ClientID].sin_addr.s_addr < 0xC0A80001)
|
||||
if (srv.ConnectionsAddr[e->ClientID].sin_addr.s_addr == 0xFE04A8C0)
|
||||
{
|
||||
emac_id = e->ClientID;
|
||||
kprintf("bmc connected");
|
||||
}
|
||||
else
|
||||
kprintf("link engaged\n");
|
||||
}
|
||||
|
||||
void shell_on_disconnect(ServerEventArgs* e)
|
||||
{
|
||||
kprintf("connection lost from %s:%d",inet_ntoa(srv.ConnectionsAddr[e->ClientID].sin_addr),ntohs(srv.ConnectionsAddr[e->ClientID].sin_port));
|
||||
}
|
||||
|
||||
void shell_on_read(ServerEventArgs* e)
|
||||
{
|
||||
if (emac_id != -1 && e->ClientID != emac_id)
|
||||
srv_sendBuffer(&srv,emac_id,e->pBuffer,e->dwLen);
|
||||
else
|
||||
// else commandos oder so
|
||||
srv_sendBuffer(&srv,emac_id,e->pBuffer,e->dwLen);
|
||||
}
|
||||
|
||||
void shell_init(int srv_or_cli)
|
||||
{
|
||||
if (!srv_or_cli)
|
||||
{
|
||||
iamsrv = 1;
|
||||
kprintf("server init");
|
||||
server_init(&srv,23,49);
|
||||
srv._OnConnect = shell_on_connect;
|
||||
srv._OnDisconnect = shell_on_disconnect;
|
||||
srv._OnRead = shell_on_read;
|
||||
}
|
||||
else
|
||||
{
|
||||
sleep(3);
|
||||
kprintf("client init");
|
||||
cli_init(&cli);
|
||||
while (cli_ConnectTo(&cli,"192.168.0.1",23,0));
|
||||
sleep(1);
|
||||
|
||||
}
|
||||
|
||||
}
|
14
kernel/shell.h
Normal file
14
kernel/shell.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef SHELL_H
|
||||
#define SHELL_H
|
||||
|
||||
#include <metalsvm/stdio.h>
|
||||
|
||||
void shelldebugprint(char* x);
|
||||
|
||||
void shell_init(int srv_or_cli);
|
||||
|
||||
extern char shellbuffer[512];
|
||||
|
||||
#define SHELLDEBUGPRINTF(x,...) ksnprintf(shellbuffer,sizeof(shellbuffer),x,##__VA_ARGS__);shelldebugprint(shellbuffer);
|
||||
|
||||
#endif // SHELL_H
|
249
kernel/tests.c
249
kernel/tests.c
|
@ -33,6 +33,12 @@
|
|||
|
||||
#include <asm/SCC_API.h>
|
||||
#include <lwip/sockets.h>
|
||||
|
||||
#include "client.h"
|
||||
#include "server.h"
|
||||
|
||||
#include "shell.h"
|
||||
|
||||
#endif
|
||||
|
||||
static sem_t consuming, producing;
|
||||
|
@ -121,129 +127,139 @@ static int join_test(void* arg)
|
|||
|
||||
kprintf("Child %u finished: result = %d\n", id, result);
|
||||
|
||||
dump_scheduling_statistics();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_LWIP) && defined(CONFIG_ROCKCREEK)
|
||||
static int server_task(void* e)
|
||||
|
||||
|
||||
#define SHELLDEBUGPRINTF(x,...) kprintf(x,##__VA_ARGS__);
|
||||
|
||||
static int srv_cnt = 0;
|
||||
static Server srv;
|
||||
void srv_on_read(ServerEventArgs* e)
|
||||
{
|
||||
int sockfd, newsockfd, portno, clilen;
|
||||
char buffer[256];
|
||||
struct sockaddr_in serv_addr, cli_addr;
|
||||
int n;
|
||||
|
||||
/* First call to socket() function */
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
kprintf("ERROR opening socket");
|
||||
return -1;
|
||||
}
|
||||
/* Initialize socket structure */
|
||||
memset((char *) &serv_addr,0, sizeof(serv_addr));
|
||||
portno = 5001;
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
serv_addr.sin_port = htons(portno);
|
||||
|
||||
kprintf("binding");
|
||||
/* Now bind the host address using bind() call.*/
|
||||
if (bind(sockfd, (struct sockaddr *) &serv_addr,
|
||||
sizeof(serv_addr)) < 0)
|
||||
{
|
||||
kprintf("ERROR on binding");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Now start listening for the clients, here process will
|
||||
* go in sleep mode and will wait for the incoming connection
|
||||
*/
|
||||
kprintf("listening");
|
||||
listen(sockfd,5);
|
||||
clilen = sizeof(cli_addr);
|
||||
|
||||
/* Accept actual connection from the client */
|
||||
kprintf("accepting");
|
||||
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
|
||||
if (newsockfd < 0)
|
||||
{
|
||||
kprintf("ERROR on accept");
|
||||
return -1;
|
||||
}
|
||||
/* If connection is established then start communicating */
|
||||
memset(buffer,0,256);
|
||||
kprintf("recieving");
|
||||
n = read( newsockfd,buffer, 255);
|
||||
if (n < 0)
|
||||
{
|
||||
kprintf("ERROR reading from socket");
|
||||
return -1;
|
||||
}
|
||||
kprintf("Here is the message: %s\n",buffer);
|
||||
|
||||
/* Write a response to the client */
|
||||
kprintf("writing");
|
||||
n = write(newsockfd,"I got your message",18);
|
||||
if (n < 0)
|
||||
{
|
||||
kprintf("ERROR writing to socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
// kprintf("%i:",srv_cnt);
|
||||
// hex_dump(e->dwLen,e->pBuffer);
|
||||
// srv_cnt++;
|
||||
/* printf("%i:",cnt);
|
||||
puts(e->pBuffer);
|
||||
puts("\n");
|
||||
cnt++;
|
||||
*/
|
||||
}
|
||||
|
||||
static int client_task(void* e)
|
||||
void srv_on_disc(ServerEventArgs*e )
|
||||
{
|
||||
char dir[256];
|
||||
int sd;
|
||||
struct sockaddr_in pin;
|
||||
kprintf("connection lost!!!\n");
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
void srv_on_conn(ServerEventArgs* e)
|
||||
{
|
||||
int i = 0, err = 0;
|
||||
int tmp1,tmp2;
|
||||
char buff[1024];
|
||||
|
||||
/* fill in the socket structure with host information */
|
||||
memset(&pin, 0, sizeof(pin));
|
||||
pin.sin_family = AF_INET;
|
||||
pin.sin_addr.s_addr = inet_addr("192.168.0.1");
|
||||
pin.sin_port = htons(5001);
|
||||
SHELLDEBUGPRINTF("someone finally connected\n");
|
||||
|
||||
/* grab an Internet domain socket */
|
||||
if ((sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
|
||||
kprintf("socketfail");
|
||||
return -1;
|
||||
}
|
||||
tmp1 = get_clock_tick();
|
||||
for (i = 0; i < 1024*4; i++)
|
||||
{
|
||||
err = srv_sendBuffer(&srv,e->ClientID,buff,sizeof(buff));
|
||||
if ( err < 0)
|
||||
{
|
||||
SHELLDEBUGPRINTF("err: %d", err);
|
||||
}
|
||||
|
||||
kprintf("connecting with socket nr : %d",sd);
|
||||
/* connect to PORT on HOST */
|
||||
if (!(i%10))
|
||||
{
|
||||
SHELLDEBUGPRINTF("\r-%d-",i);
|
||||
}
|
||||
|
||||
if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {
|
||||
kprintf("connectfail");
|
||||
return -1;
|
||||
}
|
||||
kprintf("sending");
|
||||
/* send a message to the server PORT on machine HOST */
|
||||
if (send(sd, "HELLO THERE", strlen("HELLO THERE"), 0) == -1) {
|
||||
kprintf("sendfail");
|
||||
return -1;
|
||||
}
|
||||
kprintf("recieving");
|
||||
/* wait for a message to come back from the server */
|
||||
if (recv(sd, dir, 256, 0) == -1) {
|
||||
kprintf("recvfail");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
tmp2 = get_clock_tick();
|
||||
|
||||
/* spew-out the results and bail out of here! */
|
||||
kprintf("%s\n", dir);
|
||||
// SHELLDEBUGPRINTF("send with %f kb/s",((float)i*sizeof(buff))/(tmp2-tmp1));
|
||||
SHELLDEBUGPRINTF("send %d bytes in %d ticks",i*sizeof(buff),(tmp2-tmp1));
|
||||
}
|
||||
|
||||
close(sd);
|
||||
void* server_task(void* e)
|
||||
{
|
||||
SHELLDEBUGPRINTF("created server\n");
|
||||
|
||||
return 0;
|
||||
server_init(&srv,5555,2);
|
||||
|
||||
srv._OnRead = srv_on_read;
|
||||
srv._OnDisconnect = srv_on_disc;
|
||||
srv._OnConnect = srv_on_conn;
|
||||
|
||||
while(1)
|
||||
sleep(2);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int cli_cnt = 0;
|
||||
void cli_on_read(ClientEventArgs* e)
|
||||
{
|
||||
kprintf("\r-%d-",cli_cnt);
|
||||
cli_cnt++;
|
||||
// printf("%i:",cnt);
|
||||
// hex_dump(e->dwLen,e->pBuffer);
|
||||
// cnt++;
|
||||
/* puts(e->pBuffer);
|
||||
puts("\n");
|
||||
cnt++;
|
||||
*/
|
||||
}
|
||||
|
||||
void cli_on_disc(ClientEventArgs*e )
|
||||
{
|
||||
kprintf("connection lost!!!\n");
|
||||
}
|
||||
|
||||
|
||||
void* client_task(void* e)
|
||||
{
|
||||
int err = -2;
|
||||
Client cli;
|
||||
char netbuffer[256];
|
||||
kprintf("created client");
|
||||
cli_init(&cli);
|
||||
cli._OnRead = cli_on_read;
|
||||
cli._OnDisconnect = cli_on_disc;
|
||||
sleep(2);
|
||||
|
||||
SHELLDEBUGPRINTF("Client is ready...\n");
|
||||
while (err)
|
||||
{
|
||||
sleep(1);
|
||||
err = cli_ConnectTo(&cli,"192.168.0.1",5555,0);
|
||||
SHELLDEBUGPRINTF("retry connect err = %d socket: %d\n",err,cli.sSocket);
|
||||
}
|
||||
SHELLDEBUGPRINTF("connected\n");
|
||||
|
||||
sleep(1);
|
||||
err = cli_sendBuffer(&cli,"Hallo Welt",sizeof("Hallo Welt"));
|
||||
SHELLDEBUGPRINTF("send message err = %d\n",err);
|
||||
|
||||
|
||||
while(1)
|
||||
sleep(2);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
void* alive(void*e)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
kprintf("IM ALIVE");
|
||||
sleep(2);
|
||||
}
|
||||
}
|
||||
|
||||
int test_init(void)
|
||||
{
|
||||
char* argv[] = {"/bin/tests", NULL};
|
||||
|
@ -252,20 +268,29 @@ int test_init(void)
|
|||
sem_init(&consuming, 0);
|
||||
mailbox_int32_init(&mbox);
|
||||
|
||||
#if 0 //defined(CONFIG_LWIP) && defined(CONFIG_ROCKCREEK)
|
||||
if (!RCCE_ue())
|
||||
create_kernel_task(NULL,server_task,NULL);
|
||||
else
|
||||
create_kernel_task(NULL,client_task,NULL);
|
||||
#if defined(CONFIG_LWIP) && defined(CONFIG_ROCKCREEK)
|
||||
|
||||
// shell_init(RCCE_ue());
|
||||
//
|
||||
// sleep(10);
|
||||
// SHELLDEBUGPRINTF("hello World! I AM CORE NO. %d =) \n",RCCE_ue());
|
||||
|
||||
create_kernel_task(NULL,alive,NULL);
|
||||
|
||||
if (!RCCE_ue())
|
||||
create_kernel_task(NULL,server_task,NULL);
|
||||
else
|
||||
create_kernel_task(NULL,client_task,NULL);
|
||||
|
||||
#endif
|
||||
|
||||
create_kernel_task(NULL, foo, "Hello from foo1");
|
||||
create_kernel_task(NULL, join_test, NULL);
|
||||
// create_kernel_task(NULL, foo, "Hello from foo1");
|
||||
// create_kernel_task(NULL, join_test, NULL);
|
||||
//create_kernel_task(NULL, producer, NULL);
|
||||
//create_kernel_task(NULL, consumer, NULL);
|
||||
//create_kernel_task(NULL, mail_ping, NULL);
|
||||
//create_user_task(NULL, "/bin/hello", argv);
|
||||
create_user_task(NULL, "/bin/tests", argv);
|
||||
// create_user_task(NULL, "/bin/tests", argv);
|
||||
//create_user_task(NULL, "/bin/jacobi", argv);
|
||||
//create_user_task(NULL, "/bin/jacobi", argv);
|
||||
|
||||
|
|
25
newlib/net/accept.c
Normal file
25
newlib/net/accept.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <_ansi.h>
|
||||
#include <_syslist.h>
|
||||
#include <errno.h>
|
||||
#undef errno
|
||||
extern int errno;
|
||||
#include "warning.h"
|
||||
#include "syscall.h"
|
||||
|
||||
|
||||
int
|
||||
_DEFUN (accept, (s, addr, addrlen),
|
||||
int s _AND void *addr _AND void *addrlen)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = SYSCALL3(__NR_accept, s, addr, addrlen);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
25
newlib/net/bind.c
Normal file
25
newlib/net/bind.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <_ansi.h>
|
||||
#include <_syslist.h>
|
||||
#include <errno.h>
|
||||
#undef errno
|
||||
extern int errno;
|
||||
#include "warning.h"
|
||||
#include "syscall.h"
|
||||
|
||||
|
||||
int
|
||||
_DEFUN (bind, (s, name, namelen),
|
||||
int s _AND void *name _AND int namelen)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = SYSCALL3(__NR_bind, s, name, namelen);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
25
newlib/net/closesocket.c
Normal file
25
newlib/net/closesocket.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <_ansi.h>
|
||||
#include <_syslist.h>
|
||||
#include <errno.h>
|
||||
#undef errno
|
||||
extern int errno;
|
||||
#include "warning.h"
|
||||
#include "syscall.h"
|
||||
|
||||
|
||||
int
|
||||
_DEFUN (closesocket, (s),
|
||||
int s)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = SYSCALL1(__NR_closesocket, s);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
25
newlib/net/connect.c
Normal file
25
newlib/net/connect.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <_ansi.h>
|
||||
#include <_syslist.h>
|
||||
#include <errno.h>
|
||||
#undef errno
|
||||
extern int errno;
|
||||
#include "warning.h"
|
||||
#include "syscall.h"
|
||||
|
||||
|
||||
int
|
||||
_DEFUN (connect, (s, name, namelen),
|
||||
int s _AND void *name _AND int namelen)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = SYSCALL3(__NR_connect, s, name, namelen);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
25
newlib/net/listen.c
Normal file
25
newlib/net/listen.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <_ansi.h>
|
||||
#include <_syslist.h>
|
||||
#include <errno.h>
|
||||
#undef errno
|
||||
extern int errno;
|
||||
#include "warning.h"
|
||||
#include "syscall.h"
|
||||
|
||||
|
||||
int
|
||||
_DEFUN (listen, (s, backlog),
|
||||
int s _AND int backlog)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = SYSCALL2(__NR_listen, s, backlog);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
25
newlib/net/recv.c
Normal file
25
newlib/net/recv.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <_ansi.h>
|
||||
#include <_syslist.h>
|
||||
#include <errno.h>
|
||||
#undef errno
|
||||
extern int errno;
|
||||
#include "warning.h"
|
||||
#include "syscall.h"
|
||||
|
||||
|
||||
int
|
||||
_DEFUN (recv, (s, data, size,flags),
|
||||
int s _AND void *data _AND int size _AND int flags)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = SYSCALL4(__NR_recv, s, data, size, flags);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
25
newlib/net/send.c
Normal file
25
newlib/net/send.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <_ansi.h>
|
||||
#include <_syslist.h>
|
||||
#include <errno.h>
|
||||
#undef errno
|
||||
extern int errno;
|
||||
#include "warning.h"
|
||||
#include "syscall.h"
|
||||
|
||||
|
||||
int
|
||||
_DEFUN (send, (s, data, size,flags),
|
||||
int s _AND void *data _AND int size _AND int flags)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = SYSCALL4(__NR_send, s, data, size, flags);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
25
newlib/net/socket.c
Normal file
25
newlib/net/socket.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <_ansi.h>
|
||||
#include <_syslist.h>
|
||||
#include <errno.h>
|
||||
#undef errno
|
||||
extern int errno;
|
||||
#include "warning.h"
|
||||
#include "syscall.h"
|
||||
|
||||
|
||||
int
|
||||
_DEFUN (socket, (domain, type, protocol),
|
||||
int domain _AND int type _AND int protocol)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = SYSCALL3(__NR_socket, domain, type, protocol);
|
||||
if (ret < 0) {
|
||||
errno = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
78
newlib/net/syscall.h
Normal file
78
newlib/net/syscall.h
Normal file
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
|
||||
* RWTH Aachen University
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Standard x86 syscalls for user programs running under MetalSVM
|
||||
*/
|
||||
|
||||
#ifndef __SYSCALL_H__
|
||||
#define __SYSCALL_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define __NR_exit 0
|
||||
#define __NR_write 1
|
||||
#define __NR_open 2
|
||||
#define __NR_close 3
|
||||
#define __NR_read 4
|
||||
#define __NR_lseek 6
|
||||
#define __NR_unlink 7
|
||||
#define __NR_getpid 8
|
||||
#define __NR_kill 9
|
||||
#define __NR_fstat 10
|
||||
#define __NR_sbrk 11
|
||||
#define __NR_fork 12
|
||||
#define __NR_wait 13
|
||||
#define __NR_execve 14
|
||||
#define __NR_times 15
|
||||
|
||||
#define _STR(token) #token
|
||||
#define _SYSCALLSTR(x) "int $" _STR(x) " "
|
||||
#define INT_SYSCALL 0x80
|
||||
|
||||
inline static long
|
||||
syscall(int nr, unsigned long arg0, unsigned long arg1, unsigned long arg2,
|
||||
unsigned long arg3, unsigned long arg4)
|
||||
{
|
||||
long res;
|
||||
|
||||
asm volatile (_SYSCALLSTR(INT_SYSCALL)
|
||||
: "=a" (res)
|
||||
: "0" (nr), "b" (arg0), "c" (arg1), "d" (arg2), "S" (arg3), "D" (arg4)
|
||||
: "memory", "cc");
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#define SYSCALL0(NR) \
|
||||
syscall(NR, 0, 0, 0, 0, 0)
|
||||
#define SYSCALL1(NR, ARG1) \
|
||||
syscall(NR, (unsigned long)ARG1, 0, 0, 0, 0)
|
||||
#define SYSCALL2(NR, ARG1, ARG2) \
|
||||
syscall(NR, (unsigned long)ARG1, (unsigned long)ARG2, 0, 0, 0)
|
||||
#define SYSCALL3(NR, ARG1, ARG2, ARG3) \
|
||||
syscall(NR, (unsigned long)ARG1, (unsigned long)ARG2, (unsigned long)ARG3, 0, 0)
|
||||
#define SYSCALL4(NR, ARG1, ARG2, ARG3, ARG4) \
|
||||
syscall(NR, (unsigned long)ARG1, (unsigned long)ARG2, (unsigned long)ARG3, (unsigned long) ARG4, 0)
|
||||
#define SYSCALL5(NR, ARG1, ARG2, ARG3, ARG4) \
|
||||
syscall(NR, (unsigned long)ARG1, (unsigned long)ARG2, (unsigned long)ARG3, (unsigned long) ARG4, (unsigned long) ARG5)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue