oups .. added missing files, added Makefile + .cvsignore list
authorhackbard <hackbard>
Wed, 5 May 2004 17:21:50 +0000 (17:21 +0000)
committerhackbard <hackbard>
Wed, 5 May 2004 17:21:50 +0000 (17:21 +0000)
src/.cvsignore [new file with mode: 0644]
src/Makefile [new file with mode: 0644]
src/input.c [new file with mode: 0644]
src/input.h [new file with mode: 0644]
src/network.c [new file with mode: 0644]
src/network.h [new file with mode: 0644]

diff --git a/src/.cvsignore b/src/.cvsignore
new file mode 100644 (file)
index 0000000..04f79d0
--- /dev/null
@@ -0,0 +1,2 @@
+*.o
+ivac
diff --git a/src/Makefile b/src/Makefile
new file mode 100644 (file)
index 0000000..0acc428
--- /dev/null
@@ -0,0 +1,17 @@
+# Makefile of ivac
+
+INCLUDEDIR = /usr/include
+
+CFLAGS = -DDEBUG -O3 -Wall
+LIBS =
+
+OBJS = network.o event.o input.o
+
+ivac: $(OBJS)
+       $(CC) -o $@ $(OBJS) ivac.c $(LIBS)
+all: ivac
+
+clean:
+       rm -f $(OBJS) ivac
+
+remake: clean all
diff --git a/src/input.c b/src/input.c
new file mode 100644 (file)
index 0000000..47f808b
--- /dev/null
@@ -0,0 +1,37 @@
+/* input.c -- input management stuff
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#include "input.h"
+
+int input_init(t_input *input) {
+
+  puts("[input] initializing input system ...");
+
+  if((input->content=(char *)malloc(MAX_CONTENT))==NULL) {
+    perror("[input] malloc call");
+    return K_ERROR;
+  }
+
+  return K_SUCCESS;
+}
+
+int input_shutdown(t_input *input) {
+
+  free(input->content);
+  puts("[input] shutdown");
+
+  return K_SUCCESS;
+}
+
+int input_get_char(t_input *input) {
+
+  char data[64];
+
+  puts("a hopefully nice display for user interaction will popup soon ;)");
+  read(0,data,64);
+
+  return K_SUCCESS;
+}
diff --git a/src/input.h b/src/input.h
new file mode 100644 (file)
index 0000000..7f5796b
--- /dev/null
@@ -0,0 +1,27 @@
+/* input.h -- input headers */
+
+#ifndef INPUT_H
+#define INPUT_H
+
+/* includes */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+/* defines */
+#define K_SUCCESS 1
+#define K_ERROR -1
+
+#define MENUE (1<<0)
+#define CONNECTIONS (1<<1)
+
+#define MAX_CONTENT 128
+
+/* input specific variables */
+typedef struct s_input {
+  char *content;
+  unsigned char mode;
+} t_input;
+
+#endif
diff --git a/src/network.c b/src/network.c
new file mode 100644 (file)
index 0000000..c7425ef
--- /dev/null
@@ -0,0 +1,253 @@
+/* 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;
+}
diff --git a/src/network.h b/src/network.h
new file mode 100644 (file)
index 0000000..274811e
--- /dev/null
@@ -0,0 +1,73 @@
+/* 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