2 * Copyright (C) Joerg Lenneis 2003
3 * All Rights Reserved. See COPYING.
8 #endif /* HAVE_CONFIG_H */
16 #endif /* HAVE_UNISTD_H */
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif /* HAVE_SYS_TYPES_H */
26 #ifdef HAVE_SYS_TIME_H
28 #endif /* HAVE_SYS_TIME_H */
31 #include <atalk/logger.h>
34 #include <sys/select.h>
36 int usockfd_create(char *usock_fn, mode_t mode, int backlog)
39 struct sockaddr_un addr;
42 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
43 LOG(log_error, logtype_cnid, "error in socket call: %s",
48 if (unlink(usock_fn) < 0 && errno != ENOENT) {
49 LOG(log_error, logtype_cnid, "error unlinking unix socket file %s: %s",
50 usock_fn, strerror(errno));
53 memset((char *) &addr, 0, sizeof(struct sockaddr_un));
54 addr.sun_family = AF_UNIX;
55 strncpy(addr.sun_path, usock_fn, sizeof(addr.sun_path) - 1);
56 if (bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
57 LOG(log_error, logtype_cnid, "error binding to socket for %s: %s",
58 usock_fn, strerror(errno));
62 if (listen(sockfd, backlog) < 0) {
63 LOG(log_error, logtype_cnid, "error in listen for %s: %s",
64 usock_fn, strerror(errno));
71 if (chmod(usock_fn, mode) < 0) {
72 LOG(log_error, logtype_cnid, "error changing permissions for %s: %s",
73 usock_fn, strerror(errno));
84 int tsockfd_create(char *host, char *port, int backlog)
86 int sockfd, flag, ret;
87 struct addrinfo hints, *servinfo, *p;
89 /* Prepare hint for getaddrinfo */
90 memset(&hints, 0, sizeof hints);
91 hints.ai_family = AF_UNSPEC;
92 hints.ai_socktype = SOCK_STREAM;
94 if ((ret = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
95 LOG(log_error, logtype_default, "tsockfd_create: getaddrinfo: %s\n", gai_strerror(ret));
100 /* loop through all the results and bind to the first we can */
101 for (p = servinfo; p != NULL; p = p->ai_next) {
102 if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
103 LOG(log_info, logtype_default, "tsockfd_create: socket: %s", strerror(errno));
108 * Set some socket options:
109 * SO_REUSEADDR deals w/ quick close/opens
110 * TCP_NODELAY diables Nagle
114 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
117 #ifdef USE_TCP_NODELAY
119 #define SOL_TCP IPPROTO_TCP
122 setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
123 #endif /* USE_TCP_NODELAY */
125 if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
127 LOG(log_info, logtype_default, "tsockfd_create: bind: %s\n", strerror(errno));
131 if (listen(sockfd, backlog) < 0) {
133 LOG(log_info, logtype_default, "tsockfd_create: listen: %s\n", strerror(errno));
137 /* We got a socket */
142 LOG(log_error, logtype_default, "tsockfd_create: no suitable network config %s:%s", host, port);
143 freeaddrinfo(servinfo);
147 freeaddrinfo(servinfo);
151 /* --------------------- */
152 int usockfd_check(int sockfd, const sigset_t *sigset)
160 FD_SET(sockfd, &readfds);
162 if ((ret = pselect(sockfd + 1, &readfds, NULL, NULL, NULL, sigset)) < 0) {
165 LOG(log_error, logtype_cnid, "error in select: %s",
172 if ((fd = accept(sockfd, NULL, &size)) < 0) {
175 LOG(log_error, logtype_cnid, "error in accept: %s",