00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00017
00018
00019 #include <arpa/inet.h>
00020 #include <errno.h>
00021 #include <signal.h>
00022
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <sys/socket.h>
00027 #include <iw_compat.h>
00028 #include <vif.h>
00029 #include "iwconnect.h"
00030 #include "iwc_print.h"
00031 #include "message.h"
00032
00033
00034
00035
00036 extern iwc_client_info client_info;
00037 extern iwc_model_info model_info;
00038 extern int net_socket;
00039
00040
00041
00042
00044 int wait = 1;
00045
00047 void
00048 clear_wait(void)
00049 {
00050 wait = 0;
00051 }
00052
00053
00054
00055
00062 void
00063 assemble_registration_data(int vif_index, vif_registration *vif_data,
00064 char *dev_name)
00065 {
00066 unsigned char mac[ETH_ALEN];
00067 vif_get_mac(mac, dev_name);
00068
00069
00070 strncpy(vif_data->vif_name, dev_name, IFNAMSIZ + 1);
00071 memmove(vif_data->vif_mac, mac, ETH_ALEN);
00072 vif_data->vif_index = vif_index;
00073
00074
00075
00076 vif_data->retry = vif_get_retry(dev_name);
00077 vif_data->rts = vif_get_rts(dev_name);
00078 vif_data->frag = vif_get_frag(dev_name);
00079 vif_data->sensitivity = vif_get_sensitivity(dev_name);
00080 vif_data->txpower = vif_get_txpower(dev_name);
00081 vif_data->channel = vif_get_channel(dev_name);
00082 }
00083
00088 int
00089 iwc_model_register(void)
00090 {
00091 int i = 0;
00092 int num_vif = 0;
00093 uint16_t message_id = 0;
00094 int sent_bytes = -1;
00095 int recv_bytes = -1;
00096 uint8_t success = 0;
00097 struct msg_header *header;
00098 struct registration_info *reg_info;
00099 char vif_names[MAX_NUM_VIF][IFNAMSIZ + 1];
00100
00101
00102 struct registration_msg reg_msg = { .info.msg_id = 0 };
00103
00104
00105 char *ack_buffer = calloc(DATA_MSG_SIZE, 1);
00106 memset(&vif_names, 0, sizeof(vif_names));
00107
00108 header = &(reg_msg.header);
00109 reg_info = &(reg_msg.info);
00110
00111
00112 header->type = REGISTRATION_MSG;
00113 strncpy(header->id, client_info.host_id, 2 * ETH_ALEN + 1);
00114 strncpy(reg_info->name, client_info.host_info.hostname, HOST_NAME_MAX);
00115 memmove(®_info->addr, &client_info.host_info.addr,
00116 sizeof(struct sockaddr_storage));
00117
00118
00119 num_vif = vif_get_names(vif_names);
00120 reg_info->num_vif = num_vif;
00121 for (i = 0; i < num_vif; i++)
00122 assemble_registration_data(i, &(reg_info->vif_data[i]), vif_names[i]);
00123
00124
00125 signal(SIGALRM, clear_wait);
00126
00127 while ((success == 0) && (message_id < 10))
00128 {
00129 reg_info->msg_id = message_id;
00130 wait = 1;
00131 debug_msg((char *) ®_msg);
00132
00133
00134 if (connect(net_socket,
00135 model_info.host_info.addr_info.ai_addr,
00136 model_info.host_info.addr_info.ai_addrlen) < 0)
00137 {
00138 iwc_error("Couldn't connect to model (%s)\n", strerror(errno));
00139 return -1;
00140 }
00141
00142
00143 if ((sent_bytes = sendto(net_socket, ®_msg, REGISTRATION_MSG_SIZE, 0,
00144 model_info.host_info.addr_info.ai_addr,
00145 model_info.host_info.addr_info.ai_addrlen)) < 0)
00146 {
00147 iwc_error("Couldn't send register message (%s)\n",
00148 strerror(errno));
00149 } else {
00150 iwc_debug(">>net_socket<< %i Bytes sent\n", sent_bytes);
00151 iwc_debug("\n");
00152 }
00153
00154
00155 alarm(5);
00156 while (wait == 1)
00157 {
00158 if ((recv_bytes = recv(net_socket, ack_buffer,
00159 ACK_MSG_SIZE, MSG_DONTWAIT)) > 0)
00160 {
00161 iwc_debug(">>net_socket<< %i Bytes read\n",
00162 recv_bytes);
00163 if ((recv_bytes == ACK_MSG_SIZE) &&
00164 is_ack_msg((struct msg_header *) ack_buffer) &&
00165 (((struct plain_msg *) ack_buffer)->msg_id <= message_id))
00166 {
00167 debug_msg(ack_buffer);
00168 alarm(0);
00169 wait = 0;
00170 success = 1;
00171 } else {
00172
00173
00174
00175
00176 iwc_debug("iwc_model_register(): Discard unknown packet\n");
00177 }
00178 iwc_debug("\n");
00179 } else {
00180
00181
00182
00183 if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
00184 {
00185 if (errno == ECONNREFUSED)
00186 iwc_error("Couldn't connect to WlanModel\n");
00187 else
00188 iwc_error("iwc_model_register(): recv() (%s)\n",
00189 strerror(errno));
00190 return -1;
00191 }
00192 }
00193 }
00194 message_id++;
00195 }
00196 free(ack_buffer);
00197 if (success == 0)
00198 {
00199 iwc_error("Couldn't register host at WlanModel\n");
00200 return -1;
00201 }
00202 iwc_log("Host successfully registered at WlanModel\n");
00203
00204 return 1;
00205 }
00206
00211 int
00212 iwc_model_deregister(void)
00213 {
00214 int error = 0;
00215 int sent_bytes = 0;
00216 struct plain_msg *dereg_msg = calloc(1, sizeof(struct plain_msg));;
00217
00218 dereg_msg->header.type = DEREGISTRATION_MSG;
00219 strncpy(dereg_msg->header.id, client_info.host_id, 2 * ETH_ALEN + 1);
00220 dereg_msg->msg_id = 0;
00221
00222 debug_msg((char *) dereg_msg);
00223
00224
00225 if ((sent_bytes = sendto(net_socket, dereg_msg, DEREGISTRATION_MSG_SIZE, 0,
00226 model_info.host_info.addr_info.ai_addr,
00227 model_info.host_info.addr_info.ai_addrlen)) < 1)
00228 {
00229 iwc_error("iwc_model_deregister(): sendto (%s)\n", strerror(errno));
00230 error = -1;
00231 }
00232 else
00233 iwc_debug("iwc_model_deregister(): >>net_socket<< %i Bytes sent\n",
00234 sent_bytes);
00235
00236
00237 if (error == -1)
00238 {
00239 iwc_error("Model deregistration failed\n");
00240 return -1;
00241 }
00242 iwc_debug("\n");
00243
00244 return 0;
00245 }
00246
00247
00253 void
00254 iwc_model_update(int ioctl_type, const char *dev_name)
00255 {
00256 int sent_bytes = 0;
00257
00258 struct config_msg *notify_msg = calloc(1, sizeof(struct config_msg));
00259 if (notify_msg == NULL)
00260 return;
00261
00262
00263 (*notify_msg).header.type = CONFIG_MSG;
00264 strncpy(notify_msg->header.id, client_info.host_id, 2 * ETH_ALEN + 1);
00265
00266
00267
00268 switch (ioctl_type)
00269 {
00270 case SIOCSIWFREQ:
00271
00272 notify_msg->vif_data.value.channel = vif_get_channel(dev_name);
00273 break;
00274 case SIOCSIWSENS:
00275 notify_msg->vif_data.value.sensitivity =
00276 vif_get_sensitivity(dev_name);
00277 break;
00278 case SIOCSIWRETRY:
00279 notify_msg->vif_data.value.retry = vif_get_retry(dev_name);
00280 break;
00281 case SIOCSIWRTS:
00282 notify_msg->vif_data.value.rts = vif_get_rts(dev_name);
00283 break;
00284 case SIOCSIWFRAG:
00285 notify_msg->vif_data.value.frag = vif_get_frag(dev_name);
00286 break;
00287 case SIOCSIWTXPOW:
00288 notify_msg->vif_data.value.txpower = vif_get_txpower(dev_name);
00289 break;
00290 default:
00291 iwc_debug("iwc_model_update(): Wireless extension %x not found!\n",
00292 ioctl_type);
00293 free(notify_msg);
00294 return;
00295 }
00296 notify_msg->vif_data.type = ioctl_type;
00297
00298 debug_msg((char *) notify_msg);
00299
00300
00301 if ((sent_bytes = sendto(net_socket, notify_msg, CONFIG_MSG_SIZE, 0,
00302 model_info.host_info.addr_info.ai_addr,
00303 model_info.host_info.addr_info.ai_addrlen)) < 1)
00304 iwc_error("iwc_model_update(): sendto (%s)\n", strerror(errno));
00305 iwc_debug("iwc_model_update(): >>net_socket<< %i Bytes sent\n", sent_bytes);
00306 iwc_debug("\n");
00307
00308 free(notify_msg);
00309 }