1 /*
2  * dlgNetwork.cpp - Network dialog
3  *
4  * Copyright (c) 2007 Petr Stehlik of ARAnyM dev team (see AUTHORS)
5  *
6  * This file is part of the ARAnyM project which builds a new and powerful
7  * TOS/FreeMiNT compatible virtual machine running on almost any hardware.
8  *
9  * ARAnyM is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * ARAnyM is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with ARAnyM; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #include "sysdeps.h"
25 #include "sdlgui.h"
26 #include "tools.h" // safe_strncpy
27 #include "dlgNetwork.h"
28 
29 #define BRIDGE_FILTER_NONE  0
30 #define BRIDGE_FILTER_MCAST 1
31 #define BRIDGE_FILTER_IP    2
32 #define BRIDGE_FILTER_MAC   3
33 
34 static struct {
35 	bx_ethernet_options_t config;
36 	bool bridge;
37 	bool debug;
38 	int filter_type;
39 } eth[2];
40 
41 #define SDLGUI_INCLUDE_NETWORKDLG
42 #include "sdlgui.sdl"
43 
44 
init_options(int ethX,int type)45 void DlgNetwork::init_options(int ethX, int type)
46 {
47 	eth[ethX].config = bx_options.ethernet[ethX];
48 	eth[ethX].debug = strstr(eth[ethX].config.type, "debug") != NULL;
49 	eth[ethX].bridge = strstr(eth[ethX].config.type, "bridge") != NULL;
50 
51 	dlg[type + 0].state = 0; /* ETH0_TYP_NONE */
52 	dlg[type + 1].state = 0; /* ETH0_TYP_PTP */
53 	dlg[type + 2].state = 0; /* ETH0_TYP_BRIDGE */
54 
55 	if (strlen(eth[ethX].config.type) == 0 || strcmp(eth[ethX].config.type, "none") == 0)
56 		dlg[type + 0].state = SG_SELECTED;
57 	else if (eth[ethX].bridge)
58 		dlg[type + 2].state = SG_SELECTED;
59 	else
60 		dlg[type + 1].state = SG_SELECTED;
61 
62 	if (strstr(eth[ethX].config.type, "nofilter") != NULL)
63 		eth[ethX].filter_type = BRIDGE_FILTER_NONE;
64 	else if (strstr(eth[ethX].config.type, "mcast") != NULL)
65 		eth[ethX].filter_type = BRIDGE_FILTER_MCAST;
66 	else if (strstr(eth[ethX].config.type, "ip") != NULL)
67 		eth[ethX].filter_type = BRIDGE_FILTER_IP;
68 	else
69 		eth[ethX].filter_type = BRIDGE_FILTER_MAC;
70 }
71 
DlgNetwork(SGOBJ * dlg)72 DlgNetwork::DlgNetwork(SGOBJ *dlg)
73 	: Dialog(dlg)
74 {
75 	// init
76 	init_options(0, ETH0_TYP_NONE);
77 	init_options(1, ETH1_TYP_NONE);
78 }
79 
~DlgNetwork()80 DlgNetwork::~DlgNetwork()
81 {
82 }
83 
processDialog(void)84 int DlgNetwork::processDialog(void)
85 {
86 	int retval = Dialog::GUI_CONTINUE;
87 
88 	switch(return_obj) {
89 		case APPLY:
90 			confirm();
91 			/* fall through */
92 		case CANCEL:
93 			retval = Dialog::GUI_CLOSE;
94 			break;
95 	}
96 
97 	return retval;
98 }
99 
save_options(int ethX,int type)100 void DlgNetwork::save_options(int ethX, int type)
101 {
102 	eth[ethX].bridge = (dlg[type + 2].state & SG_SELECTED);
103 	if (dlg[type + 0].state & SG_SELECTED) {
104 		eth[ethX].config.type[0] = '\0';
105 	}
106 	else {
107 		safe_strncpy(eth[ethX].config.type, eth[ethX].bridge ? "bridge" : "ptp", sizeof(eth[ethX].config.type));
108 		if (eth[ethX].debug)
109 			safe_strncat(eth[ethX].config.type, " debug", sizeof(eth[ethX].config.type));
110 		if (eth[ethX].bridge)
111 		{
112 			switch (eth[ethX].filter_type)
113 			{
114 			case BRIDGE_FILTER_NONE:
115 				safe_strncat(eth[ethX].config.type, " nofilter", sizeof(eth[ethX].config.type));
116 				break;
117 			case BRIDGE_FILTER_MCAST:
118 				safe_strncat(eth[ethX].config.type, " mcast", sizeof(eth[ethX].config.type));
119 				break;
120 			case BRIDGE_FILTER_IP:
121 				safe_strncat(eth[ethX].config.type, " ip", sizeof(eth[ethX].config.type));
122 				break;
123 			default:
124 			case BRIDGE_FILTER_MAC:
125 				break;
126 			}
127 		}
128 	}
129 	bx_options.ethernet[ethX] = eth[ethX].config;
130 }
131 
confirm(void)132 void DlgNetwork::confirm(void)
133 {
134 	save_options(0, ETH0_TYP_NONE);
135 	save_options(1, ETH1_TYP_NONE);
136 }
137 
DlgNetworkOpen(void)138 Dialog *DlgNetworkOpen(void)
139 {
140 	return new DlgNetwork(networkdlg);
141 }
142