bind.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* posix */
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <signal.h>
  11. /* socket extensions */
  12. #include <sys/uio.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <sys/un.h>
  16. /* plan 9 */
  17. #include "lib.h"
  18. #include "sys9.h"
  19. #include "priv.h"
  20. int
  21. bind(int fd, void *a, int alen)
  22. {
  23. int n, len, cfd;
  24. Rock *r;
  25. char msg[128];
  26. struct sockaddr_in *lip;
  27. /* assign the address */
  28. r = _sock_findrock(fd, 0);
  29. if(r == 0){
  30. errno = ENOTSOCK;
  31. return -1;
  32. }
  33. if(alen > sizeof(r->addr)){
  34. errno = ENAMETOOLONG;
  35. return -1;
  36. }
  37. memmove(&r->addr, a, alen);
  38. /* the rest is IP sepecific */
  39. if (r->domain != PF_INET)
  40. return 0;
  41. cfd = open(r->ctl, O_RDWR);
  42. if(cfd < 0){
  43. errno = EBADF;
  44. return -1;
  45. }
  46. lip = (struct sockaddr_in*)&r->addr;
  47. if(lip->sin_port > 0)
  48. sprintf(msg, "bind %d", ntohs(lip->sin_port));
  49. else
  50. sprintf(msg, "bind *");
  51. n = write(cfd, msg, strlen(msg));
  52. if(n < 0){
  53. errno = EOPNOTSUPP; /* Improve error reporting!!! */
  54. close(cfd);
  55. return -1;
  56. }
  57. close(cfd);
  58. if(lip->sin_port <= 0)
  59. _sock_ingetaddr(r, lip, &len, "local");
  60. return 0;
  61. }