int owl_zephyr_shutdown(void) { #ifdef HAVE_LIBZEPHYR if(owl_global_is_havezephyr(&g)) { unsuball(); ZClosePort(); } #endif return 0; }
Code_t ZOpenPort(u_short *port) { struct sockaddr_in bindin; unsigned int len; int val = 1; (void) ZClosePort(); if ((__Zephyr_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { __Zephyr_fd = -1; return errno; } bindin.sin_family = AF_INET; if (port && *port) { bindin.sin_port = *port; if (setsockopt(__Zephyr_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val) < 0) return errno; } else { bindin.sin_port = 0; } bindin.sin_addr.s_addr = INADDR_ANY; if (bind(__Zephyr_fd, (struct sockaddr *)&bindin, sizeof(bindin)) < 0) { if (errno == EADDRINUSE && port && *port) return ZERR_PORTINUSE; else return errno; } if (port && *port) { /* turn SO_REUSEADDR back off so no one else can steal it */ val = 0; if (setsockopt(__Zephyr_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val) < 0) return errno; } if (!bindin.sin_port) { len = sizeof(bindin); if (getsockname(__Zephyr_fd, (struct sockaddr *)&bindin, &len)) return errno; } __Zephyr_port = bindin.sin_port; __Zephyr_open = 1; if (port) *port = bindin.sin_port; return ZERR_NONE; }
void owl_zephyr_finish_initialization(const owl_io_dispatch *d, void *data) { Code_t code; char *perl; GSource *event_source; owl_select_remove_io_dispatch(d); ZClosePort(); if ((code = ZInitialize()) != ZERR_NONE) { owl_function_error("Initializing Zephyr: %s", error_message(code)); return; } if ((code = ZOpenPort(NULL)) != ZERR_NONE) { owl_function_error("Initializing Zephyr: %s", error_message(code)); return; } event_source = owl_zephyr_event_source_new(ZGetFD()); g_source_attach(event_source, NULL); g_source_unref(event_source); owl_global_set_havezephyr(&g); if(g.load_initial_subs) { owl_zephyr_load_initial_subs(); } while(deferred_subs != NULL) { owl_sub_list *subs = deferred_subs->data; owl_function_debugmsg("Loading %d deferred subs.", subs->nsubs); owl_zephyr_loadsubs_helper(subs->subs, subs->nsubs); deferred_subs = g_list_delete_link(deferred_subs, deferred_subs); g_free(subs); } /* zlog in if we need to */ if (owl_global_is_startuplogin(&g)) { owl_function_debugmsg("startup: doing zlog in"); owl_zephyr_zlog_in(); } /* check pseudo-logins if we need to */ if (owl_global_is_pseudologins(&g)) { owl_function_debugmsg("startup: checking pseudo-logins"); owl_function_zephyr_buddy_check(0); } perl = owl_perlconfig_execute("BarnOwl::Zephyr::_zephyr_startup()"); g_free(perl); }
void owl_zephyr_finish_initialization(owl_dispatch *d) { Code_t code; owl_select_remove_dispatch(d->fd); ZClosePort(); if ((code = ZInitialize()) != ZERR_NONE) { owl_function_error("Initializing Zephyr: %s", error_message(code)); return; } if ((code = ZOpenPort(NULL)) != ZERR_NONE) { owl_function_error("Initializing Zephyr: %s", error_message(code)); return; } d = owl_malloc(sizeof(owl_dispatch)); d->fd = ZGetFD(); d->cfunc = &owl_zephyr_process_events; d->destroy = NULL; owl_select_add_dispatch(d); owl_global_set_havezephyr(&g); if(g.load_initial_subs) { owl_zephyr_load_initial_subs(); } while(deferred_subs != NULL) { owl_sub_list *subs = deferred_subs->data; owl_function_debugmsg("Loading %d deferred subs.", subs->nsubs); owl_zephyr_loadsubs_helper(subs->subs, subs->nsubs); deferred_subs = g_list_delete_link(deferred_subs, deferred_subs); owl_free(subs); } /* zlog in if we need to */ if (owl_global_is_startuplogin(&g)) { owl_function_debugmsg("startup: doing zlog in"); owl_zephyr_zlog_in(); } }
Code_t ZInitialize() { struct servent *hmserv; struct hostent *hostent; char addr[4], hostname[MAXHOSTNAMELEN]; struct in_addr servaddr; struct sockaddr_in sin; int s; socklen_t sinsize = sizeof(sin); Code_t code; ZNotice_t notice; #ifdef ZEPHYR_USES_KERBEROS char *krealm = NULL; int krbval; char d1[ANAME_SZ], d2[INST_SZ]; /* initialize_krb_error_table(); */ #endif initialize_zeph_error_table(); (void) memset((char *)&__HM_addr, 0, sizeof(__HM_addr)); __HM_addr.sin_family = AF_INET; /* Set up local loopback address for HostManager */ addr[0] = 127; addr[1] = 0; addr[2] = 0; addr[3] = 1; hmserv = (struct servent *)getservbyname(HM_SVCNAME, "udp"); __HM_addr.sin_port = (hmserv) ? hmserv->s_port : HM_SVC_FALLBACK; (void) memcpy((char *)&__HM_addr.sin_addr, addr, 4); __HM_set = 0; /* Initialize the input queue */ __Q_Tail = NULL; __Q_Head = NULL; /* if the application is a server, there might not be a zhm. The code will fall back to something which might not be "right", but this is is ok, since none of the servers call krb_rd_req. */ servaddr.s_addr = INADDR_NONE; if (! __Zephyr_server) { if ((code = ZOpenPort(NULL)) != ZERR_NONE) return(code); if ((code = ZhmStat(NULL, ¬ice)) != ZERR_NONE) return(code); ZClosePort(); /* the first field, which is NUL-terminated, is the server name. If this code ever support a multiplexing zhm, this will have to be made smarter, and probably per-message */ #ifdef ZEPHYR_USES_KERBEROS krealm = krb_realmofhost(notice.z_message); #endif hostent = gethostbyname(notice.z_message); if (hostent && hostent->h_addrtype == AF_INET) memcpy(&servaddr, hostent->h_addr, sizeof(servaddr)); ZFreeNotice(¬ice); } #ifdef ZEPHYR_USES_KERBEROS if (krealm) { g_strlcpy(__Zephyr_realm, krealm, REALM_SZ); } else if ((krb_get_tf_fullname(TKT_FILE, d1, d2, __Zephyr_realm) != KSUCCESS) && ((krbval = krb_get_lrealm(__Zephyr_realm, 1)) != KSUCCESS)) { return (krbval); } #else g_strlcpy(__Zephyr_realm, "local-realm", REALM_SZ); #endif __My_addr.s_addr = INADDR_NONE; if (servaddr.s_addr != INADDR_NONE) { /* Try to get the local interface address by connecting a UDP * socket to the server address and getting the local address. * Some broken operating systems (e.g. Solaris 2.0-2.5) yield * INADDR_ANY (zero), so we have to check for that. */ s = socket(AF_INET, SOCK_DGRAM, 0); if (s != -1) { memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; memcpy(&sin.sin_addr, &servaddr, sizeof(servaddr)); sin.sin_port = HM_SRV_SVC_FALLBACK; if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == 0 && getsockname(s, (struct sockaddr *) &sin, &sinsize) == 0 && sin.sin_addr.s_addr != 0) memcpy(&__My_addr, &sin.sin_addr, sizeof(__My_addr)); close(s); } } if (__My_addr.s_addr == INADDR_NONE) { /* We couldn't figure out the local interface address by the * above method. Try by resolving the local hostname. (This * is a pretty broken thing to do, and unfortunately what we * always do on server machines.) */ if (gethostname(hostname, sizeof(hostname)) == 0) { hostent = gethostbyname(hostname); if (hostent && hostent->h_addrtype == AF_INET) memcpy(&__My_addr, hostent->h_addr, sizeof(__My_addr)); } } /* If the above methods failed, zero out __My_addr so things will * sort of kind of work. */ if (__My_addr.s_addr == INADDR_NONE) __My_addr.s_addr = 0; /* Get the sender so we can cache it */ (void) ZGetSender(); return (ZERR_NONE); }