2 * $Id: usockfd.c,v 1.6 2009-11-05 14:38:07 franklahm Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
10 #endif /* HAVE_CONFIG_H */
18 #endif /* HAVE_UNISTD_H */
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif /* HAVE_SYS_TYPES_H */
28 #ifdef HAVE_SYS_TIME_H
30 #endif /* HAVE_SYS_TIME_H */
33 #include <atalk/logger.h>
36 #include <sys/select.h>
38 int usockfd_create(char *usock_fn, mode_t mode, int backlog)
41 struct sockaddr_un addr;
44 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
45 LOG(log_error, logtype_cnid, "error in socket call: %s",
50 if (unlink(usock_fn) < 0 && errno != ENOENT) {
51 LOG(log_error, logtype_cnid, "error unlinking unix socket file %s: %s",
52 usock_fn, strerror(errno));
55 memset((char *) &addr, 0, sizeof(struct sockaddr_un));
56 addr.sun_family = AF_UNIX;
57 strncpy(addr.sun_path, usock_fn, sizeof(addr.sun_path) - 1);
58 if (bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) < 0) {
59 LOG(log_error, logtype_cnid, "error binding to socket for %s: %s",
60 usock_fn, strerror(errno));
64 if (listen(sockfd, backlog) < 0) {
65 LOG(log_error, logtype_cnid, "error in listen for %s: %s",
66 usock_fn, strerror(errno));
73 if (chmod(usock_fn, mode) < 0) {
74 LOG(log_error, logtype_cnid, "error changing permissions for %s: %s",
75 usock_fn, strerror(errno));
86 int tsockfd_create(char *host, char *port, int backlog)
88 int sockfd, flag, ret;
89 struct addrinfo hints, *servinfo, *p;
91 /* Prepare hint for getaddrinfo */
92 memset(&hints, 0, sizeof hints);
93 hints.ai_family = AF_UNSPEC;
94 hints.ai_socktype = SOCK_STREAM;
96 if ((ret = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
97 LOG(log_error, logtype_default, "tsockfd_create: getaddrinfo: %s\n", gai_strerror(ret));
101 /* create a socket */
102 /* loop through all the results and bind to the first we can */
103 for (p = servinfo; p != NULL; p = p->ai_next) {
104 if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
105 LOG(log_info, logtype_default, "tsockfd_create: socket: %s", strerror(errno));
110 * Set some socket options:
111 * SO_REUSEADDR deals w/ quick close/opens
112 * TCP_NODELAY diables Nagle
116 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
119 #ifdef USE_TCP_NODELAY
121 #define SOL_TCP IPPROTO_TCP
124 setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
125 #endif /* USE_TCP_NODELAY */
127 if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
129 LOG(log_info, logtype_default, "tsockfd_create: bind: %s\n", strerror(errno));
133 if (listen(sockfd, backlog) < 0) {
135 LOG(log_info, logtype_default, "tsockfd_create: listen: %s\n", strerror(errno));
139 /* We got a socket */
144 LOG(log_error, logtype_default, "tsockfd_create: no suitable network config %s:%s", host, port);
145 freeaddrinfo(servinfo);
149 freeaddrinfo(servinfo);
153 /* --------------------- */
154 int usockfd_check(int sockfd, const sigset_t *sigset)
162 FD_SET(sockfd, &readfds);
164 if ((ret = pselect(sockfd + 1, &readfds, NULL, NULL, NULL, sigset)) < 0) {
167 LOG(log_error, logtype_cnid, "error in select: %s",
174 if ((fd = accept(sockfd, NULL, &size)) < 0) {
177 LOG(log_error, logtype_cnid, "error in accept: %s",