void start_server(int port, char *path) { if( use_unix_socket ) create_unix_socket(path); if( use_inet_socket ) create_inet_socket(port); }
void opensync_init(void) { /* created unix socket to listen on */ uxsock = create_unix_socket(); if (uxsock < 0) { g_print("failed to create unix socket for opensync\n"); return; } listen_channel = g_io_channel_unix_new(uxsock); listen_source_id = g_io_add_watch(listen_channel, G_IO_IN, listen_channel_input_cb, NULL); g_io_channel_unref(listen_channel); }
bool UnixSocketAdapter::init(const string& addr) { strncpy(sock_name,addr.c_str(),UNIX_PATH_MAX-1); ::unlink(sock_name); fd = create_unix_socket(sock_name); if(fd == -1){ ERROR("could not open unix socket '%s'\n",sock_name); return false; } DBG("UnixSocketAdapter::init @ %s\n", sock_name); close_fd = true; return true; }
int main(int argc, char **argv) { if(argc < 2) { fprintf(stderr, "Insufficient arguements provided\n"); return -1; } int sockfd = 0, ret = 0; if((sockfd = create_unix_socket()) < 0) { fprintf(stderr, "socket(%d) error => %m\n", sockfd); return -1; } fprintf(stdout, "Socket created => %d \n", sockfd); if(chk_unlink_and_bind(sockfd, argv[1]) < 0) { ret = -1; goto done; } struct sockaddr_un unix_socket_temp; memset((void*)&unix_socket_temp, 0, sizeof(struct sockaddr_un)); socklen_t len = sizeof(struct sockaddr_un); if(getsockname(sockfd, (struct sockaddr *)&unix_socket_temp, &len) < 0) { fprintf(stderr, "getsockname() error => %m\n"); ret = -1; goto done; } fprintf(stdout, "Socket bound successfully to => %s\n", unix_socket_temp.sun_path); if(listen(sockfd, LISTENQ) < 0) { fprintf(stderr, "listen(%d) error => %m\n", sockfd); ret = -1; goto done; } if(relay_server(sockfd) < 0) { ret = -1; goto done; } done: close(sockfd); return ret; }
Socket_T socket_create_t(const char *host, int port, int type, Ssl_T ssl, int timeout) { int s; int proto= type==SOCKET_UDP?SOCK_DGRAM:SOCK_STREAM; ASSERT(host); ASSERT((type==SOCKET_UDP)||(type==SOCKET_TCP)); if(ssl.use_ssl) { ASSERT(type==SOCKET_TCP); } ASSERT(timeout>0); if ((host) && (strlen(host) >0) && (host[0] == '/' || host[0] == '@')) { if((s= create_unix_socket(host, timeout)) != -1) { Socket_T S= NULL; NEW(S); S->socket= s; S->length= 0; S->offset= 0; S->port= port; S->type= proto; S->timeout= timeout; S->host= xstrdup(host); S->connection_type= TYPE_LOCAL; if(ssl.use_ssl && !socket_switch2ssl(S, ssl)) { socket_free(&S); return NULL; } return S; } return NULL; } if((s= create_socket(host, port, proto, timeout)) != -1) { Socket_T S= NULL; NEW(S); S->socket= s; S->length= 0; S->offset= 0; S->port= port; S->type= proto; S->timeout= timeout; S->host= xstrdup(host); S->connection_type= TYPE_LOCAL; if(ssl.use_ssl && !socket_switch2ssl(S, ssl)) { socket_free(&S); return NULL; } return S; } return NULL; }