Does anyone have any suggestions or code referrals
for a trivial timeserver that accepts an epoch time
stamp and sets the system date entirely in C without
root privileges throughout?
I've looked at the function defs for a day and have
come up empty since settimeofday() is a little rigid.
Here's the preliminary client code:
for a trivial timeserver that accepts an epoch time
stamp and sets the system date entirely in C without
root privileges throughout?
I've looked at the function defs for a day and have
come up empty since settimeofday() is a little rigid.
Here's the preliminary client code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define DPORT 9001
#define MAXLEN 10
void doUsage(void);
void sendTime(char *, time_t);
void abortWithErr(char *);
int main(int argc , char **argv) {
int i;
time_t mytime;
if (argc < 2) {
doUsage();
return -1;
} else {
for (i=1 ; i < argc ; i++) {
time(&mytime);
printf("Updating %s\n", argv[i]);
sendTime(argv[i],mytime);
}
}
return 0;
}
void sendTime(char *address, time_t current) {
int sock, port;
struct sockaddr_in remotesrv;
if ( (sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) {
abortWithErr("sock()");
}
bzero(&remotesrv, sizeof(remotesrv));
remotesrv.sin_family = AF_INET;
remotesrv.sin_addr.s_addr = inet_addr(address);
remotesrv.sin_port = htons(DPORT);
if ( (connect(sock,(struct sockaddr *) &remotesrv,sizeof(remotesrv))) < 0) {
abortWithErr("connect()");
} else {
if (send(sock,¤t,MAXLEN,MSG_DONTWAIT) != MAXLEN) {
abortWithErr("send()");
}
close(sock);
printf("Success..opened %s and sent time string %d\n", address,(int)current);
}
}
void doUsage() {
printf("***********************Usage************************\n");
printf("\n");
printf("Please pass the addresses of the hosts you would like to update\n");
printf("as arguments to this program\n");
printf("\n");
printf("***********************Usage************************\n");
}
void abortWithErr(char *pt) {
perror(pt);
abort();
}