#include "inet.h"
+int network_init(t_ivac *ivac) {
+
+ sockaddr_in addr;
+
+ puts("[ivac] inet: initializing network ...");
-int
+ memset(ivac->net.connection,0,MAX_CONNECTIONS*sizeof(t_connection));
+
+ if((ivac->net.l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
+ perror("[ivac] inet.c: socket call");
+ return ERROR;
+ }
+
+ memset(&addr,0,sizeof(struct sockaddr));
+ addr.sin_family=AF_INET;
+ addr.sin_port=htons(ivac->net.l_port);
+ addr.sin_addr.s_addr=INADDR_ANY;
+
+ if(bind(ivac->net.l_fd,(struct sockaddr *)&addr,
+ sizeof(struct sockaddr))==-1) {
+ perror("[ivac] inet.c: bind call");
+ return ERROR;
+ }
+
+ printf("[ivac] inet: listen on %s port %d.\n",inet_ntoa(addr.sin_addr.s_addr)
+ ,ivac->net.l_port);
+
+ return SUCCESS;
+}
+
+int network_manage_connection(t_ivac *ivac) {
+
+ int i;
+ sockaddr_in addr;
+
+ for(i=0;i<MAX_CONNECTIONS;i++) {
+
+ if(ivac->net.connection[i].status&C_IN_USE) {
+
+ if(ivac->net.connection[i].status&C_HANGUP) {
+ close(ivac->net.connection[i].fd);
+ ivac->net.connection[i].status=0;
+ }
+
+ if(ivac->net.connection[i].status&C_INFO_A) {
+
+ if(!ivac->net.connection[i].status&C_SOCKET) {
+ if((ivac->net.connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
+ perror("[ivac] inet.c: socket call");
+ return ERROR;
+ }
+ }
+
+ if(!ivac->net.connection[i].status&C_ESTABL) {
+
+ memset(&addr,0,sizeof(struct sockaddr));
+ addr.sin_family=AF_INET;
+ addr.sin_port=htons(ivac->net.connection[i].port);
+ addr.sin_addr_s_addr=inet_aton(ivac->net.connection[i].ip);
+
+ if(connect(ivac->net.connection[i].fd,(struct sockaddr *)&addr,
+ sizeof(struct sockaddr))==-1) {
+ perror("[ivac] inet.c: connect call");
+ return ERROR;
+ }
+
+ }
+
+ }
+
+ }
+
+ }
+
+ return SUCCESS;
+}
+
+int network_send(unsigned char *data,int datasize,int sendmask,t_ivac *ivac) {
+
+
+ return SUCCESS;
+}
#ifndef INET_H
#define INET_H
+/* includes */
+#include "ivac.h"
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
/* net specific includes */
#include <netinet/in.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)
+
/* net specific variables */
+typedef s_connection {
+ int fd;
+ char name[CHAR_USERNAME];
+ char ip[IP_DIGITS];
+ in_port_t port;
+ unsigned char status;
+} t_connection;
+
typedef s_net {
- int l_fd,c_fd;
-
+ int l_fd; /* listen file descriptor */
+ in_port_t l_port;
+ /* limited connections by now -- replaced by list management later */
+ int c_count;
+ t_connection connection[MAX_CONNECTIONS];
} t_net;
#endif