Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / irc / net.c
blobda0e5fa74a341e66180b0c92c303889e69ced7d8
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <netdb.h>
29 #include <fcntl.h>
30 #include <time.h>
31 #include "proto.h"
32 #include "net.h"
34 int fd;
35 static struct sockaddr_in serverSock;
36 static struct sockaddr_in6 serverSock6;
37 static struct hostent *host;
39 char *buf;
41 /** Network Send function */
42 int net_send (char *buf, unsigned len)
44 int ret = send (fd, buf, len, 0);
45 #ifndef LINUX
46 schedule ();
47 #endif
48 return ret;
51 unsigned strlenn (char *str, unsigned len)
53 unsigned i = len;
55 while (str[i] != '\n') {
56 i --;
58 if (!i)
59 return 0;
61 return i+1;
64 return 0;
67 int net_loop ()
69 int ret = recv (fd, buf, BUFSIZE-1, 0);
71 if (ret > 0) {
72 buf[ret] = '\0';
74 //printf ("net_loop () -> recv (): '%s' : %d\n", buf, ret);
76 if (!proto_handler (buf, ret)) {
77 printf ("ERROR -> proto_handler () failed\n");
78 return 0;
80 } else {
81 #ifdef LINUX
82 if (ret == 0) {
83 #else
84 if (ret == -1) {
85 #endif
86 printf ("INFO -> Connection closed by server\n");
87 return 0;
91 usleep (60);
93 return 1;
96 int net_close ()
98 close (fd);
100 printf ("net_close () -> close (%d)\n", fd);
102 return 1;
105 int init_net (char *address, int port, int flags)
107 int proto = AF_INET;
109 if (flags & NET_IPV6)
110 proto = AF_INET6;
112 if ((host = gethostbyname ((char *) address)) == NULL) {
113 printf ("Wrong address -> %s:%d\n", address, port);
114 return 0;
117 if ((fd = socket (proto, SOCK_STREAM, IPPROTO_TCP)) == -1) {
118 printf ("Cant create socket\n");
119 return 0;
122 if (flags & NET_IPV6) {
123 serverSock6.sin6_family = proto;
124 serverSock6.sin6_port = htons (port);
125 memcpy (&(serverSock6.sin6_addr), host->h_addr, host->h_length);
127 if (connect (fd, (struct sockaddr *) &serverSock6, sizeof (serverSock6)) == -1) {
128 printf ("Connection cant be estabilished -> %s:%d\n", address, port);
129 return 0;
131 } else {
132 serverSock.sin_family = proto;
133 serverSock.sin_port = htons (port);
134 memcpy (&(serverSock.sin_addr), host->h_addr, host->h_length);
136 if (connect (fd, (struct sockaddr *) &serverSock, sizeof (serverSock)) == -1) {
137 printf ("Connection cant be estabilished -> %s:%d\n", address, port);
138 return 0;
142 int oldFlag = fcntl (fd, F_GETFL, 0);
143 if (fcntl (fd, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
144 printf ("Cant set socket to nonblocking mode\n");
145 return 0;
148 buf = (char *) malloc (sizeof (char) * BUFSIZE);
150 if (!buf) {
151 printf ("Receive buffer -> out of memory !\n");
152 return 0;
155 return 1;