comparison src/common.c @ 6:cf9b44b46be5

Use stderr for output instead of stdout, added debugging code to nacltai
author root <root@Really.UFO-Net.nl>
date Fri, 08 Oct 2010 23:29:56 +0000
parents a989ecbd5f53
children fd7c60905b13
comparison
equal deleted inserted replaced
5:9d449e899402 6:cf9b44b46be5
79 perror(text); 79 perror(text);
80 return -1; 80 return -1;
81 } 81 }
82 82
83 void print_header() { 83 void print_header() {
84 printf("UCIS QuickTun (c) 2010 Ivo Smits <Ivo@UCIS.nl>\n"); 84 fprintf(stderr, "UCIS QuickTun (c) 2010 Ivo Smits <Ivo@UCIS.nl>\n");
85 printf("More information: http://wiki.qontrol.nl/QuickTun\n"); 85 fprintf(stderr, "More information: http://wiki.qontrol.nl/QuickTun\n");
86 } 86 }
87 87
88 int init_udp(struct qtsession* session) { 88 int init_udp(struct qtsession* session) {
89 char* envval; 89 char* envval;
90 printf("Initializing UDP socket...\n"); 90 fprintf(stderr, "Initializing UDP socket...\n");
91 int sfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 91 int sfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
92 if (sfd < 0) return errorexitp("Could not create UDP socket"); 92 if (sfd < 0) return errorexitp("Could not create UDP socket");
93 struct sockaddr_in udpaddr; 93 struct sockaddr_in udpaddr;
94 struct hostent *he; 94 struct hostent *he;
95 udpaddr.sin_family = AF_INET; 95 udpaddr.sin_family = AF_INET;
130 return sfd; 130 return sfd;
131 } 131 }
132 132
133 int init_tuntap() { 133 int init_tuntap() {
134 char* envval; 134 char* envval;
135 printf("Initializing tap device...\n"); 135 fprintf(stderr, "Initializing tap device...\n");
136 int ttfd; //Tap device file descriptor 136 int ttfd; //Tap device file descriptor
137 struct ifreq ifr; //required for tun/tap setup 137 struct ifreq ifr; //required for tun/tap setup
138 memset(&ifr, 0, sizeof(ifr)); 138 memset(&ifr, 0, sizeof(ifr));
139 if ((ttfd = open("/dev/net/tun", O_RDWR)) < 0) return errorexitp("Could not open tap device file"); 139 if ((ttfd = open("/dev/net/tun", O_RDWR)) < 0) return errorexitp("Could not open tap device file");
140 if (envval = getconf("INTERFACE")) strcpy(ifr.ifr_name, envval); 140 if (envval = getconf("INTERFACE")) strcpy(ifr.ifr_name, envval);
159 } 159 }
160 160
161 int qtrun(struct qtproto* p) { 161 int qtrun(struct qtproto* p) {
162 struct qtsession session; 162 struct qtsession session;
163 session.protocol = *p; 163 session.protocol = *p;
164
164 init_udp(&session); 165 init_udp(&session);
166 int sfd = session.fd_socket;
167 if (sfd == -1) return -1;
168
165 session.fd_dev = init_tuntap(); 169 session.fd_dev = init_tuntap();
170 int ttfd = session.fd_dev;
171 if (ttfd == -1) return -1;
166 172
167 char protocol_data[p->protocol_data_size]; 173 char protocol_data[p->protocol_data_size];
168 session.protocol_data = &protocol_data; 174 session.protocol_data = &protocol_data;
169 if (p->init) p->init(&session); 175 if (p->init) p->init(&session);
170 176
171 int sfd = session.fd_socket; 177 fprintf(stderr, "The tunnel is now operational!\n");
172 int ttfd = session.fd_dev;
173 if (sfd == -1) return -1;
174 if (ttfd == -1) return -1;
175 printf("The tunnel is now operational!\n");
176 178
177 struct pollfd fds[2]; 179 struct pollfd fds[2];
178 fds[0].fd = ttfd; 180 fds[0].fd = ttfd;
179 fds[0].events = POLLIN; 181 fds[0].events = POLLIN;
180 fds[1].fd = sfd; 182 fds[1].fd = sfd;