Initial release MARS_NWE-PCAP 0.4 - Native Administration, Print and Login Edition
This commit is contained in:
12
native/rawprint.c
Normal file
12
native/rawprint.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
/* 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(off<n){if(time(NULL)>deadline||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;}
|
||||
Reference in New Issue
Block a user