Changes in uspace/srv/net/net/net.c [14f1db0:1bfd3d3] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/net/net.c
r14f1db0 r1bfd3d3 36 36 */ 37 37 38 #include "net.h" 39 38 40 #include <async.h> 39 41 #include <ctype.h> … … 46 48 #include <ipc/ipc.h> 47 49 #include <ipc/services.h> 48 49 #include <net_err.h> 50 #include <net_messages.h> 51 #include <net_modules.h> 50 #include <ipc/net.h> 51 #include <ipc/net_net.h> 52 #include <ipc/il.h> 53 54 #include <net/modules.h> 55 #include <net/packet.h> 56 #include <net/device.h> 57 52 58 #include <adt/char_map.h> 53 59 #include <adt/generic_char_map.h> 54 60 #include <adt/measured_strings.h> 55 61 #include <adt/module_map.h> 56 #include <packet/packet.h> 57 #include <il_messages.h> 62 58 63 #include <netif_remote.h> 59 #include <net_device.h>60 64 #include <nil_interface.h> 61 65 #include <net_interface.h> 62 66 #include <ip_interface.h> 63 #include <net_net_messages.h> 64 65 #include "net.h" 66 67 /** Networking module name. 68 * 69 */ 67 68 /** Networking module name. */ 70 69 #define NAME "net" 71 70 72 /** File read buffer size. 73 * 74 */ 71 /** File read buffer size. */ 75 72 #define BUFFER_SIZE 256 76 73 77 /** Networking module global data. 78 * 79 */ 74 /** Networking module global data. */ 80 75 net_globals_t net_globals; 81 76 82 77 GENERIC_CHAR_MAP_IMPLEMENT(measured_strings, measured_string_t); 83 78 DEVICE_MAP_IMPLEMENT(netifs, netif_t); 79 80 static int startup(void); 84 81 85 82 /** Add the configured setting to the configuration map. … … 89 86 * @param[in] value The setting value. 90 87 * 91 * @return sEOK on success.92 * @return sENOMEM if there is not enough memory left.93 * 94 */ 95 int add_configuration(measured_strings_ refconfiguration, const char *name,88 * @return EOK on success. 89 * @return ENOMEM if there is not enough memory left. 90 * 91 */ 92 int add_configuration(measured_strings_t *configuration, const char *name, 96 93 const char *value) 97 94 { 98 ERROR_DECLARE;99 100 measured_string_ refsetting =95 int rc; 96 97 measured_string_t *setting = 101 98 measured_string_create_bulk(value, 0); 102 103 99 if (!setting) 104 100 return ENOMEM; 105 101 106 102 /* Add the configuration setting */ 107 if (ERROR_OCCURRED(measured_strings_add(configuration, name, 0, setting))) { 103 rc = measured_strings_add(configuration, name, 0, setting); 104 if (rc != EOK) { 108 105 free(setting); 109 return ERROR_CODE;106 return rc; 110 107 } 111 108 … … 115 112 /** Generate new system-unique device identifier. 116 113 * 117 * @returns The system-unique devic identifier. 118 * 114 * @return The system-unique devic identifier. 119 115 */ 120 116 static device_id_t generate_new_device_id(void) … … 123 119 } 124 120 125 static int parse_line(measured_strings_ refconfiguration, char *line)126 { 127 ERROR_DECLARE;121 static int parse_line(measured_strings_t *configuration, char *line) 122 { 123 int rc; 128 124 129 125 /* From the beginning */ … … 169 165 170 166 /* Create a bulk measured string till the end */ 171 measured_string_ refsetting =167 measured_string_t *setting = 172 168 measured_string_create_bulk(value, 0); 173 169 if (!setting) … … 175 171 176 172 /* Add the configuration setting */ 177 if (ERROR_OCCURRED(measured_strings_add(configuration, name, 0, setting))) { 173 rc = measured_strings_add(configuration, name, 0, setting); 174 if (rc != EOK) { 178 175 free(setting); 179 return ERROR_CODE;176 return rc; 180 177 } 181 178 … … 184 181 185 182 static int read_configuration_file(const char *directory, const char *filename, 186 measured_strings_ref configuration) 187 { 188 ERROR_DECLARE; 189 183 measured_strings_t *configuration) 184 { 190 185 printf("%s: Reading configuration file %s/%s\n", NAME, directory, filename); 191 186 … … 206 201 unsigned int line_number = 0; 207 202 size_t index = 0; 208 while ( (!ferror(cfg)) && (!feof(cfg))) {203 while (!ferror(cfg) && !feof(cfg)) { 209 204 int read = fgetc(cfg); 210 205 if ((read > 0) && (read != '\n') && (read != '\r')) { 211 206 if (index >= BUFFER_SIZE) { 212 207 line[BUFFER_SIZE - 1] = '\0'; 213 fprintf(stderr, "%s: Configuration line %u too long: %s\n",214 NAME, line_number, line);208 fprintf(stderr, "%s: Configuration line %u too " 209 "long: %s\n", NAME, line_number, line); 215 210 216 211 /* No space left in the line buffer */ 217 212 return EOVERFLOW; 218 } else {219 /* Append the character */220 line[index] = (char) read;221 index++;222 213 } 214 /* Append the character */ 215 line[index] = (char) read; 216 index++; 223 217 } else { 224 218 /* On error or new line */ 225 219 line[index] = '\0'; 226 220 line_number++; 227 if (ERROR_OCCURRED(parse_line(configuration, line))) 228 fprintf(stderr, "%s: Configuration error on line %u: %s\n", 229 NAME, line_number, line); 221 if (parse_line(configuration, line) != EOK) { 222 fprintf(stderr, "%s: Configuration error on " 223 "line %u: %s\n", NAME, line_number, line); 224 } 230 225 231 226 index = 0; … … 242 237 * @param[in,out] netif The network interface structure. 243 238 * 244 * @return sEOK on success.245 * @return sOther error codes as defined for the add_configuration() function.239 * @return EOK on success. 240 * @return Other error codes as defined for the add_configuration() function. 246 241 * 247 242 */ … … 253 248 /** Read the networking subsystem global configuration. 254 249 * 255 * @return sEOK on success.256 * @return sOther error codes as defined for the add_configuration() function.250 * @return EOK on success. 251 * @return Other error codes as defined for the add_configuration() function. 257 252 * 258 253 */ … … 269 264 * its own one. 270 265 * 271 * @return sEOK on success.272 * @return sENOMEM if there is not enough memory left.266 * @return EOK on success. 267 * @return ENOMEM if there is not enough memory left. 273 268 * 274 269 */ 275 270 static int net_initialize(async_client_conn_t client_connection) 276 271 { 277 ERROR_DECLARE;272 int rc; 278 273 279 274 netifs_initialize(&net_globals.netifs); … … 282 277 measured_strings_initialize(&net_globals.configuration); 283 278 284 // TODO: dynamic configuration 285 ERROR_PROPAGATE(read_configuration()); 286 287 ERROR_PROPAGATE(add_module(NULL, &net_globals.modules, 288 LO_NAME, LO_FILENAME, SERVICE_LO, 0, connect_to_service)); 289 ERROR_PROPAGATE(add_module(NULL, &net_globals.modules, 290 DP8390_NAME, DP8390_FILENAME, SERVICE_DP8390, 0, connect_to_service)); 291 ERROR_PROPAGATE(add_module(NULL, &net_globals.modules, 292 ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0, 293 connect_to_service)); 294 ERROR_PROPAGATE(add_module(NULL, &net_globals.modules, 295 NILDUMMY_NAME, NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, 296 connect_to_service)); 279 /* TODO: dynamic configuration */ 280 rc = read_configuration(); 281 if (rc != EOK) 282 return rc; 283 284 rc = add_module(NULL, &net_globals.modules, LO_NAME, LO_FILENAME, 285 SERVICE_LO, 0, connect_to_service); 286 if (rc != EOK) 287 return rc; 288 rc = add_module(NULL, &net_globals.modules, DP8390_NAME, 289 DP8390_FILENAME, SERVICE_DP8390, 0, connect_to_service); 290 if (rc != EOK) 291 return rc; 292 rc = add_module(NULL, &net_globals.modules, ETHERNET_NAME, 293 ETHERNET_FILENAME, SERVICE_ETHERNET, 0, connect_to_service); 294 if (rc != EOK) 295 return rc; 296 rc = add_module(NULL, &net_globals.modules, NILDUMMY_NAME, 297 NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, connect_to_service); 298 if (rc != EOK) 299 return rc; 297 300 298 301 /* Build specific initialization */ … … 312 315 * its own one. 313 316 * 314 * @return sEOK on successful module termination.315 * @return sOther error codes as defined for the net_initialize() function.316 * @return sOther error codes as defined for the REGISTER_ME() macro function.317 * @return EOK on successful module termination. 318 * @return Other error codes as defined for the net_initialize() function. 319 * @return Other error codes as defined for the REGISTER_ME() macro function. 317 320 * 318 321 */ 319 322 static int net_module_start(async_client_conn_t client_connection) 320 323 { 321 ERROR_DECLARE; 324 ipcarg_t phonehash; 325 int rc; 322 326 323 327 async_set_client_connection(client_connection); 324 ERROR_PROPAGATE(pm_init()); 325 326 ipcarg_t phonehash; 327 328 if (ERROR_OCCURRED(net_initialize(client_connection)) 329 || ERROR_OCCURRED(REGISTER_ME(SERVICE_NETWORKING, &phonehash))){ 330 pm_destroy(); 331 return ERROR_CODE; 332 } 333 328 rc = pm_init(); 329 if (rc != EOK) 330 return rc; 331 332 333 rc = net_initialize(client_connection); 334 if (rc != EOK) 335 goto out; 336 337 rc = REGISTER_ME(SERVICE_NETWORKING, &phonehash); 338 if (rc != EOK) 339 goto out; 340 341 rc = startup(); 342 if (rc != EOK) 343 goto out; 344 345 task_retval(0); 334 346 async_manager(); 335 347 348 out: 336 349 pm_destroy(); 337 return EOK;350 return rc; 338 351 } 339 352 … … 341 354 * 342 355 * The network interface configuration is searched first. 343 &356 * 344 357 * @param[in] netif_conf The network interface configuration setting. 345 358 * @param[out] configuration The found configured values. … … 347 360 * @param[out] data The found configuration settings data. 348 361 * 349 * @return sEOK.350 * 351 */ 352 static int net_get_conf(measured_strings_ refnetif_conf,353 measured_string_ refconfiguration, size_t count, char **data)362 * @return EOK. 363 * 364 */ 365 static int net_get_conf(measured_strings_t *netif_conf, 366 measured_string_t *configuration, size_t count, char **data) 354 367 { 355 368 if (data) … … 358 371 size_t index; 359 372 for (index = 0; index < count; index++) { 360 measured_string_ refsetting =373 measured_string_t *setting = 361 374 measured_strings_find(netif_conf, configuration[index].value, 0); 362 375 if (!setting) … … 376 389 } 377 390 378 int net_get_conf_req(int net_phone, measured_string_ ref*configuration,391 int net_get_conf_req(int net_phone, measured_string_t **configuration, 379 392 size_t count, char **data) 380 393 { 381 if (! (configuration && (count > 0)))394 if (!configuration || (count <= 0)) 382 395 return EINVAL; 383 396 … … 386 399 387 400 int net_get_device_conf_req(int net_phone, device_id_t device_id, 388 measured_string_ ref*configuration, size_t count, char **data)401 measured_string_t **configuration, size_t count, char **data) 389 402 { 390 403 if ((!configuration) || (count == 0)) … … 398 411 } 399 412 400 void net_free_settings(measured_string_ refsettings, char *data)413 void net_free_settings(measured_string_t *settings, char *data) 401 414 { 402 415 } … … 409 422 * @param[in] netif The network interface specific data. 410 423 * 411 * @return sEOK on success.412 * @return sEINVAL if there are some settings missing.413 * @return sENOENT if the internet protocol module is not known.414 * @return sOther error codes as defined for the netif_probe_req() function.415 * @return sOther error codes as defined for the nil_device_req() function.416 * @return sOther error codes as defined for the needed internet layer417 * 424 * @return EOK on success. 425 * @return EINVAL if there are some settings missing. 426 * @return ENOENT if the internet protocol module is not known. 427 * @return Other error codes as defined for the netif_probe_req() function. 428 * @return Other error codes as defined for the nil_device_req() function. 429 * @return Other error codes as defined for the needed internet layer 430 * registering function. 418 431 * 419 432 */ 420 433 static int start_device(netif_t *netif) 421 434 { 422 ERROR_DECLARE;435 int rc; 423 436 424 437 /* Mandatory netif */ 425 measured_string_ refsetting =438 measured_string_t *setting = 426 439 measured_strings_find(&netif->configuration, CONF_NETIF, 0); 427 440 … … 461 474 int io = setting ? strtol(setting->value, NULL, 16) : 0; 462 475 463 ERROR_PROPAGATE(netif_probe_req_remote(netif->driver->phone, netif->id, irq, io)); 476 rc = netif_probe_req_remote(netif->driver->phone, netif->id, irq, io); 477 if (rc != EOK) 478 return rc; 464 479 465 480 /* Network interface layer startup */ … … 473 488 int mtu = setting ? strtol(setting->value, NULL, 10) : 0; 474 489 475 ERROR_PROPAGATE(nil_device_req(netif->nil->phone, netif->id, mtu, 476 netif->driver->service)); 490 rc = nil_device_req(netif->nil->phone, netif->id, mtu, 491 netif->driver->service); 492 if (rc != EOK) 493 return rc; 477 494 478 495 internet_service = netif->nil->service; … … 482 499 /* Inter-network layer startup */ 483 500 switch (netif->il->service) { 484 case SERVICE_IP: 485 ERROR_PROPAGATE(ip_device_req(netif->il->phone, netif->id, 486 internet_service)); 487 break; 488 default: 489 return ENOENT; 490 } 491 492 ERROR_PROPAGATE(netif_start_req_remote(netif->driver->phone, netif->id)); 493 return EOK; 501 case SERVICE_IP: 502 rc = ip_device_req(netif->il->phone, netif->id, 503 internet_service); 504 if (rc != EOK) 505 return rc; 506 break; 507 default: 508 return ENOENT; 509 } 510 511 return netif_start_req_remote(netif->driver->phone, netif->id); 494 512 } 495 513 496 514 /** Read the configuration and start all network interfaces. 497 515 * 498 * @return sEOK on success.499 * @return sEXDEV if there is no available system-unique device identifier.500 * @return sEINVAL if any of the network interface names are not configured.501 * @return sENOMEM if there is not enough memory left.502 * @return sOther error codes as defined for the read_configuration()503 * 504 * @return sOther error codes as defined for the read_netif_configuration()505 * 506 * @return sOther error codes as defined for the start_device() function.516 * @return EOK on success. 517 * @return EXDEV if there is no available system-unique device identifier. 518 * @return EINVAL if any of the network interface names are not configured. 519 * @return ENOMEM if there is not enough memory left. 520 * @return Other error codes as defined for the read_configuration() 521 * function. 522 * @return Other error codes as defined for the read_netif_configuration() 523 * function. 524 * @return Other error codes as defined for the start_device() function. 507 525 * 508 526 */ 509 527 static int startup(void) 510 528 { 511 ERROR_DECLARE; 512 513 const char *conf_files[] = {"lo", "ne2k"}; 529 const char *conf_files[] = { 530 "lo", 531 "ne2k" 532 }; 514 533 size_t count = sizeof(conf_files) / sizeof(char *); 534 int rc; 515 535 516 536 size_t i; … … 524 544 return EXDEV; 525 545 526 ERROR_PROPAGATE(measured_strings_initialize(&netif->configuration)); 546 rc = measured_strings_initialize(&netif->configuration); 547 if (rc != EOK) 548 return rc; 527 549 528 550 /* Read configuration files */ 529 if (ERROR_OCCURRED(read_netif_configuration(conf_files[i], netif))) { 551 rc = read_netif_configuration(conf_files[i], netif); 552 if (rc != EOK) { 530 553 measured_strings_destroy(&netif->configuration); 531 554 free(netif); 532 return ERROR_CODE;555 return rc; 533 556 } 534 557 535 558 /* Mandatory name */ 536 measured_string_ refsetting =559 measured_string_t *setting = 537 560 measured_strings_find(&netif->configuration, CONF_NAME, 0); 538 561 if (!setting) { … … 556 579 * and needed modules. 557 580 */ 558 if ((ERROR_OCCURRED(char_map_add(&net_globals.netif_names, 559 netif->name, 0, index))) || (ERROR_OCCURRED(start_device(netif)))) { 581 rc = char_map_add(&net_globals.netif_names, netif->name, 0, 582 index); 583 if (rc != EOK) { 560 584 measured_strings_destroy(&netif->configuration); 561 585 netifs_exclude_index(&net_globals.netifs, index); 562 return ERROR_CODE; 586 return rc; 587 } 588 589 rc = start_device(netif); 590 if (rc != EOK) { 591 measured_strings_destroy(&netif->configuration); 592 netifs_exclude_index(&net_globals.netifs, index); 593 return rc; 563 594 } 564 595 … … 586 617 * in the answer parameter. 587 618 * 588 * @return sEOK on success.589 * @return sENOTSUP if the message is not known.619 * @return EOK on success. 620 * @return ENOTSUP if the message is not known. 590 621 * 591 622 * @see net_interface.h … … 596 627 int *answer_count) 597 628 { 598 ERROR_DECLARE; 599 600 measured_string_ref strings; 629 measured_string_t *strings; 601 630 char *data; 631 int rc; 602 632 603 633 *answer_count = 0; 604 634 switch (IPC_GET_METHOD(*call)) { 605 case IPC_M_PHONE_HUNGUP: 606 return EOK; 607 case NET_NET_GET_DEVICE_CONF: 608 ERROR_PROPAGATE(measured_strings_receive(&strings, &data, 609 IPC_GET_COUNT(call))); 610 net_get_device_conf_req(0, IPC_GET_DEVICE(call), &strings, 611 IPC_GET_COUNT(call), NULL); 612 613 /* Strings should not contain received data anymore */ 614 free(data); 615 616 ERROR_CODE = measured_strings_reply(strings, IPC_GET_COUNT(call)); 617 free(strings); 618 return ERROR_CODE; 619 case NET_NET_GET_CONF: 620 ERROR_PROPAGATE(measured_strings_receive(&strings, &data, 621 IPC_GET_COUNT(call))); 622 net_get_conf_req(0, &strings, IPC_GET_COUNT(call), NULL); 623 624 /* Strings should not contain received data anymore */ 625 free(data); 626 627 ERROR_CODE = measured_strings_reply(strings, IPC_GET_COUNT(call)); 628 free(strings); 629 return ERROR_CODE; 630 case NET_NET_STARTUP: 631 return startup(); 632 } 635 case IPC_M_PHONE_HUNGUP: 636 return EOK; 637 case NET_NET_GET_DEVICE_CONF: 638 rc = measured_strings_receive(&strings, &data, 639 IPC_GET_COUNT(call)); 640 if (rc != EOK) 641 return rc; 642 net_get_device_conf_req(0, IPC_GET_DEVICE(call), &strings, 643 IPC_GET_COUNT(call), NULL); 644 645 /* Strings should not contain received data anymore */ 646 free(data); 647 648 rc = measured_strings_reply(strings, IPC_GET_COUNT(call)); 649 free(strings); 650 return rc; 651 case NET_NET_GET_CONF: 652 rc = measured_strings_receive(&strings, &data, 653 IPC_GET_COUNT(call)); 654 if (rc != EOK) 655 return rc; 656 net_get_conf_req(0, &strings, IPC_GET_COUNT(call), NULL); 657 658 /* Strings should not contain received data anymore */ 659 free(data); 660 661 rc = measured_strings_reply(strings, IPC_GET_COUNT(call)); 662 free(strings); 663 return rc; 664 case NET_NET_STARTUP: 665 return startup(); 666 } 667 633 668 return ENOTSUP; 634 669 } … … 661 696 int res = net_module_message(callid, &call, &answer, &answer_count); 662 697 663 /* End if said to either by the message or the processing result */698 /* End if told to either by the message or the processing result */ 664 699 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP)) 665 700 return; … … 672 707 int main(int argc, char *argv[]) 673 708 { 674 ERROR_DECLARE; 675 676 if (ERROR_OCCURRED(net_module_start(net_client_connection))) { 677 fprintf(stderr, "%s: net_module_start error %i\n", NAME, ERROR_CODE); 678 return ERROR_CODE; 709 int rc; 710 711 rc = net_module_start(net_client_connection); 712 if (rc != EOK) { 713 fprintf(stderr, "%s: net_module_start error %i\n", NAME, rc); 714 return rc; 679 715 } 680 716
Note:
See TracChangeset
for help on using the changeset viewer.