aboutsummaryrefslogtreecommitdiffstats
path: root/include/nms/util.pm
blob: 69f5392897f14a174f0660082d129224cb335935 (plain)
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
#! /usr/bin/perl
use strict;
use warnings;
package nms::util;
use Data::Dumper;

use base 'Exporter';
our @EXPORT = qw(guess_placement parse_switches_txt parse_switches parse_switch);

# Parse a single switches.txt-formatted switch
sub parse_switch {
	my ($switch, $subnet4, $subnet6, $mgtmt4, $mgtmt6, $lolid, $distro) = split(/ /);
	my %ret = (
		'sysname' => "$switch",
		'subnet4' => "$subnet4",
		'subnet6' => "$subnet6",
		'mgmt_v4_addr' => "$mgtmt4",
		'mgmt_v6_addr' => "$mgtmt6",
		'traffic_vlan' => "$lolid",
		'distro_name' => "$distro"
	);
	%{$ret{'placement'}} = guess_placement($switch);
	return %ret;
}

# Parses a switches_txt given as a filehandle on $_[0]
# (e.g.: parse_switches_txt(*STDIN) or parse_switches_txt(whatever).
sub parse_switches_txt {
	my $fh = $_[0];
	my @switches;
	while(<$fh>) {
		chomp;
		my %switch = parse_switch($_);
		push @switches, {%switch};
	}
	return @switches;
}

# Parses switches in switches.txt format given as $_[0].
# E.g: parse_switches("e1-3 88.92.0.0/26 2a06:5840:0a::/64 88.92.54.2/26 2a06:5840:54a::2/64 1013 distro0")
sub parse_switches {
	my @switches;
	my $txt = $_[0];
	foreach (split("\n",$txt)) {
		chomp;
		my %switch = parse_switch($_);
		push @switches, {%switch};
	}
	return @switches;
}

# FIXME: Derive which function from the config/db using the shortname.
# If we care.
sub guess_placement {
	return guess_placement_tg($_[0]);
}
# Guesses placement from name to get a starting point
# Digitality X layout
# FIXME: Basically a stub, since MRGLASS is too slow with the map.
sub guess_placement_dx {
	my ($x, $y, $xx, $yy);

	my $name = $_[0];
	my $src = "unknown";
	if ($name =~ /^e\d+-\d+$/) {
		$name =~ /row(\d+)-(\d+)/;
		my ($e, $s) = ($1, $2);
		$src = "main";

		$x = int(1850 - (($e-1)/2) * 63);
		$y = undef;

		if ($s > 1) {
			$y = 160;
		} else {
			$y = 480;
		}

		$xx = $x + 32;
		$yy = $y + 220;

	} elsif ($name =~ /^core$/) {
		$src = "core";
		$x = 1360;
		$y = 770;
		$xx = $x + 200;
		$yy = $y + 100;
	} elsif ($name =~ /^noc$/) {
		$src = "noc";
		$x = 132;
		$y = 880;
		$xx = $x + 230;
		$yy = $y + 40;
	} elsif ($name =~ /^s(\d).floor$/) {
		my $d = ($1);
		$d -= 1;
		$src = "distro";
		$x = 1550 - $d * 600;
		$y = 410;
		$xx = $x + 230;
		$yy = $y + 40;
	} else {
		# Fallback to have _some_ position
		$src = "random";
		$x = int(rand(1900));
		$y = int(rand(900));
		$xx = $x + 230;
		$yy = $y + 40;
	};


	my %box = (
		'src' => "$src",
		'x1' => $x,
		'y1' => $y,
		'xx' => $xx,
		'yy' => $yy
	);
	return %box;
}

# Last updated for TG16
sub guess_placement_tg {
	my ($x, $y, $xx, $yy);

	my $name = $_[0];
	my $src = "unknown";
	if ($name =~ /^e\d+-\d+$/) {
		$name =~ /e(\d+)-(\d+)/;
		my ($e, $s) = ($1, $2);
		$src = "main";

		$x = int(292 + (($e-1)/2) * 31.1);
		$y = undef;

		$x += 14 if ($e >= 21);
		$x += 14 if ($e >= 37);
		$x += 14 if ($e >= 55);
		$x += 14 if ($e >= 69);

		if ($s > 2) {
			$y = 405 - 120 * ($s-2);
		} else {
			$y = 689 - 120 * ($s);
		}

		$xx = $x + 16;
		$yy = $y + 120;

		# Justeringer
		$y += 45 if $name eq "e1-4";
		$y += 20 if $name eq "e3-4";
		$y += 15 if $name eq "e5-4";
		$yy -= 25 if $name eq "e7-1";
		$yy -= 25 if $name eq "e5-1";
		$yy -= 25 if $name eq "e3-1";
		$y += 20 if ($e >= 79 and $s == 2);
		$yy -= 20 if ($e >= 79 and $s == 1);
		$yy -= 30 if ($e >= 81 and $s == 1);

	} elsif ($name =~ /^creativia(\d+)$/) {
		my ($s) = ($1);
		$src = "creativia";
		$x = 1535;
		$y = int(160 + 32.2 * $s);
		$yy = $y + 20;
		if ($s == 1) {
			$xx = $x + 70;
		} elsif ($s == 2) {
			$xx = $x + 90;
		} elsif ($s == 3) {
			$xx = $x + 102;
		} else {
			$xx = $x + 142;
		}

	} elsif ($name =~ /^crew(\d+)-(\d+)$/) {
		my ($s, $n) = ($1, $2);
		$src = "crew";
		$x = 550 + 65 * $n;
		$y = int(759 + 20.5 * $s);
		$xx = $x + 65;
		$yy = $y + 14;
	} elsif ($name =~ /^s(\d).floor/) {
		my $d = ($1);
		$src = "distro";
		$d -= 1;
		$x = 260 + $d * 145;
		$y = 417;
		$xx = $x + 130;
		$yy = $y + 20;
	} else {
		# Fallback to have _some_ position
		$src = "random";
		$x = int(rand(1900));
		$y = int(rand(900));
		$xx = $x + 20;
		$yy = $y + 130;
	};


	my %box = (
		'src' => "$src",
		'x1' => $x,
		'y1' => $y,
		'xx' => $xx,
		'yy' => $yy
	);
	return %box;
}
an class="k">return 1; } return 0; } int aim_conn_addhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type, aim_rxcallback_t newhandler, guint16 flags) { struct aim_rxcblist_s *newcb; if (!conn) return -1; if (checkdisallowed(family, type)) { g_assert(0); return -1; } if (!(newcb = (struct aim_rxcblist_s *)g_new0(struct aim_rxcblist_s, 1))) return -1; newcb->family = family; newcb->type = type; newcb->flags = flags; newcb->handler = newhandler; newcb->next = NULL; if (!conn->handlerlist) conn->handlerlist = (void *)newcb; else { struct aim_rxcblist_s *cur; for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur->next; cur = cur->next) ; cur->next = newcb; } return 0; } int aim_clearhandlers(aim_conn_t *conn) { struct aim_rxcblist_s *cur; if (!conn) return -1; for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur; ) { struct aim_rxcblist_s *tmp; tmp = cur->next; g_free(cur); cur = tmp; } conn->handlerlist = NULL; return 0; } aim_rxcallback_t aim_callhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type) { struct aim_rxcblist_s *cur; if (!conn) return NULL; for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur; cur = cur->next) { if ((cur->family == family) && (cur->type == type)) return cur->handler; } if (type == AIM_CB_SPECIAL_DEFAULT) { return NULL; /* prevent infinite recursion */ } return aim_callhandler(sess, conn, family, AIM_CB_SPECIAL_DEFAULT); } void aim_clonehandlers(aim_session_t *sess, aim_conn_t *dest, aim_conn_t *src) { struct aim_rxcblist_s *cur; for (cur = (struct aim_rxcblist_s *)src->handlerlist; cur; cur = cur->next) { aim_conn_addhandler(sess, dest, cur->family, cur->type, cur->handler, cur->flags); } return; } static int aim_callhandler_noparam(aim_session_t *sess, aim_conn_t *conn,guint16 family, guint16 type, aim_frame_t *ptr) { aim_rxcallback_t userfunc; if ((userfunc = aim_callhandler(sess, conn, family, type))) return userfunc(sess, ptr); return 1; /* XXX */ } /* * aim_rxdispatch() * * Basically, heres what this should do: * 1) Determine correct packet handler for this packet * 2) Mark the packet handled (so it can be dequeued in purge_queue()) * 3) Send the packet to the packet handler * 4) Go to next packet in the queue and start over * 5) When done, run purge_queue() to purge handled commands * * TODO: Clean up. * TODO: More support for mid-level handlers. * TODO: Allow for NULL handlers. * */ void aim_rxdispatch(aim_session_t *sess) { int i; aim_frame_t *cur; for (cur = sess->queue_incoming, i = 0; cur; cur = cur->next, i++) { /* * XXX: This is still fairly ugly. */ if (cur->handled) continue; /* * This is a debugging/sanity check only and probably * could/should be removed for stable code. */ if (((cur->hdrtype == AIM_FRAMETYPE_OFT) && (cur->conn->type != AIM_CONN_TYPE_RENDEZVOUS)) || ((cur->hdrtype == AIM_FRAMETYPE_FLAP) && (cur->conn->type == AIM_CONN_TYPE_RENDEZVOUS))) { do_error_dialog(sess->aux_data, "incompatible frame type/connection type combination", "Gaim"); cur->handled = 1; continue; } if (cur->conn->type == AIM_CONN_TYPE_RENDEZVOUS) { if (cur->hdrtype != AIM_FRAMETYPE_OFT) { do_error_dialog(sess->aux_data, "non-OFT frames on OFT connection", "Gaim"); cur->handled = 1; /* get rid of it */ } else { /* FIXME: implement this (OFT frame) */ cur->handled = 1; /* get rid of it */ } continue; } if (cur->conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) { /* not possible */ do_error_dialog(sess->aux_data, "RENDEZVOUS packet in rxqueue", "Gaim"); cur->handled = 1; continue; } if (cur->hdr.flap.type == 0x01) { cur->handled = aim_callhandler_noparam(sess, cur->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_FLAPVER, cur); /* XXX use consumenonsnac */ continue; } else if (cur->hdr.flap.type == 0x02) { if ((cur->handled = consumesnac(sess, cur))) continue; } else if (cur->hdr.flap.type == 0x04) { cur->handled = negchan_middle(sess, cur); continue; } else if (cur->hdr.flap.type == 0x05) ; if (!cur->handled) { consumenonsnac(sess, cur, 0xffff, 0xffff); /* last chance! */ cur->handled = 1; } } /* * This doesn't have to be called here. It could easily be done * by a seperate thread or something. It's an administrative operation, * and can take a while. Though the less you call it the less memory * you'll have :) */ aim_purge_rxqueue(sess); return; }