--- /dev/null
+/* network.c -- network management stuff
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#include "network.h"
+
+int network_init(t_net *net) {
+
+ struct sockaddr_in addr;
+ int true;
+
+ puts("[network] initializing network ...");
+
+ memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection));
+ net->c_count=0;
+ net->sendmask=0;
+
+ if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
+ perror("[network] socket call");
+ return N_ERROR;
+ }
+
+ memset(&addr,0,sizeof(struct sockaddr));
+ addr.sin_family=AF_INET;
+ addr.sin_port=htons(net->l_port);
+ addr.sin_addr.s_addr=INADDR_ANY;
+
+ /* prevent addres in use error message */
+ true=1;
+ if(setsockopt(net->l_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(true))==-1) {
+ perror("[network] setsockopt call");
+ return N_ERROR;
+ }
+
+ if(bind(net->l_fd,(struct sockaddr *)&addr,
+ sizeof(struct sockaddr))==-1) {
+ perror("[network] bind call");
+ return N_ERROR;
+ }
+
+ if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
+ perror("[network] listen call");
+ return N_ERROR;
+ }
+
+ printf("[network] listen on %s port %d\n",inet_ntoa(addr.sin_addr),
+ net->l_port);
+
+ return N_SUCCESS;
+}
+
+int network_shutdown(t_net *net) {
+
+ if(close(net->l_fd)==-1) {
+ perror("[network] close call");
+ return N_ERROR;
+ }
+
+ puts("[network] shutdown");
+
+ return N_SUCCESS;
+}
+
+int network_set_listen_port(t_net *net,in_port_t port) {
+
+ net->l_port=port;
+
+ return N_SUCCESS;
+}
+
+int network_manage_connection(t_net *net) {
+
+ int i;
+ struct sockaddr_in addr;
+
+ for(i=0;i<MAX_CONNECTIONS;i++) {
+
+ if(net->connection[i].status&C_IN_USE) {
+
+ if(net->connection[i].status&C_HANGUP) {
+ if(close(net->connection[i].fd)==-1) {
+ perror("[network] close call");
+ return N_ERROR;
+ }
+ printf("[network] connection %d closed\n",i);
+ net->connection[i].status=0;
+ }
+
+ if(net->connection[i].status&C_INFO_A) {
+
+ if(!net->connection[i].status&C_SOCKET) {
+ if((net->connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
+ perror("[network] socket call");
+ return N_ERROR;
+ }
+ }
+
+ if(!net->connection[i].status&C_ESTABL) {
+
+ memset(&addr,0,sizeof(struct sockaddr));
+ addr.sin_family=AF_INET;
+ addr.sin_port=htons(net->connection[i].port);
+ if(!inet_aton(net->connection[i].ip,&(addr.sin_addr))) {
+ perror("[network] inet_aton call");
+ return N_ERROR;
+ }
+
+ if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
+ sizeof(struct sockaddr))==-1) {
+ perror("[network] connect call");
+ return N_ERROR;
+ }
+
+ printf("[network] established connection to %s port %d on channel %d\n",
+ net->connection[i].ip,net->connection[i].port,i);
+
+ }
+
+ }
+
+ }
+
+ }
+
+ return N_SUCCESS;
+}
+
+int network_manage_incoming(t_net *net) {
+
+ int channel;
+ struct sockaddr_in addr;
+ int len;
+
+ for(channel=0;channel<MAX_CONNECTIONS;channel++) {
+ if(!net->connection[channel].status&C_IN_USE) {
+ if((net->connection[channel].fd=accept(net->l_fd,
+ (struct sockaddr *)&addr,
+ &len))==-1) {
+ perror("[network] accept call");
+ return N_ERROR;
+ }
+ strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
+ net->connection[channel].port=ntohs(addr.sin_port);
+ net->connection[channel].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL;
+ printf("[network] established connection from %s port %d on channel %d\n",
+ net->connection[channel].ip,net->connection[channel].port,channel);
+ return channel;
+ }
+ }
+
+ puts("[network] maximum connections reached");
+ return N_ERROR;
+}
+
+int network_send(int fd,unsigned char *data,int datasize) {
+
+ int count,left;
+
+ count=0;
+ left=datasize;
+
+ while(left) {
+ if((count=write(fd,data+datasize-left,left))==-1) {
+ perror("[network] write call");
+ return N_ERROR;
+ }
+ left-=count;
+ }
+
+ return N_SUCCESS;
+}
+
+int network_receive(int fd,unsigned char *data,int datasize) {
+
+ int count;
+
+ if((count=read(fd,data,datasize))==-1) {
+ perror("[network] read call");
+ return N_ERROR;
+ }
+
+ return count;
+}
+
+int send_info(int channel,t_net *net,char *name) {
+
+ char data[SEND_N_MAX];
+ int size;
+
+ size=strlen(name);
+
+ data[0]=SEND_N_NAME;
+ data[1]=size;
+ strncpy(data+2,name,size);
+ size+=2;
+
+ data[size]=SEND_N_G_CAP;
+ data[size+1]=sizeof(unsigned char);
+ data[size+1+sizeof(unsigned char)]=net->cap;
+ size+=(sizeof(unsigned char)+2);
+
+ data[size]=SEND_N_AV_CAP;
+ data[size+1]=sizeof(unsigned short);
+ data[size+1+sizeof(unsigned short)]=net->avcap;
+ size+=(sizeof(unsigned short)+2);
+
+ if(network_send(net->connection[channel].fd,data,size)==N_ERROR) {
+ puts("[network] send_info failed");
+ return N_ERROR;
+ }
+
+ return N_SUCCESS;
+}
+
+int receive_info(int channel,t_net *net) {
+
+ char data[CHAR_N_UNAME+2];
+ int count,length;
+
+ count=0;
+
+ if((length=network_receive(net->connection[channel].fd,
+ data,SEND_N_MAX))==N_ERROR) {
+ puts("[network] receive_info failed");
+ return N_ERROR;
+ }
+
+ while(length-count) {
+ switch(data[count]) {
+ case SEND_N_NAME:
+ strncpy(net->connection[channel].name,&data[count+2],data[count+1]);
+ net->connection[channel].name[(int)data[count+2]]='\0';
+ count+=(data[count+2]+2);
+ break;
+ case SEND_N_G_CAP:
+ net->connection[channel].cap=data[count+4];
+ count+=(sizeof(unsigned char)+2);
+ break;
+ case SEND_N_AV_CAP:
+ net->connection[channel].avcap=data[count+3]<<8;
+ net->connection[channel].avcap|=data[count+4];
+ count+=(sizeof(unsigned short)+2);
+ break;
+ default:
+ puts("[network] receive_info, unknown character");
+ return N_ERROR;
+ }
+ }
+
+ return N_SUCCESS;
+}
--- /dev/null
+/* network.h -- network headers */
+
+#ifndef NETWORK_H
+#define NETWORK_H
+
+/* includes */
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+/* net specific includes */
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+/* defines */
+#define MAX_CONNECTIONS 32
+
+#define IP_DIGITS 16
+#define C_IN_USE (1<<0)
+#define C_INFO_A (1<<1)
+#define C_SOCKET (1<<2)
+#define C_ESTABL (1<<3)
+#define C_HANGUP (1<<4)
+
+#define CHAR_N_UNAME 32
+
+#define SEND_N_MAX 128
+#define SEND_N_NAME 'n'
+#define SEND_N_G_CAP 'g'
+#define SEND_N_AV_CAP 'c'
+
+#define N_SUCCESS 1
+#define N_ERROR -1
+
+#define MAX_LISTEN_QUEUE 32
+
+/* net specific variables */
+typedef struct s_connection {
+ int fd;
+ char name[CHAR_N_UNAME];
+ char ip[IP_DIGITS];
+ in_port_t port;
+ unsigned char status;
+ unsigned char cap; /* general capabilities */
+ unsigned short avcap; /* audio/video capabilities */
+} t_connection;
+
+typedef struct s_net {
+ int l_fd; /* listen file descriptor */
+ in_port_t l_port;
+ unsigned char cap;
+ unsigned short avcap;
+ /* limited connections by now -- replaced by list management later */
+ int c_count;
+ t_connection connection[MAX_CONNECTIONS];
+ unsigned int sendmask; /* 32 bits for maximum of 32 connections */
+} t_net;
+
+/* function prototypes */
+int network_init(t_net *net);
+int network_shutdown(t_net *net);
+int network_set_listen_port(t_net *net,in_port_t port);
+int network_manage_connection(t_net *net);
+int network_manage_incoming(t_net *net);
+int network_send(int fd,unsigned char *data,int datasize);
+int network_receive(int fd,unsigned char *data,int datasize);
+int send_info(int channel,t_net *net,char *name);
+int receive_info(int channel,t_net *net);
+
+#endif