~rimskyp/helenos/gui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (c) 2009 Lukas Mejdrech
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup lo
 *  @{
 */

/** @file
 *  Loopback network interface implementation.
 */

#include <async.h>
#include <errno.h>
#include <err.h>
#include <stdio.h>
#include <str.h>

#include <ipc/ipc.h>
#include <ipc/services.h>

#include <net/modules.h>
#include <adt/measured_strings.h>
#include <packet_client.h>
#include <net/device.h>
#include <nil_interface.h>
#include <nil_messages.h>
#include <netif_interface.h>
#include <netif_local.h>

/** Default hardware address.
 */
#define DEFAULT_ADDR		"\0\0\0\0\0\0"

/** Default address length.
 */
#define DEFAULT_ADDR_LEN	(sizeof(DEFAULT_ADDR) / sizeof(char))

/** Loopback module name.
 */
#define NAME  "lo"

/** Network interface global data.
 */
netif_globals_t	netif_globals;

/** Changes the loopback state.
 *  @param[in] device The device structure.
 *  @param[in] state The new device state.
 *  @returns The new state if changed.
 *  @returns EOK otherwise.
 */
int change_state_message(netif_device_t * device, device_state_t state);

/** Creates and returns the loopback network interface structure.
 *  @param[in] device_id The new devce identifier.
 *  @param[out] device The device structure.
 *  @returns EOK on success.
 *  @returns EXDEV if one loopback network interface already exists.
 *  @returns ENOMEM if there is not enough memory left.
 */
int create(device_id_t device_id, netif_device_t * * device);

int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
	return ENOTSUP;
}

int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
	if(! address){
		return EBADMEM;
	}
	address->value = str_dup(DEFAULT_ADDR);
	address->length = DEFAULT_ADDR_LEN;
	return EOK;
}

int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
	ERROR_DECLARE;

	netif_device_t * device;

	if(! stats){
		return EBADMEM;
	}
	ERROR_PROPAGATE(find_device(device_id, &device));
	memcpy(stats, (device_stats_ref) device->specific, sizeof(device_stats_t));
	return EOK;
}

int change_state_message(netif_device_t * device, device_state_t state)
{
	if (device->state != state) {
		device->state = state;
		
		printf("%s: State changed to %s\n", NAME,
		    (state == NETIF_ACTIVE) ? "active" : "stopped");
		
		return state;
	}
	
	return EOK;
}

int create(device_id_t device_id, netif_device_t * * device){
	int index;

	if(netif_device_map_count(&netif_globals.device_map) > 0){
		return EXDEV;
	}else{
		*device = (netif_device_t *) malloc(sizeof(netif_device_t));
		if(!(*device)){
			return ENOMEM;
		}
		(** device).specific = malloc(sizeof(device_stats_t));
		if(! (** device).specific){
			free(*device);
			return ENOMEM;
		}
		null_device_stats((device_stats_ref)(** device).specific);
		(** device).device_id = device_id;
		(** device).nil_phone = -1;
		(** device).state = NETIF_STOPPED;
		index = netif_device_map_add(&netif_globals.device_map, (** device).device_id, * device);
		if(index < 0){
			free(*device);
			free((** device).specific);
			*device = NULL;
			return index;
		}
	}
	return EOK;
}

int netif_initialize(void){
	ipcarg_t phonehash;

	return REGISTER_ME(SERVICE_LO, &phonehash);
}

int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
	ERROR_DECLARE;

	netif_device_t * device;

	// create a new device
	ERROR_PROPAGATE(create(device_id, &device));
	// print the settings
	printf("%s: Device created (id: %d)\n", NAME, device->device_id);
	return EOK;
}

int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
	ERROR_DECLARE;

	netif_device_t * device;
	size_t length;
	packet_t next;
	int phone;

	ERROR_PROPAGATE(find_device(device_id, &device));
	if(device->state != NETIF_ACTIVE){
		netif_pq_release(packet_get_id(packet));
		return EFORWARD;
	}
	next = packet;
	do{
		++ ((device_stats_ref) device->specific)->send_packets;
		++ ((device_stats_ref) device->specific)->receive_packets;
		length = packet_get_data_length(next);
		((device_stats_ref) device->specific)->send_bytes += length;
		((device_stats_ref) device->specific)->receive_bytes += length;
		next = pq_next(next);
	}while(next);
	phone = device->nil_phone;
	fibril_rwlock_write_unlock(&netif_globals.lock);
	nil_received_msg(phone, device_id, packet, sender);
	fibril_rwlock_write_lock(&netif_globals.lock);
	return EOK;
}

int netif_start_message(netif_device_t * device){
	return change_state_message(device, NETIF_ACTIVE);
}

int netif_stop_message(netif_device_t * device){
	return change_state_message(device, NETIF_STOPPED);
}

/** Default thread for new connections.
 *
 * @param[in] iid The initial message identifier.
 * @param[in] icall The initial message call structure.
 *
 */
static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
{
	/*
	 * Accept the connection
	 *  - Answer the first IPC_M_CONNECT_ME_TO call.
	 */
	ipc_answer_0(iid, EOK);
	
	while(true) {
		ipc_call_t answer;
		int answer_count;
		
		/* Clear the answer structure */
		refresh_answer(&answer, &answer_count);
		
		/* Fetch the next message */
		ipc_call_t call;
		ipc_callid_t callid = async_get_call(&call);
		
		/* Process the message */
		int res = netif_module_message(NAME, callid, &call, &answer,
		    &answer_count);
		
		/* End if said to either by the message or the processing result */
		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
			return;
		
		/* Answer the message */
		answer_call(callid, res, &answer, answer_count);
	}
}

int main(int argc, char *argv[])
{
	ERROR_DECLARE;
	
	/* Start the module */
	if (ERROR_OCCURRED(netif_module_start(netif_client_connection)))
		return ERROR_CODE;
	
	return EOK;
}

/** @}
 */