return count;
}
+
+int network_udp_listen_init(t_net *net,int port) {
+
+ struct sockaddr_in addr;
+
+ if((net->l_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
+ perror("[network] socket call (udp-receive)");
+ return N_ERROR;
+ }
+
+ memset(&addr,0,sizeof(struct sockaddr));
+ addr.sin_family=AF_INET;
+ addr.sin_port=htons(net->l_udp_port);
+ addr.sin_addr.s_addr=INADDR_ANY;
+ if(bind(net->l_udp_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr))==-1) {
+ perror("[network] bind call (udp)");
+ return N_ERROR;
+ }
+
+ printf("[network] listening on port %d (udp)\n",net->l_udp_port);
+
+ if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
+ perror("[network] socket call (udp-send)");
+ return N_ERROR;
+ }
+
+ return N_SUCCESS;
+}
+
+int network_set_udp_ports(t_net *net,int port) {
+
+ net->l_udp_port=port;
+
+ return N_SUCCESS;
+}
+
+int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) {
+
+ struct sockaddr_in addr;
+ socklen_t len;
+
+ if((count=recvfrom(net->l_udp_fd,data,count,0,
+ (struct sockaddr *)&addr,&len))==-1) {
+ perror("[network] recvfrom call");
+ return N_ERROR;
+ }
+
+ if(strncmp(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS)) {
+ printf("[network] packet from unknown: %s\n",inet_ntoa(addr.sin_addr));
+ return N_UDP_WRONG_SENDER;
+ }
+
+ return N_SUCCESS;
+}
+
+int network_udp_send(t_net *net,int channel, unsigned char *data,int size) {
+
+ int count,left;
+ struct sockaddr_in addr;
+
+ count=0;
+ left=count;
+
+ memset(&addr,0,sizeof(struct sockaddr));
+ addr.sin_family=AF_INET;
+ addr.sin_port=htons(net->l_udp_port);
+ inet_aton(net->connection[channel].ip,&(addr.sin_addr));
+
+ while(left) {
+ if((count=sendto(net->s_udp_fd,data+size-left,left,0,
+ (struct sockaddr *)&addr,sizeof(struct sockaddr)))==-1) {
+ perror("[network] sendto call");
+ return N_ERROR;
+ }
+ left-=count;
+ }
+
+ return N_SUCCESS;
+}
+
+int network_udp_shutdown(t_net *net) {
+
+ if(close(net->l_udp_fd)==-1) {
+ perror("[network] close call (udp-receive)");
+ return N_ERROR;
+ }
+
+ if(close(net->s_udp_fd)==-1) {
+ perror("[network] close call (udp-send)");
+ return N_ERROR;
+ }
+
+ return N_SUCCESS;
+}
#define N_E_ACCEPT -7
#define N_E_MAXC -8
+#define N_UDP_WRONG_SENDER -9
+
#define MAX_LISTEN_QUEUE 32
/* net specific variables */
} t_connection;
typedef struct s_net {
- int l_fd; /* listen file descriptor */
- in_port_t l_port;
+ int l_fd; /* fd for tcp conn */
+ int l_udp_fd; /* fd for udp data receive */
+ int s_udp_fd; /* fd for udp data send */
+ in_port_t l_port; /* tcp port */
+ int l_udp_port; /* udp listen port */
unsigned short cap;
/* limited connections by now -- replaced by list management later */
int c_count;
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 network_udp_listen_init(t_net *net,int port);
+int network_set_udp_ports(t_net *net,int port);
+int network_udp_receive(t_net *net,int channel, unsigned char *data,int count);
+int network_udp_send(t_net *net,int channel, unsigned char *data,int size);
+int network_udp_shutdown(t_net *net);
#endif