Пример #1
0
void NetworkDevice::EthernetAdapter::describe(TextOutput& t) const {
    t.writeSymbol("{");
    t.pushIndent();
    t.writeNewline();
    
    t.writeSymbols("hostname", "=");
    t.writeString(hostname);
    t.writeNewline();

    t.writeSymbols("name", "=");
    t.writeString(name);
    t.writeNewline();    

    t.writeSymbols("ip", "=");
    t.writeSymbol(formatIP(ip));
    t.writeNewline();    

    t.writeSymbols("subnet", "=");
    t.writeSymbol(formatIP(subnet));
    t.writeNewline();    

    t.writeSymbols("broadcast", "=");
    t.writeSymbol(formatIP(broadcast));
    t.writeNewline();    

    t.writeSymbols("mac", "=");
    t.writeSymbol(formatMAC(mac));
    t.writeNewline();    

    t.popIndent();
    t.writeSymbol("}");
    t.writeNewline();
}
Пример #2
0
// Establish a regular tcp connection
static int tcpConnect(const char* ip, short port) {
    int handle = socket(AF_INET, SOCK_STREAM, 0);
    check1(handle >= 0, SSLERR_CONNECT, "socket return error");
    setNonBlock(handle, 1);
    struct sockaddr_in server;
    bzero(&server, sizeof server);
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    char *newIP = formatIP(ip);
    inet_aton(ip, (struct in_addr *)&server.sin_addr.s_addr);
    free(newIP);
    int r = connect(handle, (struct sockaddr *) &server, sizeof(struct sockaddr));
    if (r < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
        struct pollfd pfd;
        pfd.fd = handle;
        pfd.events = POLLOUT | POLLERR;
        while (r == 0) {
            r = poll(&pfd, 1, 100);
        }
        check1(pfd.revents == POLLOUT, SSLERR_CONNECT, "poll return error events: %d", pfd.revents);
    }
    return handle;
}
Пример #3
0
static void pcap_handle(u_char *user, const struct pcap_pkthdr *h, const u_char *buf)
{
	static unsigned failCount = 0;
#ifndef NO_ARP
	if (buf[0x0c]==0x88 && buf[0x0d]==0x8e) {
#endif
		if (memcmp(destMAC, buf+6, 6)!=0 && startMode>2)	/* 服务器MAC地址不符 */
			return;
		capBuf = buf;
		if (buf[0x0F]==0x00 && buf[0x12]==0x01 && buf[0x16]==0x01) {	/* 验证用户名 */
			if (startMode < 3) {
				memcpy(destMAC, buf+6, 6);
				printf("** 认证MAC:\t%s\n", formatHex(destMAC, 6));
				startMode += 3;	/* 标记为已获取 */
			}
			if (startMode==3 && memcmp(buf+0x17, "User name", 9)==0)	/* 塞尔 */
				startMode = 5;
			switchState(ID_IDENTITY);
		}
		else if (buf[0x0F]==0x00 && buf[0x12]==0x01 && buf[0x16]==0x04)	/* 验证密码 */
			switchState(ID_CHALLENGE);
		else if (buf[0x0F]==0x00 && buf[0x12]==0x03) {	/* 认证成功 */
			printf(">> 认证成功!\n");
			fflush(stdout);
			failCount = 0;
			if (!(startMode%3 == 2)) {
				getEchoKey(buf);
				showRuijieMsg(buf, h->caplen);
			}
			if (dhcpMode==1 || dhcpMode==2)	/* 二次认证第一次或者认证后 */
				switchState(ID_DHCP);
			else if (startMode%3 == 2)
				switchState(ID_WAITECHO);
			else
				switchState(ID_ECHO);
		}
		else if (buf[0x0F]==0x00 && buf[0x12]==0x01 && buf[0x16]==0x02)	/* 显示赛尔提示信息 */
			showCernetMsg(buf);
		else if (buf[0x0F] == 0x05)	/* (赛尔)响应在线 */
			switchState(ID_ECHO);
		else if (buf[0x0F]==0x00 && buf[0x12]==0x04) {  /* 认证失败或被踢下线 */
			if (state==ID_WAITECHO || state==ID_ECHO) {
				printf(">! 认证掉线,开始重连!\n");
				fflush(stdout);
				switchState(ID_START);
			}
			else if (buf[0x1b]!=0 || startMode%3==2) {
				printf("!! 认证失败!\n");
				fflush(stdout);
				if (startMode%3 != 2)
					showRuijieMsg(buf, h->caplen);
				if (maxFail && ++failCount>=maxFail) {
					printf(">! 连续认证失败%u次,退出认证。\n", maxFail);
					fflush(stdout);
					exit(EXIT_SUCCESS);
				}
				restart();
			}
			else
				switchState(ID_START);
		}
#ifndef NO_ARP
	} else if (gateMAC[0]!=0xFE && buf[0x0c]==0x08 && buf[0x0d]==0x06) {
		if (*(u_int32_t *)(buf+0x1c) == gateway) {
			char str[50];
			if (gateMAC[0] == 0xFF) {
				memcpy(gateMAC, buf+0x16, 6);
				printf("** 网关MAC:\t%s\n", formatHex(gateMAC, 6));
				fflush(stdout);
				sprintf(str, "arp -s %s %s", formatIP(gateway), formatHex(gateMAC, 6));
				system(str);
			} else if (buf[0x15]==0x02 && *(u_int32_t *)(buf+0x26)==rip
				&& memcmp(gateMAC, buf+0x16, 6)!=0) {
				printf("** ARP欺骗:\t%s\n", formatHex(buf+0x16, 6));
				fflush(stdout);
#ifndef NO_NOTIFY
				if (showNotify) {
					sprintf(str, "欺骗源: %s", formatHex(buf+0x16, 6));
					show_notify("MentoHUST - ARP提示", str);
				}
#endif
			}
		}
	}
#endif
}
Пример #4
0
static int getAddress()
{
	struct ifreq ifr;
#ifndef SIOCGIFHWADDR	/* BSD、MacOS */
	struct ifaddrs *ifap, *p = NULL;
	struct sockaddr_dl *sdl;
#endif
	int sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0)
	{
		printf("!! 创建套接字失败!\n");
		return -1;
	}
	strcpy(ifr.ifr_name, nic);

#ifdef SIOCGIFHWADDR
	if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
		goto getMACError;
	memcpy(localMAC, ifr.ifr_hwaddr.sa_data, 6);
#else
	if (getifaddrs(&ifap) == 0)
	{
		for (p=ifap; p; p=p->ifa_next)
		{
			if (p->ifa_name && strcmp(p->ifa_name, nic)==0)
			{
				sdl = (struct sockaddr_dl *)p->ifa_addr;
				memcpy(localMAC, sdl->sdl_data + sdl->sdl_nlen, 6);
				break;
			}
		}
		freeifaddrs(ifap);
	}
	if (p == NULL)
		goto getMACError;
#endif

	if (startMode == 0)
		memcpy(destMAC, STANDARD_ADDR, 6);
	else if (startMode == 1)
		memcpy(destMAC, RUIJIE_ADDR, 6);

#ifndef NO_ARP
	gateMAC[0] = 0xFE;
	if (ioctl(sock, SIOCGIFADDR, &ifr) < 0)
		printf("!! 在网卡%s上获取IP失败!\n", nic);
	else {
		rip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
		if (gateway!=0 && (startMode%3!=2 || ((u_char *)&gateway)[3]!=0x02))
			gateMAC[0] = 0xFF;
	}
	if (dhcpMode!=0 || ip==-1)
		ip = rip;
#else
	if (dhcpMode!=0 || ip==-1) {
		if (ioctl(sock, SIOCGIFADDR, &ifr) < 0)
			printf("!! 在网卡%s上获取IP失败!\n", nic);
		else
			ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
	}
#endif

	if (dhcpMode!=0 || mask==-1) {
		if (ioctl(sock, SIOCGIFNETMASK, &ifr) < 0)
			printf("!! 在网卡%s上获取子网掩码失败!\n", nic);
		else
			mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
	}
	close(sock);

	printf("** 本机MAC:\t%s\n", formatHex(localMAC, 6));
	printf("** 使用IP:\t%s\n", formatIP(ip));
	printf("** 子网掩码:\t%s\n", formatIP(mask));
	return 0;

getMACError:
	close(sock);
	printf("!! 在网卡%s上获取MAC失败!\n", nic);
	return -1;
}
Пример #5
0
void saveConfig(const char *pName,const char *pPasswd)
{
	char *	buf = (char*)malloc(FILE_SIZE);
	buf[0]='\0';
	buf[FILE_SIZE]='\0';

	strcat(buf,"\n[BIRL]\n");

	strcat(buf,"UserName="******"\n");

	strcat(buf,"Password="******"");
	strcat(buf,"\n");

	strcat(buf,"Nic=");
	strcat(buf,nic);
	strcat(buf,"\n");

	strcat(buf,"IP=");
	strcat(buf,formatIP(ip));
	strcat(buf,"\n");

	strcat(buf,"Mask=");
	itoa_cat(buf,mask);
	strcat(buf,"\n");

	strcat(buf,"Gateway=");
	strcat(buf,formatIP(gateway));
	strcat(buf,"\n");

	strcat(buf,"DNS=");
	strcat(buf,formatIP(dns));
	strcat(buf,"\n");

	strcat(buf,"PingHost=");
	strcat(buf,formatIP(pingHost));
	strcat(buf,"\n");

	strcat(buf,"Timeout=");
	itoa_cat(buf,timeout);
	strcat(buf,"\n");

	strcat(buf,"EchoInterval=");
	itoa_cat(buf,echoInterval);
	strcat(buf,"\n");

	strcat(buf,"RestartWait=");
	itoa_cat(buf,restartWait);
	strcat(buf,"\n");

	strcat(buf,"MaxFail=");
	itoa_cat(buf,maxFail);
	strcat(buf,"\n");

	strcat(buf,"StartMode=");
	itoa_cat(buf,startMode);
	strcat(buf,"\n");

	strcat(buf,"DhcpMode=");
	itoa_cat(buf,dhcpMode);
	strcat(buf,"\n");


	strcat(buf,"DaemonMode=");
	itoa_cat(buf,daemonMode);
	strcat(buf,"\n");

#ifndef NO_NOTIFY
	strcat(buf,"ShowNotify=");
	itoa_cat(buf,showNotify);
	strcat(buf,"\n");
#endif

	strcat(buf,"Version=");
	if (bufType != 0) {
		char ver[10];
		sprintf(ver, "%u.%u", version[0], version[1]);
		strcat(buf,ver);
	} else
	strcat(buf,"0.0.0");
	strcat(buf,"\n");

	strcat(buf,"DataFile=");
	strcat(buf,dataFile);
	strcat(buf,"\n");

	strcat(buf,"DhcpScript=");
	strcat(buf,dhcpScript);
	strcat(buf,"\n");

	saveFile(buf, CFG_FILE);
	free(buf);
}
Пример #6
0
static void pcap_handle(u_char *user, const struct pcap_pkthdr *h, const u_char *buf)
{
	static unsigned failCount = 0;
	pthread_t thread_lan;

#ifndef NO_ARP
	if (buf[0x0c]==0x88 && buf[0x0d]==0x8e) {
#endif
		if (memcmp(destMAC, buf+6, 6)!=0 && startMode>2)	/* 服务器MAC地址不符 */
			return;
		capBuf = buf;
		if (buf[0x0F]==0x00 && buf[0x12]==0x01 && buf[0x16]==0x01) {	/* 验证用户名 */
			if (startMode < 3) {
				memcpy(destMAC, buf+6, 6);
				print_log(_("** 认证服务器MAC: %s\n"), formatHex(destMAC, 6));
				startMode += 3;	/* 标记认证服务器MAC为已获取,可以锁定 */
			}
			if (proxyMode == 0) {
				if (startMode==3 && memcmp(buf+0x17, "User name", 9)==0)	/* 塞尔 */
					startMode = 5;
				switchState(ID_IDENTITY);
			} else {
				if (proxyClientRequested == 1) {
					print_log(_(">> 服务器已请求用户名\n"));
					proxy_send_to_lan(buf, h->len);
				} else {
					print_log(_("!! 在代理认证完成后收到用户名请求,将重启认证!\n"));
					switchState(ID_WAITCLIENT);
				}
			}
		}
		else if (buf[0x0F]==0x00 && buf[0x12]==0x01 && buf[0x16]==0x04)	{ /* 验证密码 */
			if (proxyMode == 0) {
				switchState(ID_CHALLENGE);
			} else {
				if (proxyClientRequested == 1) {
					print_log(_(">> 服务器已请求密码\n"));
					proxy_send_to_lan(buf, h->len);
				} else {
					print_log(_("!! 在代理认证完成后收到密码请求,将重启认证!\n"));
					switchState(ID_WAITCLIENT);
				}
			}
		}
		else if (buf[0x0F]==0x00 && buf[0x12]==0x03) {	/* 认证成功 */
			print_log(_(">> 认证成功!\n"));
			failCount = 0;
			proxySuccessCount++;
			if (proxyMode != 0) {
				proxy_send_to_lan(buf, h->len);
				if (proxySuccessCount >= proxyRequireSuccessCount) {
					pcap_breakloop(hPcapLan);
					proxyClientRequested = 0;
					proxySuccessCount = 0;
					memcpy(lastSuccessClientMAC, clientMAC, 6); // 备份本次认证成功的客户端MAC,用于通知掉线
					proxy_clear_client_mac(); // 重设MAC地址,以备下次使用不同客户端认证用
					print_log(_(">> 已关闭LAN监听线程\n"));
				}
			}
			if (!(startMode%3 == 2)) {
				getEchoKey(buf);
			}
			showRuijieMsg(buf, h->caplen);
			if (dhcpMode==1 || dhcpMode==2)	/* 二次认证第一次或者认证后 */
				switchState(ID_DHCP);
			else if (startMode%3 == 2)
				switchState(ID_WAITECHO);
			else
				switchState(ID_ECHO);
		}
		else if (buf[0x0F]==0x00 && buf[0x12]==0x01 && buf[0x16]==0x02)	/* 显示赛尔提示信息 */
			showCernetMsg(buf);
		else if (buf[0x0F] == 0x05)	/* (赛尔)响应在线 */
			switchState(ID_ECHO);
		else if (buf[0x0F]==0x00 && buf[0x12]==0x04) {  /* 认证失败或被踢下线 */
			if (state==ID_WAITECHO || state==ID_ECHO) {
				if (proxyMode == 0) {
					print_log(_(">> 认证掉线!\n"));
					showRuijieMsg(buf, h->caplen);
					if (restartOnLogOff) {
						print_log(_(">> 正在重新认证...\n"));
						switchState(ID_START);					
					} else {
						exit(1);
					}
				} else {
					pthread_create(&thread_lan, NULL, lan_thread, 0);
					print_log(_(">> 认证掉线,已发回客户端并重新启用对LAN的监听\n"));
					showRuijieMsg(buf, h->caplen);
					// clientMAC已经在成功时被清除了,所以使用lastSuccessClientMAC发送,发完清除
					memmove(clientMAC, lastSuccessClientMAC, 6);
					proxy_send_to_lan(buf, h->len);
					proxy_clear_client_mac();
					switchState(ID_WAITCLIENT);
				}
			}
			else if (buf[0x1b]!=0 || startMode%3==2) {
				print_log(_(">> 认证失败!\n"));
				showRuijieMsg(buf, h->caplen);
				if (maxFail && ++failCount>=maxFail) {
					print_log(_(">> 连续认证失败%u次,退出认证。\n"), maxFail);
					exit(EXIT_SUCCESS);
				}
				restart();
			} else {
				if (proxyMode == 0)
					switchState(ID_START);
				else
					switchState(ID_WAITCLIENT);
			}
		}
#ifndef NO_ARP
	} else if (gateMAC[0]!=0xFE && buf[0x0c]==0x08 && buf[0x0d]==0x06) {
		if (*(u_int32_t *)(buf+0x1c) == gateway) {
			char str[50];
			if (gateMAC[0] == 0xFF) {
				memcpy(gateMAC, buf+0x16, 6);
				print_log(_("** 网关MAC:\t%s\n"), formatHex(gateMAC, 6));
				sprintf(str, "arp -s %s %s", formatIP(gateway), formatHex(gateMAC, 6));
				system(str);
			} else if (buf[0x15]==0x02 && memcmp(&rip, buf+0x26, 4)==0
				&& memcmp(gateMAC, buf+0x16, 6)!=0) {
				print_log(_("** ARP欺骗:\t%s\n"), formatHex(buf+0x16, 6));
#ifndef NO_NOTIFY
				if (showNotify) {
					sprintf(str, _("欺骗源: %s"), formatHex(buf+0x16, 6));
					if (show_notify(_("MentoHUST - ARP提示"), str, 1000*showNotify) < 0)
						showNotify = 0;
				}
#endif
			}
		}
	}
#endif
}
Пример #7
0
int
main(int argc, char **argv)
{
    char * nic_link_latency = "10ns";
    FILE *output = stdout;
    FILE *input = stdin;

    if ( collect_parameters(input) ) {
        fprintf(stderr, "Parameter collection failed!\n");
        return 1;
    }


    fprintf(output, "<?xml version=\"1.0\"?>\n");
    fprintf(output, "\n<sdl version=\"2.0\"/>\n");
    fprintf(output, "\n");

    fprintf(output, "<config>\n");
    fprintf(output, "    run-mode=both\n");
    fprintf(output, "</config>\n");
    fprintf(output, "\n");

    fprintf(output, "<param_include>\n");

    fprintf(output, "  <rtr_params>\n");
    fprintf(output, "    <debug> 0 </debug>\n");
    fprintf(output, "    <num_ports> %d </num_ports>\n", params.k);
    fprintf(output, "    <num_vcs> 2 </num_vcs>\n");
    fprintf(output, "    <link_bw> %s </link_bw>\n", params.link_bw);
    fprintf(output, "    <xbar_bw> %s </xbar_bw>\n", params.xbar_bw);
    fprintf(output, "    <topology> fattree </topology>\n");
    fprintf(output, "    <fattree:loading> %d </fattree:loading>\n",  params.numnodes);
    fprintf(output, "  </rtr_params>\n");
    fprintf(output, "\n");
    fprintf(output, "  <nic_params>\n");
    fprintf(output, "    <topology> fattree </topology>\n");
    fprintf(output, "    <fattree:loading> %d </fattree:loading>\n", params.numnodes);
    fprintf(output, "    <fattree:radix> %d </fattree:radix>\n", params.k);
    fprintf(output, "    <num_peers> %d </num_peers>\n", params.peers);
    fprintf(output, "    <num_vcs> 2 </num_vcs>\n");
    fprintf(output, "    <link_bw> %s </link_bw>\n", params.link_bw);
    fprintf(output, "  </nic_params>\n");
    fprintf(output, "\n");
    fprintf(output, "</param_include>\n");
    fprintf(output, "\n");

    fprintf(output, "<sst>\n");


    addr myip;
    myip.x[0] = 10;
    myip.x[1] = params.k;
    myip.x[2] = 1;
    myip.x[3] = 1;
    char myip_str[16] = {0};

    int router_num = 0;
    fprintf(output, "  <!-- CORE ROUTERS -->\n");
    int num_core = (params.k/2) * (params.k/2);
    for ( int i = 0 ; i < num_core ; i++ ) {
        myip.x[2] = 1 + i/(params.k/2);
        myip.x[3] = 1 + i%(params.k/2);
        formatIP(myip, myip_str);
        fprintf(output, "  <component name=core:%s type=merlin.hr_router>\n", myip_str);
        fprintf(output, "    <params include=rtr_params>\n");
        fprintf(output, "      <id> %d </id>\n", router_num++);
        fprintf(output, "      <fattree:addr> %d </fattree:addr>\n", myip.s);
        fprintf(output, "      <fattree:level> 3 </fattree:level>\n");
        fprintf(output, "    </params>\n");

        for ( int l = 0 ; l < params.k ; l++ ) {
            fprintf(output, "    <link name=link:pod%d_core%d port=port%d latency=%s />\n",
                    l, i, l, params.link_lat);
        }

        fprintf(output, "  </component>\n");
        fprintf(output, "\n");

    }

    for ( int pod = 0 ; pod < params.k ; pod++ ) {
        fprintf(output, "\n\n\n");
        fprintf(output, "  <!-- POD %d -->\n", pod);
        myip.x[1] = pod;
        fprintf(output, "  <!-- AGGREGATION ROUTERS -->\n");
        for ( int r = 0 ; r < params.k/2 ; r++ ) {
            int router = params.k/2 + r;
            myip.x[2] = router;
            myip.x[3] = 1;
            formatIP(myip, myip_str);
            fprintf(output, "  <component name=agg:%s type=merlin.hr_router>\n", myip_str);
            fprintf(output, "    <params include=rtr_params>\n");
            fprintf(output, "      <id> %d </id>\n", router_num++);
            fprintf(output, "      <fattree:addr> %d </fattree:addr>\n", myip.s);
            fprintf(output, "      <fattree:level> 2 </fattree:level>\n");
            fprintf(output, "    </params>\n");

            for ( int l = 0 ; l < params.k/2 ; l++ ) {
                fprintf(output, "    <link name=link:pod%d_aggr%d_edge%d port=port%d latency=%s />\n",
                        pod, r, l, l, params.link_lat);
            }
            for ( int l = 0 ; l < params.k/2 ; l++ ) {
                int core = (params.k/2) * (r) + l;
                fprintf(output, "    <link name=link:pod%d_core%d port=port%d latency=%s />\n",
                        pod, core, l + params.k/2, params.link_lat);
            }
            fprintf(output, "  </component>\n");
            fprintf(output, "\n");

        }

        fprintf(output, "\n");
        fprintf(output, "  <!-- EDGE ROUTERS -->\n");
        for ( int r = 0 ; r < params.k/2 ; r++ ) {
            myip.x[2] = r;
            myip.x[3] = 1;
            formatIP(myip, myip_str);
            fprintf(output, "  <component name=edge:%s type=merlin.hr_router>\n", myip_str);
            fprintf(output, "    <params include=rtr_params>\n");
            fprintf(output, "      <id> %d </id>\n", router_num++);
            fprintf(output, "      <fattree:addr> %d </fattree:addr>\n", myip.s);
            fprintf(output, "      <fattree:level> 1 </fattree:level>\n");
            fprintf(output, "    </params>\n");

            for ( int l = 0 ; l < params.numnodes ; l++ ) {
                int node_id = pod * (params.k/2) * params.numnodes;
                node_id += r * params.numnodes;
                node_id += l;
                fprintf(output, "    <link name=link:pod%d_edge%d_node%d port=port%d latency=%s />\n",
                        pod, r, node_id, l, params.link_lat);
            }

            for ( int l = 0 ; l < params.k/2 ; l++ ) {
                fprintf(output, "    <link name=link:pod%d_aggr%d_edge%d port=port%d latency=%s />\n",
                        pod, l, r, l+params.k/2, params.link_lat);
            }

            fprintf(output, "  </component>\n");
            fprintf(output, "\n");
            fprintf(output, "  <!-- NODES -->\n");
            for ( int n = 0 ; n < params.numnodes ; n++ ) {
                int node_id = pod * (params.k/2) * params.numnodes;
                node_id += r * params.numnodes;
                node_id += n;
                myip.x[3] = n+2;
                formatIP(myip, myip_str);
                fprintf(output, "  <component name=nic:%s type=merlin.test_nic>\n", myip_str);
                fprintf(output, "    <params include=nic_params>\n");
                fprintf(output, "      <!-- node number %d -->\n", node_id);
                fprintf(output, "      <id> %d </id>\n", node_id);
                fprintf(output, "      <fattree:addr> %d </fattree:addr>\n", myip.s);
                fprintf(output, "    </params>\n");
                fprintf(output, "    <link name=link:pod%d_edge%d_node%d port=rtr latency=%s />\n",
                        pod, r, node_id, params.link_lat);
                fprintf(output, "  </component>\n");
                fprintf(output, "\n");
            }
        }


    }





    fprintf(output, "\n");
    fprintf(output, "</sst>\n");

    return 0;
}