database_server.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Write a network server that stores name-value pairs. The server should allow names
  3. to be added, deleted, modified, and retrieved by clients. Write one or more client
  4. programs to test the server. Optionally, implement some kind of security
  5. mechanism that allows only the client that created the name to delete it or to
  6. modify the value associated with it.
  7. */
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <stdbool.h>
  12. #include <sys/socket.h>
  13. #include <netdb.h>
  14. #include <unistd.h>
  15. static int
  16. inet_connect(const char *host, const char *service, int type)
  17. {
  18. struct addrinfo hint;
  19. memset(&hint, 0, sizeof hint);
  20. hint.ai_family = AF_UNSPEC;
  21. hint.ai_socktype = type;
  22. struct addrinfo *result, *rp;
  23. int ret = getaddrinfo(host, service, &hint, &result);
  24. if (ret != 0)
  25. {
  26. errno = ENOSYS;
  27. return -1;
  28. }
  29. int sfd;
  30. for (rp = result; rp != NULL; rp = rp->ai_next)
  31. {
  32. sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  33. if (sfd == -1) continue;
  34. if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) break;
  35. close(sfd);
  36. }
  37. freeaddrinfo(result);
  38. return (rp == NULL) ? -1 : sfd;
  39. }
  40. static int
  41. inet_passive_socket(const char *service, int type, socklen_t *addrlen,
  42. bool listen, int backlog)
  43. {
  44. struct addrinfo hint;
  45. memset(&hint, 0, sizeof hint);
  46. hint.ai_family = AF_UNSPEC;
  47. hint.ai_socktype = type;
  48. hint.ai_flags = AI_PASSIVE;
  49. struct addrinfo *result, *rp;
  50. int ret = getaddrinfo(NULL, service, &hint, &result);
  51. if (ret != 0)
  52. return -1;
  53. }
  54. static int
  55. inet_listen(const char *service, int backlog, socklen_t *addrlen)
  56. {
  57. }
  58. static int
  59. inet_bind(const char *service, int type, socklen_t *addrlen)
  60. {
  61. }
  62. static char *
  63. inet_address_str(const struct sockaddr *addr, socklen_t addrlen,
  64. char *addrstr, int addrstrlen)
  65. {
  66. }
  67. int
  68. main(void)
  69. {
  70. return EXIT_SUCCESS;
  71. }