My os is 64bits Centos6.4. I have a question about how to fd_set manage FD_SET add the fd.Follow code is case:
fd_set my_set;
FD_SET(31, &my_set);
Then, I show my_set.fds_bits[...].The my_set.fds_bits[0] is equal to 0x0000000080000000, my_set.fds_bits[1~...] is zero.I can understand the result.But I also write an case,Follow code:
fd_set my_set;
FD_SET(63, &my_set);
I show my_set.fds_bits[...].The my_set.fds_bits[0] is equal to 0x0.In my opinion, the result should be my_set.fds_bits[0] is equal to 0x8000000000000000.I really don't understand why sencond result is 0x0. Doesn't fd that is equal to 63 have a state in my_set.
Here is full testing code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <pthread.h>
#define BUFSIZE 1024
void printFDSET(fd_set target_sets)
{
int uIndex = 0;
int model_num = __NFDBITS;
for(uIndex = 0; uIndex < 10/*FD_SETSIZE/model_num*/; uIndex++)
{
printf("0x%016x\t", target_sets.fds_bits[uIndex]);
if(uIndex % 4 == 3)
printf("\n");
}
printf("\n");
return;
}
int main(int argc, char* argv[])
{
fd_set my_set;
FD_ZERO(&my_set);
printf("sizeof(long int) = %d\n", sizeof(long int));
printf("FD_SETSIZE = %d\n", FD_SETSIZE);
printf("size = %zu\n", sizeof __FDS_BITS(&my_set)[0]);
printf("\n\n");
int unfd = 31;
FD_SET(unfd, &my_set);
printf("%d is added to my_set:\n", unfd);
printFDSET(my_set);
printf("\n\n");
FD_CLR(unfd, &my_set);
unfd = 63;
printf("%d is added to my_set:\n", unfd);
FD_SET(unfd, &my_set);
printFDSET(my_set);
return 0;
}
Doesn't fd that is equal to 63 have a state in my_set.
It has. You just didn't correctly print the value, because you forgot the length modifier l in the printf format string.
printf("0x%016lx\t", __FDS_BITS(&target_sets)[uIndex]);
You probably compiled your code as 32 bit. set.fds_bits is an array of long int. Check the value of my_set.fds_bits[1]
Related
I have a basic server-client program that I'm writing in C and I am stuck with an issue regarding strings and concatenation.
Basically I have some strings (in the example below just 2) that I have to put into a buffer which size is determined by:
total # of registered people * 33
Both the strings in the example have a length which is much less than the length of the buffer. I want to obtain something like this after the concatenation:
[0] [32]
people_list=Mark Amy\0;
where Mark(which is inserted secondly) is right at the start of the buffer (people_list) and Amy is 32 characters away from the start of Mark (I hope I've made myself clear).
This is because the client code was given to me and I cannot modify it. The client code takes the buffer and reads the first element, then jumps of 32 chars and reads again.
The output I get from the printf of the client is this:
connected to server
Registered people:
Mark
while I'd like this:
connected to server
Registered people:
Mark
Amy
The communication is implemented through sockets, which I have already checked, but if you wish to suggest some changes I would appreciate that.
Server code:
#include <stdio.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#define LENGTH 32
struct person {
char name[LENGTH+1];
struct person *next;
struct person *prev;
};
struct person *p_head = NULL;
void addPerson(char* name_p){
struct person *new = (struct person*) malloc(sizeof(struct person));
strcpy(new->name, name_p);
new->name[LENGTH]='\0';
new->next=p_head;
new->prev=NULL;
p_head=new;
}
int main(){
int fd_ser;
int fd_c;
int N=100;
char buf[N];
int times=0;
char* path="tmp/sock";
struct sockaddr_un sa;
unlink(path);
sa.sun_family=AF_UNIX;
strncpy(sa.sun_path,(char*) path, sizeof(sa.sun_path));
if((fd_ser=socket(AF_UNIX,SOCK_STREAM,0))<0){ //socket
perror((const char*) (size_t) errno);
exit(EXIT_FAILURE);
}
if ( bind(fd_ser,(struct sockaddr *)&sa, sizeof(sa))<0){
perror("bind\n");
}
listen(fd_ser,10); //listen
struct sockaddr_un addr;
int addr_size= sizeof(struct sockaddr_un);
fd_c=0;
while( (fd_c=accept(fd_ser,(struct sockaddr*) &addr, (socklen_t*)&addr_size))<0){
printf("waiting for connections...\n");
sleep(2);
}
//initialize list of people
char* Amy="Amy";
char* Mark="Mark";
addPerson(Amy);
addPerson(Mark);
//now concat the name strings in a buffer to be sent to the client
char* people_list;
unsigned int list_len;
int value;
struct person* ptr=(struct person*) malloc(sizeof(struct person));
ptr=p_head;
int offset=0;
int i=0;
while(ptr!=NULL){
i++;
people_list=realloc(people_list,i*LENGTH); //every single name has to be LENGTH characters
strcpy(&people_list[offset],ptr->name);
ptr=ptr->next;
offset=offset+LENGTH;
}
people_list[i*LENGTH]='\0';
list_len=(i*LENGTH)+1;
value=write(fd_c, &(list_len), sizeof(unsigned int));
if(value==-1){
perror("write length");
return -1;
}
int toWrite=list_len;
char *toRead=people_list;
while(toWrite>0){
value=write(fd_c, toRead, toWrite);
if(value==-1){
perror("write data");
return -1;
}
toWrite=toWrite-value;
toRead=toRead+value;
if(toRead<=people_list + list_len) break;
}
close(fd_c);
close(fd_ser);
return 0;
}
Client code:
#include <stdio.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <assert.h>
#include <errno.h>
#define MAX_LENGTH 2048
#define LENGTH 32
int main(){
int fd_cli;
char* path="tmp/sock";
struct sockaddr_un sa;
sa.sun_family=AF_UNIX;
strncpy(sa.sun_path,(char*) path, sizeof(sa.sun_path));
if( (fd_cli = socket(AF_UNIX, SOCK_STREAM, 0))==-1){
perror((const char*) (size_t) errno);
return -1;
}
while( (connect(fd_cli,(struct sockaddr*)&sa, sizeof(struct sockaddr_un))) == -1 ) {
if ( errno == ENOENT ) { sleep(1); }
else perror("connect:");
errno=0;
sleep(1);
}
printf("connected to server\n");
int value;
unsigned int len_data;
value=read(fd_cli,&(len_data),sizeof(unsigned int));
if(value==-1){
perror("read length");
return -1;
}
char* buffer=malloc(len_data*sizeof(char));
int toRead=len_data;
char *toWrite=buffer;
while(toRead>0){
value=read(fd_cli, toWrite, toRead);
if(value==-1){
perror("read buffer");
return -1;
}
toRead=toRead-value;
toWrite=toWrite+value;
if(toWrite<=buffer + len_data) break;
}
int people_n = len_data / (LENGTH+1);
assert(people_n > 0); //proceeds only if there is at least one person registered
printf("Registered people:\n");
for(int i=0,p=0;i<people_n; ++i, p+=(LENGTH+1)) {
printf(" %s\n", &buffer[p]);
}
close(fd_cli);
return 0;
}
I really hope I've explained the problem clearly! Thank you for your help!
It's printing only the first user (Mark) because of this line:
int people_n = len_data / (LENGTH+1);
In this example, len_data = 65, LENGTH = 32. So when you are adding 1 to LENGTH, it will return 1 (65/33 = 1.96 => you get 1). Then it only prints the first user.
Consider using Valgrind. It will help you checking the use of the memory.
This is a short version of my code: I have a 64 bit fedora 21 on which the code works fine. However on my other 32bit machine on which I have installed ubuntu Kylin 16.04 32 bit version, poll does not return when text is entered on stdin, it just stays blocked. When I hit Ctrl-C in gdb after the text is entered, and then try executing next command it works... Is this a bug of ubuntu 32 bit version or I have to use poll differently on 32 bit version?
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
int sockfd;
int const myOPEN_MAX=100,MAXLINE=4096;
struct pollfd client[myOPEN_MAX];
int maxi,nready,n;
char buf[4096];
client[0].fd = fileno(stdin);
client[0].events = POLLRDNORM;
for (int i = 1; i < myOPEN_MAX; i++){
client[i].fd = -1; /* -1 indicates available entry */
}
maxi = 1; /* max index into client[] array */
printf("enter something\n");
for ( ; ; ) {
nready = poll(client, maxi+1, -1);
for (int i = 0; i <= maxi; i++) { /* check all clients for data */
if ( (sockfd = client[i].fd) < 0)
continue;
if (client[i].revents & (POLLRDNORM | POLLERR)) {
if ( (n = read(sockfd, buf, MAXLINE)) > 0) {
if (i == 0) { //stdin
printf("works\n");
return 0;
}
if (--nready <= 0)
break; /* no more readable descriptors */
}
}
}
}
return 0;
}
Please take a look at this simple utility program. It works fine with AIX when compiled to 32 bit using libexpect5.42 provided by the operating system.
However, when linking with a 64 bit of libexpect which we manually compiled, we are getting an illegal instruction error on the call to exp_expectl(). Any ideas are welcomed.
#include <stdio.h>
#include <stdlib.h>
#include <expect.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
int
main (int argc, char** argv, char** arge)
{
int returnValue = 0;
int iFd = 0;
int res = 0;
exp_loguser = 1;
exp_timeout = 5;
exp_is_debugging = 0;
printf("spawning process without argument\n");
iFd = exp_spawnl ("ls", "ls",(char*)0 );
printf ("spawned\n");
printf ("iFd = %d\n", iFd);
if (iFd < 0)
{
printf ("Return %d\n",iFd);
}
else
{
res = exp_expectl (iFd, exp_glob,"ls", 0, exp_end);
printf ("Return %d \n",res);
}
return iFd;
}
gethostbyname works fine in fedora 32 bit but is failing on 64 bit environment giving a segmentation fault? In such scenario what is the problem and how can we resolve it?
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
struct hostent *he;
struct in_addr a;
int main (int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s hostname\n", argv[0]);
return 1;
}
he = gethostbyname (argv[1]);
if (he) {
printf("name :- %s\n", he->h_name);
while (*he->h_aliases)
printf("alias:- %s\n", *he->h_aliases++);
while (*he->h_addr_list) {
bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
printf("address:- %s\n", inet_ntoa(a));
}
}
else
herror(argv[0]);
return 0;
}
You are missing the correct includes:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
Without the correct includes, inet_ntoa is assumed to have a return type of int. Because int has the same size as char* on x86, there is no problem. This is not true on x86_64, so the read of that string by printf causes a fault.
I'm attempting to poll networking device names. I've pieced this together from various snippets,
http://unixhelp.ed.ac.uk/CGI/man-cgi?netdevice+7
http://lists.apple.com/archives/Unix-porting/2002/Apr/msg00134.html
http://ubuntuforums.org/showthread.php?t=1421487
But my output is just gibberish.
#include <stdio.h>
#include <stdlib.h>
#include <net/route.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#define BUFLEN 1024
#define SEQ 9999
int main (int argc, const char* argv[])
{
// File descriptor for socket
int socketfd;
struct ifconf conf;
struct ifreq req[10];
struct ifreq *ifr;
printf("Opening socket...");
socketfd = socket(AF_ROUTE, SOCK_RAW, 0);
if (socketfd >= 0) {
printf(" OK\n");
conf.ifc_len = sizeof(req);
conf.ifc_buf = (__caddr_t) req;
ioctl(socketfd,SIOCGIFCONF,&conf);
printf("Discovering interfaces...\n");
int i;
for (i=0; i<conf.ifc_len/sizeof(req[0]); i++) {
ifr = &conf.ifc_req[i];
printf("%d. %s\n", i+1, req[i].ifr_name);
}
}
else {
printf("Failed!\n");
}
return 0;
}
Output:
Opening socket... OK
Discovering interfaces...
?u???}??Gh???
2. p?9}?
3.
4. v?=?n??u?`?y??]g?<?~?v??
5.
6.
7.
8. ?v?T?
9. ?|?mw??j??v??h??|??v?T00~??v?$?|??|?#
10. T00~??v?$?|??|?#
I tried outputting each char of the ifr_name array one-by-one to see if they were null terminated but that didn't change much. Each iteration of my program outputs something different so this leads me to think I'm referencing something wrong. Can someone provide me some insight as to what I may be doing wrong?
Here's some code I put together for Mac OS X:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>
/* This is defined on Mac OS X */
#ifndef _SIZEOF_ADDR_IFREQ
#define _SIZEOF_ADDR_IFREQ sizeof
#endif
int main (int argc, const char* argv[])
{
// File descriptor for socket
int socketfd;
struct ifconf conf;
char data[4096];
struct ifreq *ifr;
char addrbuf[1024];
int i;
printf("Opening socket...");
socketfd = socket(AF_INET, SOCK_DGRAM, 0);
if (socketfd >= 0) {
printf(" OK\n");
conf.ifc_len = sizeof(data);
conf.ifc_buf = (caddr_t) data;
if (ioctl(socketfd,SIOCGIFCONF,&conf) < 0) {
perror("ioctl");
}
printf("Discovering interfaces...\n");
i = 0;
ifr = (struct ifreq*)data;
while ((char*)ifr < data+conf.ifc_len) {
switch (ifr->ifr_addr.sa_family) {
case AF_INET:
++i;
printf("%d. %s : %s\n", i, ifr->ifr_name, inet_ntop(ifr->ifr_addr.sa_family, &((struct sockaddr_in*)&ifr->ifr_addr)->sin_addr, addrbuf, sizeof(addrbuf)));
break;
#if 0
case AF_INET6:
++i;
printf("%d. %s : %s\n", i, ifr->ifr_name, inet_ntop(ifr->ifr_addr.sa_family, &((struct sockaddr_in6*)&ifr->ifr_addr)->sin6_addr, addrbuf, sizeof(addrbuf)));
break;
#endif
}
ifr = (struct ifreq*)((char*)ifr +_SIZEOF_ADDR_IFREQ(*ifr));
}
close(socketfd);
}
else {
printf(" Failed!\n");
}
return 0;
}
Poll as in you want to be notified if an interface is added or removed? Or polled as in you just want to find out the interface names once from the system? If the latter, take a look at getifaddrs().
Please see http://git.netfilter.org/cgi-bin/gitweb.cgi?p=libmnl.git;a=blob;f=examples/rtnl/rtnl-link-dump.c;hb=HEAD on how to get the list of interfaces on Linux. AF_ROUTE is some BSD thing and the use of ioctl is discouraged on Linux for its apparent limitations (such as to convey multiple addresses on a single interface).