"Fossies" - the Fresh Open Source Software Archive

Member "links-1.03/af_unix.c" (11 Sep 2006, 3279 Bytes) of archive /linux/www/links-1.03.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "af_unix.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.8_vs_1.03.

    1 #include "links.h"
    2 
    3 #ifdef DONT_USE_AF_UNIX
    4 
    5 int bind_to_af_unix()
    6 {
    7     return -1;
    8 }
    9 
   10 void af_unix_close()
   11 {
   12 }
   13 
   14 #else
   15 
   16 #ifdef USE_AF_UNIX
   17 #include <sys/un.h>
   18 #endif
   19 
   20 void af_unix_connection(void *);
   21 
   22 struct sockaddr *s_unix_acc = NULL;
   23 struct sockaddr *s_unix = NULL;
   24 socklen_t s_unix_l;
   25 int s_unix_fd = -1;
   26 
   27 #ifdef USE_AF_UNIX
   28 
   29 int get_address()
   30 {
   31     struct sockaddr_un *su;
   32     unsigned char *path;
   33     if (!links_home) return -1;
   34     path = stracpy(links_home);
   35     su = mem_alloc(sizeof(struct sockaddr_un) + strlen(path) + 1);
   36     s_unix_acc = mem_alloc(sizeof(struct sockaddr_un) + strlen(path) + 1);
   37     memset(su, 0, sizeof(struct sockaddr_un) + strlen(path) + 1);
   38     su->sun_family = AF_UNIX;
   39     add_to_strn(&path, LINKS_SOCK_NAME);
   40     strcpy(su->sun_path, path);
   41     mem_free(path);
   42     s_unix = (struct sockaddr *)su;
   43     s_unix_l = (char *)&su->sun_path - (char *)su + strlen(su->sun_path) + 1;
   44     return PF_UNIX;
   45 }
   46 
   47 void unlink_unix()
   48 {
   49     if (unlink(((struct sockaddr_un *)s_unix)->sun_path)) {
   50         /*perror("unlink");
   51         debug("unlink: %s", ((struct sockaddr_un *)s_unix)->sun_path);*/
   52     }
   53 }
   54 
   55 #else
   56 
   57 int get_address()
   58 {
   59     struct sockaddr_in *sin;
   60     sin = mem_alloc(sizeof(struct sockaddr_in));
   61     s_unix_acc = mem_alloc(sizeof(struct sockaddr_in));
   62     memset(sin, 0, sizeof(struct sockaddr_in));
   63     sin->sin_family = AF_INET;
   64     sin->sin_port = LINKS_PORT;
   65     sin->sin_addr.s_addr = htonl(0x7f000001);
   66     s_unix = (struct sockaddr *)sin;
   67     s_unix_l = sizeof(struct sockaddr_in);
   68     return PF_INET;
   69 }
   70 
   71 void unlink_unix()
   72 {
   73 }
   74 
   75 #endif
   76 
   77 int bind_to_af_unix()
   78 {
   79     int u = 0;
   80     int a1 = 1;
   81     int cnt = 0;
   82     int af;
   83     if ((af = get_address()) == -1) return -1;
   84     again:
   85     if ((s_unix_fd = socket(af, SOCK_STREAM, 0)) == -1) return -1;
   86 #if defined(SOL_SOCKET) && defined(SO_REUSEADDR)
   87     setsockopt(s_unix_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&a1, sizeof a1);
   88 #endif
   89     if (bind(s_unix_fd, s_unix, s_unix_l)) {
   90         /*perror("");
   91         debug("bind: %d", errno);*/
   92         close(s_unix_fd);
   93         if ((s_unix_fd = socket(af, SOCK_STREAM, 0)) == -1) return -1;
   94 #if defined(SOL_SOCKET) && defined(SO_REUSEADDR)
   95         setsockopt(s_unix_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&a1, sizeof a1);
   96 #endif
   97         if (connect(s_unix_fd, s_unix, s_unix_l)) {
   98             /*perror("");
   99             debug("connect: %d", errno);*/
  100             if (++cnt < MAX_BIND_TRIES) {
  101                 struct timeval tv = { 0, 100000 };
  102                 fd_set dummy;
  103                 FD_ZERO(&dummy);
  104                 select(0, &dummy, &dummy, &dummy, &tv);
  105                 close(s_unix_fd);
  106                 goto again;
  107             }
  108             close(s_unix_fd), s_unix_fd = -1;
  109             if (!u) {
  110                 unlink_unix();
  111                 u = 1;
  112                 goto again;
  113             }
  114             mem_free(s_unix), s_unix = NULL;
  115             return -1;
  116         }
  117         mem_free(s_unix), s_unix = NULL;
  118         return s_unix_fd;
  119     }
  120     if (listen(s_unix_fd, 100)) {
  121         error("ERROR: listen failed: %d", errno);
  122         mem_free(s_unix), s_unix = NULL;
  123         close(s_unix_fd), s_unix_fd = -1;
  124         return -1;
  125     }
  126     set_handlers(s_unix_fd, af_unix_connection, NULL, NULL, NULL);
  127     return -1;
  128 }
  129 
  130 void af_unix_connection(void *xxx)
  131 {
  132     socklen_t l = s_unix_l;
  133     int ns;
  134     memset(s_unix_acc, 0, l);
  135     ns = accept(s_unix_fd, (struct sockaddr *)s_unix_acc, &l);
  136     if (ns == -1) return;
  137     init_term(ns, ns, win_func);
  138     set_highpri();
  139 }
  140 
  141 void af_unix_close()
  142 {
  143     if (s_unix_fd != -1) close(s_unix_fd);
  144     if (s_unix) unlink_unix(), mem_free(s_unix), s_unix = NULL;
  145     if (s_unix_acc) mem_free(s_unix_acc), s_unix_acc = NULL;
  146 }
  147 
  148 #endif