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));
70 if (chmod(usock_fn, mode) < 0) {
71 LOG(log_error, logtype_cnid, "error changing permissions for %s: %s",
72 usock_fn, strerror(errno));
83 int tsockfd_create(char *host, char *port, int backlog)
85 int sockfd, flag, ret;
86 struct addrinfo hints, *servinfo, *p;
88 /* Prepare hint for getaddrinfo */
89 memset(&hints, 0, sizeof hints);
90 hints.ai_family = AF_UNSPEC;
91 hints.ai_socktype = SOCK_STREAM;
93 if ((ret = getaddrinfo(host, port, &hints, &servinfo)) != 0) {
94 LOG(log_error, logtype_default, "tsockfd_create: getaddrinfo: %s\n", gai_strerror(ret));
99 /* loop through all the results and bind to the first we can */
100 for (p = servinfo; p != NULL; p = p->ai_next) {
101 if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
102 LOG(log_info, logtype_default, "tsockfd_create: socket: %s", strerror(errno));
107 * Set some socket options:
108 * SO_REUSEADDR deals w/ quick close/opens
109 * TCP_NODELAY diables Nagle
113 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
116 #ifdef USE_TCP_NODELAY
118 #define SOL_TCP IPPROTO_TCP
121 setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
122 #endif /* USE_TCP_NODELAY */
124 if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
126 LOG(log_info, logtype_default, "tsockfd_create: bind: %s\n", strerror(errno));
130 if (listen(sockfd, backlog) < 0) {
132 LOG(log_info, logtype_default, "tsockfd_create: listen: %s\n", strerror(errno));
136 /* We got a socket */
141 LOG(log_error, logtype_default, "tsockfd_create: no suitable network config %s:%s", host, port);
142 freeaddrinfo(servinfo);
146 freeaddrinfo(servinfo);
150 /* --------------------- */
151 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",
181 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
182 LOG(log_error, logtype_cnid, "set SO_RCVTIMEO: %s", strerror(errno));