summaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/net')
-rw-r--r--src/net/dns.c18
-rw-r--r--src/net/dns.c~27
-rwxr-xr-xsrc/net/tcp.c547
-rw-r--r--src/net/tcp.c~401
-rw-r--r--src/net/udp.c517
-rw-r--r--src/net/udp.c~415
6 files changed, 549 insertions, 1376 deletions
diff --git a/src/net/dns.c b/src/net/dns.c
index 98e6be9..84c917a 100644
--- a/src/net/dns.c
+++ b/src/net/dns.c
@@ -1,5 +1,5 @@
#ifndef __WIN32__
-# include <sys/socket.h>
+# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>
@@ -12,16 +12,16 @@
#include "main.h"
-char *
-getIPFormDNS(char *domain)
+char* getIPFormDNS(char *domain)
{
- struct hostent *host;
+ struct hostent *host;
- host = gethostbyname(domain);
+ host = gethostbyname(domain);
- if (host == NULL || host->h_addr_list[0] == NULL) {
- return NULL;
- }
+ if( host == NULL || host->h_addr_list[0] == NULL )
+ {
+ return NULL;
+ }
- return strdup(inet_ntoa(*((struct in_addr *) host->h_addr_list[0])));
+ return strdup( inet_ntoa( * ( (struct in_addr *) host->h_addr_list[0] ) ) );
}
diff --git a/src/net/dns.c~ b/src/net/dns.c~
deleted file mode 100644
index 84c917a..0000000
--- a/src/net/dns.c~
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef __WIN32__
-# include <sys/socket.h>
-# include <netinet/in.h>
-# include <arpa/inet.h>
-# include <netdb.h>
-#else
-# include <windows.h>
-# include <wininet.h>
-#endif
-
-#include <string.h>
-
-#include "main.h"
-
-char* getIPFormDNS(char *domain)
-{
- struct hostent *host;
-
- host = gethostbyname(domain);
-
- if( host == NULL || host->h_addr_list[0] == NULL )
- {
- return NULL;
- }
-
- return strdup( inet_ntoa( * ( (struct in_addr *) host->h_addr_list[0] ) ) );
-}
diff --git a/src/net/tcp.c b/src/net/tcp.c
index 87c97ba..aae5e3c 100755
--- a/src/net/tcp.c
+++ b/src/net/tcp.c
@@ -16,371 +16,386 @@
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
-#include <netinet/tcp.h>
+#include <netinet/tcp.h>
#include <fcntl.h>
#include "tcp.h"
-sock_tcp_t *
-newSockTcp(int proto)
+sock_tcp_t* newSockTcp(int proto)
{
- sock_tcp_t *new;
+ sock_tcp_t *new;
- new = malloc(sizeof(sock_tcp_t));
- memset(new, 0, sizeof(sock_tcp_t));
- new->proto = proto;
+ new = malloc( sizeof(sock_tcp_t) );
+ memset(new, 0, sizeof(sock_tcp_t));
+ new->proto = proto;
- return new;
+ return new;
}
-void
-destroySockTcp(sock_tcp_t * p)
+void destroySockTcp(sock_tcp_t *p)
{
- assert(p != NULL);
- free(p);
+ assert( p != NULL );
+ free(p);
}
-sock_tcp_t *
-bindTcpSocket(char *address, int port, int proto)
+sock_tcp_t* bindTcpSocket(char *address, int port, int proto)
{
- sock_tcp_t *new;
- unsigned long param_setsock = 1;
- int len;
- int ret;
+ sock_tcp_t *new;
+ unsigned long param_setsock = 1;
+ int len;
+ int ret;
- assert(port > 0 && port < 65535);
+ assert( port > 0 && port < 65535 );
- new = newSockTcp(proto);
- ret = -1; // no Warnnings
+ new = newSockTcp(proto);
+ ret = -1; // no Warnnings
- assert(new != NULL);
+ assert( new != NULL );
- if (new->proto == PROTO_TCPv4) {
- new->sock = socket(AF_INET, SOCK_STREAM, 0);
- }
+ if( new->proto == PROTO_TCPv4 )
+ {
+ new->sock = socket(AF_INET, SOCK_STREAM, 0);
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_TCPv6) {
- new->sock = socket(AF_INET6, SOCK_STREAM, 0);
- }
-#endif
-
- if (new->sock < 0) {
- fprintf(stderr, _("Unable to create socket when connecting!\n"));
- destroySockTcp(new);
- return NULL;
- }
-
- setsockopt(new->sock, SOL_SOCKET, SO_REUSEADDR, (char *) &param_setsock,
- sizeof(param_setsock));
-
- if (new->proto == PROTO_TCPv4) {
- new->sockAddr.sin_family = AF_INET;
- inet_pton(AF_INET, address, &(new->sockAddr.sin_addr));
- new->sockAddr.sin_port = htons(port);
-
- len = sizeof(new->sockAddr);
- ret = bind(new->sock, (struct sockaddr *) &new->sockAddr, len);
- }
+ if( new->proto == PROTO_TCPv6 )
+ {
+ new->sock = socket(AF_INET6, SOCK_STREAM, 0);
+ }
+#endif
+
+ if( new->sock < 0 )
+ {
+ fprintf(stderr, _("Unable to create socket when connecting!\n"));
+ destroySockTcp(new);
+ return NULL;
+ }
+
+ setsockopt(new->sock, SOL_SOCKET, SO_REUSEADDR, (char *) &param_setsock, sizeof (param_setsock));
+
+ if( new->proto == PROTO_TCPv4 )
+ {
+ new->sockAddr.sin_family = AF_INET;
+ inet_pton(AF_INET, address, &(new->sockAddr.sin_addr));
+ new->sockAddr.sin_port = htons(port);
+
+ len = sizeof(new->sockAddr);
+ ret = bind(new->sock, (struct sockaddr *)&new->sockAddr, len);
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_TCPv6) {
- new->sockAddr6.sin6_family = AF_INET6;
- //new->sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
- new->sockAddr6.sin6_port = htons(port);
-
- len = sizeof(new->sockAddr6);
- ret = bind(new->sock, (struct sockaddr *) &new->sockAddr6, len);
- }
+ if( new->proto == PROTO_TCPv6 )
+ {
+ new->sockAddr6.sin6_family = AF_INET6;
+ //new->sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
+ new->sockAddr6.sin6_port = htons(port);
+
+ len = sizeof(new->sockAddr6);
+ ret = bind(new->sock, (struct sockaddr *)&new->sockAddr6, len);
+ }
#endif
- if (ret < 0) {
- fprintf(stderr, _("Unable to bint to port: %d\n"), port);
- destroySockTcp(new);
- return NULL;
- }
+ if( ret < 0 )
+ {
+ fprintf(stderr, _("Unable to bint to port: %d\n"), port);
+ destroySockTcp(new);
+ return NULL;
+ }
- listen(new->sock, 5);
+ listen(new->sock, 5);
- return new;
+ return new;
}
-sock_tcp_t *
-getTcpNewClient(sock_tcp_t * p)
+sock_tcp_t* getTcpNewClient(sock_tcp_t *p)
{
- sock_tcp_t *new;
- int client_len;
-
- assert(p != NULL);
- assert(p->sock >= 0);
+ sock_tcp_t *new;
+ int client_len;
- new = newSockTcp(p->proto);
+ assert( p != NULL );
+ assert( p->sock >= 0 );
- if (new->proto == PROTO_TCPv4) {
- client_len = sizeof(new->sockAddr);
+ new = newSockTcp(p->proto);
- new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr,
- (socklen_t *) & client_len);
- }
+ if( new->proto == PROTO_TCPv4 )
+ {
+ client_len = sizeof(new->sockAddr);
+
+ new->sock = accept(p->sock, (struct sockaddr *)&new->sockAddr,
+ (socklen_t *)&client_len);
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_TCPv6) {
- client_len = sizeof(new->sockAddr6);
-
- new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr6,
- (socklen_t *) & client_len);
- }
+ if( new->proto == PROTO_TCPv6 )
+ {
+ client_len = sizeof(new->sockAddr6);
+
+ new->sock = accept(p->sock, (struct sockaddr *)&new->sockAddr6,
+ (socklen_t *)&client_len);
+ }
#endif
- if (new->sock < 0) {
- //printf("XXX\n");
- destroySockTcp(new);
- return NULL;
- }
+ if( new->sock < 0 )
+ {
+ //printf("XXX\n");
+ destroySockTcp(new);
+ return NULL;
+ }
- return new;
+ return new;
}
-void
-getSockTcpIp(sock_tcp_t * p, char *str_ip, int len)
+void getSockTcpIp(sock_tcp_t *p, char *str_ip, int len)
{
- assert(p != NULL);
- assert(str_ip != NULL);
-
+ assert( p != NULL );
+ assert( str_ip != NULL );
- if (p->proto == PROTO_TCPv4) {
- inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len);
- strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
- //printf("strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));\n");
- }
+
+ if( p->proto == PROTO_TCPv4 )
+ {
+ inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len);
+ strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
+ //printf("strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));\n");
+ }
#ifdef SUPPORT_IPv6
- if (p->proto == PROTO_TCPv6) {
- inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len);
- }
+ if( p->proto == PROTO_TCPv6 )
+ {
+ inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len);
+ }
#endif
}
-int
-getSockTcpPort(sock_tcp_t * p)
+int getSockTcpPort(sock_tcp_t *p)
{
- assert(p != NULL);
+ assert( p != NULL );
- if (p->proto == PROTO_TCPv4) {
- return htons(p->sockAddr.sin_port);
- }
+ if( p->proto == PROTO_TCPv4 )
+ {
+ return htons(p->sockAddr.sin_port);
+ }
#ifdef SUPPORT_IPv6
- if (p->proto == PROTO_TCPv6) {
- return htons(p->sockAddr6.sin6_port);
- }
+ if( p->proto == PROTO_TCPv6 )
+ {
+ return htons(p->sockAddr6.sin6_port);
+ }
#endif
- assert(!_("Bad IP proto!"));
+ assert( ! _("Bad IP proto!") );
- return -1;
+ return -1;
}
-sock_tcp_t *
-connectTcpSocket(char *ip, int port, int proto)
+sock_tcp_t* connectTcpSocket(char *ip, int port, int proto)
{
- sock_tcp_t *new;
- int len;
- int ret;
+ sock_tcp_t *new;
+ int len;
+ int ret;
- assert(ip != NULL);
- assert(port > 0 && port < 65535);
+ assert( ip != NULL );
+ assert( port > 0 && port < 65535 );
- new = newSockTcp(proto);
- ret = -1; // no Warnnings
+ new = newSockTcp(proto);
+ ret = -1; // no Warnnings
- if (new->proto == PROTO_TCPv4) {
- new->sock = socket(AF_INET, SOCK_STREAM, 0);
- }
+ if( new->proto == PROTO_TCPv4 )
+ {
+ new->sock = socket(AF_INET, SOCK_STREAM, 0);
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_TCPv6) {
- new->sock = socket(AF_INET6, SOCK_STREAM, 0);
- }
-#endif
-
- if (new->sock < 0) {
- fprintf(stderr, _("Unable to create TCP socket!\n"));
- destroySockTcp(new);
- return NULL;
- }
-
- if (new->proto == PROTO_TCPv4) {
- new->sockAddr.sin_family = AF_INET;
- new->sockAddr.sin_addr.s_addr = inet_addr(ip);
- new->sockAddr.sin_port = htons(port);
-
- len = sizeof(new->sockAddr);
- ret = connect(new->sock, (struct sockaddr *) &new->sockAddr, len);
- }
+ if( new->proto == PROTO_TCPv6 )
+ {
+ new->sock = socket(AF_INET6, SOCK_STREAM, 0);
+ }
+#endif
+
+ if( new->sock < 0 )
+ {
+ fprintf(stderr, _("Unable to create TCP socket!\n"));
+ destroySockTcp(new);
+ return NULL;
+ }
+
+ if( new->proto == PROTO_TCPv4 )
+ {
+ new->sockAddr.sin_family = AF_INET;
+ new->sockAddr.sin_addr.s_addr = inet_addr(ip);
+ new->sockAddr.sin_port = htons(port);
+
+ len = sizeof(new->sockAddr);
+ ret = connect(new->sock, (struct sockaddr *)&new->sockAddr, len);
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_TCPv6) {
- new->sockAddr6.sin6_family = AF_INET6;
- inet_pton(AF_INET6, ip, &(new->sockAddr6.sin6_addr));
- new->sockAddr6.sin6_port = htons(port);
-
- len = sizeof(new->sockAddr6);
- ret = connect(new->sock, (struct sockaddr *) &new->sockAddr6, len);
- }
+ if( new->proto == PROTO_TCPv6 )
+ {
+ new->sockAddr6.sin6_family = AF_INET6;
+ inet_pton(AF_INET6, ip, &(new->sockAddr6.sin6_addr));
+ new->sockAddr6.sin6_port = htons(port);
+
+ len = sizeof(new->sockAddr6);
+ ret = connect(new->sock, (struct sockaddr *)&new->sockAddr6, len);
+ }
#endif
- if (ret < 0) {
- fprintf(stderr, "Unable to connect on: \"%s\" port: \"%d\"\n", ip,
- port);
- destroySockTcp(new);
- return NULL;
- }
+ if( ret < 0 )
+ {
+ fprintf(stderr, "Unable to connect on: \"%s\" port: \"%d\"\n", ip, port);
+ destroySockTcp(new);
+ return NULL;
+ }
- return new;
+ return new;
}
-int
-disableNagle(sock_tcp_t * p)
+int disableNagle(sock_tcp_t *p)
{
- int flag = 1;
-
- int result;
-
- result = setsockopt(p->sock, /* socket affected */
- IPPROTO_TCP, /* set option at TCP level */
- TCP_NODELAY, /* name of option */
- (char *) &flag, /* the cast is historical cruft */
- sizeof(int)); /* length of option value */
-
+ int flag = 1;
+
+ int result;
+
+ result = setsockopt(p->sock, /* socket affected */
+ IPPROTO_TCP, /* set option at TCP level */
+ TCP_NODELAY, /* name of option */
+ (char *) &flag, /* the cast is historical cruft */
+ sizeof(int)); /* length of option value */
+
#if 0
- if (result < 0) {
- printf("disabled nagle error\n");
- }
- else {
- printf("disabled nagle OK\n");
- }
+ if( result < 0 )
+ {
+ printf("disabled nagle error\n");
+ }
+ else
+ {
+ printf("disabled nagle OK\n");
+ }
#endif
- return result;
+ return result;
}
-int
-setTcpSockNonBlock(sock_tcp_t * p)
+int setTcpSockNonBlock(sock_tcp_t *p)
{
- /* Set to nonblocking socket mode */
+ /* Set to nonblocking socket mode */
#ifndef __WIN32__
- int oldFlag;
+ int oldFlag;
- oldFlag = fcntl(p->sock, F_GETFL, 0);
+ oldFlag = fcntl (p->sock, F_GETFL, 0);
- if (fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
- //printf("error setTcpSockNonBlock\n");
- return -1;
- }
+ if( fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1 )
+ {
+ //printf("error setTcpSockNonBlock\n");
+ return -1;
+ }
- //printf("setTcpSockNonBlock OK\n");
+ //printf("setTcpSockNonBlock OK\n");
#else
- unsigned long arg = 1;
- // Operation is FIONBIO. Parameter is pointer on non-zero number.
- if (ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR) {
- WSACleanup();
- return -1;
- }
+ unsigned long arg = 1;
+ // Operation is FIONBIO. Parameter is pointer on non-zero number.
+ if( ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR )
+ {
+ WSACleanup();
+ return -1;
+ }
#endif
- return 0;
+ return 0;
}
-int
-readTcpSocket(sock_tcp_t * p, void *address, int len)
+int readTcpSocket(sock_tcp_t *p, void *address, int len)
{
- assert(p != NULL);
- assert(address != NULL);
+ assert( p != NULL );
+ assert( address != NULL );
- return read(p->sock, address, len);
+ return read(p->sock, address, len);
}
-int
-writeTcpSocket(sock_tcp_t * p, void *address, int len)
+int writeTcpSocket(sock_tcp_t *p, void *address, int len)
{
- assert(p != NULL);
- assert(address != NULL);
+ assert( p != NULL );
+ assert( address != NULL );
- return write(p->sock, address, len);
+ return write(p->sock, address, len);
}
-void
-closeTcpSocket(sock_tcp_t * p)
+void closeTcpSocket(sock_tcp_t *p)
{
- assert(p != NULL);
+ assert( p != NULL );
- close(p->sock);
- destroySockTcp(p);
+ close(p->sock);
+ destroySockTcp(p);
}
#if 0
#define MAX_CLIENTS 32
#define BUF_SIZE 512
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
{
- sock_tcp_t *sock;
- sock_tcp_t *client[MAX_CLIENTS];
- int count_client;
- int changed, i;
- struct pollfd *tmp;
-
- sock = bindTcpSocket("127.0.0.1", 2200, PROTO_TCPv4);
- disableNagle(sock);
- count_client = 0;
-
- for (;;) {
- tmp = malloc((count_client + 1) * sizeof(struct pollfd));
-
- tmp[0].fd = sock->sock;
- tmp[0].events = POLLIN;
- tmp[0].revents = 0;
-
- for (i = 0; i < count_client; i++) {
- tmp[1 + i].fd = client[i]->sock;
- tmp[1 + i].events = POLLIN;
- tmp[1 + i].revents = 0;
- }
-
- changed = poll(tmp, count_client + 1, 1000);
-
- if (tmp[0].revents & POLLIN != 0) {
- client[count_client] = getTcpNewClient(sock);
- disableNagle(client[count_client]);
- count_client++;
- }
-
- for (i = 0; i < count_client; i++) {
- if (tmp[1 + i].revents & POLLIN != 0) {
- char buf[BUF_SIZE];
- int ret;
-
- ret = read(client[i]->sock, buf, BUF_SIZE);
-
- if (ret <= 0) {
- closeTcpSocket(client[i]);
-
- memmove(client + i,
- client + (i + 1),
- ((count_client - 1) - i) * sizeof(sock_tcp_t *));
-
- count_client--;
- continue;
- }
-
- write(client[i]->sock, buf, ret);
- }
- }
-
- free(tmp);
- }
+ sock_tcp_t *sock;
+ sock_tcp_t *client[MAX_CLIENTS];
+ int count_client;
+ int changed, i;
+ struct pollfd *tmp;
+
+ sock = bindTcpSocket( "127.0.0.1", 2200, PROTO_TCPv4 );
+ disableNagle(sock);
+ count_client = 0;
+
+ for(;;)
+ {
+ tmp = malloc( (count_client+1) * sizeof(struct pollfd) );
+
+ tmp[0].fd = sock->sock;
+ tmp[0].events = POLLIN;
+ tmp[0].revents = 0;
+
+ for( i = 0 ; i < count_client ; i++ )
+ {
+ tmp[1+i].fd = client[i]->sock;
+ tmp[1+i].events = POLLIN;
+ tmp[1+i].revents = 0;
+ }
+
+ changed = poll(tmp, count_client+1, 1000);
+
+ if( tmp[0].revents & POLLIN != 0 )
+ {
+ client[count_client] = getTcpNewClient(sock);
+ disableNagle(client[count_client]);
+ count_client++;
+ }
+
+ for( i = 0 ; i < count_client ; i++ )
+ {
+ if( tmp[1+i].revents & POLLIN != 0 )
+ {
+ char buf[BUF_SIZE];
+ int ret;
+
+ ret = read(client[i]->sock, buf, BUF_SIZE);
+
+ if( ret <= 0 )
+ {
+ closeTcpSocket(client[i]);
+
+ memmove(client + i,
+ client + (i+1),
+ ((count_client-1) - i) * sizeof(sock_tcp_t *) );
+
+ count_client--;
+ continue;
+ }
+
+ write(client[i]->sock, buf, ret);
+ }
+ }
+
+ free(tmp);
+ }
}
#endif
+
diff --git a/src/net/tcp.c~ b/src/net/tcp.c~
deleted file mode 100644
index aae5e3c..0000000
--- a/src/net/tcp.c~
+++ /dev/null
@@ -1,401 +0,0 @@
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <sys/poll.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-#include <sys/select.h>
-#include <stdio.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <unistd.h>
-#include <assert.h>
-#include <netdb.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <fcntl.h>
-
-#include "tcp.h"
-
-sock_tcp_t* newSockTcp(int proto)
-{
- sock_tcp_t *new;
-
- new = malloc( sizeof(sock_tcp_t) );
- memset(new, 0, sizeof(sock_tcp_t));
- new->proto = proto;
-
- return new;
-}
-
-void destroySockTcp(sock_tcp_t *p)
-{
- assert( p != NULL );
- free(p);
-}
-
-sock_tcp_t* bindTcpSocket(char *address, int port, int proto)
-{
- sock_tcp_t *new;
- unsigned long param_setsock = 1;
- int len;
- int ret;
-
- assert( port > 0 && port < 65535 );
-
- new = newSockTcp(proto);
- ret = -1; // no Warnnings
-
- assert( new != NULL );
-
- if( new->proto == PROTO_TCPv4 )
- {
- new->sock = socket(AF_INET, SOCK_STREAM, 0);
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_TCPv6 )
- {
- new->sock = socket(AF_INET6, SOCK_STREAM, 0);
- }
-#endif
-
- if( new->sock < 0 )
- {
- fprintf(stderr, _("Unable to create socket when connecting!\n"));
- destroySockTcp(new);
- return NULL;
- }
-
- setsockopt(new->sock, SOL_SOCKET, SO_REUSEADDR, (char *) &param_setsock, sizeof (param_setsock));
-
- if( new->proto == PROTO_TCPv4 )
- {
- new->sockAddr.sin_family = AF_INET;
- inet_pton(AF_INET, address, &(new->sockAddr.sin_addr));
- new->sockAddr.sin_port = htons(port);
-
- len = sizeof(new->sockAddr);
- ret = bind(new->sock, (struct sockaddr *)&new->sockAddr, len);
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_TCPv6 )
- {
- new->sockAddr6.sin6_family = AF_INET6;
- //new->sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
- new->sockAddr6.sin6_port = htons(port);
-
- len = sizeof(new->sockAddr6);
- ret = bind(new->sock, (struct sockaddr *)&new->sockAddr6, len);
- }
-#endif
-
- if( ret < 0 )
- {
- fprintf(stderr, _("Unable to bint to port: %d\n"), port);
- destroySockTcp(new);
- return NULL;
- }
-
- listen(new->sock, 5);
-
- return new;
-}
-
-sock_tcp_t* getTcpNewClient(sock_tcp_t *p)
-{
- sock_tcp_t *new;
- int client_len;
-
- assert( p != NULL );
- assert( p->sock >= 0 );
-
- new = newSockTcp(p->proto);
-
- if( new->proto == PROTO_TCPv4 )
- {
- client_len = sizeof(new->sockAddr);
-
- new->sock = accept(p->sock, (struct sockaddr *)&new->sockAddr,
- (socklen_t *)&client_len);
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_TCPv6 )
- {
- client_len = sizeof(new->sockAddr6);
-
- new->sock = accept(p->sock, (struct sockaddr *)&new->sockAddr6,
- (socklen_t *)&client_len);
- }
-#endif
-
- if( new->sock < 0 )
- {
- //printf("XXX\n");
- destroySockTcp(new);
- return NULL;
- }
-
- return new;
-}
-
-void getSockTcpIp(sock_tcp_t *p, char *str_ip, int len)
-{
- assert( p != NULL );
- assert( str_ip != NULL );
-
-
- if( p->proto == PROTO_TCPv4 )
- {
- inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len);
- strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
- //printf("strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));\n");
- }
-
-#ifdef SUPPORT_IPv6
- if( p->proto == PROTO_TCPv6 )
- {
- inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len);
- }
-#endif
-}
-
-int getSockTcpPort(sock_tcp_t *p)
-{
- assert( p != NULL );
-
- if( p->proto == PROTO_TCPv4 )
- {
- return htons(p->sockAddr.sin_port);
- }
-
-#ifdef SUPPORT_IPv6
- if( p->proto == PROTO_TCPv6 )
- {
- return htons(p->sockAddr6.sin6_port);
- }
-#endif
-
- assert( ! _("Bad IP proto!") );
-
- return -1;
-}
-
-sock_tcp_t* connectTcpSocket(char *ip, int port, int proto)
-{
- sock_tcp_t *new;
- int len;
- int ret;
-
- assert( ip != NULL );
- assert( port > 0 && port < 65535 );
-
- new = newSockTcp(proto);
- ret = -1; // no Warnnings
-
- if( new->proto == PROTO_TCPv4 )
- {
- new->sock = socket(AF_INET, SOCK_STREAM, 0);
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_TCPv6 )
- {
- new->sock = socket(AF_INET6, SOCK_STREAM, 0);
- }
-#endif
-
- if( new->sock < 0 )
- {
- fprintf(stderr, _("Unable to create TCP socket!\n"));
- destroySockTcp(new);
- return NULL;
- }
-
- if( new->proto == PROTO_TCPv4 )
- {
- new->sockAddr.sin_family = AF_INET;
- new->sockAddr.sin_addr.s_addr = inet_addr(ip);
- new->sockAddr.sin_port = htons(port);
-
- len = sizeof(new->sockAddr);
- ret = connect(new->sock, (struct sockaddr *)&new->sockAddr, len);
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_TCPv6 )
- {
- new->sockAddr6.sin6_family = AF_INET6;
- inet_pton(AF_INET6, ip, &(new->sockAddr6.sin6_addr));
- new->sockAddr6.sin6_port = htons(port);
-
- len = sizeof(new->sockAddr6);
- ret = connect(new->sock, (struct sockaddr *)&new->sockAddr6, len);
- }
-#endif
-
- if( ret < 0 )
- {
- fprintf(stderr, "Unable to connect on: \"%s\" port: \"%d\"\n", ip, port);
- destroySockTcp(new);
- return NULL;
- }
-
-
- return new;
-}
-
-int disableNagle(sock_tcp_t *p)
-{
- int flag = 1;
-
- int result;
-
- result = setsockopt(p->sock, /* socket affected */
- IPPROTO_TCP, /* set option at TCP level */
- TCP_NODELAY, /* name of option */
- (char *) &flag, /* the cast is historical cruft */
- sizeof(int)); /* length of option value */
-
-#if 0
- if( result < 0 )
- {
- printf("disabled nagle error\n");
- }
- else
- {
- printf("disabled nagle OK\n");
- }
-#endif
-
- return result;
-}
-
-int setTcpSockNonBlock(sock_tcp_t *p)
-{
- /* Set to nonblocking socket mode */
-#ifndef __WIN32__
- int oldFlag;
-
- oldFlag = fcntl (p->sock, F_GETFL, 0);
-
- if( fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1 )
- {
- //printf("error setTcpSockNonBlock\n");
- return -1;
- }
-
- //printf("setTcpSockNonBlock OK\n");
-#else
- unsigned long arg = 1;
- // Operation is FIONBIO. Parameter is pointer on non-zero number.
- if( ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR )
- {
- WSACleanup();
- return -1;
- }
-#endif
- return 0;
-}
-
-int readTcpSocket(sock_tcp_t *p, void *address, int len)
-{
- assert( p != NULL );
- assert( address != NULL );
-
- return read(p->sock, address, len);
-}
-
-int writeTcpSocket(sock_tcp_t *p, void *address, int len)
-{
- assert( p != NULL );
- assert( address != NULL );
-
- return write(p->sock, address, len);
-}
-
-void closeTcpSocket(sock_tcp_t *p)
-{
- assert( p != NULL );
-
- close(p->sock);
- destroySockTcp(p);
-}
-
-#if 0
-#define MAX_CLIENTS 32
-#define BUF_SIZE 512
-
-int main(int argc, char **argv)
-{
- sock_tcp_t *sock;
- sock_tcp_t *client[MAX_CLIENTS];
- int count_client;
- int changed, i;
- struct pollfd *tmp;
-
- sock = bindTcpSocket( "127.0.0.1", 2200, PROTO_TCPv4 );
- disableNagle(sock);
- count_client = 0;
-
- for(;;)
- {
- tmp = malloc( (count_client+1) * sizeof(struct pollfd) );
-
- tmp[0].fd = sock->sock;
- tmp[0].events = POLLIN;
- tmp[0].revents = 0;
-
- for( i = 0 ; i < count_client ; i++ )
- {
- tmp[1+i].fd = client[i]->sock;
- tmp[1+i].events = POLLIN;
- tmp[1+i].revents = 0;
- }
-
- changed = poll(tmp, count_client+1, 1000);
-
- if( tmp[0].revents & POLLIN != 0 )
- {
- client[count_client] = getTcpNewClient(sock);
- disableNagle(client[count_client]);
- count_client++;
- }
-
- for( i = 0 ; i < count_client ; i++ )
- {
- if( tmp[1+i].revents & POLLIN != 0 )
- {
- char buf[BUF_SIZE];
- int ret;
-
- ret = read(client[i]->sock, buf, BUF_SIZE);
-
- if( ret <= 0 )
- {
- closeTcpSocket(client[i]);
-
- memmove(client + i,
- client + (i+1),
- ((count_client-1) - i) * sizeof(sock_tcp_t *) );
-
- count_client--;
- continue;
- }
-
- write(client[i]->sock, buf, ret);
- }
- }
-
- free(tmp);
- }
-}
-#endif
-
diff --git a/src/net/udp.c b/src/net/udp.c
index 6852834..f654a75 100644
--- a/src/net/udp.c
+++ b/src/net/udp.c
@@ -26,389 +26,390 @@
#include "udp.h"
-sock_udp_t *
-newSockUdp(int proto)
+sock_udp_t* newSockUdp(int proto)
{
- sock_udp_t *new;
+ sock_udp_t *new;
- new = malloc(sizeof(sock_udp_t));
- memset(new, 0, sizeof(sock_udp_t));
- new->proto = proto;
+ new = malloc( sizeof(sock_udp_t) );
+ memset(new, 0, sizeof(sock_udp_t));
+ new->proto = proto;
- return new;
+ return new;
}
-void
-destroySockUdp(sock_udp_t * p)
+void destroySockUdp(sock_udp_t *p)
{
- assert(p != NULL);
- free(p);
+ assert( p != NULL );
+ free(p);
}
-sock_udp_t *
-bindUdpSocket(char *address, int port, int proto)
+sock_udp_t* bindUdpSocket(char *address, int port, int proto)
{
- sock_udp_t *new;
- int res = -1; // no warninng
+ sock_udp_t *new;
+ int res = -1; // no warninng
- assert(port > 0 && port < 65535);
+ assert( port > 0 && port < 65535 );
- new = newSockUdp(proto);
+ new = newSockUdp(proto);
- assert(new != NULL);
+ assert( new != NULL );
- if (new->proto == PROTO_UDPv4) {
- new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- }
+ if( new->proto == PROTO_UDPv4 )
+ {
+ new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_UDPv6) {
- new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
- }
+ if( new->proto == PROTO_UDPv6 )
+ {
+ new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
+ }
#endif
- if (new->sock < 0) {
- fprintf(stderr, _("Unable to create socket when binding!\n"));
- destroySockUdp(new);
+ if( new->sock < 0 )
+ {
+ fprintf(stderr, _("Unable to create socket when binding!\n"));
+ destroySockUdp(new);
#ifdef __WIN32__
- WSACleanup();
+ WSACleanup();
#endif
- return NULL;
- }
+ return NULL;
+ }
- memset(&(new->sockAddr), 0, sizeof(new->sockAddr));
+ memset(&(new->sockAddr), 0, sizeof(new->sockAddr));
- if (new->proto == PROTO_UDPv4) {
- new->sockAddr.sin_family = AF_INET;
- new->sockAddr.sin_port = htons(port);
- new->sockAddr.sin_addr.s_addr = inet_addr(address);
+ if( new->proto == PROTO_UDPv4 )
+ {
+ new->sockAddr.sin_family = AF_INET;
+ new->sockAddr.sin_port = htons(port);
+ new->sockAddr.sin_addr.s_addr = inet_addr(address);
- res =
- bind(new->sock, (struct sockaddr *) &(new->sockAddr),
- sizeof(new->sockAddr));
- }
+ res = bind(new->sock, (struct sockaddr *)&(new->sockAddr), sizeof(new->sockAddr));
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_UDPv6) {
- new->sockAddr6.sin6_family = AF_INET6;
- new->sockAddr6.sin6_port = htons(port);
- inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
-
- res =
- bind(new->sock, (struct sockaddr *) &(new->sockAddr6),
- sizeof(new->sockAddr6));
- }
+ if( new->proto == PROTO_UDPv6 )
+ {
+ new->sockAddr6.sin6_family = AF_INET6;
+ new->sockAddr6.sin6_port = htons(port);
+ inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
+
+ res = bind(new->sock, (struct sockaddr *)&(new->sockAddr6), sizeof(new->sockAddr6));
+ }
#endif
- if (res < 0) {
- fprintf(stderr, _("Unable to set socket %s %d!\n"), address, port);
- destroySockUdp(new);
+ if( res < 0 )
+ {
+ fprintf(stderr, _("Unable to set socket %s %d!\n"), address, port);
+ destroySockUdp(new);
#ifdef __WIN32__
- WSACleanup();
+ WSACleanup();
#endif
- return NULL;
- }
+ return NULL;
+ }
- return new;
+ return new;
}
-sock_udp_t *
-connectUdpSocket(char *address, int port, int proto)
+sock_udp_t* connectUdpSocket(char *address, int port, int proto)
{
- sock_udp_t *new;
+ sock_udp_t *new;
- assert(address != NULL);
- assert(port > 0 && port < 65536);
+ assert( address != NULL );
+ assert( port > 0 && port < 65536 );
- new = newSockUdp(proto);
+ new = newSockUdp(proto);
- assert(new != NULL);
+ assert( new != NULL );
- if (new->proto == PROTO_UDPv4) {
- new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- }
+ if( new->proto == PROTO_UDPv4 )
+ {
+ new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_UDPv6) {
- new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
- }
+ if( new->proto == PROTO_UDPv6 )
+ {
+ new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
+ }
#endif
- if (new->sock < 0) {
- fprintf(stderr, _("Unable to create socket when connecting!\n"));
- destroySockUdp(new);
+ if( new->sock < 0 )
+ {
+ fprintf(stderr, _("Unable to create socket when connecting!\n"));
+ destroySockUdp(new);
#ifdef __WIN32__
- WSACleanup();
+ WSACleanup();
#endif
- return NULL;
- }
+ return NULL;
+ }
- memset(&(new->sockAddr), 0, sizeof(new->sockAddr));
+ memset(&(new->sockAddr), 0, sizeof(new->sockAddr));
- if (new->proto == PROTO_UDPv4) {
- new->sockAddr.sin_family = AF_INET;
- new->sockAddr.sin_port = htons(port);
- new->sockAddr.sin_addr.s_addr = inet_addr(address);
- }
+ if( new->proto == PROTO_UDPv4 )
+ {
+ new->sockAddr.sin_family = AF_INET;
+ new->sockAddr.sin_port = htons(port);
+ new->sockAddr.sin_addr.s_addr = inet_addr(address);
+ }
#ifdef SUPPORT_IPv6
- if (new->proto == PROTO_UDPv6) {
- new->sockAddr6.sin6_family = AF_INET6;
- new->sockAddr6.sin6_port = htons(port);
- inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
- }
+ if( new->proto == PROTO_UDPv6 )
+ {
+ new->sockAddr6.sin6_family = AF_INET6;
+ new->sockAddr6.sin6_port = htons(port);
+ inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
+ }
#endif
- return new;
+ return new;
}
-int
-setUdpSockNonBlock(sock_udp_t * p)
+int setUdpSockNonBlock(sock_udp_t *p)
{
- /* Set to nonblocking socket mode */
+ /* Set to nonblocking socket mode */
#ifndef __WIN32__
- int oldFlag;
+ int oldFlag;
- oldFlag = fcntl(p->sock, F_GETFL, 0);
+ oldFlag = fcntl (p->sock, F_GETFL, 0);
- if (fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
- return -1;
- }
+ if( fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1 )
+ {
+ return -1;
+ }
#else
- unsigned long arg = 1;
- // Operation is FIONBIO. Parameter is pointer on non-zero number.
- if (ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR) {
- WSACleanup();
- return -1;
- }
+ unsigned long arg = 1;
+ // Operation is FIONBIO. Parameter is pointer on non-zero number.
+ if( ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR )
+ {
+ WSACleanup();
+ return -1;
+ }
#endif
- return 0;
+ return 0;
}
-int
-readUdpSocket(sock_udp_t * src, sock_udp_t * dst, void *address, int len)
+int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len)
{
- int addrlen;
- int size = -1; // no warninng
+ int addrlen;
+ int size = -1; // no warninng
- assert(src != NULL);
- assert(dst != NULL);
- assert(address != NULL);
+ assert( src != NULL );
+ assert( dst != NULL );
+ assert( address != NULL );
- if (dst->proto == PROTO_UDPv4) {
- addrlen = sizeof(src->sockAddr);
+ if( dst->proto == PROTO_UDPv4 )
+ {
+ addrlen = sizeof(src->sockAddr);
#ifndef __WIN32
- size =
- recvfrom(src->sock, address, len, 0,
- (struct sockaddr *) &dst->sockAddr,
- (socklen_t *) & addrlen);
+ size = recvfrom(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, (socklen_t *)&addrlen);
#else
- size =
- recvfrom(src->sock, address, len, 0,
- (struct sockaddr *) &dst->sockAddr, (int *) &addrlen);
+ size = recvfrom(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, (int *)&addrlen);
#endif
- }
+ }
#ifdef SUPPORT_IPv6
- if (dst->proto == PROTO_UDPv6) {
- addrlen = sizeof(src->sockAddr6);
+ if( dst->proto == PROTO_UDPv6 )
+ {
+ addrlen = sizeof(src->sockAddr6);
- size = recvfrom(src->sock, address, len, 0,
- (struct sockaddr *) &dst->sockAddr6,
- (socklen_t *) & addrlen);
- }
+ size = recvfrom(src->sock, address, len, 0,
+ (struct sockaddr *)&dst->sockAddr6, (socklen_t *)&addrlen);
+ }
#endif
- dst->sock = src->sock;
+ dst->sock = src->sock;
- if (size < 0) {
- char str_ip[STR_IP_SIZE];
+ if( size < 0 )
+ {
+ char str_ip[STR_IP_SIZE];
- getSockUdpIp(dst, str_ip, STR_IP_SIZE);
+ getSockUdpIp(dst, str_ip, STR_IP_SIZE);
- fprintf(stderr, _("Unable to read form socket %d %s %d!\n"), size,
- str_ip, getSockUdpPort(dst));
+ fprintf(stderr, _("Unable to read form socket %d %s %d!\n"), size, str_ip, getSockUdpPort(dst));
#ifdef __WIN32__
- WSACleanup();
+ WSACleanup();
#endif
- return -1;
- }
+ return -1;
+ }
- return size;
+ return size;
}
-int
-writeUdpSocket(sock_udp_t * src, sock_udp_t * dst, void *address, int len)
+int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len)
{
- int addrlen;
- int size = -1; // no warninng
+ int addrlen;
+ int size = -1; // no warninng
- assert(src != NULL);
- assert(dst != NULL);
- assert(address != NULL);
+ assert( src != NULL );
+ assert( dst != NULL );
+ assert( address != NULL );
- if (dst->proto == PROTO_UDPv4) {
- addrlen = sizeof(src->sockAddr);
+ if( dst->proto == PROTO_UDPv4 )
+ {
+ addrlen = sizeof(src->sockAddr);
- size =
- sendto(src->sock, address, len, 0,
- (struct sockaddr *) &dst->sockAddr, addrlen);
- }
+ size = sendto(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, addrlen);
+ }
#ifdef SUPPORT_IPv6
- if (dst->proto == PROTO_UDPv6) {
- addrlen = sizeof(src->sockAddr6);
+ if( dst->proto == PROTO_UDPv6 )
+ {
+ addrlen = sizeof(src->sockAddr6);
- size =
- sendto(src->sock, address, len, 0,
- (struct sockaddr *) &dst->sockAddr6, addrlen);
- }
+ size = sendto(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr6, addrlen);
+ }
#endif
- if (size < 0) {
- char str_ip[STR_IP_SIZE];
+ if( size < 0 )
+ {
+ char str_ip[STR_IP_SIZE];
- getSockUdpIp(dst, str_ip, STR_IP_SIZE);
+ getSockUdpIp(dst, str_ip, STR_IP_SIZE);
- fprintf(stderr, _("Unable to write on socket %d %s %d!\n"), size,
- str_ip, getSockUdpPort(dst));
+ fprintf(stderr, _("Unable to write on socket %d %s %d!\n"), size, str_ip, getSockUdpPort(dst));
#ifdef __WIN32__
- WSACleanup();
+ WSACleanup();
#endif
- return -1;
- }
+ return -1;
+ }
- return size;
+ return size;
}
-void
-getSockUdpIp(sock_udp_t * p, char *str_ip, int len)
+void getSockUdpIp(sock_udp_t *p, char *str_ip, int len)
{
- assert(p != NULL);
- assert(str_ip != NULL);
+ assert( p != NULL );
+ assert( str_ip != NULL );
-
- if (p->proto == PROTO_UDPv4) {
+
+ if( p->proto == PROTO_UDPv4 )
+ {
#ifndef __WIN32__
- inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len);
+ inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len);
#else
- strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
- //printf("strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));\n");
+ strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
+ //printf("strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));\n");
#endif
- }
+ }
#ifdef SUPPORT_IPv6
- if (p->proto == PROTO_UDPv6) {
- inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len);
- }
+ if( p->proto == PROTO_UDPv6 )
+ {
+ inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len);
+ }
#endif
}
-int
-getSockUdpPort(sock_udp_t * p)
+int getSockUdpPort(sock_udp_t *p)
{
- assert(p != NULL);
+ assert( p != NULL );
- if (p->proto == PROTO_UDPv4) {
- return htons(p->sockAddr.sin_port);
- }
+ if( p->proto == PROTO_UDPv4 )
+ {
+ return htons(p->sockAddr.sin_port);
+ }
#ifdef SUPPORT_IPv6
- if (p->proto == PROTO_UDPv6) {
- return htons(p->sockAddr6.sin6_port);
- }
+ if( p->proto == PROTO_UDPv6 )
+ {
+ return htons(p->sockAddr6.sin6_port);
+ }
#endif
- assert(!_("Bad IP proto!"));
+ assert( ! _("Bad IP proto!") );
- return -1;
+ return -1;
}
-void
-closeUdpSocket(sock_udp_t * p)
+void closeUdpSocket(sock_udp_t *p)
{
- assert(p != NULL);
+ assert( p != NULL );
#ifndef __WIN32__
- close(p->sock);
+ close(p->sock);
#else
- closesocket(p->sock);
- //WSACleanup(); //kdyz ukoncime socket dobre neni treba cleanup
+ closesocket(p->sock);
+ //WSACleanup(); //kdyz ukoncime socket dobre neni treba cleanup
#endif
- destroySockUdp(p);
+ destroySockUdp(p);
}
#if 0
-static int
-myWait(sock_udp_t * p)
+static int myWait(sock_udp_t *p)
{
- fd_set readfds;
- fd_set errorfds;
- struct timeval tv;
- int max_fd;
-
- tv.tv_sec = 1;
- tv.tv_usec = 0;
-
- FD_ZERO(&readfds);
- FD_ZERO(&errorfds);
- max_fd = p->sock;
-
- FD_SET(p->sock, &readfds);
- FD_SET(p->sock, &errorfds);
-
- select(max_fd + 1, &readfds, (fd_set *) 0, &errorfds, &tv);
-
- if (FD_ISSET(p->sock, &readfds)) {
- FD_CLR(p->sock, &readfds);
- return 1;
- }
-
- return 0;
+ fd_set readfds;
+ fd_set errorfds;
+ struct timeval tv;
+ int max_fd;
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+
+ FD_ZERO(&readfds);
+ FD_ZERO(&errorfds);
+ max_fd = p->sock;
+
+ FD_SET(p->sock, &readfds);
+ FD_SET(p->sock, &errorfds);
+
+ select(max_fd+1, &readfds, (fd_set *)0, &errorfds, &tv);
+
+ if( FD_ISSET(p->sock, &readfds) )
+ {
+ FD_CLR(p->sock, &readfds);
+ return 1;
+ }
+
+ return 0;
}
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
- sock_udp_t *sock;
- char buf[BUFSIZE];
- int ret;
-
- if (strcmp(argv[1], "s") == 0) {
- sock_udp_t *cli;
-
- //sock = bindUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6 );
- sock = bindUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4);
-
- //cli = newSockUdp(PROTO_UDPv6);
- cli = newSockUdp(PROTO_UDPv4);
-
- while (1) {
- while (myWait(sock) == 0);
-
- memset(buf, 0, BUFSIZE);
- ret = readUdpSocket(sock, cli, buf, BUFSIZE);
- writeUdpSocket(sock, cli, buf, ret);
- }
- }
-
- if (strcmp(argv[1], "c") == 0) {
- //sock = connectUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6);
- sock = connectUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4);
-
- strcpy(buf, "Hello world !\n");
- writeUdpSocket(sock, sock, buf, strlen(buf));
-
- memset(buf, 0, BUFSIZE);
- readUdpSocket(sock, sock, buf, BUFSIZE);
-
- printf("buf = %s", buf);
- //while(1)sleep(1);
- closeUdpSocket(sock);
- }
-
- return 0;
+ sock_udp_t *sock;
+ char buf[BUFSIZE];
+ int ret;
+
+ if( strcmp(argv[1], "s") == 0 )
+ {
+ sock_udp_t *cli;
+
+ //sock = bindUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6 );
+ sock = bindUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4 );
+
+ //cli = newSockUdp(PROTO_UDPv6);
+ cli = newSockUdp(PROTO_UDPv4);
+
+ while(1)
+ {
+ while( myWait(sock) == 0 )
+ ;
+
+ memset(buf, 0, BUFSIZE);
+ ret = readUdpSocket(sock, cli, buf, BUFSIZE);
+ writeUdpSocket(sock, cli, buf, ret);
+ }
+ }
+
+ if( strcmp(argv[1], "c") == 0 )
+ {
+ //sock = connectUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6);
+ sock = connectUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4);
+
+ strcpy(buf, "Hello world !\n");
+ writeUdpSocket(sock, sock, buf, strlen(buf));
+
+ memset(buf, 0, BUFSIZE);
+ readUdpSocket(sock, sock, buf, BUFSIZE);
+
+ printf("buf = %s", buf);
+ //while(1)sleep(1);
+ closeUdpSocket(sock);
+ }
+
+ return 0;
}
#endif
+
diff --git a/src/net/udp.c~ b/src/net/udp.c~
deleted file mode 100644
index f654a75..0000000
--- a/src/net/udp.c~
+++ /dev/null
@@ -1,415 +0,0 @@
-
-
-#ifndef __WIN32__
-# include <sys/socket.h>
-# include <sys/types.h>
-# include <netinet/in.h>
-# include <arpa/inet.h>
-# include <netdb.h>
-#else
-# include <windows.h>
-# include <wininet.h>
-#endif
-
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <assert.h>
-#include <fcntl.h>
-
-
-#define BUFSIZE 1000
-
-#include "main.h"
-#include "net_multiplayer.h"
-
-#include "udp.h"
-
-sock_udp_t* newSockUdp(int proto)
-{
- sock_udp_t *new;
-
- new = malloc( sizeof(sock_udp_t) );
- memset(new, 0, sizeof(sock_udp_t));
- new->proto = proto;
-
- return new;
-}
-
-void destroySockUdp(sock_udp_t *p)
-{
- assert( p != NULL );
- free(p);
-}
-
-sock_udp_t* bindUdpSocket(char *address, int port, int proto)
-{
- sock_udp_t *new;
- int res = -1; // no warninng
-
- assert( port > 0 && port < 65535 );
-
- new = newSockUdp(proto);
-
- assert( new != NULL );
-
- if( new->proto == PROTO_UDPv4 )
- {
- new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_UDPv6 )
- {
- new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
- }
-#endif
-
- if( new->sock < 0 )
- {
- fprintf(stderr, _("Unable to create socket when binding!\n"));
- destroySockUdp(new);
-#ifdef __WIN32__
- WSACleanup();
-#endif
- return NULL;
- }
-
- memset(&(new->sockAddr), 0, sizeof(new->sockAddr));
-
- if( new->proto == PROTO_UDPv4 )
- {
- new->sockAddr.sin_family = AF_INET;
- new->sockAddr.sin_port = htons(port);
- new->sockAddr.sin_addr.s_addr = inet_addr(address);
-
- res = bind(new->sock, (struct sockaddr *)&(new->sockAddr), sizeof(new->sockAddr));
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_UDPv6 )
- {
- new->sockAddr6.sin6_family = AF_INET6;
- new->sockAddr6.sin6_port = htons(port);
- inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
-
- res = bind(new->sock, (struct sockaddr *)&(new->sockAddr6), sizeof(new->sockAddr6));
- }
-#endif
-
- if( res < 0 )
- {
- fprintf(stderr, _("Unable to set socket %s %d!\n"), address, port);
- destroySockUdp(new);
-#ifdef __WIN32__
- WSACleanup();
-#endif
- return NULL;
- }
-
- return new;
-}
-
-sock_udp_t* connectUdpSocket(char *address, int port, int proto)
-{
- sock_udp_t *new;
-
- assert( address != NULL );
- assert( port > 0 && port < 65536 );
-
- new = newSockUdp(proto);
-
- assert( new != NULL );
-
- if( new->proto == PROTO_UDPv4 )
- {
- new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_UDPv6 )
- {
- new->sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
- }
-#endif
-
- if( new->sock < 0 )
- {
- fprintf(stderr, _("Unable to create socket when connecting!\n"));
- destroySockUdp(new);
-#ifdef __WIN32__
- WSACleanup();
-#endif
- return NULL;
- }
-
- memset(&(new->sockAddr), 0, sizeof(new->sockAddr));
-
- if( new->proto == PROTO_UDPv4 )
- {
- new->sockAddr.sin_family = AF_INET;
- new->sockAddr.sin_port = htons(port);
- new->sockAddr.sin_addr.s_addr = inet_addr(address);
- }
-
-#ifdef SUPPORT_IPv6
- if( new->proto == PROTO_UDPv6 )
- {
- new->sockAddr6.sin6_family = AF_INET6;
- new->sockAddr6.sin6_port = htons(port);
- inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
- }
-#endif
- return new;
-}
-
-int setUdpSockNonBlock(sock_udp_t *p)
-{
- /* Set to nonblocking socket mode */
-#ifndef __WIN32__
- int oldFlag;
-
- oldFlag = fcntl (p->sock, F_GETFL, 0);
-
- if( fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1 )
- {
- return -1;
- }
-#else
- unsigned long arg = 1;
- // Operation is FIONBIO. Parameter is pointer on non-zero number.
- if( ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR )
- {
- WSACleanup();
- return -1;
- }
-#endif
- return 0;
-}
-
-int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len)
-{
- int addrlen;
- int size = -1; // no warninng
-
- assert( src != NULL );
- assert( dst != NULL );
- assert( address != NULL );
-
- if( dst->proto == PROTO_UDPv4 )
- {
- addrlen = sizeof(src->sockAddr);
-
-#ifndef __WIN32
- size = recvfrom(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, (socklen_t *)&addrlen);
-#else
- size = recvfrom(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, (int *)&addrlen);
-#endif
- }
-
-#ifdef SUPPORT_IPv6
- if( dst->proto == PROTO_UDPv6 )
- {
- addrlen = sizeof(src->sockAddr6);
-
- size = recvfrom(src->sock, address, len, 0,
- (struct sockaddr *)&dst->sockAddr6, (socklen_t *)&addrlen);
- }
-#endif
-
- dst->sock = src->sock;
-
- if( size < 0 )
- {
- char str_ip[STR_IP_SIZE];
-
- getSockUdpIp(dst, str_ip, STR_IP_SIZE);
-
- fprintf(stderr, _("Unable to read form socket %d %s %d!\n"), size, str_ip, getSockUdpPort(dst));
-#ifdef __WIN32__
- WSACleanup();
-#endif
- return -1;
- }
-
- return size;
-}
-
-int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len)
-{
- int addrlen;
- int size = -1; // no warninng
-
- assert( src != NULL );
- assert( dst != NULL );
- assert( address != NULL );
-
- if( dst->proto == PROTO_UDPv4 )
- {
- addrlen = sizeof(src->sockAddr);
-
- size = sendto(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr, addrlen);
- }
-
-#ifdef SUPPORT_IPv6
- if( dst->proto == PROTO_UDPv6 )
- {
- addrlen = sizeof(src->sockAddr6);
-
- size = sendto(src->sock, address, len, 0, (struct sockaddr *)&dst->sockAddr6, addrlen);
- }
-#endif
-
- if( size < 0 )
- {
- char str_ip[STR_IP_SIZE];
-
- getSockUdpIp(dst, str_ip, STR_IP_SIZE);
-
- fprintf(stderr, _("Unable to write on socket %d %s %d!\n"), size, str_ip, getSockUdpPort(dst));
-#ifdef __WIN32__
- WSACleanup();
-#endif
- return -1;
- }
-
- return size;
-}
-
-void getSockUdpIp(sock_udp_t *p, char *str_ip, int len)
-{
- assert( p != NULL );
- assert( str_ip != NULL );
-
-
- if( p->proto == PROTO_UDPv4 )
- {
-#ifndef __WIN32__
- inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len);
-#else
- strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
- //printf("strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));\n");
-#endif
- }
-
-#ifdef SUPPORT_IPv6
- if( p->proto == PROTO_UDPv6 )
- {
- inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len);
- }
-#endif
-}
-
-int getSockUdpPort(sock_udp_t *p)
-{
- assert( p != NULL );
-
- if( p->proto == PROTO_UDPv4 )
- {
- return htons(p->sockAddr.sin_port);
- }
-
-#ifdef SUPPORT_IPv6
- if( p->proto == PROTO_UDPv6 )
- {
- return htons(p->sockAddr6.sin6_port);
- }
-#endif
-
- assert( ! _("Bad IP proto!") );
-
- return -1;
-}
-
-
-void closeUdpSocket(sock_udp_t *p)
-{
- assert( p != NULL );
-
-#ifndef __WIN32__
- close(p->sock);
-#else
- closesocket(p->sock);
- //WSACleanup(); //kdyz ukoncime socket dobre neni treba cleanup
-#endif
- destroySockUdp(p);
-}
-
-#if 0
-
-static int myWait(sock_udp_t *p)
-{
- fd_set readfds;
- fd_set errorfds;
- struct timeval tv;
- int max_fd;
-
- tv.tv_sec = 1;
- tv.tv_usec = 0;
-
- FD_ZERO(&readfds);
- FD_ZERO(&errorfds);
- max_fd = p->sock;
-
- FD_SET(p->sock, &readfds);
- FD_SET(p->sock, &errorfds);
-
- select(max_fd+1, &readfds, (fd_set *)0, &errorfds, &tv);
-
- if( FD_ISSET(p->sock, &readfds) )
- {
- FD_CLR(p->sock, &readfds);
- return 1;
- }
-
- return 0;
-}
-
-int main(int argc, char *argv[])
-{
- sock_udp_t *sock;
- char buf[BUFSIZE];
- int ret;
-
- if( strcmp(argv[1], "s") == 0 )
- {
- sock_udp_t *cli;
-
- //sock = bindUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6 );
- sock = bindUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4 );
-
- //cli = newSockUdp(PROTO_UDPv6);
- cli = newSockUdp(PROTO_UDPv4);
-
- while(1)
- {
- while( myWait(sock) == 0 )
- ;
-
- memset(buf, 0, BUFSIZE);
- ret = readUdpSocket(sock, cli, buf, BUFSIZE);
- writeUdpSocket(sock, cli, buf, ret);
- }
- }
-
- if( strcmp(argv[1], "c") == 0 )
- {
- //sock = connectUdpSocket("::1", atoi(argv[2]), PROTO_UDPv6);
- sock = connectUdpSocket("127.0.0.1", atoi(argv[2]), PROTO_UDPv4);
-
- strcpy(buf, "Hello world !\n");
- writeUdpSocket(sock, sock, buf, strlen(buf));
-
- memset(buf, 0, BUFSIZE);
- readUdpSocket(sock, sock, buf, BUFSIZE);
-
- printf("buf = %s", buf);
- //while(1)sleep(1);
- closeUdpSocket(sock);
- }
-
- return 0;
-}
-#endif
-