added fast(er) list to be included (just in header) + small bug fix
[my-code/api.git] / network / network.h
1 /* network.h -- network headers */
2
3 #ifndef NETWORK_H
4 #define NETWORK_H
5
6 /* includes */
7 #define _GNU_SOURCE
8 #include <stdio.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <unistd.h>
14
15 /* net specific includes */
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18
19 /* defines */
20 #define MAX_CONNECTIONS 32
21 #define MAX_NIC_DEVICE 32
22
23 #define IP_DIGITS 16
24 #define C_IN_USE (1<<0)
25 #define C_INFO_A (1<<1)
26 #define C_SOCKET (1<<2)
27 #define C_ESTABL (1<<3)
28 #define C_HANGUP (1<<4)
29
30 #define SEND_N_MAX 128
31
32 #define N_SUCCESS 1
33 #define N_ERROR -1
34 #define N_E_IN_USE -2
35 #define N_E_NO_INFO -3
36 #define N_E_CLOSE -4
37 #define N_E_CONNECT -5
38 #define N_E_NC -6
39 #define N_E_ACCEPT -7
40 #define N_E_MAXC -8
41
42 #define N_UDP_WRONG_SENDER -9
43
44 #define MAX_LISTEN_QUEUE 32
45
46 #define MTU 1448
47
48 /* net specific variables */
49 typedef struct s_connection {
50   int fd;
51   char ip[IP_DIGITS];
52   in_port_t port;
53   unsigned char status;
54   unsigned short cap; /* capabilities */
55 } t_connection;
56
57 typedef struct s_net {
58   int outfd;
59   int l_fd; /* fd for tcp conn */
60   int l_udp_fd; /* fd for udp data receive */
61   int s_udp_fd; /* fd for udp data send */
62   in_port_t l_port; /* tcp port */
63   int l_udp_port; /* udp listen port */
64   unsigned short cap;
65   /* limited connections by now -- replaced by list management later */
66   int c_count;
67   t_connection connection[MAX_CONNECTIONS];
68   unsigned int sendmask; /* 32 bits for maximum of 32 connections */
69   char nic[MAX_NIC_DEVICE];
70 } t_net;
71
72 /* function prototypes */
73 int network_init(t_net *net,int outfd);
74 int network_listen(t_net *net);
75 int network_shutdown(t_net *net);
76 int network_set_listen_port(t_net *net,in_port_t port);
77 int network_manage_connection(t_net *net);
78 int network_connect(t_net *net,int channel);
79 int network_close(t_net *net,int channel);
80 int network_close_all(t_net *net);
81 int network_set_connection_info(t_net *net,int channel,char *ip,int port);
82 int network_select(t_net *net,int channel);
83 int network_deselect(t_net *net,int channel);
84 int network_manage_incoming(t_net *net);
85 int network_send(int fd,unsigned char *data,int datasize);
86 int network_send_chan(t_net *net,int chan,unsigned char *data,int datasize);
87 int network_receive(int fd,unsigned char *data,int datasize);
88 int network_receive_chan(t_net *net,int chan,unsigned char *data,int datasize);
89 int network_udp_listen_init(t_net *net);
90 int network_udp_receive(t_net *net,int channel, unsigned char *data,int count);
91 int network_udp_send(t_net *net,int channel, unsigned char *data,int size);
92 int network_udp_shutdown(t_net *net);
93
94 #endif