static void logger_open(struct logger_ctl *ctl) { if (ctl->server) { ctl->fd = inet_socket(ctl->server, ctl->port, ctl->socket_type); if (!ctl->syslogfp) ctl->syslogfp = syslog_rfc5424; return; } if (ctl->unix_socket) { ctl->fd = unix_socket(ctl->unix_socket, ctl->socket_type); if (!ctl->syslogfp) ctl->syslogfp = syslog_rfc5424; return; } ctl->fd = unix_socket("/dev/log", ctl->socket_type); ctl->syslogfp = syslog_local; }
OnelabNetworkClient::OnelabNetworkClient(std::string name, const char *sockname) : VirtualClient(name) { _ip.address = 0; _ip.port = 0; _connected = false; _sockname = std::string(sockname); _fds = unix_socket(SOCK_STREAM); }
int main(int argc,char* argv[]) { int usfd=unix_socket("soc.socket"); int sfd=inet_socket(atoi(argv[1])); sockaddr_in cliaddr; sockaddr_un cliuaddr; socklen_t clen,cen; int pid; int nsfd,nusfd; int flag=1; while(1) { //sleep(2); printf("accepting\n"); nsfd=accept(sfd,NULL,NULL); if(flag==1) { flag=0; pid=fork(); if(pid==0) { close(sfd); close(usfd); execlp("./s1","./s1",(char*)0); } else { nusfd=accept(usfd,NULL,NULL); printf("nusfd%d\n",nusfd); sendfd(nusfd,nsfd); close(nsfd); } } else { sendfd(nusfd,nsfd); close(nsfd); } } }
static void lmtp_open(struct deliver *deliver) { FILE *fp; char *buf = NULL, hn[HOST_NAME_MAX + 1], *rcpt = deliver->to, *to = deliver->to; size_t sz = 0; ssize_t len; int s; strsep(&rcpt, " "); s = (to[0] == '/') ? unix_socket(to) : inet_socket(to); if ((fp = fdopen(s, "r+")) == NULL) err(1, "fdopen"); if (lmtp_banner(&buf, &sz, '2', fp) != 0) errx(1, "Invalid LHLO reply: %s", buf); if (gethostname(hn, sizeof hn) == -1) err(1, "gethostname"); if (lmtp_cmd(&buf, &sz, '2', fp, "LHLO %s", hn) != 0) errx(1, "Invalid LHLO reply: %s", buf); if (lmtp_cmd(&buf, &sz, '2', fp, "MAIL FROM:<%s>", deliver->from) != 0) errx(1, "MAIL FROM rejected: %s", buf); if (lmtp_cmd(&buf, &sz, '2', fp, "RCPT TO:<%s>", rcpt ? deliver->dest : deliver->user) != 0) errx(1, "RCPT TO rejected: %s", buf); if (lmtp_cmd(&buf, &sz, '3', fp, "DATA") != 0) errx(1, "Invalid DATA reply: %s", buf); while ((len = getline(&buf, &sz, stdin)) != -1) { if (buf[len - 1] == '\n') buf[len - 1] = '\0'; if (fprintf(fp, "%s%s\r\n", buf[0] == '.' ? "." : "", buf) < 0) errx(1, "fprintf failed"); } if (lmtp_cmd(&buf, &sz, '2', fp, ".") != 0) errx(1, "Delivery error: %s", buf); if (lmtp_cmd(&buf, &sz, '2', fp, "QUIT") != 0) errx(1, "Error on QUIT: %s", buf); exit(0); }
int main(int argc,char* argv[]) { n=0;limit=1; pthread_mutex_init(&mut,NULL); int sfd=inet_socket(atoi(argv[1])); int usfd1,usfd2; usfd1=unix_socket("soc2");//serv sock int nusfd=accept(usfd1,NULL,NULL); usfd2=socket(AF_UNIX,SOCK_STREAM,0); //cli sock struct sockaddr_un servaddr; servaddr.sun_family=AF_UNIX; strcpy(servaddr.sun_path,"soc3"); int len=sizeof(sa_family_t)+strlen(servaddr.sun_path); int re=connect(usfd2,(struct sockaddr*)&servaddr,len); printf("res %d\n",re); // sleep(2); pthread_t t; fds f; f.usfd1=nusfd; f.usfd2=usfd2; pthread_create(&t,NULL,fdreceiver,&f); while(1) { int nsfd=accept(sfd,NULL,NULL); pthread_mutex_lock(&mut); printf("one entering mall 2\n"); n++; pthread_mutex_unlock(&mut); if(n>limit) { printf("m2 full\n"); sendfd(usfd2,nsfd); close(nsfd); } else { data td; td.nsfd=nsfd; pthread_create(&t,NULL,&service,&td); } } }
int main() { int id=atoi(argv[1]); limit=atoi(argv[2]); int sfd=inet_socket(atoi(argv[3])); int usfd1,usfd2; if(id>1) usfd1=unix_socket(argv[4]);//serv sock if(id==1) { usfd2=socket(AF_UNIX,SOCK_STREAM,0); struct sockaddr_un servaddr; servaddr.sun_family=AF_UNIX; strcpy(servaddr.sun_path,argv[5]); int len=sizeof(sa_familt_t)+strlen(servaddr.sun_path); int re=connect(usfd2,(struct sockaddr*)&servaddr,len); printf("res %d\n",re); } nusfd=accept(usfd1,NULL,NULL); pthread_create(&t,NULL,fdreceiver,&usfd); while(1) { nsfd=accept(nsfd,NULL,NULL); n++; if(n>=limit) { sendfd(nusfd,nsfd); close(nsfd); } if(id==3 && n==limit) { kill(getppid(),SIGUSR1); } else { sendfd(usfd,nsfd); } } }
static void logger_open(struct logger_ctl *ctl) { if (ctl->server) { ctl->fd = inet_socket(ctl->server, ctl->port, &ctl->socket_type); if (!ctl->syslogfp) ctl->syslogfp = syslog_rfc5424_header; } else { if (!ctl->unix_socket) ctl->unix_socket = _PATH_DEVLOG; ctl->fd = unix_socket(ctl, ctl->unix_socket, &ctl->socket_type); if (!ctl->syslogfp) ctl->syslogfp = syslog_local_header; } if (!ctl->tag) ctl->tag = xgetlogin(); generate_syslog_header(ctl); }
static void delivery_lmtp_open(struct deliver *deliver) { char *buffer, *lbuf, *rcpt = deliver->to; char lhloname[255]; int s; FILE *fp = NULL; enum lmtp_state state = LMTP_BANNER; size_t len; strsep(&rcpt, " "); if (deliver->to[0] == '/') s = unix_socket(deliver->to); else s = inet_socket(deliver->to); if (s == -1 || (fp = fdopen(s, "r+")) == NULL) err(1, "couldn't establish connection"); while (!feof(fp) && !ferror(fp) && state != LMTP_BYE) { buffer = lmtp_getline(fp); if (buffer == NULL) errx(1, "No input received"); switch (state) { case LMTP_BANNER: if (strncmp("220 ", buffer, 4) != 0) errx(1, "Invalid LMTP greeting: %s\n", buffer); gethostname(lhloname, sizeof lhloname ); fprintf(fp, "LHLO %s\r\n", lhloname); state = LMTP_LHLO; break; case LMTP_LHLO: if (buffer[0] != '2') errx(1, "LHLO rejected: %s\n", buffer); if (strlen(buffer) < 4) errx(1, "Invalid LMTP LHLO answer: %s\n", buffer); if (buffer[3] == '-') continue; /* multi-line */ fprintf(fp, "MAIL FROM:<%s>\r\n", deliver->from); state = LMTP_MAIL_FROM; break; case LMTP_MAIL_FROM: if (buffer[0] != '2') errx(1, "MAIL FROM rejected: %s\n", buffer); fprintf(fp, "RCPT TO:<%s>\r\n", rcpt ? deliver->dest : deliver->user); state = LMTP_RCPT_TO; break; case LMTP_RCPT_TO: if (buffer[0] != '2') errx(1, "RCPT TO rejected: %s\n", buffer); fprintf(fp, "DATA\r\n"); state = LMTP_DATA; break; case LMTP_DATA: if (buffer[0] != '3') errx(1, "DATA rejected: %s\n", buffer); lbuf = NULL; while ((buffer = fgetln(stdin, &len))) { if (buffer[len- 1] == '\n') buffer[len - 1] = '\0'; else { if ((lbuf = malloc(len + 1)) == NULL) err(1, NULL); memcpy(lbuf, buffer, len); lbuf[len] = '\0'; buffer = lbuf; } fprintf(fp, "%s%s\r\n", *buffer == '.' ? "." : "", buffer); } free(lbuf); fprintf(fp, ".\r\n"); state = LMTP_QUIT; break; case LMTP_QUIT: if (buffer[0] != '2') errx(1, "Delivery error: %s\n", buffer); fprintf(fp, "QUIT\r\n"); state = LMTP_BYE; break; default: errx(1, "Bogus state %d", state); } } _exit(0); }
/* * logger -- read and log utility * * Reads from an input and arranges to write the result on the system * log. */ int main(int argc, char **argv) { int ch, logflags, pri, prio_prefix; char *tag, buf[1024]; char *usock = NULL; char *server = NULL; char *port = NULL; int LogSock = -1, socket_type = ALL_TYPES; #ifdef HAVE_LIBSYSTEMD FILE *jfd = NULL; #endif static const struct option longopts[] = { { "id", no_argument, 0, 'i' }, { "stderr", no_argument, 0, 's' }, { "file", required_argument, 0, 'f' }, { "priority", required_argument, 0, 'p' }, { "tag", required_argument, 0, 't' }, { "socket", required_argument, 0, 'u' }, { "udp", no_argument, 0, 'd' }, { "tcp", no_argument, 0, 'T' }, { "server", required_argument, 0, 'n' }, { "port", required_argument, 0, 'P' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX }, #ifdef HAVE_LIBSYSTEMD { "journald", optional_argument, 0, OPT_JOURNALD }, #endif { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); tag = NULL; pri = LOG_NOTICE; logflags = 0; prio_prefix = 0; while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh", longopts, NULL)) != -1) { switch (ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(EXIT_FAILURE, _("file %s"), optarg); break; case 'i': /* log process id also */ logflags |= LOG_PID; break; case 'p': /* priority */ pri = pencode(optarg); break; case 's': /* log to standard error */ logflags |= LOG_PERROR; break; case 't': /* tag */ tag = optarg; break; case 'u': /* unix socket */ usock = optarg; break; case 'd': socket_type = TYPE_UDP; break; case 'T': socket_type = TYPE_TCP; break; case 'n': server = optarg; break; case 'P': port = optarg; break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); case OPT_PRIO_PREFIX: prio_prefix = 1; break; #ifdef HAVE_LIBSYSTEMD case OPT_JOURNALD: if (optarg) { jfd = fopen(optarg, "r"); if (!jfd) err(EXIT_FAILURE, _("cannot open %s"), optarg); } else jfd = stdin; break; #endif case '?': default: usage(stderr); } } argc -= optind; argv += optind; /* setup for logging */ #ifdef HAVE_LIBSYSTEMD if (jfd) { int ret = journald_entry(jfd); if (stdin != jfd) fclose(jfd); return ret ? EXIT_FAILURE : EXIT_SUCCESS; } #endif if (server) LogSock = inet_socket(server, port, socket_type); else if (usock) LogSock = unix_socket(usock, socket_type); else openlog(tag ? tag : getlogin(), logflags, 0); /* log input line if appropriate */ if (argc > 0) { register char *p, *endp; size_t len; for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { if (!usock && !server) syslog(pri, "%s", buf); else mysyslog(LogSock, logflags, pri, tag, buf); p = buf; } if (len > sizeof(buf) - 1) { if (!usock && !server) syslog(pri, "%s", *argv++); else mysyslog(LogSock, logflags, pri, tag, *argv++); } else { if (p != buf) *p++ = ' '; memmove(p, *argv++, len); *(p += len) = '\0'; } } if (p != buf) { if (!usock && !server) syslog(pri, "%s", buf); else mysyslog(LogSock, logflags, pri, tag, buf); } } else { char *msg; int default_priority = pri; while (fgets(buf, sizeof(buf), stdin) != NULL) { /* glibc is buggy and adds an additional newline, so we have to remove it here until glibc is fixed */ int len = strlen(buf); if (len > 0 && buf[len - 1] == '\n') buf[len - 1] = '\0'; msg = buf; pri = default_priority; if (prio_prefix && msg[0] == '<') msg = get_prio_prefix(msg, &pri); if (!usock && !server) syslog(pri, "%s", msg); else mysyslog(LogSock, logflags, pri, tag, msg); } } if (!usock && !server) closelog(); else close(LogSock); return EXIT_SUCCESS; }
int main(int argc,char* argv[]) { char buffer[100]; int n=0; struct sigaction s; s.sa_sigaction = &handler; sigemptyset(&s.sa_mask); s.sa_flags =SA_SIGINFO; sigaction (SIGUSR1, &s, NULL); //create platforms for(int i=0;i<3;i++) { pids[i]=fork(); if(pids[i]==0) { char buf[24]; sprintf(buf,"%d",32004+i); execlp("./pf","./pf",buf,(char*)NULL); } } //initially each platform is free for(int i=0;i<3;i++) { idle[i]=1; } int rsfd=raw_socket(143); printf("SM - rsfd:%d\n",rsfd); int sfd[3]; //accept unix domain socket conn from platforms int usfd=unix_socket("soc.socket"); for(int i=0;i<3;i++) { nusfd[i]=accept(usfd,NULL,NULL); printf("nusfd %d : %d\n",i,nusfd[i]); } //tracks for different directions are laid int p[3]={32001,32002,32003}; int maxfd=-1; struct sockaddr_in myaddr[3]; fd_set readset; FD_ZERO(&readset); for(int i=0;i<3;i++) { sfd[i]=inet_serv_socket(p[i]); if(maxfd<sfd[i]) { maxfd=sfd[i]; } } sockaddr_in cliaddr; socklen_t clen; int pid; int nsfd; int pos=-1; while(1) { //poll on tracks from differnt directions for incoming trains FD_ZERO(&readset); for(int i=0;i<3;i++) { if(sfd[i]!=-1) FD_SET(sfd[i],&readset); } select(maxfd+1,&readset,NULL,NULL,NULL); pos=-1; for(int i=0;i<3;i++) { if(FD_ISSET(sfd[i],&readset)) { pos=i; break; } } printf("all running\n"); //TODO:pass annoncements to all platforms //get free platform int r=getfree(); if(r>=0) { int nsfd=accept(sfd[pos],NULL,NULL); //send announcements to all platforms char buff[1000];char tmp[20]; struct sockaddr_in servaddr; servaddr.sin_family=AF_INET; servaddr.sin_port=htons(0); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); bzero(buff,1000); bzero(tmp,20); sprintf(tmp,"%d\n",r); strcpy(buff,"a train arriving at platform "); strcat(buff,tmp); sendto(rsfd,buff,1000,0,(struct sockaddr*)&servaddr,sizeof(servaddr)); //mark platform selected busy idle[r]=0; //pass fd to selected platform sendfd(nusfd[r],nsfd); close(nsfd); } } return 0; }
/*-------------------------------------------------------------------*/ int bind_device_ex (DEVBLK* dev, char* spec, ONCONNECT fn, void* arg ) { bind_struct* bs; int was_list_empty; if (!init_done) init_sockdev(); if (sysblk.shutdown) return 0; logdebug("bind_device (%4.4X, %s)\n", dev->devnum, spec); /* Error if device already bound */ if (dev->bs) { logmsg (_("HHCSD001E Device %4.4X already bound to socket %s\n"), dev->devnum, dev->bs->spec); return 0; /* (failure) */ } /* Create a new bind_struct entry */ bs = malloc(sizeof(bind_struct)); if (!bs) { logmsg (_("HHCSD002E bind_device malloc() failed for device %4.4X\n"), dev->devnum); return 0; /* (failure) */ } memset(bs,0,sizeof(bind_struct)); bs->fn = fn; bs->arg = arg; if (!(bs->spec = strdup(spec))) { logmsg (_("HHCSD003E bind_device strdup() failed for device %4.4X\n"), dev->devnum); free (bs); return 0; /* (failure) */ } /* Create a listening socket */ if (bs->spec[0] == '/') bs->sd = unix_socket (bs->spec); else bs->sd = inet_socket (bs->spec); if (bs->sd == -1) { /* (error message already issued) */ free( bs->spec ); free( bs ); return 0; /* (failure) */ } /* Chain device and bind_struct to each other */ dev->bs = bs; bs->dev = dev; /* Add the new entry to our list of bound devices and create the socket thread that will listen for connections (if it doesn't already exist) */ obtain_lock( &bind_lock ); was_list_empty = IsListEmpty( &bind_head ); InsertListTail( &bind_head, &bs->bind_link ); if ( was_list_empty ) { if ( create_thread( &sysblk.socktid, JOINABLE, socket_thread, NULL, "socket_thread" ) ) { logmsg( _( "HHCSD023E Cannot create socketdevice thread: errno=%d: %s\n" ), errno, strerror( errno ) ); RemoveListEntry( &bs->bind_link ); close_socket(bs->sd); free( bs->spec ); free( bs ); release_lock( &bind_lock ); return 0; /* (failure) */ } } SIGNAL_SOCKDEV_THREAD(); release_lock( &bind_lock ); logmsg (_("HHCSD004I Device %4.4X bound to socket %s\n"), dev->devnum, dev->bs->spec); return 1; /* (success) */ }
/*-------------------------------------------------------------------*/ int bind_device_ex (DEVBLK* dev, char* spec, ONCONNECT fn, void* arg ) { bind_struct* bs; int was_list_empty; int rc; if (!init_done) init_sockdev(); if (sysblk.shutdown) return 0; logdebug("bind_device (%4.4X, %s)\n", dev->devnum, spec); /* Error if device already bound */ if (dev->bs) { WRMSG (HHC01041, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, dev->bs->spec); return 0; /* (failure) */ } /* Create a new bind_struct entry */ bs = malloc(sizeof(bind_struct)); if (!bs) { char buf[40]; MSGBUF( buf, "malloc(%d)", (int)sizeof(bind_struct)); WRMSG (HHC01000, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, buf, strerror(errno)); return 0; /* (failure) */ } memset(bs, 0, sizeof(bind_struct)); bs->fn = fn; bs->arg = arg; if (!(bs->spec = strdup(spec))) { WRMSG (HHC01000, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "strdup()", strerror(errno) ); free (bs); return 0; /* (failure) */ } /* Create a listening socket */ if (bs->spec[0] == '/') bs->sd = unix_socket (bs->spec); else bs->sd = inet_socket (bs->spec); if (bs->sd == -1) { /* (error message already issued) */ free( bs->spec ); free( bs ); return 0; /* (failure) */ } /* Chain device and bind_struct to each other */ dev->bs = bs; bs->dev = dev; /* Add the new entry to our list of bound devices and create the socket thread that will listen for connections (if it doesn't already exist) */ obtain_lock( &bind_lock ); was_list_empty = IsListEmpty( &bind_head ); InsertListTail( &bind_head, &bs->bind_link ); if ( was_list_empty ) { rc = create_thread( &sysblk.socktid, JOINABLE, socket_thread, NULL, "socket_thread" ); if (rc) { WRMSG(HHC00102, "E", strerror( rc ) ); RemoveListEntry( &bs->bind_link ); close_socket(bs->sd); free( bs->spec ); free( bs ); release_lock( &bind_lock ); return 0; /* (failure) */ } } SIGNAL_SOCKDEV_THREAD(); release_lock( &bind_lock ); WRMSG (HHC01042, "I", SSID_TO_LCSS(dev->ssid), dev->devnum, dev->bs->spec); return 1; /* (success) */ }