net: Remove unused variables in net_available()
[platform/core/system/initrd-flash.git] / src / net.c
1 /*
2  * flash-manager - Tizen kernel-level image flashing solution
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <arpa/inet.h>
26 #include <netdb.h>
27 #include <ifaddrs.h>
28
29 #include "interface.h"
30
31 #define DEFAULT_PORT            23456
32
33 static ssize_t net_rx_data(int fd, void *buf, ssize_t len)
34 {
35         return recv(fd, buf, len, MSG_WAITALL);
36 }
37
38 static ssize_t net_tx_data(int fd, void *buf, ssize_t len)
39 {
40         return send(fd, buf, len, 0);
41 }
42
43 static int net_connect(struct tfm_interface *intf)
44 {
45         struct sockaddr_in servaddr;
46         int listener, ret = 0;
47         int port;
48
49         listener = socket(AF_INET, SOCK_STREAM, 0);
50         if (listener < 0) {
51                 fprintf(stderr, "Failed to create socket\n");
52                 return -EINVAL;
53         }
54
55         port = (unsigned long) intf->priv;
56         if (port <= 1024)
57                 port = DEFAULT_PORT;
58
59         fprintf(stdout, "%s using port: %d\n", intf->name, port);
60
61         memset(&servaddr, 0, sizeof(servaddr));
62         servaddr.sin_family = AF_INET;
63         servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
64         servaddr.sin_port = htons(port);
65
66         ret = bind(listener, (struct sockaddr *) &servaddr, sizeof(servaddr));
67         if (ret < 0) {
68                 fprintf(stderr, "Failed to bind socket\n");
69                 goto err;
70         }
71
72         ret = listen(listener, 1024);
73         if (ret < 0) {
74                 fprintf(stderr, "Failed to call listen\n");
75                 goto err;
76         }
77
78         ret = accept(listener, NULL, NULL);
79         if (ret < 0) {
80                 fprintf(stderr, "Failed to accept connection\n");
81                 goto err;
82         }
83
84         intf->rxd = intf->txd = ret;
85
86         close(listener);
87
88         return 0;
89
90 err:
91         close(listener);
92         return ret;
93 }
94
95 static int net_disconnect(struct tfm_interface *intf)
96 {
97         /* since rxd == txd, we close only rxd */
98         return close(intf->rxd);
99 }
100
101 static int net_available(void)
102 {
103         struct ifaddrs *ifs, *cur;
104         int ret;
105
106         ret = getifaddrs(&ifs);
107         if (ret < 0)
108                 return 0;
109
110         /*
111          * TODO:
112          * On RPI-family targets it works well checking network availabilty
113          * via presence of NIC 'eth0' or 'wlan0', but it looks quite hack
114          * anyway. So, it should be changed if there is more generic way.
115          */
116         for (cur = ifs; cur != NULL; cur = cur->ifa_next) {
117                 if (!strcmp(cur->ifa_name, "eth0"))
118                         return 1;
119
120                 if (!strcmp(cur->ifa_name, "wlan0"))
121                         return 1;
122         }
123
124         return 0;
125 }
126
127 static struct tfm_interface_driver network = {
128         .name = "net",
129         .ops = {
130                 .connect = net_connect,
131                 .disconnect = net_disconnect,
132                 .rx_data = net_rx_data,
133                 .tx_data = net_tx_data,
134                 .available = net_available,
135         },
136 };
137 INTERFACE_REGISTER(network)