missing files and previous update were fixed
[ZeXOS.git] / apps / im / src / net.c
blobba44bd1d4ad57aaa9d207c5f92c39655df05dd48
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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 "platform.h"
22 #include "config.h"
23 #include "proto.h"
24 #include "net.h"
25 #include "ui.h"
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/socket.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <sys/types.h>
40 static net_t net;
42 int net_send (char *data, unsigned len)
44 if (!data || !len)
45 return -1;
47 int ret;
49 if ((ret = send (net.sock, data, len, 0)) == -1) {
50 printf ("ERROR -> send (%d) = -1\n", net.sock);
51 return -1;
54 return ret;
57 int net_connect (char *server, int port)
59 struct hostent *host;
61 /* connect to im server */
63 // Check info about remote host
64 if ((host = gethostbyname ((char *) server)) == NULL) {
65 printf ("Wrong address -> %s:%d\n", server, port);
66 return 0;
68 // Create socket
69 if ((net.sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
70 printf ("Cant create socket\n");
71 return 0;
74 // Fill structure sockaddr_in
75 // 1) Protocol family
76 net.conn.sin_family = AF_INET;
77 // 2) Port of remote server
78 net.conn.sin_port = htons (port);
79 // 3) Remote ip address
80 memcpy (&(net.conn.sin_addr), host->h_addr, host->h_length);
82 // P�ipojen� soketu
83 if (connect (net.sock, (struct sockaddr *) &net.conn, sizeof (net.conn)) == -1) {
84 printf ("Connection cant be estabilished -> %s:%d\n", server, port);
85 return 0;
88 net.buffer = (char *) malloc (sizeof (char) * NET_BUFFER_SIZE);
90 if (!net.buffer)
91 return 0;
93 usleep (10);
95 net_switchmode ();
97 return 1;
100 int net_switchmode ()
102 /* set socket "sock" to non-blocking */
103 int oldFlag = fcntl (net.sock, F_GETFL, 0);
104 if (fcntl (net.sock, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
105 printf ("Cant set socket to nonblocking mode\n");
106 return 0;
109 return 1;
112 static int strnchrn (char *str, unsigned len)
114 unsigned l = 0;
116 while (l < len) {
117 if (str[l] == '\n') {
118 str[l] = '\0';
120 return l;
123 l ++;
126 return 0;
129 int net_loop ()
131 int ret = recv (net.sock, net.buffer, NET_BUFFER_SIZE-1, 0);
133 /* incoming data */
134 if (ret > 0) {
135 int len = 0;
136 net.buffer[ret] = '\0';
138 while (len < ret) {
139 int l = strnchrn (net.buffer+len, ret-len);
141 if (!l)
142 break;
144 proto_handler (net.buffer+len, l);
146 len += (l + 1);
149 return 1;
152 #ifndef __zexos__
153 if (!ret) {
154 #else
155 if (ret == -1) {
156 #endif
157 printf ("ERROR -> Disconnected by server\n");
158 return -1;
161 ui_getcommand ();
163 usleep (40);
165 #ifdef __zexos__
166 schedule ();
167 #endif
169 return 0;
172 /** INIT NETWORK function */
173 int init_net ()
175 return net_connect (cfg->server, cfg->port);;