-rw-r--r-- | noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc | 103 | ||||
-rw-r--r-- | noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh | 14 |
2 files changed, 107 insertions, 10 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc index 0d0f075..dcdc16b 100644 --- a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc +++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc | |||
@@ -1,100 +1,191 @@ | |||
1 | /* | 1 | /* |
2 | * Communication protocol | 2 | * Communication protocol |
3 | * | 3 | * |
4 | * $Id$ | 4 | * $Id$ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #include "wl_types.hh" | 7 | #include "wl_types.hh" |
8 | #include "wl_proto.hh" | 8 | #include "wl_proto.hh" |
9 | #include "wl_log.hh" | 9 | #include "wl_log.hh" |
10 | #include "wl_sock.hh" | 10 | #include "wl_sock.hh" |
11 | 11 | ||
12 | /* Adds a field to the buffer */ | 12 | /* Adds a field to the buffer */ |
13 | int add_field(char *buffer, const char *string, int len) | 13 | unsigned int add_field(char *buffer, const char *string, int len) |
14 | { | 14 | { |
15 | char newlen[5]; | 15 | char newlen[5]; |
16 | 16 | ||
17 | /* 3 Byte = Length */ | 17 | /* 3 Byte = Length */ |
18 | memset(newlen, 0, sizeof(newlen)); | 18 | memset(newlen, 0, sizeof(newlen)); |
19 | snprintf(newlen, sizeof(newlen) - 1, "%.3d", len); | 19 | snprintf(newlen, sizeof(newlen) - 1, "%.3d", len); |
20 | memcpy(buffer, newlen, 3); | 20 | memcpy(buffer, newlen, 3); |
21 | 21 | ||
22 | /* Length bytes = Value */ | 22 | /* Length bytes = Value */ |
23 | memcpy(buffer + 3, string, atoi(newlen)); | 23 | memcpy(buffer + 3, string, atoi(newlen)); |
24 | 24 | ||
25 | /* Return length of attached field */ | 25 | /* Return length of attached field */ |
26 | return (atoi(newlen) + 3); | 26 | return (atoi(newlen) + 3); |
27 | } | 27 | } |
28 | 28 | ||
29 | int get_field(const char *buffer, char *out, int maxlen) | 29 | unsigned int get_field(const char *buffer, char *out, int maxlen) |
30 | { | 30 | { |
31 | char len[5]; | 31 | char len[5]; |
32 | 32 | ||
33 | /* Get length of value */ | 33 | /* Get length of value */ |
34 | memset(len, 0, sizeof(len)); | 34 | memset(len, 0, sizeof(len)); |
35 | memcpy(len, buffer, 3); | 35 | memcpy(len, buffer, 3); |
36 | 36 | ||
37 | /* Copy buffer to out pointer */ | 37 | /* Copy buffer to out pointer */ |
38 | memset(out, 0, maxlen); | 38 | memset(out, 0, maxlen); |
39 | if(atoi(len) > maxlen -1) | 39 | if(atoi(len) > maxlen -1) |
40 | memcpy(out, buffer + 3, maxlen - 1); | 40 | memcpy(out, buffer + 3, maxlen - 1); |
41 | else | 41 | else |
42 | memcpy(out, buffer + 3, atoi(len)); | 42 | memcpy(out, buffer + 3, atoi(len)); |
43 | 43 | ||
44 | /* Return length of whole field (including 3 byte length) */ | 44 | /* Return length of whole field (including 3 byte length) */ |
45 | return (atoi(len) + 3); | 45 | return (atoi(len) + 3); |
46 | } | 46 | } |
47 | 47 | ||
48 | /* Send ok message */ | ||
49 | int send_ok(const char *guihost, int guiport, int code) | ||
50 | { | ||
51 | unsigned int len = 0; | ||
52 | char buffer[128], temp[5]; | ||
53 | |||
54 | memset(buffer, 0, sizeof(buffer)); | ||
55 | |||
56 | memset(temp, 0, sizeof(temp)); | ||
57 | snprintf(temp, sizeof(temp) - 1, "%.2d", CMD_OK); | ||
58 | memcpy(buffer, temp, 2); | ||
59 | len += 2; | ||
60 | |||
61 | memset(temp, 0, sizeof(temp)); | ||
62 | snprintf(temp, sizeof(temp) - 1, "%.2d", code); | ||
63 | len += add_field(buffer + len, temp, 2); | ||
64 | |||
65 | /* Send prepared buffer to UI */ | ||
66 | #ifdef DEBUG | ||
67 | wl_loginfo("Sent ok to UI: '%s'", buffer); | ||
68 | #endif | ||
69 | |||
70 | return ((!wl_send(guihost, guiport, buffer)) ? 0 : 1); | ||
71 | } | ||
72 | |||
73 | /* Send fail message */ | ||
74 | int send_fail(const char *guihost, int guiport, | ||
75 | int code, const char *errstr) | ||
76 | { | ||
77 | unsigned int len = 0; | ||
78 | char buffer[128], temp[5]; | ||
79 | |||
80 | memset(buffer, 0, sizeof(buffer)); | ||
81 | |||
82 | memset(temp, 0, sizeof(temp)); | ||
83 | snprintf(temp, sizeof(temp) - 1, "%.2d", CMD_FAIL); | ||
84 | memcpy(buffer, temp, 2); | ||
85 | len += 2; | ||
86 | |||
87 | memset(temp, 0, sizeof(temp)); | ||
88 | snprintf(temp, sizeof(temp) - 1, "%.2d", code); | ||
89 | len += add_field(buffer + len, temp, 2); | ||
90 | |||
91 | len += add_field(buffer + len, errstr, strlen(errstr)); | ||
92 | |||
93 | /* Send prepared buffer to UI */ | ||
94 | #ifdef DEBUG | ||
95 | wl_loginfo("Send CMD_FAIL to UI: '%s'", buffer); | ||
96 | #endif | ||
97 | |||
98 | return ((!wl_send(guihost, guiport, buffer)) ? 0 : 1); | ||
99 | } | ||
100 | |||
101 | int get_ok(const char *buffer) | ||
102 | { | ||
103 | char temp[5]; | ||
104 | unsigned int len = 0; | ||
105 | |||
106 | /* packet type already determined, skip check */ | ||
107 | len += 2; | ||
108 | |||
109 | /* what is ok for? */ | ||
110 | memset(temp, 0, sizeof(temp)); | ||
111 | len += get_field(buffer + len, temp, sizeof(temp)); | ||
112 | |||
113 | return atoi(temp); | ||
114 | } | ||
115 | |||
116 | /* put failmessage into buffer */ | ||
117 | int get_fail(char *out, const char *buffer, size_t len) | ||
118 | { | ||
119 | char temp[5]; | ||
120 | int error=0; | ||
121 | unsigned int len = 0; | ||
122 | |||
123 | /* packet type already determined, skip check */ | ||
124 | len += 2; | ||
125 | |||
126 | /* what is fail for? */ | ||
127 | memset(temp, 0, sizeof(temp)); | ||
128 | len += get_field(buffer + len, temp, sizeof(temp)); | ||
129 | error=atoi(temp); | ||
130 | |||
131 | /* get errorstring and fill into buffer */ | ||
132 | memset(out, 0, len); | ||
133 | len += get_field(buffer + len, out, len - 1); | ||
134 | |||
135 | return error; | ||
136 | } | ||
137 | |||
138 | |||
48 | /* Send found network to UI */ | 139 | /* Send found network to UI */ |
49 | int send_network_found (const char *guihost, int guiport, void *structure) | 140 | int send_network_found (const char *guihost, int guiport, void *structure) |
50 | { | 141 | { |
51 | wl_network_t *ptr; | 142 | wl_network_t *ptr; |
52 | char buffer[2048], temp[5]; | 143 | char buffer[2048], temp[5]; |
53 | unsigned int len = 0; | 144 | unsigned int len = 0; |
54 | 145 | ||
55 | ptr = (wl_network_t *)structure; | 146 | ptr = (wl_network_t *)structure; |
56 | 147 | ||
57 | memset(buffer,0,sizeof(buffer)); | 148 | memset(buffer,0,sizeof(buffer)); |
58 | /* Type = Found new net (without length field) */ | 149 | /* Type = Found new net (without length field) */ |
59 | memset(temp, 0, sizeof(temp)); | 150 | memset(temp, 0, sizeof(temp)); |
60 | snprintf(temp, sizeof(temp), "%.2d", WL_NETFOUND); | 151 | snprintf(temp, sizeof(temp) - 1, "%.2d", WL_NETFOUND); |
61 | memcpy(buffer, temp, 2); | 152 | memcpy(buffer, temp, 2); |
62 | len += 2; | 153 | len += 2; |
63 | 154 | ||
64 | /* Set Net-type */ | 155 | /* Set Net-type */ |
65 | memset(temp, 0, sizeof(temp)); | 156 | memset(temp, 0, sizeof(temp)); |
66 | snprintf(temp, sizeof(temp), "%d", ptr->net_type); | 157 | snprintf(temp, sizeof(temp) - 1, "%d", ptr->net_type); |
67 | len += add_field(buffer + len, temp, 1); | 158 | len += add_field(buffer + len, temp, 1); |
68 | 159 | ||
69 | /* Set channel */ | 160 | /* Set channel */ |
70 | memset(temp, 0, sizeof(temp)); | 161 | memset(temp, 0, sizeof(temp)); |
71 | snprintf(temp, sizeof(temp), "%.2d", ptr->channel); | 162 | snprintf(temp, sizeof(temp) - 1, "%.2d", ptr->channel); |
72 | len += add_field(buffer + len, temp, 2); | 163 | len += add_field(buffer + len, temp, 2); |
73 | 164 | ||
74 | /* Set WEP y/n */ | 165 | /* Set WEP y/n */ |
75 | memset(temp, 0, sizeof(temp)); | 166 | memset(temp, 0, sizeof(temp)); |
76 | snprintf(temp, sizeof(temp), "%d", ptr->wep); | 167 | snprintf(temp, sizeof(temp) - 1, "%d", ptr->wep); |
77 | len += add_field(buffer + len, temp, 1); | 168 | len += add_field(buffer + len, temp, 1); |
78 | 169 | ||
79 | /* Set Mac */ | 170 | /* Set Mac */ |
80 | len += add_field(buffer + len, ptr->mac, 17); | 171 | len += add_field(buffer + len, ptr->mac, 17); |
81 | 172 | ||
82 | /* Set ssid */ | 173 | /* Set ssid */ |
83 | if(len + ptr->ssid_len < sizeof(buffer) - 1) | 174 | if(len + ptr->ssid_len < sizeof(buffer) - 1) |
84 | len += add_field(buffer + len, ptr->bssid, ptr->ssid_len); | 175 | len += add_field(buffer + len, ptr->bssid, ptr->ssid_len); |
85 | else | 176 | else |
86 | len += add_field(buffer + len, ptr->bssid, sizeof(buffer) - len - 1); | 177 | len += add_field(buffer + len, ptr->bssid, sizeof(buffer) - len - 1); |
87 | 178 | ||
88 | /* Send prepared buffer to UI */ | 179 | /* Send prepared buffer to UI */ |
89 | #ifdef DEBUG | 180 | #ifdef DEBUG |
90 | wl_loginfo("Sending network to UI: '%s'", buffer); | 181 | wl_loginfo("Sending network to UI: '%s'", buffer); |
91 | #endif | 182 | #endif |
92 | 183 | ||
93 | return ((!wl_send(guihost, guiport, buffer)) ? 0 : 1); | 184 | return ((!wl_send(guihost, guiport, buffer)) ? 0 : 1); |
94 | } | 185 | } |
95 | 186 | ||
96 | /* Fill buffer into structur */ | 187 | /* Fill buffer into structur */ |
97 | int get_network_found (void *structure, const char *buffer) | 188 | int get_network_found (void *structure, const char *buffer) |
98 | { | 189 | { |
99 | wl_network_t *ptr; | 190 | wl_network_t *ptr; |
100 | char temp[5]; | 191 | char temp[5]; |
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh index 46ef8e5..63ac53b 100644 --- a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh +++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh | |||
@@ -1,21 +1,27 @@ | |||
1 | /* $Id$ */ | 1 | /* $Id$ */ |
2 | 2 | ||
3 | #ifndef WLPROTO_HH | 3 | #ifndef WLPROTO_HH |
4 | #define WLPROTO_HH | 4 | #define WLPROTO_HH |
5 | 5 | ||
6 | #include <stdio.h> | 6 | #include <stdio.h> |
7 | #include <string.h> | 7 | #include <string.h> |
8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
9 | 9 | ||
10 | /* Type definitions, to be continued */ | 10 | /* Type definitions, to be continued */ |
11 | #define NETFOUND 01 | 11 | #define NETFOUND 01 |
12 | #define NETLOST 02 | 12 | #define NETLOST 02 |
13 | #define CMD_FAIL 50 | ||
14 | #define CMD_OK 51 | ||
13 | #define STARTSNIFF 98 | 15 | #define STARTSNIFF 98 |
14 | #define STOPSNIFF 99 | 16 | #define STOPSNIFF 99 |
15 | 17 | ||
16 | int add_field(char *, const char *, int); | 18 | unsigned int add_field(char *, const char *, int); |
17 | int get_field(const char *, char *, int); | 19 | unsigned int get_field(const char *, char *, int); |
20 | int send_ok(const char *, int, int); | ||
21 | int get_ok(const char *); | ||
22 | int send_fail(const char *, int, int, const char *); | ||
23 | int get_fail(char *, const char *, size_t); | ||
18 | int send_network_found (const char *, int, void *); | 24 | int send_network_found (const char *, int, void *); |
19 | int get_network_found (void *, const char *); | 25 | int get_network_found (void *, const char *); |
20 | 26 | ||
21 | #endif /* WLPROTO_HH */ | 27 | #endif /* WLPROTO_HH */ |