int main(int argc,char *argv[]) { int sockfd; const int on = 1; pid_t pid; struct sockaddr_in *sa,cliaddr,wildaddr; struct ifi_info *ifi,*ifihead; for(ifi = ifihead = get_ifi_info(AF_INET,1);ifi != NULL; ifi=ifi->ifi_next){ sa = (struct sockaddr_in*)ifi->ifi_addr; sa->sin_port = htons(SERV_PORT); sa->sin_family = AF_INET; sockfd = Socket(AF_INET,SOCK_DGRAM,0); setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); Bind(sockfd,(SA*)sa,sizeof(*sa)); if((pid = fork()) == 0){ mydg_echo(sockfd,*cliaddr,sizeof(cliaddr),(SA*)sa); exit(0); } if(ifi->ifi_flags & IFF_BROADCAST){ sockfd = Socket(AF_INET,SOCK_DAGRAM,0); setsockopt(sockfd,SOL_SOCKET,SOCK_DGRAM,&on,sizeof(on)); sa = (struct sockaddr_in *)ifi->ifi_brdaddr; sa ->sin_port = htons(SERV_PORT); sa->sin_family = AF_INET; if(Bind(sockfd,sa,sizeof(*sa)) < 0){ if(errno == EINUSEADDR){ err_quit("EINUSERADDR:for %s\n",Sock_ntop(sa,sizeof(*sa)); }else{ err_quit("error bind for %s\n",Sock_ntop(sa,sizeof(*sa))); } }
int main(int argc, char **argv) { struct ifi_info *ifi, *ifihead; struct sockaddr *sa; u_char *ptr; int i, family, doaliases; if (argc != 3) err_quit("usage: prifinfo <inet4|inet6> <doaliases>"); if (strcmp(argv[1], "inet4") == 0) family = AF_INET; #ifdef AF_INET6 else if (strcmp(argv[1], "inet6") == 0) family = AF_INET6; #endif else err_quit("invalid <address-family>"); doaliases = atoi(argv[2]); for (ifihead = ifi = Get_ifi_info(family, doaliases); ifi != NULL; ifi = ifi->ifi_next) { printf("%s: <", ifi->ifi_name); if (ifi->ifi_flags & IFF_UP) printf("UP "); if (ifi->ifi_flags & IFF_BROADCAST) printf("BCAST "); if (ifi->ifi_flags & IFF_MULTICAST) printf("MCAST "); if (ifi->ifi_flags & IFF_LOOPBACK) printf("LOOP "); if (ifi->ifi_flags & IFF_POINTOPOINT) printf("P2P "); printf(">\n"); if ( (i = ifi->ifi_hlen) > 0) { ptr = ifi->ifi_haddr; do { printf("%s%x", (i == ifi->ifi_hlen) ? " " : ":", *ptr++); } while (--i > 0); printf("\n"); } if ( (sa = ifi->ifi_addr) != NULL) printf(" IP addr: %s\n", Sock_ntop(sa, sa->sa_len)); if ( (sa = ifi->ifi_brdaddr) != NULL) printf(" broadcast addr: %s\n", Sock_ntop(sa, sa->sa_len)); if ( (sa = ifi->ifi_dstaddr) != NULL) printf(" destination addr: %s\n", Sock_ntop(sa, sa->sa_len)); } free_ifi_info(ifihead); exit(0); }
int main(int argc,char *argv[]) { int listenfd,connfd; socklen_t addrlen,len; struct sockaddr *cliaddr; char buffer[MAXLINE]; time_t ticks; if(argc == 2) listenfd = tcp_listen(NULL,argv[1],&addrlen); if(argc == 3) listenfd = tcp_listen(argv[1],argv[2],&addrlen); else err_quit("usage:daytimetcpsrv2:[<host>] <service or port>]"); cliaddr = malloc(addrlen); for (;;) { len = addrlen; connfd = accpet(listenfd,cliaddr,&len); printf("connection from %s\n",Sock_ntop(cliaddr,len)); ticks = time(NULL); snprintf(buffer,sizeof(buffer),"%.24s\r\n",ctime(&ticks)); Writen(connfd,buffer,strlen(buffer)); Close(connfd); } }
int main(int argc, char **argv) { int listenfd; int connfd; socklen_t len; char buff[MAXLINE]; time_t ticks; struct sockaddr_storage cliaddr; if (argc != 2) err_quit("usage: daytimetcpsrv1 <service or port#>"); listenfd = Tcp_listen(NULL, argv[1], NULL); for ( ; ; ) { len = sizeof(cliaddr); connfd = Accept(listenfd, (SA *)&cliaddr, &len); printf("connectoin from %s\n", Sock_ntop((SA *)&cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff)); Close(connfd); } }
int main(int argc, char **argv) { int sockfd; socklen_t len; struct sockaddr_in cliaddr, servaddr; if (argc != 2) err_quit("usage: udpcli <IPaddress>"); sockfd = Socket(AF_INET, SOCK_DGRAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)); len = sizeof(cliaddr); Getsockname(sockfd, (SA *) &cliaddr, &len); printf("local address %s\n", Sock_ntop((SA *) &cliaddr, len)); exit(0); }
int main(int argc, char **argv) { int sockfd; ssize_t n; char buff[MAXLINE]; time_t ticks; socklen_t len; struct sockaddr_storage cliaddr; if (argc == 2) sockfd = Udp_server(NULL, argv[1], NULL); else if (argc == 3) sockfd = Udp_server(argv[1], argv[2], NULL); else err_quit("usage: daytimeupdsrv [ host ] <service or port>"); for ( ; ; ) { len = sizeof(cliaddr); n = Recvfrom(sockfd, buff, MAXLINE, 0, (SA *)&cliaddr, &len); printf("datagram from %s\n", Sock_ntop((SA *)&cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Sendto(sockfd, buff, strlen(buff), 0, (SA *)&cliaddr, len); } }
int main(int argc, char** argv) { if(argc != 3) { fprintf(stderr, "usage: daytimeserv <host> <service>\n"); exit(-1); } struct sockaddr_storage cliaddr; int listenfd = tcp_listen(argv[1], argv[2],NULL); socklen_t len; int connfd ; time_t ticks; char buff[MAXLINE]; for(; ; ) { len = sizeof(cliaddr); connfd = Accept(listenfd, (SA*) &cliaddr,&len); printf("connection from %s\n", Sock_ntop((SA*)&cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff)); Close(connfd); } }
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int icmpfd, maxfdp1; char sendline[MAXLINE], recvline[MAXLINE + 1]; fd_set rset; ssize_t n; struct timeval tv; struct icmpd_err icmpd_err; struct sockaddr_un sun; Sock_bind_wild(sockfd, pservaddr->sa_family); icmpfd = Socket(AF_LOCAL, SOCK_STREAM, 0); sun.sun_family = AF_LOCAL; strcpy(sun.sun_path, ICMPD_PATH); Connect(icmpfd, (SA *)&sun, sizeof(sun)); Write_fd(icmpfd, "1", 1, sockfd); n = Read(icmpfd, recvline, 1); if (n != 1 || recvline[0] != '1') err_quit("error creating icmp socket, n = %d, char = %c", n, recvline[0]); FD_ZERO(&rset); maxfdp1 = max(sockfd, icmpfd) + 1; while (Fgets(sendline, MAXLINE, fp) != NULL) { Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); tv.tv_sec = 5; tv.tv_usec = 0; FD_SET(sockfd, &rset); FD_SET(icmpfd, &rset); if ((n = Select(maxfdp1, &rset, NULL, NULL, &tv)) == 0) { fprintf(stderr, "socket timeout\n"); continue; } if (FD_ISSET(sockfd, &rset)) { n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); recvline[n] = 0; Fputs(recvline, stdout); } if (FD_ISSET(icmpfd, &rset)) { if ((n = Read(icmpfd, &icmpd_err, sizeof(icmpd_err))) == 0) err_quit("ICMP daemon terminate"); else if (n != sizeof(icmpd_err)) err_quit("n = %d, expected %d", n, sizeof(icmpd_err)); printf("ICMP error: dest = %s, %s, type = %d," " code = %d\n", Sock_ntop((SA *)&icmpd_err.icmpd_dest, icmpd_err.icmpd_len), strerror(icmpd_err.icmpd_errno), icmpd_err.icmpd_type, icmpd_err.icmpd_code); } } }
int main(int argc, char **argv) { int sockfd; struct ifi_info *ifi; unsigned char *ptr; struct arpreq arpreq; struct sockaddr_in *sin; sockfd = Socket(AF_INET, SOCK_DGRAM, 0); for (ifi = get_ifi_info(AF_INET, 0); ifi != NULL; ifi = ifi->ifi_next) { printf("%s: ", (char *)Sock_ntop(ifi->ifi_addr, sizeof(struct sockaddr_in))); sin = (struct sockaddr_in *)&arpreq.arp_pa; memcpy(sin, ifi->ifi_addr, sizeof(struct sockaddr_in)); if (ioctl(sockfd, SIOCGARP, &arpreq) < 0) { err_ret("ioctl SIOCGARP"); continue; } ptr = &arpreq.arp_ha.sa_data[0]; printf("%x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)); } exit(0); }
int main(int argc, char **argv) { int listenfd, connfd; socklen_t addrlen, len; struct sockaddr *cliaddr; char buff[MAXLINE]; time_t ticks; if (argc < 2 || argc > 3) err_quit("usage: daytimetcpsrv2 [ <host> ] <service or port#>"); daemon_init(argv[0], 0); if (argc == 2) listenfd = Tcp_listen(NULL, argv[1], &addrlen); else listenfd = Tcp_listen(argv[1], argv[2], &addrlen); cliaddr = Malloc(addrlen); for (;;) { len = addrlen; connfd = Accept(listenfd, cliaddr, &len); err_msg("connection from %s", Sock_ntop(cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff)); Close(connfd); } }
void sctp_print_addresses(struct sockaddr_storage *addrs, int num) { struct sockaddr_storage *ss; int i,salen; ss = addrs; for(i=0; i<num; i++){ printf("%s\n", Sock_ntop((SA *)ss, salen)); #ifdef HAVE_SOCKADDR_SA_LEN salen = ss->ss_len; #else switch(ss->ss_family) { case AF_INET: salen = sizeof(struct sockaddr_in); break; #ifdef IPV6 case AF_INET6: salen = sizeof(struct sockaddr_in6); break; #endif default: err_quit("sctp_print_addresses: unknown AF"); break; } #endif ss = (struct sockaddr_storage *)((char *)ss + salen); } }
int main(int argc, char **argv) { int i, listenfd, connfd; socklen_t addrlen, len; struct sockaddr *cliaddr; char buff[MAXLINE]; time_t ticks; if (argc == 2) listenfd = Tcp_listen(NULL, argv[1], &addrlen); else if (argc == 3) listenfd = Tcp_listen(argv[1], argv[2], &addrlen); else err_quit("usage: daytimetcpsrv2 [ <host> ] <service or port>"); cliaddr = Malloc(addrlen); for ( ; ; ) { len = addrlen; connfd = Accept(listenfd, cliaddr, &len); printf("connection from %s\n", Sock_ntop(cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); for (i = 0; i < strlen(buff); i++) Send(connfd, &buff[i], 1, MSG_EOR); Close(connfd); } }
int main(int argc, char** argv) { int listenfd, connfd; socklen_t len; char buff[MAXLINE]; time_t ticks; struct sockaddr_in6 servaddr, cliaddr; listenfd = Socket(AF_INET6, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin6_family = AF_INET6; servaddr.sin6_addr = in6addr_any; servaddr.sin6_port = htons(13); /* 时间服务器 */ Bind(listenfd, (SA*)&servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ); for (;;) { len = sizeof(cliaddr); connfd = Accept(listenfd, (SA*)&cliaddr, &len); printf("来自 %s 的连接\n", Sock_ntop((SA*)&cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff)); Close(connfd); } }
void *sendscl_tcps_conn(void *arg) { ssize_t n; char buf[MAXLINE]; char conninfo[128]; newconn_t *conn; int connfd; conn = (newconn_t *)arg; connfd = conn->fd; // show connection information memset(conninfo, 0, sizeof(conninfo)); snprintf(conninfo, sizeof(conninfo), "[addr: %s][addrlen: %d]", Sock_ntop((SA *)&conn->cliaddr, conn->len), conn->len); SEND_DBG(DBG_FLAG_SCL, "new connection--> %s\n", conninfo); free(conn); for(; ;) { n = Recvfrom(unixfd, buf, MAXLINE, 0, NULL, NULL); if(n > 0) { //for debug, print received data SEND_PRINT(sendg.debug_scl, conninfo, buf, n); Write(connfd, buf, n); } } Close(connfd); return NULL; }
int main(int argc, char **argv) { int sockfd; ssize_t n; char buff[MAXLINE]; time_t ticks; socklen_t addrlen, len; struct sockaddr *cliaddr; if (argc == 2) sockfd = Udp_server_reuseaddr(NULL, argv[1], &addrlen); else if (argc == 3) sockfd = Udp_server_reuseaddr(argv[1], argv[2], &addrlen); else err_quit("usage: daytimeudpsrv [ <host> ] <service or port>"); cliaddr = Malloc(addrlen); for ( ; ; ) { len = addrlen; n = Recvfrom(sockfd, buff, MAXLINE, 0, cliaddr, &len); printf("datagram from %s\n", Sock_ntop(cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Sendto(sockfd, buff, strlen(buff), 0, cliaddr, len); } }
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int n; char sendline[MAXLINE], recvline[MAXLINE + 1]; socklen_t len; struct sockaddr *preply_addr; preply_addr = Malloc(servlen); while (Fgets(sendline, MAXLINE, fp) != NULL) { Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); len = servlen; n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len); if (len != servlen || memcmp(pservaddr, preply_addr, len) != 0) { printf("reply from %s (ignored)\n", Sock_ntop(preply_addr, len)); continue; } recvline[n] = 0; /* null terminate */ Fputs(recvline, stdout); } }
void dg_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) { int flags; const int on = 1; socklen_t len; ssize_t n; char mesg[MAXLINE], str[INET6_ADDRSTRLEN], ifname[IFNAMSIZ]; struct in_addr in_zero; struct unp_in_pktinfo pktinfo; /* 若支持IP_RECVDSATDDR和IP_RECVIF套接字选项,则设置它们 */ #ifdef IP_RECVDSTADDR if (setsockopt(sockfd, IPPROTO_IP, IP_RECVDSTADDR, &on, sizeof(on)) < 0) err_ret("setsockopt of IP_RECVDSTADDR"); #endif #ifdef IP_RECVIF if (setsockopt(sockfd, IPPROTO_IP, IP_RECVIF, &on, sizeof(on)) < 0) err_ret("setsockopt of IP_RECVIF"); #endif bzero(&in_zero, sizeof(struct in_addr)); /* all 0 IPv4 address */ for ( ; ; ) { len = clilen; flags = 0; /* 读取来自套接字的数据报 */ n = recvfrom_flag(sockfd, mesg, MAXLINE, &flags, pcliaddr, &len, &pktinfo); /* 把所读取的字节数显示,最大字节数不能超过20字节,若超过,则发生截断情况; * 调用sock_ntop把源IP地址和端口号转换为表达格式并输出 */ printf("%d-byte datagram from %s", n, Sock_ntop(pcliaddr, len)); /* 若返回的IP地址不为0,则调用inet_ntop转化目的IP地址格式并输出 */ if (memcmp(&pktinfo.ipi_addr, &in_zero, sizeof(in_zero)) != 0) printf(", to %s", inet_ntop(AF_INET, &pktinfo.ipi_addr, str, sizeof(str))); printf(", index = %d", pktinfo.ipi_ifindex); /* 若返回的接口索引不为0,则调用if_indextoname获取接口名字并显示 */ if (pktinfo.ipi_ifindex > 0) printf(", recv i/f = %s", if_indextoname(pktinfo.ipi_ifindex, ifname)); printf(", flags = %d",flags); /* 以下是测试4个标志 */ #ifdef MSG_TRUNC if (flags & MSG_TRUNC) printf(" (datagram truncated)"); #endif #ifdef MSG_CTRUNC if (flags & MSG_CTRUNC) printf(" (control info truncated)"); #endif #ifdef MSG_BCAST if (flags & MSG_BCAST) printf(" (broadcast)"); #endif #ifdef MSG_MCAST if (flags & MSG_MCAST) printf(" (multicast)"); #endif printf("\n"); /* 回射文本字符串给客户端 */ sendto(sockfd, mesg, n, 0, pcliaddr, len); } }
void mydg_echo(int sockfd, SA* myaddr, socklen_t salen) { int n; char mesg[MAXLINE]; socklen_t len; struct sockaddr* cli; cli = Malloc(salen); for (;;) { len = salen; n = Recvfrom(sockfd, mesg, MAXLINE, 0, cli, &len); printf("child %d, datagram from %s", getpid(), Sock_ntop(cli, len)); printf(", to %s\n", Sock_ntop(myaddr, salen)); Sendto(sockfd, mesg, n, 0, cli, len); } }
void adv_mydg_echo(int sockfd, SA *pcliaddr, socklen_t clilen, SA *myaddr) { int n; char mesg[MAXLINE]; socklen_t len; for (;;) { len = clilen; n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len); printf("child %d, datagram from %s", getpid(), Sock_ntop(pcliaddr, len)); printf(", to %s\n", Sock_ntop(myaddr, clilen)); Sendto(sockfd, mesg, n, 0, pcliaddr, len); } }
int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; struct in_addr **pptr; struct in_addr *inetaddrp[2]; struct in_addr inetaddr; struct hostent *hp; struct servent *sp; if (argc != 3) err_quit("Usage: servent <hostname> <service>"); if ((hp = gethostbyname(argv[1])) == NULL) { if (inet_aton(argv[1], &inetaddr) == 0) err_quit("hostname error for %s: %s", argv[1], hstrerror(h_errno)); else inetaddrp[0] = &inetaddr; inetaddrp[1] = NULL; pptr = inetaddrp; } else pptr = (struct in_addr **)hp->h_addr_list; if ((sp = getservbyname(argv[2], "tcp")) == NULL) err_quit("getserbyname error for %s", argv[2]); for ( ; *pptr != NULL; pptr++) { sockfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = sp->s_port; memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr)); printf("trying %s\n", Sock_ntop((SA *)&servaddr, sizeof(servaddr))); if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) == 0) break; err_ret("connect error"); close(sockfd); } if (*pptr == NULL) err_quit("unable to connect"); while ((n = Read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; Fputs(recvline, stdout); } exit(0); }
int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; struct in_addr **pptr, *addrs[2]; struct hostent *hp; struct servent *sp; if (argc != 3) err_quit("usage: daytimetcpcli2 <hostname> <service>"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) == 1) { addrs[0] = &servaddr.sin_addr; addrs[1] = NULL; pptr = &addrs[0]; } else if ( (hp = gethostbyname(argv[1])) != NULL) { pptr = (struct in_addr **) hp->h_addr_list; } else err_quit("hostname error for %s: %s", argv[1], hstrerror(h_errno)); if ( (n = atoi(argv[2])) > 0) servaddr.sin_port = htons(n); else if ( (sp = getservbyname(argv[2], "tcp")) != NULL) servaddr.sin_port = sp->s_port; else err_quit("getservbyname error for %s", argv[2]); for ( ; *pptr != NULL; pptr++) { sockfd = Socket(AF_INET, SOCK_STREAM, 0); memmove(&servaddr.sin_addr, *pptr, sizeof(struct in_addr)); printf("trying %s\n", Sock_ntop((SA *) &servaddr, sizeof(servaddr))); if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) == 0) break; /* success */ err_ret("connect error"); close(sockfd); } if (*pptr == NULL) err_quit("unable to connect"); while ( (n = Read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ Fputs(recvline, stdout); } exit(0); }
void dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen) { int flags; const int on = 1; socklen_t len; ssize_t n; char mesg[MAXLINE], str[INET6_ADDRSTRLEN], ifname[IFNAMSIZ]; struct in_addr in_zero; struct unp_in_pktinfo pktinfo; #ifdef IP_RECVDSTADDR if (setsockopt(sockfd, IPPROTO_IP, IP_RECVDSTADDR, &on, sizeof(on)) < 0) err_ret("setsockopt of IP_RECVDSTADDR"); #endif #ifdef IP_RECVIF if (setsockopt(sockfd, IPPROTO_IP, IP_RECVIF, &on, sizeof(on)) < 0) err_ret("setsockopt of IP_RECVIF"); #endif bzero(&in_zero, sizeof(struct in_addr)); /* all 0 IPv4 address */ for ( ; ; ) { len = clilen; flags = 0; n = Recvfrom_flags(sockfd, mesg, MAXLINE, &flags, pcliaddr, &len, &pktinfo); printf("%d-byte datagram from %s", n, Sock_ntop(pcliaddr, len)); if (memcmp(&pktinfo.ipi_addr, &in_zero, sizeof(in_zero)) != 0) printf(", to %s", Inet_ntop(AF_INET, &pktinfo.ipi_addr, str, sizeof(str))); if (pktinfo.ipi_ifindex > 0) printf(", recv i/f = %s", If_indextoname(pktinfo.ipi_ifindex, ifname)); #ifdef MSG_TRUNC if (flags & MSG_TRUNC) printf(" (datagram truncated)"); #endif #ifdef MSG_CTRUNC if (flags & MSG_CTRUNC) printf(" (control info truncated)"); #endif #ifdef MSG_BCAST if (flags & MSG_BCAST) printf(" (broadcast)"); #endif #ifdef MSG_MCAST if (flags & MSG_MCAST) printf(" (multicast)"); #endif printf("\n"); Sendto(sockfd, mesg, n, 0, pcliaddr, len); } }
int main(int argc, char **argv) { int sockfd; ssize_t n; char buff[MAXLINE]; time_t ticks; socklen_t len; struct sockaddr_storage cliaddr; struct addrinfo hints, *res; int on = 1; if (argc == 2) sockfd = Udp_server(NULL, argv[1], NULL); else if (argc == 3) { bzero(&hints, sizeof(struct addrinfo)); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; if ( ( n = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0) err_quit("udp_server error for %s, %s: %s", argv[1], argv[2], gai_strerror(n)); do { sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (sockfd < 0) continue; /* error - try next one */ Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); if (bind(sockfd, res->ai_addr, res->ai_addrlen) == 0) break; /* success */ Close(sockfd); } while ( (res = res->ai_next) != NULL); if (res == NULL) err_sys("udp_server error"); } else err_quit("usage: daytimeudpsrv [ <host> ] <service or port>"); for ( ; ; ) { len = sizeof(cliaddr); n = Recvfrom(sockfd, buff, MAXLINE, 0, (SA *)&cliaddr, &len); printf("datagram from %s\n", Sock_ntop((SA *)&cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Sendto(sockfd, buff, strlen(buff), 0, (SA *)&cliaddr, len); } }
void *recvscl_tcps_conn(void *arg) { int connfd; newconn_t *conn; ssize_t n; char buf[MAXLINE]; int sclfd = -1; char conninfo[128]; // show connection information conn = (newconn_t *)arg; connfd = conn->fd; memset(conninfo, 0, sizeof(conninfo)); snprintf(conninfo, sizeof(conninfo), "[%s]", Sock_ntop((SA *)&conn->cliaddr, conn->len)); RECVSCL_DBG("new connection--> %s\n", conninfo); free(conn); // todo, whether need it? Pthread_detach(pthread_self()); // ricann todo, decide save file name from camid sclfd = recv_openfile(recvg.save_scl, 0, recvg.save_scl_name); for(; ;) { n = read(connfd, buf, MAXLINE); if(n == 0) break; if(n < 0) { if(errno == EINTR) continue; recv_closefile(recvg.save_scl, sclfd); err_sys("[recvscl_tcps_conn]read error"); } // debug info RECVSCL_DBG("[tcps]%s: recv %d bytes...\n", conninfo, n); // print receive data RECV_PRINT(recvg.debug_scl, conninfo, buf, n); // write info file recv_writefile(recvg.save_scl, sclfd, buf, n); } recv_closefile(recvg.save_scl, sclfd); return NULL; }
void recv_all(int sockfd,socklen_t salen) { static char buf[MAXLINE+1]; struct sockaddr *sa; socklen_t len; int n; sa = Malloc(salen); for(;;){ len = salen; n = recvfrom(sockfd,buf,MAXLINE,0,sa,&len); buf[n] = 0; printf("from %s : %s",Sock_ntop(sa,salen),buf); } }
void get_conn(client_conn *conn) { const char *server_ip = (const char*)cargs->ip_addr; int server_port = cargs->serv_portno; struct sockaddr *serv_sa = inet_pton_sa(server_ip, server_port); vector *ifaces = get_all_interfaces(); struct ifi_info serv_ifi; memset(&serv_ifi, 0, sizeof(serv_ifi)); serv_ifi.ifi_addr = serv_sa; conn->is_local = FALSE; // Check if the server is on the same machine as the client. algorithm_reduce(ifaces, is_local_interface_reducer, &serv_ifi); // serv_ifi.ifi_myflags is == 1 if the client & server share an IP // address. if (serv_ifi.ifi_myflags) { // Client & server are on the same machine. conn->is_local = TRUE; conn->cli_sa = inet_pton_sa("127.0.0.1", 0); conn->serv_sa = inet_pton_sa("127.0.0.1", server_port); return; } serv_ifi.ifi_myflags = 0; // The length of the longest match. algorithm_reduce(ifaces, longest_match_reducer, &serv_ifi); if (serv_ifi.ifi_brdaddr) { conn->is_local = TRUE; conn->serv_sa = serv_sa; char client_ip[40]; strcpy(client_ip, Sock_ntop_host(serv_ifi.ifi_brdaddr, sizeof(SA))); // Client bind(2)s to port 0 (locally). conn->cli_sa = inet_pton_sa(client_ip, 0); return; } // We could not find any local interfaces that match. Let the kernel // choose the outgoing interface for us. VERBOSE("serv_sa: %s\n", Sock_ntop(serv_sa, sizeof(*serv_sa))); conn->cli_sa = inet_pton_sa("0.0.0.0", 0); conn->serv_sa = serv_sa; }
int main(int argc, char **argv) { int listenfd, connfd; socklen_t clilen; struct sockaddr_un cliaddr, servaddr; char buff[MAXLINE]; time_t ticks; size_t nbytes; int i; if (argc != 1) err_quit("usage: daytimetcpsrv2 [ <host> ] <service or port>"); listenfd = Socket(AF_UNIX, SOCK_STREAM, 0); unlink(UNIXSTR_PATH); bzero(&servaddr, sizeof(struct sockaddr_un)); servaddr.sun_family = AF_UNIX; strcpy(servaddr.sun_path, UNIXSTR_PATH); Bind(listenfd, (SA *) &servaddr, SUN_LEN(&servaddr)); Listen(listenfd, LISTENQ); for ( ; ; ) { clilen = sizeof(cliaddr); if ( (connfd = accept(listenfd, (SA *)&cliaddr, &clilen)) < 0) { if (errno == EINTR) continue; else err_sys("accept error"); } printf("connection from %s\n", Sock_ntop((SA *)&cliaddr, clilen)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); nbytes = strlen(buff); for (i = 0; i < nbytes; i++) { if (send(connfd, buff+i, 1, MSG_EOR) < 0) err_sys("send error"); /* Write(connfd, buff+i, 1); */ } Close(connfd); } }
int main(int argc, char **argv) { int sockfd; ssize_t n; char buff[MAXLINE]; time_t ticks; socklen_t len; struct sockaddr_storage cliaddr; if ( argc == 2) sockfd = Udp_server( NULL, argv[1], NULL); else if ( argc == 3) sockfd = Udp_server( argv[1], argv[2], NULL); else err_quit( "usage: daytimeudpsrv [ <host> ] <service or port>"); for ( ; ; ) { len = sizeof(cliaddr); /* since socket is unconnected we use Recvfrom and Sendto, * not Read, Write */ n = Recvfrom(sockfd, buff, MAXLINE, 0, (SA *)&cliaddr, &len); printf( "datagram from %s\n", Sock_ntop((SA *)&cliaddr, len)); /* response */ time_t rawTime; time( &rawTime); struct tm * timeInfo; timeInfo = localtime( &rawTime); strftime( buff, MAXLINE, "cf16 daytime service on port 13. " "The time on this server now is: %A, %B %d, %G %T %p-%Z\n", timeInfo); //ticks = time(NULL); //snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Sendto( sockfd, buff, strlen(buff), 0, (SA *)&cliaddr, len); /* At this moment we don't know if peer is reachable or not * If it is not reachable sendto return Success but Recvfrom * will block forever (it doesn't see an ICMP "destination * unreachable" packet coming back from peer because socket * is unconnected) */ } }
int main(int argc, char const *argv[]) { socklen_t len; struct sockaddr *cliaddr; char buff[MAXLINE]; time_t ticks; daemon_inetd(argv[0],LOG_LOCAL2); cliaddr=(struct sockaddr *)Malloc(sizeof(struct sockaddr_storage)); len=sizeof(struct sockaddr_storage); Getpeername(0,cliaddr,&len); err_msg("connection from %s",Sock_ntop(cliaddr,len)); ticks=time(NULL); snprintf(buff,sizeof(buff),"%.24s\r\n",ctime(&ticks)); Write(0,buff,strlen(buff)); Close(0);//close TCP connection return 0; }
void recv_all(int recvfd, socklen_t salen) { int n; char line[MAXLINE+1]; socklen_t len; struct sockaddr *safrom; safrom = Malloc(salen); for ( ; ; ) { len = salen; n = Recvfrom(recvfd, line, MAXLINE, 0, safrom, &len); line[n] = 0; /* null terminate */ printf("from %s: %s", Sock_ntop(safrom, len), line); } }