#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include /* Raw transport only for an explicitly configured numeric IPv4 printer. */ int rawprint(const char *host,const char *file){struct sockaddr_in a={.sin_family=AF_INET,.sin_port=htons(9100)};if(inet_pton(AF_INET,host,&a.sin_addr)!=1)return -1;int fd=socket(AF_INET,SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC,0);if(fd<0)return -1;int in=-1,rc=-1,err=0;socklen_t el=sizeof err;struct pollfd p={fd,POLLOUT,0};if(connect(fd,(struct sockaddr*)&a,sizeof a)&&errno!=EINPROGRESS)goto end;if(poll(&p,1,5000)<=0||getsockopt(fd,SOL_SOCKET,SO_ERROR,&err,&el)||err)goto end;in=open(file,O_RDONLY|O_CLOEXEC|O_NOFOLLOW);if(in<0)goto end;char b[65536];ssize_t n;time_t deadline=time(NULL)+120;while((n=read(in,b,sizeof b))>0){ssize_t off=0;while(offdeadline||poll(&p,1,5000)<=0)goto end;ssize_t w=send(fd,b+off,n-off,MSG_NOSIGNAL);if(w<0&&(errno==EAGAIN||errno==EINTR))continue;if(w<=0)goto end;off+=w;}}if(n==0&&shutdown(fd,SHUT_WR)==0)rc=0;end:if(in>=0)close(in);close(fd);return rc;}