* Receive a control message */
| 657 | * Receive a control message |
| 658 | */ |
| 659 | static int |
| 660 | ng_ksocket_rcvmsg(node_p node, item_p item, hook_p lasthook) |
| 661 | { |
| 662 | struct thread *td = curthread; /* XXX broken */ |
| 663 | const priv_p priv = NG_NODE_PRIVATE(node); |
| 664 | struct socket *const so = priv->so; |
| 665 | struct ng_mesg *resp = NULL; |
| 666 | int error = 0; |
| 667 | struct ng_mesg *msg; |
| 668 | ng_ID_t raddr; |
| 669 | |
| 670 | NGI_GET_MSG(item, msg); |
| 671 | switch (msg->header.typecookie) { |
| 672 | case NGM_KSOCKET_COOKIE: |
| 673 | switch (msg->header.cmd) { |
| 674 | case NGM_KSOCKET_BIND: |
| 675 | { |
| 676 | struct sockaddr *const sa |
| 677 | = (struct sockaddr *)msg->data; |
| 678 | |
| 679 | /* Sanity check */ |
| 680 | if (msg->header.arglen < SADATA_OFFSET |
| 681 | || msg->header.arglen < sa->sa_len) |
| 682 | ERROUT(EINVAL); |
| 683 | if (so == NULL) |
| 684 | ERROUT(ENXIO); |
| 685 | |
| 686 | /* Bind */ |
| 687 | error = sobind(so, sa, td); |
| 688 | break; |
| 689 | } |
| 690 | case NGM_KSOCKET_LISTEN: |
| 691 | { |
| 692 | /* Sanity check */ |
| 693 | if (msg->header.arglen != sizeof(int32_t)) |
| 694 | ERROUT(EINVAL); |
| 695 | if (so == NULL) |
| 696 | ERROUT(ENXIO); |
| 697 | |
| 698 | /* Listen */ |
| 699 | so->so_state |= SS_NBIO; |
| 700 | error = solisten(so, *((int32_t *)msg->data), td); |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | case NGM_KSOCKET_ACCEPT: |
| 705 | { |
| 706 | /* Sanity check */ |
| 707 | if (msg->header.arglen != 0) |
| 708 | ERROUT(EINVAL); |
| 709 | if (so == NULL) |
| 710 | ERROUT(ENXIO); |
| 711 | |
| 712 | /* Make sure the socket is capable of accepting */ |
| 713 | if (!(so->so_options & SO_ACCEPTCONN)) |
| 714 | ERROUT(EINVAL); |
| 715 | if (priv->flags & KSF_ACCEPTING) |
| 716 | ERROUT(EALREADY); |