From 6d4e1eae16b84918009625a866104ec26a234277 Mon Sep 17 00:00:00 2001 From: oroborus Date: Fri, 1 Feb 2008 22:59:06 +0000 Subject: nahranie tarballu tuxanci-ng na SVN server Dusan Durech ( Oroborus ) git-svn-id: http://opensvn.csie.org/tuxanci_ng@1 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- src/network.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100755 src/network.c (limited to 'src/network.c') diff --git a/src/network.c b/src/network.c new file mode 100755 index 0000000..ebd209a --- /dev/null +++ b/src/network.c @@ -0,0 +1,99 @@ + +#include "network.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +int newSocket(int port,const char *ip) +{ + struct sockaddr_in address; + int sockfd; + int len; + + assert( port > 0 && port < 65535 ); + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + + if( sockfd < 0 ) + { + fprintf(stderr, "Nemozem vytvorit sietovy socket\n"); + return -1; + } + + address.sin_family = AF_INET; + + if( ip != NULL ) + address.sin_addr.s_addr = inet_addr(ip); + else + address.sin_addr.s_addr = htonl(INADDR_ANY); + + address.sin_port = htons(port); + + len = sizeof(address); + + if( ip == NULL ) + { + if ( bind(sockfd, (struct sockaddr *)&address, len) < 0 ) + { + fprintf(stderr, "Nemozem obsadit sietovy port %d\n", port); + return -1; + } + + listen(sockfd, 5); + } + else + { + if ( connect(sockfd, (struct sockaddr *)&address, len) < 0 ) + { + fprintf(stderr, "Nemozem sa pripojit na %s %d\n", ip, port); + return -1; + } + } + + return sockfd; +} + +int getNewClient(int sockfd) +{ + struct sockaddr_in client_address; + int client_sockfd; + int client_len; + + assert( sockfd >= 0 ); + + client_len = sizeof(client_address); + + client_sockfd = accept(sockfd, (struct sockaddr *)&client_address, + (socklen_t *)&client_len); + + //printf("ip = %s\n",inet_ntoa(client_address.sin_addr)); + + return client_sockfd; +} + +int getNewClientIP(int sockfd,char **str_ip) +{ + struct sockaddr_in client_address; + int client_sockfd; + int client_len; + + assert( sockfd >= 0 ); + + client_len = sizeof(client_address); + + client_sockfd = accept(sockfd, (struct sockaddr *)&client_address, + (socklen_t *)&client_len); + + *str_ip = strdup( inet_ntoa(client_address.sin_addr) ); + + return client_sockfd; +} -- cgit v1.2.3